refactor: make PRECHECKs an array again
This commit is contained in:
parent
5ee654f280
commit
62eba8612b
4 changed files with 30 additions and 21 deletions
|
@ -56,7 +56,7 @@ export type PRECHECK = (
|
||||||
request: Request,
|
request: Request,
|
||||||
meta: Record<string, any>
|
meta: Record<string, any>
|
||||||
) => undefined | Response | Promise<undefined | Response>;
|
) => undefined | Response | Promise<undefined | Response>;
|
||||||
export type PRECHECKS_TABLE = Record<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', PRECHECK>;
|
export type PRECHECKS_TABLE = Record<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', PRECHECK[]>;
|
||||||
export type ROUTE_HANDLER_METHOD = (request: Request, meta: Record<string, any>) => Promise<Response> | Response;
|
export type ROUTE_HANDLER_METHOD = (request: Request, meta: Record<string, any>) => Promise<Response> | Response;
|
||||||
|
|
||||||
export interface ROUTE_HANDLER {
|
export interface ROUTE_HANDLER {
|
||||||
|
@ -73,7 +73,7 @@ export interface ROUTE_HANDLER {
|
||||||
A default exported method will be called for any unspecified methods and can
|
A default exported method will be called for any unspecified methods and can
|
||||||
decide what to do with the request itself.
|
decide what to do with the request itself.
|
||||||
|
|
||||||
`PRECHECKS` can defined a precheck per-method, eg: `PRECHECKS.GET = ( request, meta ) => ...`
|
`PRECHECKS` can defined a precheck per-method, eg: `PRECHECKS.GET = [( request, meta ) => ...]`
|
||||||
|
|
||||||
A precheck method should return a `Response` if there's an error that should stop
|
A precheck method should return a `Response` if there's an error that should stop
|
||||||
the request from proceeding. For example, if you require a session for a given route,
|
the request from proceeding. For example, if you require a session for a given route,
|
||||||
|
@ -82,6 +82,11 @@ session, perhaps adding it to the `meta` data that will be passed to the `GET`
|
||||||
handler itself. If there is no session, however, it should return an HTTP `Response`
|
handler itself. If there is no session, however, it should return an HTTP `Response`
|
||||||
object indicating permission is denied or similar.
|
object indicating permission is denied or similar.
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
|
||||||
|
- [ ] reload typescript if it is modified on disk
|
||||||
|
|
||||||
|
|
||||||
#### NOTE ON WINDOWS
|
#### NOTE ON WINDOWS
|
||||||
|
|
||||||
Because Windows has more restrictions on filenames, you can use `___` in place of `:` in
|
Because Windows has more restrictions on filenames, you can use `___` in place of `:` in
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"name": "@andyburke/serverus",
|
"name": "@andyburke/serverus",
|
||||||
"description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.",
|
"description": "A flexible HTTP server for mixed content. Throw static files, markdown, Typescript and (hopefully, eventually) more into a directory and serverus can serve it up a bit more like old-school CGI.",
|
||||||
"version": "0.0.12",
|
"version": "0.1.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./serverus.ts",
|
".": "./serverus.ts",
|
||||||
|
|
|
@ -15,8 +15,8 @@ export type PRECHECK = (
|
||||||
meta: Record<string, any>
|
meta: Record<string, any>
|
||||||
) => undefined | Response | Promise<undefined | Response>;
|
) => undefined | Response | Promise<undefined | Response>;
|
||||||
|
|
||||||
/** A `PRECHECK_TABLE` maps from HTTP methods to your `PRECHECK`s. */
|
/** A `PRECHECK_TABLE` maps from HTTP methods to an array of `PRECHECK`s to be run. */
|
||||||
export type PRECHECKS_TABLE = Record<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', PRECHECK>;
|
export type PRECHECKS_TABLE = Record<'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', PRECHECK[]>;
|
||||||
|
|
||||||
/** A `ROUTE_HANDLER_METHOD` must take a `Request` and `meta` data and return a `Response`. */
|
/** A `ROUTE_HANDLER_METHOD` must take a `Request` and `meta` data and return a `Response`. */
|
||||||
export type ROUTE_HANDLER_METHOD = (request: Request, meta: Record<string, any>) => Promise<Response> | Response;
|
export type ROUTE_HANDLER_METHOD = (request: Request, meta: Record<string, any>) => Promise<Response> | Response;
|
||||||
|
@ -108,13 +108,15 @@ export default async function handle_typescript(request: Request): Promise<Respo
|
||||||
query
|
query
|
||||||
};
|
};
|
||||||
|
|
||||||
const precheck: PRECHECK | undefined = handler_module.PRECHECKS?.[request.method as keyof PRECHECKS_TABLE];
|
const prechecks: PRECHECK[] | undefined = handler_module.PRECHECKS?.[request.method as keyof PRECHECKS_TABLE];
|
||||||
if (precheck) {
|
if (Array.isArray(prechecks)) {
|
||||||
|
for (const precheck of prechecks) {
|
||||||
const result = await precheck(request, metadata);
|
const result = await precheck(request, metadata);
|
||||||
if (result) {
|
if (result) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return await method_handler(request, metadata);
|
return await method_handler(request, metadata);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
type PRECHECK = (req: Request, meta: Record<string, any>) => Promise<Response | undefined> | Response | undefined;
|
type PRECHECK = (req: Request, meta: Record<string, any>) => Promise<Response | undefined> | Response | undefined;
|
||||||
export const PRECHECKS: Record<string, PRECHECK> = {};
|
export const PRECHECKS: Record<string, PRECHECK[]> = {};
|
||||||
|
|
||||||
PRECHECKS.GET = (request: Request, _meta: Record<string, any>): Response | undefined => {
|
PRECHECKS.GET = [
|
||||||
|
(request: Request, _meta: Record<string, any>): Response | undefined => {
|
||||||
const secret = request.headers.get('x-secret');
|
const secret = request.headers.get('x-secret');
|
||||||
if (secret !== 'very secret') {
|
if (secret !== 'very secret') {
|
||||||
return new Response('Permission Denied', {
|
return new Response('Permission Denied', {
|
||||||
|
@ -11,7 +12,8 @@ PRECHECKS.GET = (request: Request, _meta: Record<string, any>): Response | undef
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
];
|
||||||
export function GET(_req: Request, _meta: Record<string, any>): Response {
|
export function GET(_req: Request, _meta: Record<string, any>): Response {
|
||||||
return new Response('this is secret', {
|
return new Response('this is secret', {
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue