Skip to content

Commit

Permalink
refactor hadnleRequestWithEdgeSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Jan 19, 2024
1 parent ef308a9 commit 2859825
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 30 deletions.
9 changes: 7 additions & 2 deletions src/adapters/module-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ export const createModuleService: EdgeSpecAdapter<[], ModuleService> = (
return await pathnameOverrideResult.failed(request)
}

const url = new URL(request.url)

if (pathnameOverrideResult.pathnameOverride) {
url.pathname = pathnameOverrideResult.pathnameOverride
}

const response = await handleRequestWithEdgeSpec(
edgeSpec,
request,
pathnameOverrideResult.pathnameOverride
)
)(request)

return response
}
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const startServer: EdgeSpecAdapter<[EdgeSpecNodeAdapterOptions]> = (
})

const server = http.createServer(
transformToNode((req) => handleRequestWithEdgeSpec(edgeSpec, req))
transformToNode(handleRequestWithEdgeSpec(edgeSpec))
)
server.listen(port)

Expand Down
2 changes: 1 addition & 1 deletion src/adapters/wintercg-minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const addFetchListener: EdgeSpecAdapter = (edgeSpec) => {
const fetchEvent = event as unknown as EdgeSpecFetchEvent

fetchEvent.respondWith(
await handleRequestWithEdgeSpec(edgeSpec, fetchEvent.request)
await handleRequestWithEdgeSpec(edgeSpec)(fetchEvent.request)
)
})
}
2 changes: 1 addition & 1 deletion src/serve/create-server-from-route-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const createNodeServerFromRouteMap = async (
const transformToNode = transformToNodeBuilder(transformToNodeOptions)

const server = createServer(
transformToNode((req) => handleRequestWithEdgeSpec(edgeSpec, req))
transformToNode(handleRequestWithEdgeSpec(edgeSpec))
)

return server
Expand Down
47 changes: 24 additions & 23 deletions src/types/edge-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,36 @@ export type EdgeSpecAdapter<
ReturnValue = void,
> = (edgeSpec: EdgeSpecRouteBundle, ...options: Options) => ReturnValue

export async function handleRequestWithEdgeSpec(
export function handleRequestWithEdgeSpec(
edgeSpec: EdgeSpecRouteBundle,
request: Request,
pathnameOverride?: string
): Promise<Response> {
const {
routeMatcher,
routeMapWithHandlers,
handle404 = () =>
new Response("Not found", {
status: 404,
}),
} = edgeSpec
): (request: Request) => Promise<Response> {
return async (request: Request) => {
const {
routeMatcher,
routeMapWithHandlers,
handle404 = () =>
new Response("Not found", {
status: 404,
}),
} = edgeSpec

const pathname = pathnameOverride ?? new URL(request.url).pathname
const { matchedRoute, routeParams } = routeMatcher(pathname) ?? {}
const pathname = pathnameOverride ?? new URL(request.url).pathname
const { matchedRoute, routeParams } = routeMatcher(pathname) ?? {}

const routeFn = matchedRoute && routeMapWithHandlers[matchedRoute]
const routeFn = matchedRoute && routeMapWithHandlers[matchedRoute]

const edgeSpecRequest = createEdgeSpecRequest(request, {
edgeSpec,
pathParams: routeParams,
})
const edgeSpecRequest = createEdgeSpecRequest(request, {
edgeSpec,
pathParams: routeParams,
})

if (!routeFn) {
return await handle404(edgeSpecRequest)
}
if (!routeFn) {
return await handle404(edgeSpecRequest)
}

const response: Response = await routeFn(edgeSpecRequest)
const response: Response = await routeFn(edgeSpecRequest)

return response
return response
}
}
2 changes: 1 addition & 1 deletion src/types/web-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type EdgeSpecFetchEvent = FetchEvent & {

export function createEdgeSpecRequest(
request: Request,
options: EdgeSpecRequestOptions
options: EdgeSpecRequestOptions & { url?: URL }
): EdgeSpecRequest {
return Object.assign(request, options)
}
4 changes: 3 additions & 1 deletion tests/adapters/module-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { createEdgeSpecFromRouteMap } from "src/serve/create-server-from-route-m
test("module service with simple endpoint", async (t) => {
const ModuleService = createModuleService(
createEdgeSpecFromRouteMap({
"/health": async () => {
"/health": async (req) => {
console.log(req.url)

return new Response(
JSON.stringify({
ok: true,
Expand Down

0 comments on commit 2859825

Please sign in to comment.