feat: initial commit
This commit is contained in:
63
libraries/action.ts
Normal file
63
libraries/action.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import z, { ZodObject, ZodRawShape } from "zod";
|
||||
|
||||
export class Action<TActionState extends ActionState = ActionState> {
|
||||
constructor(readonly state: TActionState) {}
|
||||
|
||||
/**
|
||||
* Input object required by the action to fulfill its function.
|
||||
*
|
||||
* @param input - Schema defining the input requirements of the action.
|
||||
*/
|
||||
input<TInput extends ZodRawShape>(input: TInput): Action<Omit<TActionState, "input"> & { input: ZodObject<TInput> }> {
|
||||
return new Action({ ...this.state, input: z.object(input) as any });
|
||||
}
|
||||
|
||||
/**
|
||||
* Output object defining the result shape of the action.
|
||||
*
|
||||
* @param output - Schema defining the result shape.
|
||||
*/
|
||||
output<TOutput extends ZodRawShape>(output: TOutput): Action<Omit<TActionState, "output"> & { output: ZodObject<TOutput> }> {
|
||||
return new Action({ ...this.state, output: z.object(output) as any });
|
||||
}
|
||||
|
||||
/**
|
||||
* Add handler method to the action.
|
||||
*
|
||||
* @param handle - Handler method.
|
||||
*/
|
||||
handle<THandleFn extends ActionHandlerFn<this["state"]["input"], this["state"]["output"]>>(
|
||||
handle: THandleFn,
|
||||
): Action<Omit<TActionState, "handle"> & { handle: THandleFn }> {
|
||||
return new Action({ ...this.state, handle });
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Factory
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
export const action = {
|
||||
make(name: string) {
|
||||
return new Action({ name });
|
||||
},
|
||||
};
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Types
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
type ActionState = {
|
||||
name: string;
|
||||
input?: ZodObject;
|
||||
output?: ZodObject;
|
||||
handle?: ActionHandlerFn;
|
||||
};
|
||||
|
||||
type ActionHandlerFn<TInput = any, TOutput = any> = TInput extends ZodObject
|
||||
? (input: z.infer<TInput>) => TOutput extends ZodObject ? Promise<z.infer<TOutput>> : Promise<void>
|
||||
: () => TOutput extends ZodObject ? Promise<z.infer<TOutput>> : Promise<void>;
|
||||
227
libraries/errors.ts
Normal file
227
libraries/errors.ts
Normal file
@@ -0,0 +1,227 @@
|
||||
export abstract class RelayError<D = unknown> extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
readonly status: number,
|
||||
readonly data?: D,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
status: this.status,
|
||||
message: this.message,
|
||||
data: this.data,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class BadRequestError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new BadRequestError.
|
||||
*
|
||||
* The **HTTP 400 Bad Request** response status code indicates that the server
|
||||
* cannot or will not process the request due to something that is perceived to
|
||||
* be a client error.
|
||||
*
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Bad Request", data?: D) {
|
||||
super(message, 400, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class UnauthorizedError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new UnauthorizedError.
|
||||
*
|
||||
* The **HTTP 401 Unauthorized** response status code indicates that the client
|
||||
* request has not been completed because it lacks valid authentication
|
||||
* credentials for the requested resource.
|
||||
*
|
||||
* This status code is sent with an HTTP WWW-Authenticate response header that
|
||||
* contains information on how the client can request for the resource again after
|
||||
* prompting the user for authentication credentials.
|
||||
*
|
||||
* This status code is similar to the **403 Forbidden** status code, except that
|
||||
* in situations resulting in this status code, user authentication can allow
|
||||
* access to the resource.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Unauthorized".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Unauthorized", data?: D) {
|
||||
super(message, 401, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class ForbiddenError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new ForbiddenError.
|
||||
*
|
||||
* The **HTTP 403 Forbidden** response status code indicates that the server
|
||||
* understands the request but refuses to authorize it.
|
||||
*
|
||||
* This status is similar to **401**, but for the **403 Forbidden** status code
|
||||
* re-authenticating makes no difference. The access is permanently forbidden and
|
||||
* tied to the application logic, such as insufficient rights to a resource.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/403
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Forbidden".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Forbidden", data?: D) {
|
||||
super(message, 403, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new NotFoundError.
|
||||
*
|
||||
* The **HTTP 404 Not Found** response status code indicates that the server
|
||||
* cannot find the requested resource. Links that lead to a 404 page are often
|
||||
* called broken or dead links and can be subject to link rot.
|
||||
*
|
||||
* A 404 status code only indicates that the resource is missing: not whether the
|
||||
* absence is temporary or permanent. If a resource is permanently removed,
|
||||
* use the **410 _(Gone)_** status instead.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Not Found".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Not Found", data?: D) {
|
||||
super(message, 404, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class NotAcceptableError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new NotAcceptableError.
|
||||
*
|
||||
* The **HTTP 406 Not Acceptable** client error response code indicates that the
|
||||
* server cannot produce a response matching the list of acceptable values
|
||||
* defined in the request, and that the server is unwilling to supply a default
|
||||
* representation.
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Not Acceptable".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Not Acceptable", data?: D) {
|
||||
super(message, 406, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class ConflictError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new ConflictError.
|
||||
*
|
||||
* The **HTTP 409 Conflict** response status code indicates a request conflict
|
||||
* with the current state of the target resource.
|
||||
*
|
||||
* Conflicts are most likely to occur in response to a PUT request. For example,
|
||||
* you may get a 409 response when uploading a file that is older than the
|
||||
* existing one on the server, resulting in a version control conflict.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Conflict".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Conflict", data?: D) {
|
||||
super(message, 409, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class GoneError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new GoneError.
|
||||
*
|
||||
* The **HTTP 410 Gone** indicates that the target resource is no longer
|
||||
* available at the origin server and that this condition is likely to be
|
||||
* permanent. A 410 response is cacheable by default.
|
||||
*
|
||||
* Clients should not repeat requests for resources that return a 410 response,
|
||||
* and website owners should remove or replace links that return this code. If
|
||||
* server owners don't know whether this condition is temporary or permanent,
|
||||
* a 404 status code should be used instead.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/410
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Gone".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Gone", data?: D) {
|
||||
super(message, 410, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class UnprocessableContentError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new UnprocessableContentError.
|
||||
*
|
||||
* The **HTTP 422 Unprocessable Content** client error response status code
|
||||
* indicates that the server understood the content type of the request entity,
|
||||
* and the syntax of the request entity was correct, but it was unable to
|
||||
* process the contained instructions.
|
||||
*
|
||||
* Clients that receive a 422 response should expect that repeating the request
|
||||
* without modification will fail with the same error.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Unprocessable Content".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Unprocessable Content", data?: D) {
|
||||
super(message, 422, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class InternalServerError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new InternalServerError.
|
||||
*
|
||||
* The **HTTP 500 Internal Server Error** server error response code indicates that
|
||||
* the server encountered an unexpected condition that prevented it from fulfilling
|
||||
* the request.
|
||||
*
|
||||
* This error response is a generic "catch-all" response. Usually, this indicates
|
||||
* the server cannot find a better 5xx error code to response. Sometimes, server
|
||||
* administrators log error responses like the 500 status code with more details
|
||||
* about the request to prevent the error from happening again in the future.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Internal Server Error".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Internal Server Error", data?: D) {
|
||||
super(message, 500, data);
|
||||
}
|
||||
}
|
||||
|
||||
export class ServiceUnavailableError<D = unknown> extends RelayError<D> {
|
||||
/**
|
||||
* Instantiate a new ServiceUnavailableError.
|
||||
*
|
||||
* The **HTTP 503 Service Unavailable** server error response status code indicates
|
||||
* that the server is not ready to handle the request.
|
||||
*
|
||||
* This response should be used for temporary conditions and the Retry-After HTTP header
|
||||
* should contain the estimated time for the recovery of the service, if possible.
|
||||
*
|
||||
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503
|
||||
*
|
||||
* @param message - Optional message to send with the error. Default: "Service Unavailable".
|
||||
* @param data - Optional data to send with the error.
|
||||
*/
|
||||
constructor(message = "Service Unavailable", data?: D) {
|
||||
super(message, 503, data);
|
||||
}
|
||||
}
|
||||
449
libraries/relay.ts
Normal file
449
libraries/relay.ts
Normal file
@@ -0,0 +1,449 @@
|
||||
import z, { ZodType } from "zod";
|
||||
|
||||
import { BadRequestError, NotFoundError, RelayError } from "./errors.ts";
|
||||
import { Route, RouteMethod } from "./route.ts";
|
||||
|
||||
const SUPPORTED_MEHODS = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
||||
|
||||
export class Relay<TRoutes extends Route[]> {
|
||||
/**
|
||||
* Route maps funneling registered routes to the specific methods supported by
|
||||
* the relay instance.
|
||||
*/
|
||||
readonly routes: Routes = {
|
||||
POST: [],
|
||||
GET: [],
|
||||
PUT: [],
|
||||
PATCH: [],
|
||||
DELETE: [],
|
||||
};
|
||||
|
||||
/**
|
||||
* List of paths in the '${method} ${path}' format allowing us to quickly throw
|
||||
* errors if a duplicate route path is being added.
|
||||
*/
|
||||
readonly #paths = new Set<string>();
|
||||
|
||||
/**
|
||||
* Route index in the '${method} ${path}' format allowing for quick access to
|
||||
* a specific route.
|
||||
*/
|
||||
readonly #index = new Map<string, Route>();
|
||||
|
||||
/**
|
||||
* Instantiate a new Relay instance.
|
||||
*
|
||||
* @param config - Relay configuration to apply to the instance.
|
||||
* @param routes - Routes to register with the instance.
|
||||
*/
|
||||
constructor(
|
||||
readonly config: RelayConfig,
|
||||
routes: TRoutes,
|
||||
) {
|
||||
const methods: (keyof typeof this.routes)[] = [];
|
||||
for (const route of routes) {
|
||||
this.#validateRoutePath(route);
|
||||
this.routes[route.method].push(route);
|
||||
methods.push(route.method);
|
||||
this.#index.set(`${route.method} ${route.path}`, route);
|
||||
}
|
||||
for (const method of methods) {
|
||||
this.routes[method].sort(byStaticPriority);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Agnostic
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Retrieve a route for the given method/path combination which can be further extended
|
||||
* for serving incoming third party requests.
|
||||
*
|
||||
* @param method - Method the route is registered for.
|
||||
* @param path - Path the route is registered under.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* const relay = new Relay([
|
||||
* route
|
||||
* .post("/users")
|
||||
* .body(
|
||||
* z.object({
|
||||
* name: z.object({ family: z.string(), given: z.string() }),
|
||||
* email: z.string().check(z.email()),
|
||||
* })
|
||||
* )
|
||||
* ]);
|
||||
*
|
||||
* relay
|
||||
* .route("POST", "/users")
|
||||
* .actions([hasSessionUser, hasAccess("users", "create")])
|
||||
* .handle(async ({ name, email, sessionUserId }) => {
|
||||
* // await db.users.insert({ name, email, createdBy: sessionUserId });
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
route<
|
||||
TMethod extends RouteMethod,
|
||||
TPath extends Extract<TRoutes[number], { state: { method: TMethod } }>["state"]["path"],
|
||||
TRoute extends Extract<TRoutes[number], { state: { method: TMethod; path: TPath } }>,
|
||||
>(method: TMethod, path: TPath): TRoute {
|
||||
const route = this.#index.get(`${method} ${path}`);
|
||||
if (route === undefined) {
|
||||
throw new Error(`Relay > Route not found at '${method} ${path}' index`);
|
||||
}
|
||||
return route as TRoute;
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Client
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Send a "POST" request through the relay `fetch` adapter.
|
||||
*
|
||||
* @param path - Path to send request to.
|
||||
* @param args - List of request arguments.
|
||||
*/
|
||||
async post<
|
||||
TPath extends Extract<TRoutes[number], { state: { method: "POST" } }>["state"]["path"],
|
||||
TRoute extends Extract<TRoutes[number], { state: { method: "POST"; path: TPath } }>,
|
||||
>(path: TPath, ...args: TRoute["args"]): Promise<RelayResponse<TRoute>> {
|
||||
return this.#send("POST", path, args) as RelayResponse<TRoute>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a "GET" request through the relay `fetch` adapter.
|
||||
*
|
||||
* @param path - Path to send request to.
|
||||
* @param args - List of request arguments.
|
||||
*/
|
||||
async get<
|
||||
TPath extends Extract<TRoutes[number], { state: { method: "GET" } }>["state"]["path"],
|
||||
TRoute extends Extract<TRoutes[number], { state: { method: "GET"; path: TPath } }>,
|
||||
>(path: TPath, ...args: TRoute["args"]): Promise<RelayResponse<TRoute>> {
|
||||
return this.#send("GET", path, args) as RelayResponse<TRoute>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a "PUT" request through the relay `fetch` adapter.
|
||||
*
|
||||
* @param path - Path to send request to.
|
||||
* @param args - List of request arguments.
|
||||
*/
|
||||
async put<
|
||||
TPath extends Extract<TRoutes[number], { state: { method: "PUT" } }>["state"]["path"],
|
||||
TRoute extends Extract<TRoutes[number], { state: { method: "PUT"; path: TPath } }>,
|
||||
>(path: TPath, ...args: TRoute["args"]): Promise<RelayResponse<TRoute>> {
|
||||
return this.#send("PUT", path, args) as RelayResponse<TRoute>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a "PATCH" request through the relay `fetch` adapter.
|
||||
*
|
||||
* @param path - Path to send request to.
|
||||
* @param args - List of request arguments.
|
||||
*/
|
||||
async patch<
|
||||
TPath extends Extract<TRoutes[number], { state: { method: "PATCH" } }>["state"]["path"],
|
||||
TRoute extends Extract<TRoutes[number], { state: { method: "PATCH"; path: TPath } }>,
|
||||
>(path: TPath, ...args: TRoute["args"]): Promise<RelayResponse<TRoute>> {
|
||||
return this.#send("PATCH", path, args) as RelayResponse<TRoute>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a "DELETE" request through the relay `fetch` adapter.
|
||||
*
|
||||
* @param path - Path to send request to.
|
||||
* @param args - List of request arguments.
|
||||
*/
|
||||
async delete<
|
||||
TPath extends Extract<TRoutes[number], { state: { method: "DELETE" } }>["state"]["path"],
|
||||
TRoute extends Extract<TRoutes[number], { state: { method: "DELETE"; path: TPath } }>,
|
||||
>(path: TPath, ...args: TRoute["args"]): Promise<RelayResponse<TRoute>> {
|
||||
return this.#send("DELETE", path, args) as RelayResponse<TRoute>;
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Server
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Handle a incoming fetch request.
|
||||
*
|
||||
* @param request - Fetch request to pass to a route handler.
|
||||
*/
|
||||
async handle(request: Request) {
|
||||
const url = new URL(request.url);
|
||||
|
||||
const matched = this.#resolve(request.method, request.url);
|
||||
if (matched === undefined) {
|
||||
return toResponse(
|
||||
new NotFoundError(`Invalid routing path provided for ${request.url}`, {
|
||||
method: request.method,
|
||||
url: request.url,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const { route, params } = matched;
|
||||
|
||||
// ### Context
|
||||
// Context is passed to every route handler and provides a suite of functionality
|
||||
// and request data.
|
||||
|
||||
const context = {
|
||||
...params,
|
||||
...toSearch(url.searchParams),
|
||||
};
|
||||
|
||||
// ### Params
|
||||
// If the route has params we want to coerce the values to the expected types.
|
||||
|
||||
if (route.state.params !== undefined) {
|
||||
const result = await route.state.params.safeParseAsync(context.params);
|
||||
if (result.success === false) {
|
||||
return toResponse(new BadRequestError("Invalid request params", z.prettifyError(result.error)));
|
||||
}
|
||||
context.params = result.data;
|
||||
}
|
||||
|
||||
// ### Query
|
||||
// If the route has a query schema we need to validate and parse the query.
|
||||
|
||||
if (route.state.search !== undefined) {
|
||||
const result = await route.state.search.safeParseAsync(context.query ?? {});
|
||||
if (result.success === false) {
|
||||
return toResponse(new BadRequestError("Invalid request query", z.prettifyError(result.error)));
|
||||
}
|
||||
context.query = result.data;
|
||||
}
|
||||
|
||||
// ### Body
|
||||
// If the route has a body schema we need to validate and parse the body.
|
||||
|
||||
const body: Record<string, unknown> = {};
|
||||
|
||||
if (route.state.body !== undefined) {
|
||||
const result = await route.state.body.safeParseAsync(body);
|
||||
if (result.success === false) {
|
||||
return toResponse(new BadRequestError("Invalid request body", z.prettifyError(result.error)));
|
||||
}
|
||||
context.body = result.data;
|
||||
}
|
||||
|
||||
// ### Actions
|
||||
// Run through all assigned actions for the route.
|
||||
|
||||
if (route.state.actions !== undefined) {
|
||||
for (const action of route.state.actions) {
|
||||
const result = (await action.state.input?.safeParseAsync(context)) ?? { success: true, data: {} };
|
||||
if (result.success === false) {
|
||||
return toResponse(new BadRequestError("Invalid action input", z.prettifyError(result.error)));
|
||||
}
|
||||
const output = (await action.state.handle?.(result.data)) ?? {};
|
||||
for (const key in output) {
|
||||
context[key] = output[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ### Handler
|
||||
// Execute the route handler and apply the result.
|
||||
|
||||
return toResponse(await route.state.handle?.(context).catch((error) => error));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to resolve a route based on the given method and pathname.
|
||||
*
|
||||
* @param method - HTTP method.
|
||||
* @param url - HTTP request url.
|
||||
*/
|
||||
#resolve(method: string, url: string): ResolvedRoute | undefined {
|
||||
this.#assertMethod(method);
|
||||
for (const route of this.routes[method]) {
|
||||
if (route.match(url) === true) {
|
||||
return { route, params: route.getParsedParams(url) };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#validateRoutePath(route: Route): void {
|
||||
const path = `${route.method} ${route.path}`;
|
||||
if (this.#paths.has(path)) {
|
||||
throw new Error(`Router > Path ${path} already exists`);
|
||||
}
|
||||
this.#paths.add(path);
|
||||
}
|
||||
|
||||
async #send(method: RouteMethod, url: string, args: any[]) {
|
||||
const route = this.route(method, url);
|
||||
|
||||
// ### Input
|
||||
|
||||
const input: RequestInput = { method, url, search: "" };
|
||||
|
||||
let index = 0; // argument incrementor
|
||||
|
||||
if (route.state.params !== undefined) {
|
||||
const params = args[index++] as { [key: string]: string };
|
||||
for (const key in params) {
|
||||
input.url = input.url.replace(`:${key}`, params[key]);
|
||||
}
|
||||
}
|
||||
|
||||
if (route.state.search !== undefined) {
|
||||
const search = args[index++] as { [key: string]: string };
|
||||
const pieces: string[] = [];
|
||||
for (const key in search) {
|
||||
pieces.push(`${key}=${search[key]}`);
|
||||
}
|
||||
if (pieces.length > 0) {
|
||||
input.search = `?${pieces.join("&")}`;
|
||||
}
|
||||
}
|
||||
|
||||
if (route.state.body !== undefined) {
|
||||
input.body = JSON.stringify(args[index++]);
|
||||
}
|
||||
|
||||
// ### Fetch
|
||||
|
||||
const data = await this.config.adapter.fetch(input);
|
||||
if (route.state.output !== undefined) {
|
||||
return route.state.output.parse(data);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
#assertMethod(method: string): asserts method is RouteMethod {
|
||||
if (!SUPPORTED_MEHODS.includes(method)) {
|
||||
throw new Error(`Router > Unsupported method '${method}'`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Helpers
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sorting method for routes to ensure that static properties takes precedence
|
||||
* for when a route is matched against incoming requests.
|
||||
*
|
||||
* @param a - Route A
|
||||
* @param b - Route B
|
||||
*/
|
||||
function byStaticPriority(a: Route, b: Route) {
|
||||
const aSegments = a.path.split("/");
|
||||
const bSegments = b.path.split("/");
|
||||
|
||||
const maxLength = Math.max(aSegments.length, bSegments.length);
|
||||
|
||||
for (let i = 0; i < maxLength; i++) {
|
||||
const aSegment = aSegments[i] || "";
|
||||
const bSegment = bSegments[i] || "";
|
||||
|
||||
const isADynamic = aSegment.startsWith(":");
|
||||
const isBDynamic = bSegment.startsWith(":");
|
||||
|
||||
if (isADynamic !== isBDynamic) {
|
||||
return isADynamic ? 1 : -1;
|
||||
}
|
||||
|
||||
if (isADynamic === false && aSegment !== bSegment) {
|
||||
return aSegment.localeCompare(bSegment);
|
||||
}
|
||||
}
|
||||
|
||||
return a.path.localeCompare(b.path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve and return query object from the provided search parameters, or undefined
|
||||
* if the search parameters does not have any entries.
|
||||
*
|
||||
* @param searchParams - Search params to create a query object from.
|
||||
*/
|
||||
function toSearch(searchParams: URLSearchParams): object | undefined {
|
||||
if (searchParams.size === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const result: Record<string, string> = {};
|
||||
for (const [key, value] of searchParams.entries()) {
|
||||
result[key] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a server side request result and returns a fetch Response.
|
||||
*
|
||||
* @param result - Result to send back as a Response.
|
||||
*/
|
||||
function toResponse(result: object | RelayError | Response | void): Response {
|
||||
if (result instanceof Response) {
|
||||
return result;
|
||||
}
|
||||
if (result instanceof RelayError) {
|
||||
return new Response(result.message, {
|
||||
status: result.status,
|
||||
});
|
||||
}
|
||||
if (result === undefined) {
|
||||
return new Response(null, { status: 204 });
|
||||
}
|
||||
return new Response(JSON.stringify(result), {
|
||||
status: 200,
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Types
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
type Routes = {
|
||||
POST: Route[];
|
||||
GET: Route[];
|
||||
PUT: Route[];
|
||||
PATCH: Route[];
|
||||
DELETE: Route[];
|
||||
};
|
||||
|
||||
type ResolvedRoute = {
|
||||
route: Route;
|
||||
params: any;
|
||||
};
|
||||
|
||||
type RelayResponse<TRoute extends Route> = TRoute["state"]["output"] extends ZodType ? z.infer<TRoute["state"]["output"]> : void;
|
||||
|
||||
type RelayConfig = {
|
||||
adapter: RelayAdapter;
|
||||
};
|
||||
|
||||
export type RelayAdapter = {
|
||||
fetch(input: RequestInput): Promise<unknown>;
|
||||
};
|
||||
|
||||
export type RequestInput = {
|
||||
method: RouteMethod;
|
||||
url: string;
|
||||
search: string;
|
||||
body?: string;
|
||||
};
|
||||
332
libraries/route.ts
Normal file
332
libraries/route.ts
Normal file
@@ -0,0 +1,332 @@
|
||||
import z, { ZodObject, ZodRawShape, ZodType } from "zod";
|
||||
|
||||
import { Action } from "./action.ts";
|
||||
|
||||
export class Route<TRouteState extends RouteState = RouteState> {
|
||||
#pattern?: URLPattern;
|
||||
|
||||
declare readonly args: RouteArgs<TRouteState>;
|
||||
declare readonly context: RouteContext<TRouteState>;
|
||||
|
||||
constructor(readonly state: TRouteState) {}
|
||||
|
||||
/**
|
||||
* HTTP Method
|
||||
*/
|
||||
get method(): RouteMethod {
|
||||
return this.state.method;
|
||||
}
|
||||
|
||||
/**
|
||||
* URL pattern of the route.
|
||||
*/
|
||||
get pattern(): URLPattern {
|
||||
if (this.#pattern === undefined) {
|
||||
this.#pattern = new URLPattern({ pathname: this.path });
|
||||
}
|
||||
return this.#pattern;
|
||||
}
|
||||
|
||||
/**
|
||||
* URL path
|
||||
*/
|
||||
get path(): string {
|
||||
return this.state.path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the provided URL matches the route pattern.
|
||||
*
|
||||
* @param url - HTTP request.url
|
||||
*/
|
||||
match(url: string): boolean {
|
||||
return this.pattern.test(url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract parameters from the provided URL based on the route pattern.
|
||||
*
|
||||
* @param url - HTTP request.url
|
||||
*/
|
||||
getParsedParams(url: string): TRouteState["params"] extends ZodObject ? z.infer<TRouteState["params"]> : object {
|
||||
const params = this.pattern.exec(url)?.pathname.groups;
|
||||
if (params === undefined) {
|
||||
return {};
|
||||
}
|
||||
return this.state.params?.parse(params) ?? params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Params allows for custom casting of URL parameters. If a parameter does not
|
||||
* have a corresponding zod schema the default param type is "string".
|
||||
*
|
||||
* @param params - URL params.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route
|
||||
* .post("/foo/:bar")
|
||||
* .params({
|
||||
* bar: z.number({ coerce: true })
|
||||
* })
|
||||
* .handle(async ({ params: { bar } }) => {
|
||||
* console.log(typeof bar); // => number
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
params<TParams extends ZodRawShape>(params: TParams): Route<Omit<TRouteState, "params"> & { params: ZodObject<TParams> }> {
|
||||
return new Route({ ...this.state, params }) as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search allows for custom casting of URL search parameters. If a parameter does
|
||||
* not have a corresponding zod schema the default param type is "string".
|
||||
*
|
||||
* @param search - URL search arguments.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route
|
||||
* .post("/foo")
|
||||
* .search({
|
||||
* bar: z.number({ coerce: true })
|
||||
* })
|
||||
* .handle(async ({ search: { bar } }) => {
|
||||
* console.log(typeof bar); // => number
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
search<TSearch extends ZodRawShape>(search: TSearch): Route<Omit<TRouteState, "search"> & { search: ZodObject<TSearch> }> {
|
||||
return new Route({ ...this.state, search }) as any;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shape of the body this route expects to receive. This is used by all
|
||||
* mutator routes and has no effect when defined on "GET" methods.
|
||||
*
|
||||
* @param body - Body the route expects.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route
|
||||
* .post("/foo")
|
||||
* .body(
|
||||
* z.object({
|
||||
* bar: z.number()
|
||||
* })
|
||||
* )
|
||||
* .handle(async ({ bar }) => {
|
||||
* console.log(typeof bar); // => number
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
body<TBody extends ZodObject>(body: TBody): Route<Omit<TRouteState, "body"> & { body: TBody }> {
|
||||
return new Route({ ...this.state, body });
|
||||
}
|
||||
|
||||
/**
|
||||
* List of route level middleware action to execute before running the
|
||||
* route handler.
|
||||
*
|
||||
* @param actions - Actions to execute on this route.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* const hasFooBar = action
|
||||
* .make("hasFooBar")
|
||||
* .response(z.object({ foobar: z.number() }))
|
||||
* .handle(async () => {
|
||||
* return {
|
||||
* foobar: 1,
|
||||
* };
|
||||
* });
|
||||
*
|
||||
* route
|
||||
* .post("/foo")
|
||||
* .actions([hasFooBar])
|
||||
* .handle(async ({ foobar }) => {
|
||||
* console.log(typeof foobar); // => number
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
actions<TAction extends Action>(actions: TAction[]): Route<Omit<TRouteState, "actions"> & { actions: TAction[] }> {
|
||||
return new Route({ ...this.state, actions });
|
||||
}
|
||||
|
||||
/**
|
||||
* Shape of the response this route produces. This is used by the transform
|
||||
* tools to ensure the client receives parsed data.
|
||||
*
|
||||
* @param response - Response shape of the route.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route
|
||||
* .post("/foo")
|
||||
* .response(
|
||||
* z.object({
|
||||
* bar: z.number()
|
||||
* })
|
||||
* )
|
||||
* .handle(async () => {
|
||||
* return {
|
||||
* bar: 1
|
||||
* }
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
response<TResponse extends ZodType>(output: TResponse): Route<Omit<TRouteState, "output"> & { output: TResponse }> {
|
||||
return new Route({ ...this.state, output });
|
||||
}
|
||||
|
||||
/**
|
||||
* Server handler callback method.
|
||||
*
|
||||
* @param handle - Handle function to trigger when the route is executed.
|
||||
*/
|
||||
handle<THandleFn extends HandleFn<this["context"], this["state"]["output"]>>(handle: THandleFn): Route<Omit<TRouteState, "handle"> & { handle: THandleFn }> {
|
||||
return new Route({ ...this.state, handle });
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Factories
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/**
|
||||
* Route factories allowing for easy generation of relay compliant routes.
|
||||
*/
|
||||
export const route = {
|
||||
/**
|
||||
* Create a new "POST" route for the given path.
|
||||
*
|
||||
* @param path - Path to generate route for.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route
|
||||
* .post("/foo")
|
||||
* .body(
|
||||
* z.object({ bar: z.string() })
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
post<TPath extends string>(path: TPath) {
|
||||
return new Route({ method: "POST", path });
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new "GET" route for the given path.
|
||||
*
|
||||
* @param path - Path to generate route for.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route.get("/foo");
|
||||
* ```
|
||||
*/
|
||||
get<TPath extends string>(path: TPath) {
|
||||
return new Route({ method: "GET", path });
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new "PUT" route for the given path.
|
||||
*
|
||||
* @param path - Path to generate route for.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route
|
||||
* .put("/foo")
|
||||
* .body(
|
||||
* z.object({ bar: z.string() })
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
put<TPath extends string>(path: TPath) {
|
||||
return new Route({ method: "PUT", path });
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new "PATCH" route for the given path.
|
||||
*
|
||||
* @param path - Path to generate route for.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route
|
||||
* .patch("/foo")
|
||||
* .body(
|
||||
* z.object({ bar: z.string() })
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
patch<TPath extends string>(path: TPath) {
|
||||
return new Route({ method: "PATCH", path });
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new "DELETE" route for the given path.
|
||||
*
|
||||
* @param path - Path to generate route for.
|
||||
*
|
||||
* @examples
|
||||
*
|
||||
* ```ts
|
||||
* route.delete("/foo");
|
||||
* ```
|
||||
*/
|
||||
delete<TPath extends string>(path: TPath) {
|
||||
return new Route({ method: "DELETE", path });
|
||||
},
|
||||
};
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------------
|
||||
| Types
|
||||
|--------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
type RouteState = {
|
||||
method: RouteMethod;
|
||||
path: string;
|
||||
params?: ZodObject;
|
||||
search?: ZodObject;
|
||||
body?: ZodObject;
|
||||
actions?: Array<Action>;
|
||||
output?: ZodType;
|
||||
handle?: HandleFn;
|
||||
};
|
||||
|
||||
export type RouteMethod = "POST" | "GET" | "PUT" | "PATCH" | "DELETE";
|
||||
|
||||
export type HandleFn<TContext = any, TResponse = any> = (context: TContext) => TResponse extends ZodType ? Promise<z.infer<TResponse>> : Promise<void>;
|
||||
|
||||
type RouteContext<TRouteState extends RouteState = RouteState> = (TRouteState["params"] extends ZodObject ? z.infer<TRouteState["params"]> : object) &
|
||||
(TRouteState["search"] extends ZodObject ? z.infer<TRouteState["search"]> : object) &
|
||||
(TRouteState["body"] extends ZodObject ? z.infer<TRouteState["body"]> : object) &
|
||||
(TRouteState["actions"] extends Array<Action> ? UnionToIntersection<MergeAction<TRouteState["actions"]>> : object);
|
||||
|
||||
type RouteArgs<TRouteState extends RouteState = RouteState> = [
|
||||
...TupleIfZod<TRouteState["params"]>,
|
||||
...TupleIfZod<TRouteState["search"]>,
|
||||
...TupleIfZod<TRouteState["body"]>,
|
||||
];
|
||||
|
||||
type TupleIfZod<TState> = TState extends ZodObject ? [z.infer<TState>] : [];
|
||||
|
||||
type MergeAction<TActions extends Array<Action>> =
|
||||
TActions[number] extends Action<infer TActionState> ? (TActionState["output"] extends ZodObject ? z.infer<TActionState["output"]> : object) : object;
|
||||
|
||||
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
||||
Reference in New Issue
Block a user