-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add module service adapter, unify route handling to `handleRequ…
…estWithEdgeSpec` * F * unneeded import * EdgeSpec -> EdgeSpecRouteBundle * rename create server from route map * refactor hadnleRequestWithEdgeSpec * fixes * better EdgeSpecOptions * rename file
- Loading branch information
Showing
15 changed files
with
428 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { | ||
type EdgeSpecAdapter, | ||
type EdgeSpecRouteBundle, | ||
handleRequestWithEdgeSpec, | ||
} from "src/types/edge-spec" | ||
import { EdgeSpecRequest, EdgeSpecRouteFn } from "src/types/web-handler" | ||
|
||
export interface ModuleServiceOptions { | ||
routeParam?: string | ||
handleRouteParamNotFound?: EdgeSpecRouteFn | ||
allowMatchingOnAnyCatchAllRouteParam?: boolean | ||
} | ||
|
||
export type ModuleService = (options?: ModuleServiceOptions) => EdgeSpecRouteFn | ||
|
||
export const createModuleService: EdgeSpecAdapter<[], ModuleService> = ( | ||
moduleServiceEdgeSpec | ||
) => { | ||
return (options) => async (request) => { | ||
// cascade options down the edge spec chain | ||
const edgeSpec: EdgeSpecRouteBundle = { | ||
...request.edgeSpec, | ||
...moduleServiceEdgeSpec, | ||
...(options?.handleRouteParamNotFound && { | ||
handleModuleServiceRouteNotFound: options?.handleRouteParamNotFound, | ||
}), | ||
} | ||
|
||
const pathnameOverrideResult = getPathnameOverride(request, options ?? {}) | ||
|
||
if ("failed" in pathnameOverrideResult) { | ||
return await pathnameOverrideResult.failed(request) | ||
} | ||
|
||
const response = await handleRequestWithEdgeSpec( | ||
edgeSpec, | ||
pathnameOverrideResult.pathnameOverride | ||
)(request) | ||
|
||
return response | ||
} | ||
} | ||
|
||
function getPathnameOverride( | ||
request: EdgeSpecRequest, | ||
options: ModuleServiceOptions | ||
): | ||
| { | ||
pathnameOverride: string | undefined | ||
} | ||
| { | ||
failed: EdgeSpecRouteFn | ||
} { | ||
const { | ||
routeParam, | ||
handleRouteParamNotFound = request.edgeSpec | ||
.handleModuleServiceRouteNotFound ?? | ||
(() => { | ||
throw new Error("Module service route not found!") | ||
}), | ||
allowMatchingOnAnyCatchAllRouteParam = true, | ||
} = options | ||
|
||
let paths: string[] | undefined | ||
|
||
if (routeParam) { | ||
const candidate = request.pathParams?.[routeParam] | ||
|
||
if (candidate && Array.isArray(candidate)) { | ||
paths = candidate | ||
} | ||
} | ||
|
||
if (!paths && allowMatchingOnAnyCatchAllRouteParam) { | ||
for (const routes of Object.values(request.pathParams ?? {})) { | ||
if (Array.isArray(routes)) { | ||
paths = routes | ||
break | ||
} | ||
} | ||
} | ||
|
||
if (!paths) { | ||
return { failed: handleRouteParamNotFound } | ||
} | ||
|
||
return { pathnameOverride: "/" + paths.join("/") } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import { EdgeSpecAdapter } from "src/types/edge-spec" | ||
import { EdgeSpecAdapter, handleRequestWithEdgeSpec } from "src/types/edge-spec" | ||
import { EdgeSpecFetchEvent } from "src/types/web-handler" | ||
|
||
export const addFetchListener: EdgeSpecAdapter = (edgeSpec) => { | ||
addEventListener("fetch", async (event) => { | ||
// TODO: find a better way to cast this | ||
const fetchEvent = event as unknown as EdgeSpecFetchEvent | ||
|
||
const { matchedRoute, routeParams } = edgeSpec.routeMatcher( | ||
new URL(fetchEvent.request.url).pathname | ||
fetchEvent.respondWith( | ||
await handleRequestWithEdgeSpec(edgeSpec)(fetchEvent.request) | ||
) | ||
const handler = edgeSpec.routeMapWithHandlers[matchedRoute] | ||
fetchEvent.request.pathParams = routeParams | ||
|
||
fetchEvent.respondWith(await handler(fetchEvent.request)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./types/edge-spec.js" | ||
export * from "./codegen/generate-module-code.js" | ||
export * from "./create-with-edge-spec.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { createServer } from "node:http" | ||
import { getRouteMatcher } from "next-route-matcher" | ||
import { normalizeRouteMap } from "../lib/normalize-route-map.js" | ||
import { | ||
type TransformToNodeOptions, | ||
transformToNodeBuilder, | ||
} from "src/edge-runtime/transform-to-node.js" | ||
import { EdgeSpecRouteFn } from "src/types/web-handler.js" | ||
import { | ||
EdgeSpecRouteBundle, | ||
EdgeSpecOptions, | ||
handleRequestWithEdgeSpec, | ||
} from "src/types/edge-spec.js" | ||
|
||
export const createEdgeSpecFromRouteMap = ( | ||
routeMap: Record<string, EdgeSpecRouteFn>, | ||
edgeSpecOptions?: Partial<EdgeSpecOptions> | ||
): EdgeSpecRouteBundle => { | ||
const formattedRoutes = normalizeRouteMap(routeMap) | ||
const routeMatcher = getRouteMatcher(Object.keys(formattedRoutes)) | ||
|
||
const routeMapWithHandlers = Object.fromEntries( | ||
Object.entries(formattedRoutes).map(([routeFormatted, route]) => [ | ||
routeFormatted, | ||
routeMap[route], | ||
]) | ||
) | ||
|
||
return { | ||
routeMatcher, | ||
routeMapWithHandlers, | ||
...edgeSpecOptions, | ||
} | ||
} | ||
|
||
export const createNodeServerFromRouteMap = async ( | ||
routeMap: Record<string, EdgeSpecRouteFn>, | ||
transformToNodeOptions: TransformToNodeOptions, | ||
edgeSpecOptions?: Partial<EdgeSpecOptions> | ||
) => { | ||
const edgeSpec = createEdgeSpecFromRouteMap(routeMap, edgeSpecOptions) | ||
|
||
const transformToNode = transformToNodeBuilder(transformToNodeOptions) | ||
|
||
const server = createServer( | ||
transformToNode(handleRequestWithEdgeSpec(edgeSpec)) | ||
) | ||
|
||
return server | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.