-
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: route params available on runtime * chore: explicit core exports for better tree-shaking support * feat: new params() util * chore: make params helpers more generic * doc: add route parameters recipe * test(hono): route params * test(hattip): route params * test(h3): route params * test(express): route params * test(fastify): route params * chore(cloudflare): route params * chore(webroute): route params * doc: complete route params * chore: create params handler in example * doc: finish route params doc * chore(ci): add delay for wrangler tests
- Loading branch information
Showing
59 changed files
with
694 additions
and
190 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
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,119 @@ | ||
# Using route parameters | ||
|
||
Most adapters natively support route parameters (also called _parametric path_ or _path parameters_) such as `/hello/:name`. | ||
`@universal-middleware/core` provides the `params` helper to universally retrieve those. | ||
|
||
We recommend to follow this next example when using route parameters: | ||
|
||
<<< @/../examples/tool/src/handlers/params.handler.ts | ||
|
||
> [!NOTE] | ||
> For servers supporting route parameters (`app.get("/user/:name", myHandler())`), the parameters are available under `runtime.params`. | ||
> | ||
> For other adapters (`app.get("/user/*", myHandler({ route: "/user/:name" }))`), the 3rd argument of `params` helper must be present and not _undefined_. | ||
> Then parameters are extracted with [regexparam](https://github.com/lukeed/regexparam). | ||
After bundling and publishing this middleware, one can then use this middleware as follows: | ||
|
||
::: code-group | ||
|
||
```ts twoslash [hono.ts] | ||
import { Hono } from "hono"; | ||
import paramHandler from "@universal-middleware-examples/tool/params-handler-hono"; | ||
|
||
const app = new Hono(); | ||
|
||
app.get("/user/:name", paramHandler()); | ||
|
||
export default app; | ||
``` | ||
|
||
```ts twoslash [h3.ts] | ||
import { createApp, createRouter } from "h3"; | ||
import paramHandler from "@universal-middleware-examples/tool/params-handler-h3"; | ||
import { universalOnBeforeResponse } from "@universal-middleware/h3"; | ||
|
||
const app = createApp({ | ||
// /!\ This is required for universal-middleware to operate properly | ||
onBeforeResponse: universalOnBeforeResponse, | ||
}); | ||
|
||
const router = createRouter(); | ||
|
||
router.get("/user/:name", paramHandler()); | ||
|
||
app.use(router); | ||
|
||
export default app; | ||
``` | ||
|
||
```ts twoslash [hattip.ts] | ||
import { createRouter } from "@hattip/router"; | ||
import paramHandler from "@universal-middleware-examples/tool/params-handler-hattip"; | ||
|
||
const app = createRouter(); | ||
|
||
app.get("/user/:name", paramHandler()); | ||
|
||
const hattipHandler = app.buildHandler(); | ||
|
||
export default hattipHandler; | ||
``` | ||
|
||
```ts twoslash [cloudflare-worker.ts] | ||
import paramsHandler from "@universal-middleware-examples/tool/params-handler"; | ||
import { createHandler } from "@universal-middleware/cloudflare"; | ||
import { pipe } from "@universal-middleware/core"; | ||
|
||
const paramsHandlerInstance = paramsHandler({ | ||
// Mandatory when targeting Cloudflare Worker | ||
route: "/user/:name", | ||
}); | ||
|
||
// Cloudflare Workers have no native routing support. | ||
// We recommend using Hono as it fully supports Cloudflare Worker. | ||
const wrapped = pipe( | ||
(request, ctx, runtime) => { | ||
const url = new URL(request.url); | ||
// intercept `/user/*` routes with this handler | ||
if (url.pathname.startsWith("/user/")) { | ||
return paramsHandlerInstance(request, ctx, runtime); | ||
} | ||
}, | ||
// Other handlers | ||
); | ||
|
||
export default createHandler(() => wrapped)(); | ||
``` | ||
|
||
```ts twoslash [cloudflare-pages] | ||
// functions/user/[name].ts | ||
|
||
import paramHandler from "@universal-middleware-examples/tool/params-handler-cloudflare-pages"; | ||
|
||
export const onRequest = paramHandler(); | ||
``` | ||
|
||
```ts twoslash [express.ts] | ||
import paramHandler from "@universal-middleware-examples/tool/params-handler-express"; | ||
import express from "express"; | ||
|
||
const app = express(); | ||
|
||
app.get("/user/:name", paramHandler()); | ||
|
||
export default app; | ||
``` | ||
|
||
```ts twoslash [fastify.ts] | ||
import paramHandler from "@universal-middleware-examples/tool/params-handler-fastify"; | ||
import fastify from "fastify"; | ||
|
||
const app = fastify(); | ||
|
||
app.get("/user/:name", paramHandler()); | ||
|
||
export default app; | ||
``` | ||
|
||
::: |
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 |
---|---|---|
|
@@ -206,6 +206,51 @@ | |
"types": "./dist/middlewares/universal-cloudflare-pages-middleware-guard.middleware.d.ts", | ||
"import": "./dist/middlewares/universal-cloudflare-pages-middleware-guard.middleware.js", | ||
"default": "./dist/middlewares/universal-cloudflare-pages-middleware-guard.middleware.js" | ||
}, | ||
"./params-handler": { | ||
"types": "./dist/params.d.ts", | ||
"import": "./dist/params.js", | ||
"default": "./dist/params.js" | ||
}, | ||
"./params-handler-hono": { | ||
"types": "./dist/handlers/universal-hono-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-hono-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-hono-handler-params.handler.js" | ||
}, | ||
"./params-handler-express": { | ||
"types": "./dist/handlers/universal-express-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-express-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-express-handler-params.handler.js" | ||
}, | ||
"./params-handler-hattip": { | ||
"types": "./dist/handlers/universal-hattip-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-hattip-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-hattip-handler-params.handler.js" | ||
}, | ||
"./params-handler-webroute": { | ||
"types": "./dist/handlers/universal-webroute-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-webroute-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-webroute-handler-params.handler.js" | ||
}, | ||
"./params-handler-fastify": { | ||
"types": "./dist/handlers/universal-fastify-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-fastify-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-fastify-handler-params.handler.js" | ||
}, | ||
"./params-handler-h3": { | ||
"types": "./dist/handlers/universal-h3-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-h3-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-h3-handler-params.handler.js" | ||
}, | ||
"./params-handler-cloudflare-worker": { | ||
"types": "./dist/handlers/universal-cloudflare-worker-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-cloudflare-worker-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-cloudflare-worker-handler-params.handler.js" | ||
}, | ||
"./params-handler-cloudflare-pages": { | ||
"types": "./dist/handlers/universal-cloudflare-pages-handler-params.handler.d.ts", | ||
"import": "./dist/handlers/universal-cloudflare-pages-handler-params.handler.js", | ||
"default": "./dist/handlers/universal-cloudflare-pages-handler-params.handler.js" | ||
} | ||
}, | ||
"author": "Joël Charles <[email protected]>", | ||
|
@@ -220,6 +265,7 @@ | |
"@hono/node-server": "^1.12.0", | ||
"@swc/core": "^1.7.11", | ||
"@types/node": "catalog:", | ||
"@universal-middleware/core": "workspace:*", | ||
"rimraf": "^6.0.0", | ||
"tsup": "^8.2.4", | ||
"typescript": "^5.5.4", | ||
|
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,23 @@ | ||
import type { UniversalHandler } from "@universal-middleware/core"; | ||
import { params } from "@universal-middleware/core"; | ||
|
||
interface RouteParamOption { | ||
route?: string; | ||
} | ||
|
||
const handler = ((options?) => (request, _context, runtime) => { | ||
const myParams = params(request, runtime, options?.route); | ||
|
||
if (myParams === null || !myParams.name) { | ||
// Provide a useful error message to the user | ||
throw new Error( | ||
"A route parameter named `:name` is required. " + | ||
"You can set your server route as `/user/:name`, or use the `route` option of this middleware " + | ||
"to achieve the same purpose.", | ||
); | ||
} | ||
|
||
return new Response(`User name is: ${myParams.name}`); | ||
}) satisfies (options?: RouteParamOption) => UniversalHandler; | ||
|
||
export default handler; |
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
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
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,4 @@ | ||
import { routeParamHandler } from "@universal-middleware/tests/utils"; | ||
import { createPagesFunction } from "../../src/index.js"; | ||
|
||
export const onRequest = createPagesFunction(routeParamHandler)(); |
Oops, something went wrong.