Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add module service adapter, unify route handling to handleRequestWithEdgeSpec #23

Merged
merged 9 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
mxsdev marked this conversation as resolved.
Show resolved Hide resolved

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
mxsdev marked this conversation as resolved.
Show resolved Hide resolved
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
Loading