Skip to content

Commit

Permalink
fix bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
chizuki committed Oct 14, 2022
1 parent ff37f0d commit 672476b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 46 deletions.
6 changes: 3 additions & 3 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ export function defineFourze(options: FourzeOptions | FourzeBaseRoute[] | Fourze
hooks: {
get() {
return hooks.map(e => {
return defineFourzeHook({
return {
...e,
path: e.path ? resolvePath(e.path, _base) : _base
})
path: resolvePath(e.path, _base)
}
})
}
},
Expand Down
18 changes: 6 additions & 12 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { delayHook } from "./hooks"
import { createLogger } from "./logger"
import {
createRequestContext,
defineFourzeHook,
defineRoute,
FourzeContext,
FourzeHook,
Expand All @@ -17,7 +16,7 @@ import {
FourzeRoute,
FourzeSetupContext
} from "./shared"
import { asyncLock, DelayMsType, isMatch, relativePath, resolvePath, unique } from "./utils"
import { asyncLock, DelayMsType, isMatch, relativePath, unique } from "./utils"

export interface FourzeRouter extends FourzeMiddleware {
/**
Expand Down Expand Up @@ -149,9 +148,7 @@ export function createRouter(params: FourzeRouterOptions | Fourze[] | MaybeAsync
...route.meta
}

console.log("path", path, "meta", request.meta)

const activeHooks = router.hooks.filter(e => !e.path || path.startsWith(e.path))
const activeHooks = router.hooks.filter(e => isMatch(path, e.path))

const handle = async () => {
const hook = activeHooks.shift()
Expand Down Expand Up @@ -206,7 +203,6 @@ export function createRouter(params: FourzeRouterOptions | Fourze[] | MaybeAsync

router.match = function (this: FourzeRouter, url: string, method?: string, allowed = false): [FourzeRoute, RegExpMatchArray] | [] {
if (allowed || this.isAllow(url)) {
console.log("match", url)
for (const route of this.routes) {
const matches = route.match(url, method)
if (matches) {
Expand Down Expand Up @@ -285,12 +281,10 @@ export function createRouter(params: FourzeRouterOptions | Fourze[] | MaybeAsync
}

for (const hook of newHooks) {
hooks.add(
defineFourzeHook({
...hook,
path: hook.path ? resolvePath(hook.path, options.base) : options.base
})
)
hooks.add({
...hook,
path: relativePath(hook.path, options.base)
})
}
})

Expand Down
12 changes: 3 additions & 9 deletions packages/core/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export function defineRoute(route: FourzeBaseRoute): FourzeRoute {
match(this: FourzeRoute, url: string, method?: string) {
if (!this.method || !method || this.method.toLowerCase() === method.toLowerCase()) {
const regex = new RegExp(`^${path.replace(PARAM_KEY_REGEX, "([a-zA-Z0-9_-\\s]+)?")}$`, "i")
console.log("Route match", regex, url, url.match(regex))
return url.match(regex)
}
return null
Expand All @@ -134,7 +133,7 @@ export interface FourzeBaseHook extends FourzeMiddleware<any> {

export interface FourzeHook {
handle: FourzeMiddleware<any>
path?: string
path: string
readonly [FOURZE_HOOK_SYMBOL]: true
}

Expand All @@ -145,9 +144,10 @@ export function defineFourzeHook(interceptor: FourzeBaseHook): FourzeHook
export function defineFourzeHook(interceptor: DefineFourzeHook): FourzeHook

export function defineFourzeHook(param0: string | DefineFourzeHook | FourzeBaseHook, param1?: FourzeBaseHook) {
const path = typeof param0 === "string" ? param0 : param0.path
const path = typeof param0 === "string" ? param0 : param0.path ?? ""

const hook = {
path,
handle:
param1 ?? typeof param0 === "string"
? param1
Expand All @@ -161,12 +161,6 @@ export function defineFourzeHook(param0: string | DefineFourzeHook | FourzeBaseH
}
} as FourzeHook

Object.defineProperty(hook, "path", {
get() {
return path
}
})

Object.defineProperty(hook, FOURZE_HOOK_SYMBOL, {
get() {
return true
Expand Down
40 changes: 18 additions & 22 deletions test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,33 @@ describe("hooks", async () => {
req.meta.token = req.headers["token"].toString().toUpperCase()
}
res.setHeader("token", data.token)
console.log("set-meta", req.meta)
return await next?.()
})

// route.hook("/api/test", async (req, res, next) => {
// if (req.method == "post") {
// res.result = "others"
// }
// return next?.()
// })
route.hook("/api/test", async (req, res, next) => {
if (req.method == "post") {
res.result = "others"
}
return next?.()
})

// route.hook("/api/test", (req, res, next) => {
// if (req.method == "post") {
// return "something"
// }
// return next?.()
// })
route.hook("/api/test", (req, res, next) => {
if (req.method == "post") {
return "something"
}
return next?.()
})

// route.hook("/api/test", (req, res, next) => {
// if (req.method == "post") {
// console.log("after somethings")
// return "unknown"
// }
// return next?.()
// })
route.hook("/api/test", (req, res, next) => {
if (req.method == "post") {
return "unknown"
}
return next?.()
})
})

await router.setup()

console.log("hooks", router.hooks)

const res = await fetch("/api/test", {
headers: {
token: data.token
Expand Down

0 comments on commit 672476b

Please sign in to comment.