phero
phero copied to clipboard
implements the PheroRequest utility type
Building further upon #138 (PheroUnchecked), this utility type utilizes PheroUnchecked. When the user adds this to the middleware context Phero will populate the context automatically with the NodeJS' underlying IncomingMessage
(aka. the request). A user can use this to retrieve data from the request (like headers). Like so:
import { PheroContext, PheroRequest, PheroNextFunction, createService } from "@phero/server"
async function getArticle(ctx: PheroContext<{ ip: string }>): Promise<string> {
return ctx.ip
}
async function myMiddleware(ctx: PheroContext<{ req: PheroRequest }>, next: PheroNextFunction<{ ip: string }>) {
const ip = ctx.req.headers["x-forwarded-for"]
if (typeof ip !== "string") {
throw new Error("IP should be a string")
}
await next({ ip })
}
export const articleService = createService({
getArticle,
}, {
middleware: [myMiddleware]
})
Some limitations:
- for now is that the property should be named "req" and not "request" or something else
- you can not directly use
req: PheroRequest
in a function, only in middleware