Template
1
0

feat: add functional authentication

This commit is contained in:
2025-08-12 23:11:08 +02:00
parent f0630d43b7
commit 82d7a0d9cd
74 changed files with 763 additions and 396 deletions

View File

@@ -10,7 +10,7 @@ export class Route<const TState extends RouteState = RouteState> {
declare readonly $params: TState["params"] extends ZodObject ? z.input<TState["params"]> : never;
declare readonly $query: TState["query"] extends ZodObject ? z.input<TState["query"]> : never;
declare readonly $body: TState["body"] extends ZodType ? z.input<TState["body"]> : never;
declare readonly $response: TState["output"] extends ZodType ? z.output<TState["output"]> : never;
declare readonly $response: TState["response"] extends ZodType ? z.output<TState["response"]> : never;
#matchFn?: MatchFunction<any>;
@@ -69,16 +69,6 @@ export class Route<const TState extends RouteState = RouteState> {
return result.params as TParams;
}
/**
* Set the content the route expects, 'json' or 'form-data' which the client uses
* to determine which adapter operation to execute on requests.
*
* @param content - Content expected during transfers.
*/
content<TContent extends RouteContent>(content: TContent): Route<Omit<TState, "content"> & { content: TContent }> {
return new Route({ ...this.state, content });
}
/**
* Set the meta data for this route which can be used in e.g. OpenAPI generation
*
@@ -218,33 +208,6 @@ export class Route<const TState extends RouteState = RouteState> {
return new Route({ ...this.state, body });
}
/**
* Shape of the success 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<TState, "output"> & { output: TResponse }> {
return new Route({ ...this.state, output });
}
/**
* Instances of the possible error responses this route produces.
*
@@ -267,6 +230,33 @@ export class Route<const TState extends RouteState = RouteState> {
return new Route({ ...this.state, errors });
}
/**
* Shape of the success 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>(response: TResponse): Route<Omit<TState, "response"> & { response: TResponse }> {
return new Route({ ...this.state, response });
}
/**
* Server handler callback method.
*
@@ -286,7 +276,7 @@ export class Route<const TState extends RouteState = RouteState> {
* .handle(async ({ bar }, [ "string", number ]) => {});
* ```
*/
handle<THandleFn extends HandleFn<ServerArgs<TState>, TState["output"]>>(
handle<THandleFn extends HandleFn<ServerArgs<TState>, TState["response"]>>(
handle: THandleFn,
): Route<Omit<TState, "handle"> & { handle: THandleFn }> {
return new Route({ ...this.state, handle });
@@ -433,14 +423,13 @@ export type Routes = {
type RouteState = {
method: RouteMethod;
path: string;
content: RouteContent;
meta?: RouteMeta;
access?: RouteAccess;
params?: ZodObject;
query?: ZodObject;
body?: ZodType;
output?: ZodType;
errors: ServerErrorClass[];
response?: ZodType;
handle?: HandleFn;
hooks?: Hooks;
};
@@ -454,8 +443,6 @@ export type RouteMeta = {
export type RouteMethod = "POST" | "GET" | "PUT" | "PATCH" | "DELETE";
export type RouteContent = "json" | "form-data";
export type RouteAccess = "public" | "session" | (() => boolean)[];
export type AccessFn = (resource: string, action: string) => () => boolean;
@@ -466,8 +453,8 @@ export interface ServerContext {}
type HandleFn<TArgs extends Array<any> = any[], TResponse = any> = (
...args: TArgs
) => TResponse extends ZodType
? Promise<z.infer<TResponse> | Response | ServerError | unknown>
: Promise<Response | ServerError | unknown | void>;
? Promise<z.infer<TResponse> | Response | ServerError>
: Promise<Response | ServerError | void>;
type ServerArgs<TState extends RouteState> =
HasInputArgs<TState> extends true