@taserjs/router 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +24 -0
- package/dist/cjs/constants.cjs +17 -0
- package/dist/cjs/constants.cjs.map +1 -0
- package/dist/cjs/constants.d.cts +8 -0
- package/dist/cjs/create-context.cjs +8 -0
- package/dist/cjs/create-context.cjs.map +1 -0
- package/dist/cjs/create-context.d.cts +5 -0
- package/dist/cjs/define/handler.cjs +38 -0
- package/dist/cjs/define/handler.cjs.map +1 -0
- package/dist/cjs/define/handler.d.cts +13 -0
- package/dist/cjs/define/middleware.cjs +36 -0
- package/dist/cjs/define/middleware.cjs.map +1 -0
- package/dist/cjs/define/middleware.d.cts +85 -0
- package/dist/cjs/define/validators.cjs +14 -0
- package/dist/cjs/define/validators.cjs.map +1 -0
- package/dist/cjs/define/validators.d.cts +8 -0
- package/dist/cjs/factories.cjs +29 -0
- package/dist/cjs/factories.cjs.map +1 -0
- package/dist/cjs/factories.d.cts +61 -0
- package/dist/cjs/index.cjs +42 -0
- package/dist/cjs/index.d.cts +12 -0
- package/dist/cjs/middleware/auth.cjs +34 -0
- package/dist/cjs/middleware/auth.cjs.map +1 -0
- package/dist/cjs/middleware/auth.d.cts +9 -0
- package/dist/cjs/middleware/body-limit.cjs +12 -0
- package/dist/cjs/middleware/body-limit.cjs.map +1 -0
- package/dist/cjs/middleware/body-limit.d.cts +3 -0
- package/dist/cjs/middleware/compress.cjs +12 -0
- package/dist/cjs/middleware/compress.cjs.map +1 -0
- package/dist/cjs/middleware/compress.d.cts +3 -0
- package/dist/cjs/middleware/cors.cjs +12 -0
- package/dist/cjs/middleware/cors.cjs.map +1 -0
- package/dist/cjs/middleware/cors.d.cts +3 -0
- package/dist/cjs/middleware/csrf.cjs +12 -0
- package/dist/cjs/middleware/csrf.cjs.map +1 -0
- package/dist/cjs/middleware/csrf.d.cts +3 -0
- package/dist/cjs/middleware/etag.cjs +12 -0
- package/dist/cjs/middleware/etag.cjs.map +1 -0
- package/dist/cjs/middleware/etag.d.cts +3 -0
- package/dist/cjs/middleware/jwk.cjs +25 -0
- package/dist/cjs/middleware/jwk.cjs.map +1 -0
- package/dist/cjs/middleware/jwk.d.cts +15 -0
- package/dist/cjs/middleware/jwt.cjs +16 -0
- package/dist/cjs/middleware/jwt.cjs.map +1 -0
- package/dist/cjs/middleware/jwt.d.cts +10 -0
- package/dist/cjs/middleware/secure-headers.cjs +12 -0
- package/dist/cjs/middleware/secure-headers.cjs.map +1 -0
- package/dist/cjs/middleware/secure-headers.d.cts +3 -0
- package/dist/cjs/middleware/timing.cjs +12 -0
- package/dist/cjs/middleware/timing.cjs.map +1 -0
- package/dist/cjs/middleware/timing.d.cts +3 -0
- package/dist/cjs/middleware.cjs +17 -0
- package/dist/cjs/middleware.cjs.map +1 -0
- package/dist/cjs/middleware.d.cts +2 -0
- package/dist/cjs/register.d.cts +2 -0
- package/dist/cjs/route.cjs +94 -0
- package/dist/cjs/route.cjs.map +1 -0
- package/dist/cjs/route.d.cts +5 -0
- package/dist/cjs/taser-app.cjs +42 -0
- package/dist/cjs/taser-app.cjs.map +1 -0
- package/dist/cjs/taser-app.d.cts +21 -0
- package/dist/cjs/taser-router.cjs +83 -0
- package/dist/cjs/taser-router.cjs.map +1 -0
- package/dist/cjs/taser-router.d.cts +44 -0
- package/dist/cjs/types/app.d.cts +46 -0
- package/dist/cjs/types/index.d.cts +305 -0
- package/dist/cjs/types/returns.d.cts +37 -0
- package/dist/cjs/types/schema.d.cts +14 -0
- package/dist/cjs/types/type-utils.d.cts +52 -0
- package/dist/cjs/types/units.cjs +8 -0
- package/dist/cjs/types/units.cjs.map +1 -0
- package/dist/cjs/types/units.d.cts +98 -0
- package/dist/esm/constants.d.ts +8 -0
- package/dist/esm/constants.js +16 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/create-context.d.ts +5 -0
- package/dist/esm/create-context.js +8 -0
- package/dist/esm/create-context.js.map +1 -0
- package/dist/esm/define/handler.d.ts +13 -0
- package/dist/esm/define/handler.js +38 -0
- package/dist/esm/define/handler.js.map +1 -0
- package/dist/esm/define/middleware.d.ts +85 -0
- package/dist/esm/define/middleware.js +36 -0
- package/dist/esm/define/middleware.js.map +1 -0
- package/dist/esm/define/validators.d.ts +8 -0
- package/dist/esm/define/validators.js +14 -0
- package/dist/esm/define/validators.js.map +1 -0
- package/dist/esm/factories.d.ts +61 -0
- package/dist/esm/factories.js +21 -0
- package/dist/esm/factories.js.map +1 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/middleware/auth.d.ts +9 -0
- package/dist/esm/middleware/auth.js +33 -0
- package/dist/esm/middleware/auth.js.map +1 -0
- package/dist/esm/middleware/body-limit.d.ts +3 -0
- package/dist/esm/middleware/body-limit.js +11 -0
- package/dist/esm/middleware/body-limit.js.map +1 -0
- package/dist/esm/middleware/compress.d.ts +3 -0
- package/dist/esm/middleware/compress.js +11 -0
- package/dist/esm/middleware/compress.js.map +1 -0
- package/dist/esm/middleware/cors.d.ts +3 -0
- package/dist/esm/middleware/cors.js +11 -0
- package/dist/esm/middleware/cors.js.map +1 -0
- package/dist/esm/middleware/csrf.d.ts +3 -0
- package/dist/esm/middleware/csrf.js +11 -0
- package/dist/esm/middleware/csrf.js.map +1 -0
- package/dist/esm/middleware/etag.d.ts +3 -0
- package/dist/esm/middleware/etag.js +11 -0
- package/dist/esm/middleware/etag.js.map +1 -0
- package/dist/esm/middleware/jwk.d.ts +15 -0
- package/dist/esm/middleware/jwk.js +24 -0
- package/dist/esm/middleware/jwk.js.map +1 -0
- package/dist/esm/middleware/jwt.d.ts +10 -0
- package/dist/esm/middleware/jwt.js +15 -0
- package/dist/esm/middleware/jwt.js.map +1 -0
- package/dist/esm/middleware/secure-headers.d.ts +3 -0
- package/dist/esm/middleware/secure-headers.js +11 -0
- package/dist/esm/middleware/secure-headers.js.map +1 -0
- package/dist/esm/middleware/timing.d.ts +3 -0
- package/dist/esm/middleware/timing.js +11 -0
- package/dist/esm/middleware/timing.js.map +1 -0
- package/dist/esm/middleware.d.ts +2 -0
- package/dist/esm/middleware.js +17 -0
- package/dist/esm/middleware.js.map +1 -0
- package/dist/esm/register.d.ts +2 -0
- package/dist/esm/route.d.ts +5 -0
- package/dist/esm/route.js +94 -0
- package/dist/esm/route.js.map +1 -0
- package/dist/esm/taser-app.d.ts +21 -0
- package/dist/esm/taser-app.js +40 -0
- package/dist/esm/taser-app.js.map +1 -0
- package/dist/esm/taser-router.d.ts +44 -0
- package/dist/esm/taser-router.js +82 -0
- package/dist/esm/taser-router.js.map +1 -0
- package/dist/esm/types/app.d.ts +46 -0
- package/dist/esm/types/index.d.ts +305 -0
- package/dist/esm/types/returns.d.ts +37 -0
- package/dist/esm/types/schema.d.ts +14 -0
- package/dist/esm/types/type-utils.d.ts +52 -0
- package/dist/esm/types/units.d.ts +98 -0
- package/dist/esm/types/units.js +8 -0
- package/dist/esm/types/units.js.map +1 -0
- package/package.json +149 -0
- package/src/constants.ts +10 -0
- package/src/create-context.ts +19 -0
- package/src/define/handler.ts +72 -0
- package/src/define/middleware.ts +289 -0
- package/src/define/validators.ts +22 -0
- package/src/factories.ts +183 -0
- package/src/index.ts +44 -0
- package/src/middleware/auth.ts +51 -0
- package/src/middleware/body-limit.ts +11 -0
- package/src/middleware/compress.ts +11 -0
- package/src/middleware/cors.ts +11 -0
- package/src/middleware/csrf.ts +11 -0
- package/src/middleware/etag.ts +11 -0
- package/src/middleware/jwk.ts +47 -0
- package/src/middleware/jwt.ts +18 -0
- package/src/middleware/secure-headers.ts +11 -0
- package/src/middleware/timing.ts +11 -0
- package/src/middleware.ts +19 -0
- package/src/register.ts +1 -0
- package/src/route.ts +145 -0
- package/src/taser-app.ts +43 -0
- package/src/taser-router.ts +160 -0
- package/src/types/app.ts +65 -0
- package/src/types/index.ts +1064 -0
- package/src/types/returns.ts +135 -0
- package/src/types/schema.ts +22 -0
- package/src/types/type-utils.ts +86 -0
- package/src/types/units.ts +167 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import { Awaitable, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
+
import { ReplyResult } from '@taserjs/router-utils';
|
|
3
|
+
import { RouterRegister } from '../register.js';
|
|
4
|
+
import { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape, RuntimeContextFields, Simplify } from './type-utils.js';
|
|
5
|
+
import { ReturnsMap, ValidHandlerReply } from './returns.js';
|
|
6
|
+
import { Schema } from './schema.js';
|
|
7
|
+
import { AppContext, HandlerContext, HandlerUnit, Method, MiddlewareDefinition, MiddlewareReturnFromParts, MiddlewareUnit, NativeContext, ValidatorParts } from './units.js';
|
|
8
|
+
export type { Awaitable } from '@taserjs/router-core';
|
|
9
|
+
export type { SchemaRequestShape, InferInput, InferOutput, Schema, StandardSchemaV1, } from './schema.js';
|
|
10
|
+
export type { RouterRegister } from '../register.js';
|
|
11
|
+
export type { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape, Simplify, UnwrapPart, } from './type-utils.js';
|
|
12
|
+
export type { EmptyReturns, EnforceHandlerReply, HandlerReply, HasReturns, MergeReturns, ReplyResultFor, ReturnsMap, StatusCode, ValidHandlerReply, } from './returns.js';
|
|
13
|
+
export type { AppContext, HandlerContext, HandlerUnit, HttpMethod, InlineMiddlewareOptions, Method, MiddlewareDefinition, MiddlewareNext, MiddlewareReturnFromParts, MiddlewareUnit, NativeContext, StandaloneMiddlewareContext, ValidatorParts, } from './units.js';
|
|
14
|
+
export type RoutePath = RouterRegister extends {
|
|
15
|
+
RoutePath: infer P;
|
|
16
|
+
} ? P : never;
|
|
17
|
+
export type LayoutId = RouterRegister extends {
|
|
18
|
+
LayoutId: infer L;
|
|
19
|
+
} ? L : never;
|
|
20
|
+
export type LayoutTree = RouterRegister extends {
|
|
21
|
+
LayoutTree: infer T;
|
|
22
|
+
} ? T : Record<never, never>;
|
|
23
|
+
export type RouteByPathMethod = RouterRegister extends {
|
|
24
|
+
RouteByPathMethod: infer R;
|
|
25
|
+
} ? R : Record<never, never>;
|
|
26
|
+
type ParseParam<Segment extends string> = Segment extends '*' ? {
|
|
27
|
+
_splat: string;
|
|
28
|
+
} : Segment extends `:${infer Name extends string}` ? Name extends '' ? {} : {
|
|
29
|
+
[K in Name]: string;
|
|
30
|
+
} : {};
|
|
31
|
+
type ParseParamsFromSegments<S extends string> = S extends `${infer Segment}/${infer Rest}` ? ParseParam<Segment> & ParseParamsFromSegments<Rest> : ParseParam<S>;
|
|
32
|
+
export type PathParams<Path extends string> = Path extends `/${infer Rest}` ? ParseParamsFromSegments<Rest> : {};
|
|
33
|
+
type LayoutParent<Layout extends LayoutId> = LayoutTree[Layout] extends {
|
|
34
|
+
parent: infer Parent extends LayoutId | null;
|
|
35
|
+
} ? Parent : null;
|
|
36
|
+
type ResolveLayoutMiddlewares<Layout extends LayoutId | null> = Layout extends null ? readonly [] : Layout extends LayoutId ? readonly [
|
|
37
|
+
...ResolveLayoutMiddlewares<LayoutParent<Layout>>,
|
|
38
|
+
...LayoutTree[Layout]['middlewares']
|
|
39
|
+
] : readonly [];
|
|
40
|
+
/** Fold layout middleware Acc from a route's layoutChain (shallow → deep). */
|
|
41
|
+
type ResolveLayoutChainMiddlewares<Chain extends readonly LayoutId[]> = Chain extends readonly [infer Head extends LayoutId, ...infer Rest] ? readonly [
|
|
42
|
+
...LayoutTree[Head]['middlewares'],
|
|
43
|
+
...ResolveLayoutChainMiddlewares<Rest extends readonly LayoutId[] ? Rest : readonly []>
|
|
44
|
+
] : readonly [];
|
|
45
|
+
type RouteEntry<Path extends RoutePath, TMethod extends Method> = RouteByPathMethod[Path] extends {
|
|
46
|
+
[K in TMethod]: infer Entry;
|
|
47
|
+
} ? Entry : never;
|
|
48
|
+
type RouteParent<Path extends RoutePath, TMethod extends Method> = RouteEntry<Path, TMethod> extends {
|
|
49
|
+
parent: infer Parent extends LayoutId | null;
|
|
50
|
+
} ? Parent : null;
|
|
51
|
+
type RouteLayoutChain<Path extends RoutePath, TMethod extends Method> = RouteEntry<Path, TMethod> extends {
|
|
52
|
+
layoutChain: infer Chain extends readonly LayoutId[];
|
|
53
|
+
} ? Chain : RouteParent<Path, TMethod> extends LayoutId ? readonly [RouteParent<Path, TMethod>] : readonly [];
|
|
54
|
+
type RouteResolvedField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends 'query' | 'params' | 'body' | 'state' | 'ctx'> = MergeMiddlewareField<ResolveLayoutChainMiddlewares<RouteLayoutChain<Path, TMethod>>, Field> & MergeMiddlewareField<Acc, Field>;
|
|
55
|
+
type MiddlewareCtxField<Layout extends LayoutId, Field extends 'query' | 'params' | 'body' | 'state' | 'ctx'> = MergeMiddlewareField<ResolveLayoutMiddlewares<LayoutParent<Layout>>, Field>;
|
|
56
|
+
type RouteChainField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends 'query' | 'params' | 'body' | 'state' | 'ctx'> = RouteResolvedField<Path, TMethod, Acc, Field>;
|
|
57
|
+
type UnitRuntimeContext = RuntimeContextFields<NativeContext>;
|
|
58
|
+
export type RouteChainContext<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], TQuery, TParams, TBody, TAppContext extends Record<string, unknown> = AppContext> = Simplify<TAppContext & UnitRuntimeContext & RouteChainField<Path, TMethod, Acc, 'ctx'> & {
|
|
59
|
+
query: Simplify<MergePart<TQuery, RouteChainField<Path, TMethod, Acc, 'query'>>>;
|
|
60
|
+
params: Simplify<PathParams<Path> & MergePart<TParams, RouteChainField<Path, TMethod, Acc, 'params'>>>;
|
|
61
|
+
body: Simplify<MergePart<TBody, RouteChainField<Path, TMethod, Acc, 'body'>>>;
|
|
62
|
+
state: Simplify<RouteChainField<Path, TMethod, Acc, 'state'>>;
|
|
63
|
+
headers: TaserHeaders;
|
|
64
|
+
cookies: TaserCookieJar;
|
|
65
|
+
}>;
|
|
66
|
+
export type RouteHandleContext<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends {
|
|
67
|
+
query?: unknown;
|
|
68
|
+
params?: unknown;
|
|
69
|
+
body?: unknown;
|
|
70
|
+
}, TAppContext extends Record<string, unknown> = AppContext> = Simplify<TAppContext & UnitRuntimeContext & RouteResolvedField<Path, TMethod, Acc, 'ctx'> & {
|
|
71
|
+
path: Path;
|
|
72
|
+
method: TMethod;
|
|
73
|
+
query: Simplify<MergePart<Validators extends {
|
|
74
|
+
query?: infer Q;
|
|
75
|
+
} ? Q : unknown, RouteResolvedField<Path, TMethod, Acc, 'query'>>>;
|
|
76
|
+
params: Simplify<PathParams<Path> & MergePart<Validators extends {
|
|
77
|
+
params?: infer P;
|
|
78
|
+
} ? P : unknown, RouteResolvedField<Path, TMethod, Acc, 'params'>>>;
|
|
79
|
+
body: Simplify<MergePart<Validators extends {
|
|
80
|
+
body?: infer B;
|
|
81
|
+
} ? B : unknown, RouteResolvedField<Path, TMethod, Acc, 'body'>>>;
|
|
82
|
+
state: Simplify<RouteResolvedField<Path, TMethod, Acc, 'state'>>;
|
|
83
|
+
headers: TaserHeaders;
|
|
84
|
+
cookies: TaserCookieJar;
|
|
85
|
+
}>;
|
|
86
|
+
export type RouteHandleContextWithoutBody<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends {
|
|
87
|
+
query?: unknown;
|
|
88
|
+
params?: unknown;
|
|
89
|
+
}, TAppContext extends Record<string, unknown> = AppContext> = Simplify<TAppContext & UnitRuntimeContext & RouteResolvedField<Path, TMethod, Acc, 'ctx'> & {
|
|
90
|
+
path: Path;
|
|
91
|
+
method: TMethod;
|
|
92
|
+
query: Simplify<MergePart<Validators extends {
|
|
93
|
+
query?: infer Q;
|
|
94
|
+
} ? Q : unknown, RouteResolvedField<Path, TMethod, Acc, 'query'>>>;
|
|
95
|
+
params: Simplify<PathParams<Path> & MergePart<Validators extends {
|
|
96
|
+
params?: infer P;
|
|
97
|
+
} ? P : unknown, RouteResolvedField<Path, TMethod, Acc, 'params'>>>;
|
|
98
|
+
body: never;
|
|
99
|
+
state: Simplify<RouteResolvedField<Path, TMethod, Acc, 'state'>>;
|
|
100
|
+
headers: TaserHeaders;
|
|
101
|
+
cookies: TaserCookieJar;
|
|
102
|
+
}>;
|
|
103
|
+
type RouteResolvedInputField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends 'query' | 'params' | 'body'> = MergeMiddlewareInputField<ResolveLayoutChainMiddlewares<RouteLayoutChain<Path, TMethod>>, Field> & MergeMiddlewareInputField<Acc, Field>;
|
|
104
|
+
type IsNever<T> = [T] extends [never] ? true : false;
|
|
105
|
+
type IsEmptyObject<T> = [keyof T] extends [never] ? true : false;
|
|
106
|
+
type InputFacet<T, K extends string> = IsNever<T> extends true ? {} : IsEmptyObject<T> extends true ? {} : {
|
|
107
|
+
[Key in K]: T;
|
|
108
|
+
};
|
|
109
|
+
type RouteQueryInput<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts> = Simplify<MergePart<RequestShape<Validators extends {
|
|
110
|
+
queryIn?: infer QI;
|
|
111
|
+
} ? QI : unknown, Validators extends {
|
|
112
|
+
query?: infer Q;
|
|
113
|
+
} ? Q : unknown>, RouteResolvedInputField<Path, TMethod, Acc, 'query'>>>;
|
|
114
|
+
type RouteParamsInput<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts> = Simplify<PathParams<Path> & MergePart<RequestShape<Validators extends {
|
|
115
|
+
paramsIn?: infer PI;
|
|
116
|
+
} ? PI : unknown, Validators extends {
|
|
117
|
+
params?: infer P;
|
|
118
|
+
} ? P : unknown>, RouteResolvedInputField<Path, TMethod, Acc, 'params'>>>;
|
|
119
|
+
type RouteBodyInput<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts> = Simplify<MergePart<RequestShape<Validators extends {
|
|
120
|
+
bodyIn?: infer BI;
|
|
121
|
+
} ? BI : unknown, Validators extends {
|
|
122
|
+
body?: infer B;
|
|
123
|
+
} ? B : unknown>, RouteResolvedInputField<Path, TMethod, Acc, 'body'>>>;
|
|
124
|
+
export type RouteHandleInputWithoutBody<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts = {}> = Simplify<InputFacet<RouteQueryInput<Path, TMethod, Acc, Validators>, 'query'> & InputFacet<RouteParamsInput<Path, TMethod, Acc, Validators>, 'params'>>;
|
|
125
|
+
export type RouteHandleInput<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts = {}> = Simplify<RouteHandleInputWithoutBody<Path, TMethod, Acc, Validators> & InputFacet<RouteBodyInput<Path, TMethod, Acc, Validators>, 'body'>>;
|
|
126
|
+
export type InferRouteInput<Path extends RoutePath, TMethod extends Method> = RouteEntry<Path, TMethod> extends {
|
|
127
|
+
route: infer Route;
|
|
128
|
+
} ? Route extends {
|
|
129
|
+
$Infer: {
|
|
130
|
+
Input: infer I;
|
|
131
|
+
};
|
|
132
|
+
} ? I : never : never;
|
|
133
|
+
type MiddlewareChainField<Layout extends LayoutId, Acc extends readonly unknown[], Field extends 'query' | 'params' | 'body' | 'state' | 'ctx'> = MiddlewareCtxField<Layout, Field> & MergeMiddlewareField<Acc, Field>;
|
|
134
|
+
export type MiddlewareChainContext<Layout extends LayoutId, Acc extends readonly unknown[], TQuery, TParams, TBody, TAppContext extends Record<string, unknown> = AppContext> = Simplify<TAppContext & UnitRuntimeContext & MiddlewareChainField<Layout, Acc, 'ctx'> & {
|
|
135
|
+
query: Simplify<MergePart<TQuery, MiddlewareChainField<Layout, Acc, 'query'>>>;
|
|
136
|
+
params: Simplify<MergePart<TParams, MiddlewareChainField<Layout, Acc, 'params'>>>;
|
|
137
|
+
body: Simplify<MergePart<TBody, MiddlewareChainField<Layout, Acc, 'body'>>>;
|
|
138
|
+
state: Simplify<MiddlewareChainField<Layout, Acc, 'state'>>;
|
|
139
|
+
headers: TaserHeaders;
|
|
140
|
+
cookies: TaserCookieJar;
|
|
141
|
+
}>;
|
|
142
|
+
type StatefulUseOptions<Ctx, TState, TQuery, TParams, TBody, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown> = {
|
|
143
|
+
state: Schema<TState>;
|
|
144
|
+
query?: Schema<TQuery, TQueryIn>;
|
|
145
|
+
params?: Schema<TParams, TParamsIn>;
|
|
146
|
+
body?: Schema<TBody, TBodyIn>;
|
|
147
|
+
returns?: TReturns;
|
|
148
|
+
handler: (ctx: Ctx, next: (args: {
|
|
149
|
+
state: TState;
|
|
150
|
+
}) => Promise<ReplyResult>) => Awaitable<ReplyResult | Response | unknown>;
|
|
151
|
+
};
|
|
152
|
+
type CtxUseOptions<Ctx, TCtx, TQuery, TParams, TBody, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown> = {
|
|
153
|
+
ctx: Schema<TCtx>;
|
|
154
|
+
query?: Schema<TQuery, TQueryIn>;
|
|
155
|
+
params?: Schema<TParams, TParamsIn>;
|
|
156
|
+
body?: Schema<TBody, TBodyIn>;
|
|
157
|
+
returns?: TReturns;
|
|
158
|
+
handler: (ctx: Ctx, next: (args: {
|
|
159
|
+
ctx: TCtx;
|
|
160
|
+
}) => Promise<ReplyResult>) => Awaitable<ReplyResult | Response | unknown>;
|
|
161
|
+
};
|
|
162
|
+
type StateAndCtxUseOptions<Ctx, TState, TCtx, TQuery, TParams, TBody, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown> = {
|
|
163
|
+
state: Schema<TState>;
|
|
164
|
+
ctx: Schema<TCtx>;
|
|
165
|
+
query?: Schema<TQuery, TQueryIn>;
|
|
166
|
+
params?: Schema<TParams, TParamsIn>;
|
|
167
|
+
body?: Schema<TBody, TBodyIn>;
|
|
168
|
+
returns?: TReturns;
|
|
169
|
+
handler: (ctx: Ctx, next: (args: {
|
|
170
|
+
state: TState;
|
|
171
|
+
ctx: TCtx;
|
|
172
|
+
}) => Promise<ReplyResult>) => Awaitable<ReplyResult | Response | unknown>;
|
|
173
|
+
};
|
|
174
|
+
type StatelessUseOptions<Ctx, TQuery, TParams, TBody, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown> = {
|
|
175
|
+
query?: Schema<TQuery, TQueryIn>;
|
|
176
|
+
params?: Schema<TParams, TParamsIn>;
|
|
177
|
+
body?: Schema<TBody, TBodyIn>;
|
|
178
|
+
returns?: TReturns;
|
|
179
|
+
handler: (ctx: Ctx, next: () => Promise<ReplyResult>) => Awaitable<ReplyResult | Response | unknown>;
|
|
180
|
+
};
|
|
181
|
+
export type MiddlewareBuilder<Layout extends LayoutId, Acc extends readonly unknown[] = readonly [], TAppContext extends Record<string, unknown> = AppContext> = Acc & {
|
|
182
|
+
readonly layout: Layout;
|
|
183
|
+
readonly middlewares: readonly MiddlewareDefinition[];
|
|
184
|
+
readonly $Infer: {
|
|
185
|
+
Context: MiddlewareChainContext<Layout, Acc, unknown, unknown, unknown, TAppContext>;
|
|
186
|
+
};
|
|
187
|
+
use<TAcc, TReturns extends ReturnsMap = {}>(unit: MiddlewareUnit<TAcc, TReturns>): MiddlewareBuilder<Layout, readonly [...Acc, TAcc], TAppContext>;
|
|
188
|
+
use<TState, TCtx, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StateAndCtxUseOptions<MiddlewareChainContext<Layout, Acc, TQuery, TParams, TBody, TAppContext>, TState, TCtx, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn>): MiddlewareBuilder<Layout, readonly [
|
|
189
|
+
...Acc,
|
|
190
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TCtx, TQueryIn, TParamsIn, TBodyIn>
|
|
191
|
+
], TAppContext>;
|
|
192
|
+
use<TState, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StatefulUseOptions<MiddlewareChainContext<Layout, Acc, TQuery, TParams, TBody, TAppContext>, TState, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn>): MiddlewareBuilder<Layout, readonly [
|
|
193
|
+
...Acc,
|
|
194
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, unknown, TQueryIn, TParamsIn, TBodyIn>
|
|
195
|
+
], TAppContext>;
|
|
196
|
+
use<TCtx, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: CtxUseOptions<MiddlewareChainContext<Layout, Acc, TQuery, TParams, TBody, TAppContext>, TCtx, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn>): MiddlewareBuilder<Layout, readonly [
|
|
197
|
+
...Acc,
|
|
198
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, {}, TCtx, TQueryIn, TParamsIn, TBodyIn>
|
|
199
|
+
], TAppContext>;
|
|
200
|
+
use<TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StatelessUseOptions<MiddlewareChainContext<Layout, Acc, TQuery, TParams, TBody, TAppContext>, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn>): MiddlewareBuilder<Layout, readonly [
|
|
201
|
+
...Acc,
|
|
202
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, {}, unknown, TQueryIn, TParamsIn, TBodyIn>
|
|
203
|
+
], TAppContext>;
|
|
204
|
+
};
|
|
205
|
+
export type RouteExport<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], TReturns extends ReturnsMap = {}, TOutput = Response> = Acc & {
|
|
206
|
+
readonly path: Path;
|
|
207
|
+
readonly method: TMethod | 'ANY' | 'ALL';
|
|
208
|
+
readonly methods?: readonly Method[];
|
|
209
|
+
readonly middlewares: readonly MiddlewareDefinition[];
|
|
210
|
+
readonly handlerMiddlewares: readonly MiddlewareDefinition[];
|
|
211
|
+
readonly returns?: TReturns;
|
|
212
|
+
handler: (ctx: unknown) => Awaitable<Response>;
|
|
213
|
+
readonly $Infer: {
|
|
214
|
+
Context: TMethod extends 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' ? RouteHandleContextWithoutBody<Path, TMethod, Acc, {}> : RouteHandleContext<Path, TMethod, Acc, {}>;
|
|
215
|
+
/** Concrete handler reply (`ReplyOf` union) for client inference. */
|
|
216
|
+
Output: TOutput;
|
|
217
|
+
};
|
|
218
|
+
query?: Schema<unknown>;
|
|
219
|
+
params?: Schema<unknown>;
|
|
220
|
+
body?: Schema<unknown>;
|
|
221
|
+
handlerQuery?: Schema<unknown>;
|
|
222
|
+
handlerParams?: Schema<unknown>;
|
|
223
|
+
handlerBody?: Schema<unknown>;
|
|
224
|
+
};
|
|
225
|
+
type RouteHandleResult<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts, TReturns extends ReturnsMap = {}, TOutput = Response, TAppContext extends Record<string, unknown> = AppContext> = Omit<RouteExport<Path, TMethod, Acc, TReturns, TOutput>, '$Infer'> & {
|
|
226
|
+
readonly $Infer: {
|
|
227
|
+
Context: TMethod extends 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' ? RouteHandleContextWithoutBody<Path, TMethod, Acc, Validators, TAppContext> : RouteHandleContext<Path, TMethod, Acc, Validators, TAppContext>;
|
|
228
|
+
Input: TMethod extends 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' ? RouteHandleInputWithoutBody<Path, TMethod, Acc, Validators> : RouteHandleInput<Path, TMethod, Acc, Validators>;
|
|
229
|
+
Output: TOutput;
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
export type RouteBuilder<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[] = readonly [], Validators extends ValidatorParts = {}, TReturns extends ReturnsMap = {}, TAppContext extends Record<string, unknown> = AppContext> = {
|
|
233
|
+
readonly path: Path;
|
|
234
|
+
readonly method: TMethod;
|
|
235
|
+
readonly $Infer: {
|
|
236
|
+
Context: TMethod extends 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' ? RouteHandleContextWithoutBody<Path, TMethod, Acc, Validators, TAppContext> : RouteHandleContext<Path, TMethod, Acc, Validators, TAppContext>;
|
|
237
|
+
};
|
|
238
|
+
returns<const M extends ReturnsMap>(map: M): RouteBuilder<Path, TMethod, Acc, Validators, Omit<TReturns, keyof M> & M, TAppContext>;
|
|
239
|
+
use<TAcc, UReturns extends ReturnsMap = {}>(unit: MiddlewareUnit<TAcc, UReturns>): RouteBuilder<Path, TMethod, readonly [...Acc, TAcc], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
240
|
+
use<TState, TCtx, TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StateAndCtxUseOptions<RouteChainContext<Path, TMethod, Acc, TQuery, TParams, TBody, TAppContext>, TState, TCtx, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): RouteBuilder<Path, TMethod, readonly [
|
|
241
|
+
...Acc,
|
|
242
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TCtx, TQueryIn, TParamsIn, TBodyIn>
|
|
243
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
244
|
+
use<TState, TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StatefulUseOptions<RouteChainContext<Path, TMethod, Acc, TQuery, TParams, TBody, TAppContext>, TState, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): RouteBuilder<Path, TMethod, readonly [
|
|
245
|
+
...Acc,
|
|
246
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, unknown, TQueryIn, TParamsIn, TBodyIn>
|
|
247
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
248
|
+
use<TCtx, TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: CtxUseOptions<RouteChainContext<Path, TMethod, Acc, TQuery, TParams, TBody, TAppContext>, TCtx, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): RouteBuilder<Path, TMethod, readonly [
|
|
249
|
+
...Acc,
|
|
250
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, {}, TCtx, TQueryIn, TParamsIn, TBodyIn>
|
|
251
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
252
|
+
use<TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StatelessUseOptions<RouteChainContext<Path, TMethod, Acc, TQuery, TParams, TBody, TAppContext>, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): RouteBuilder<Path, TMethod, readonly [
|
|
253
|
+
...Acc,
|
|
254
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, {}, unknown, TQueryIn, TParamsIn, TBodyIn>
|
|
255
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
256
|
+
handler<R extends Response = Response>(fn: (ctx: TMethod extends 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' ? RouteHandleContextWithoutBody<Path, TMethod, Acc, Validators, TAppContext> : RouteHandleContext<Path, TMethod, Acc, Validators, TAppContext>) => Awaitable<R>, ..._assert: [R] extends [ValidHandlerReply<R, TReturns>] ? [] : [
|
|
257
|
+
{
|
|
258
|
+
expected: ValidHandlerReply<R, TReturns>;
|
|
259
|
+
actual: R;
|
|
260
|
+
}
|
|
261
|
+
]): RouteHandleResult<Path, TMethod, Acc, Validators, TReturns, R, TAppContext>;
|
|
262
|
+
handler<HandlerAcc extends readonly unknown[] = readonly [], HandlerValidators extends ValidatorParts = ValidatorParts, HReturns extends ReturnsMap = {}, HOutput = Response>(unit: HandlerUnit<HandlerAcc, HandlerValidators, HReturns, HOutput>): RouteHandleResult<Path, TMethod, readonly [...Acc, ...HandlerAcc], Validators, Omit<TReturns, keyof HReturns> & HReturns, HOutput, TAppContext>;
|
|
263
|
+
};
|
|
264
|
+
export type HandlerBuilder<Acc extends readonly unknown[] = readonly [], Validators extends ValidatorParts = {}, TReturns extends ReturnsMap = {}, TAppContext extends Record<string, unknown> = AppContext> = {
|
|
265
|
+
returns<const M extends ReturnsMap>(map: M): HandlerBuilder<Acc, Validators, Omit<TReturns, keyof M> & M, TAppContext>;
|
|
266
|
+
use<TAcc, UReturns extends ReturnsMap = {}>(unit: MiddlewareUnit<TAcc, UReturns>): HandlerBuilder<readonly [...Acc, TAcc], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
267
|
+
use<TState, TCtx, TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StateAndCtxUseOptions<HandlerContext<Acc, {
|
|
268
|
+
query: TQuery;
|
|
269
|
+
params: TParams;
|
|
270
|
+
body: TBody;
|
|
271
|
+
}, TAppContext>, TState, TCtx, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): HandlerBuilder<readonly [
|
|
272
|
+
...Acc,
|
|
273
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TCtx, TQueryIn, TParamsIn, TBodyIn>
|
|
274
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
275
|
+
use<TState, TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StatefulUseOptions<HandlerContext<Acc, {
|
|
276
|
+
query: TQuery;
|
|
277
|
+
params: TParams;
|
|
278
|
+
body: TBody;
|
|
279
|
+
}, TAppContext>, TState, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): HandlerBuilder<readonly [
|
|
280
|
+
...Acc,
|
|
281
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, unknown, TQueryIn, TParamsIn, TBodyIn>
|
|
282
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
283
|
+
use<TCtx, TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: CtxUseOptions<HandlerContext<Acc, {
|
|
284
|
+
query: TQuery;
|
|
285
|
+
params: TParams;
|
|
286
|
+
body: TBody;
|
|
287
|
+
}, TAppContext>, TCtx, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): HandlerBuilder<readonly [
|
|
288
|
+
...Acc,
|
|
289
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, {}, TCtx, TQueryIn, TParamsIn, TBodyIn>
|
|
290
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
291
|
+
use<TQuery = unknown, TParams = unknown, TBody = unknown, UReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown>(options: StatelessUseOptions<HandlerContext<Acc, {
|
|
292
|
+
query: TQuery;
|
|
293
|
+
params: TParams;
|
|
294
|
+
body: TBody;
|
|
295
|
+
}, TAppContext>, TQuery, TParams, TBody, UReturns, TQueryIn, TParamsIn, TBodyIn>): HandlerBuilder<readonly [
|
|
296
|
+
...Acc,
|
|
297
|
+
MiddlewareReturnFromParts<TQuery, TParams, TBody, {}, unknown, TQueryIn, TParamsIn, TBodyIn>
|
|
298
|
+
], Validators, Omit<TReturns, keyof UReturns> & UReturns, TAppContext>;
|
|
299
|
+
handler<R extends Response = Response>(fn: (ctx: HandlerContext<Acc, Validators, TAppContext>) => Awaitable<R>, ..._assert: [R] extends [ValidHandlerReply<R, TReturns>] ? [] : [
|
|
300
|
+
{
|
|
301
|
+
expected: ValidHandlerReply<R, TReturns>;
|
|
302
|
+
actual: R;
|
|
303
|
+
}
|
|
304
|
+
]): HandlerUnit<Acc, Validators, TReturns, R>;
|
|
305
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ReplyOf } from '@taserjs/router-utils';
|
|
2
|
+
import { Simplify } from './type-utils.js';
|
|
3
|
+
import { Schema, InferOutput } from './schema.js';
|
|
4
|
+
/**
|
|
5
|
+
* Known HTTP status codes. `number & {}` allows custom codes while keeping
|
|
6
|
+
* literal autocomplete / inference for well-known values.
|
|
7
|
+
*/
|
|
8
|
+
export type StatusCode = 100 | 101 | 102 | 103 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 421 | 422 | 423 | 424 | 425 | 426 | 428 | 429 | 431 | 451 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 510 | 511 | (number & {});
|
|
9
|
+
/** Status-code → Standard Schema map for route/middleware/handler outputs. */
|
|
10
|
+
export type ReturnsMap = {
|
|
11
|
+
readonly [K in StatusCode]?: Schema<unknown>;
|
|
12
|
+
};
|
|
13
|
+
/** Later map wins on overlapping status keys. */
|
|
14
|
+
export type MergeReturns<A extends ReturnsMap, B extends ReturnsMap> = Omit<A, keyof B> & B;
|
|
15
|
+
/** Typed reply union for statuses declared in a returns map. */
|
|
16
|
+
export type ReplyResultFor<M extends ReturnsMap> = {
|
|
17
|
+
[S in keyof M]-?: S extends StatusCode ? M[S] extends Schema<unknown> ? ReplyOf<S, Simplify<InferOutput<M[S]>>> : never : never;
|
|
18
|
+
}[keyof M];
|
|
19
|
+
export type EmptyReturns = {};
|
|
20
|
+
/** True when no status schemas have been declared yet. */
|
|
21
|
+
export type HasReturns<M extends ReturnsMap> = [keyof M] extends [never] ? false : true;
|
|
22
|
+
/**
|
|
23
|
+
* Validate a concrete reply `R` against returns map `M`.
|
|
24
|
+
* Declared statuses must match schema output; undeclared statuses stay open
|
|
25
|
+
* (same rule as runtime `validateReply`).
|
|
26
|
+
*/
|
|
27
|
+
export type ValidHandlerReply<R, M extends ReturnsMap> = R extends ReplyOf<infer S, infer B> ? S extends keyof M ? M[S] extends Schema<unknown> ? B extends Simplify<InferOutput<M[S]>> ? R : never : R : R : R extends Response ? R : never;
|
|
28
|
+
/**
|
|
29
|
+
* Narrow a handler return so declared statuses are schema-checked without
|
|
30
|
+
* breaking multi-status unions (avoids circular `R extends …<R>` constraints).
|
|
31
|
+
*/
|
|
32
|
+
export type EnforceHandlerReply<R, M extends ReturnsMap> = [R] extends [ValidHandlerReply<R, M>] ? R : ValidHandlerReply<R, M>;
|
|
33
|
+
/**
|
|
34
|
+
* Expected `.handler` return type for a returns map.
|
|
35
|
+
* Prefer constraining handler generics with {@link EnforceHandlerReply}.
|
|
36
|
+
*/
|
|
37
|
+
export type HandlerReply<M extends ReturnsMap> = HasReturns<M> extends true ? ReplyResultFor<M> | Response : Response;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { RequestShape } from './type-utils.js';
|
|
3
|
+
export type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
+
/**
|
|
5
|
+
* A Standard Schema validator.
|
|
6
|
+
* `TOutput` is the validated/output type; `TInput` is the pre-parse input (request shape).
|
|
7
|
+
*/
|
|
8
|
+
export type Schema<TOutput = unknown, TInput = unknown> = StandardSchemaV1<TInput, TOutput>;
|
|
9
|
+
/** Infers the output type from a Standard Schema. */
|
|
10
|
+
export type InferOutput<S> = S extends StandardSchemaV1<unknown, infer O> ? O : never;
|
|
11
|
+
/** Infers the input type from a Standard Schema. */
|
|
12
|
+
export type InferInput<S> = S extends StandardSchemaV1<infer I, unknown> ? I : never;
|
|
13
|
+
/** Pre-parse request shape from a Standard Schema. */
|
|
14
|
+
export type SchemaRequestShape<S> = S extends StandardSchemaV1<infer I, infer O> ? RequestShape<I, O> : S;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD';
|
|
2
|
+
export type HttpMethod = Method;
|
|
3
|
+
export type MiddlewareFieldName = 'query' | 'params' | 'body' | 'state' | 'ctx';
|
|
4
|
+
export type MiddlewareInputFieldName = 'query' | 'params' | 'body';
|
|
5
|
+
export type UnwrapPart<T> = unknown extends T ? {} : T;
|
|
6
|
+
export type MergePart<T, Base> = unknown extends T ? Base : Base & T;
|
|
7
|
+
/** Collapse intersections so `Omit`/`keyof` treat the result as one object type. */
|
|
8
|
+
export type Simplify<T> = {
|
|
9
|
+
[K in keyof T]: T[K];
|
|
10
|
+
} & {};
|
|
11
|
+
/**
|
|
12
|
+
* Request prop: when schema input is `unknown` (e.g. zod coerce),
|
|
13
|
+
* fall back to the validated output type.
|
|
14
|
+
*/
|
|
15
|
+
type RequestPropType<TIn, TOut> = [unknown] extends [Exclude<TIn, undefined>] ? TOut : TIn;
|
|
16
|
+
/**
|
|
17
|
+
* Pre-parse request shape. Prefer input types (defaults → optional),
|
|
18
|
+
* but replace `unknown` input props with output types.
|
|
19
|
+
*/
|
|
20
|
+
export type RequestShape<TIn, TOut> = unknown extends TIn ? UnwrapPart<TOut> : Simplify<{
|
|
21
|
+
[K in keyof TIn]: K extends keyof TOut ? RequestPropType<TIn[K], TOut[K]> : TIn[K];
|
|
22
|
+
}>;
|
|
23
|
+
export type UnionToIntersection<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer Value) => void ? Value : never;
|
|
24
|
+
export type MiddlewareField<Middleware, Field extends MiddlewareFieldName> = Field extends 'query' ? Middleware extends {
|
|
25
|
+
query: infer Query;
|
|
26
|
+
} ? Query : {} : Field extends 'params' ? Middleware extends {
|
|
27
|
+
params: infer Params;
|
|
28
|
+
} ? Params : {} : Field extends 'body' ? Middleware extends {
|
|
29
|
+
body: infer Body;
|
|
30
|
+
} ? Body : {} : Field extends 'state' ? Middleware extends {
|
|
31
|
+
state: infer State;
|
|
32
|
+
} ? State : {} : Field extends 'ctx' ? Middleware extends {
|
|
33
|
+
ctx: infer Ctx;
|
|
34
|
+
} ? Ctx : {} : {};
|
|
35
|
+
export type MiddlewareInputField<Middleware, Field extends MiddlewareInputFieldName> = Middleware extends {
|
|
36
|
+
input: {
|
|
37
|
+
[K in Field]: infer V;
|
|
38
|
+
};
|
|
39
|
+
} ? V : {};
|
|
40
|
+
export type MergeMiddlewareField<Middlewares extends readonly unknown[], Field extends MiddlewareFieldName> = [Middlewares] extends [readonly []] ? {} : UnionToIntersection<MiddlewareField<Middlewares[number], Field>>;
|
|
41
|
+
export type MergeMiddlewareInputField<Middlewares extends readonly unknown[], Field extends MiddlewareInputFieldName> = [Middlewares] extends [readonly []] ? {} : UnionToIntersection<MiddlewareInputField<Middlewares[number], Field>>;
|
|
42
|
+
export type RuntimeContextFields<TNative = unknown> = {
|
|
43
|
+
request: Request;
|
|
44
|
+
method: Method;
|
|
45
|
+
url: URL;
|
|
46
|
+
headers: import('@taserjs/router-core').TaserHeaders;
|
|
47
|
+
cookies: import('@taserjs/router-core').TaserCookieJar;
|
|
48
|
+
reply: typeof import('@taserjs/router-utils').reply;
|
|
49
|
+
var: Record<string, unknown>;
|
|
50
|
+
native?: TNative;
|
|
51
|
+
};
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Awaitable, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
+
import { ReplyResult } from '@taserjs/router-utils';
|
|
3
|
+
import { RouterRegister } from '../register.js';
|
|
4
|
+
import { MergeMiddlewareField, MergePart, RequestShape, RuntimeContextFields, Simplify, UnwrapPart } from './type-utils.js';
|
|
5
|
+
import { ReturnsMap } from './returns.js';
|
|
6
|
+
import { Schema } from './schema.js';
|
|
7
|
+
export type { HttpMethod, Method } from './type-utils.js';
|
|
8
|
+
export type { EmptyReturns, MergeReturns, ReturnsMap, HandlerReply, HasReturns, StatusCode, ValidHandlerReply, EnforceHandlerReply } from './returns.js';
|
|
9
|
+
/** Default empty app context for standalone units without a bound router instance. */
|
|
10
|
+
export type AppContext = Record<never, never>;
|
|
11
|
+
export type NativeContext = RouterRegister extends {
|
|
12
|
+
NativeContext: infer N;
|
|
13
|
+
} ? N : unknown;
|
|
14
|
+
export type ValidatorParts = {
|
|
15
|
+
query?: unknown;
|
|
16
|
+
params?: unknown;
|
|
17
|
+
body?: unknown;
|
|
18
|
+
queryIn?: unknown;
|
|
19
|
+
paramsIn?: unknown;
|
|
20
|
+
bodyIn?: unknown;
|
|
21
|
+
};
|
|
22
|
+
export type MiddlewareNext = {
|
|
23
|
+
(args?: {
|
|
24
|
+
state?: unknown;
|
|
25
|
+
ctx?: unknown;
|
|
26
|
+
}): Promise<ReplyResult>;
|
|
27
|
+
};
|
|
28
|
+
export type MiddlewareDefinition = {
|
|
29
|
+
state?: unknown;
|
|
30
|
+
ctx?: unknown;
|
|
31
|
+
query?: unknown;
|
|
32
|
+
params?: unknown;
|
|
33
|
+
body?: unknown;
|
|
34
|
+
returns?: ReturnsMap;
|
|
35
|
+
handler: (ctx: unknown, next: MiddlewareNext) => Awaitable<ReplyResult | Response | unknown>;
|
|
36
|
+
};
|
|
37
|
+
export type MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TCtx = unknown, TQueryIn = TQuery, TParamsIn = TParams, TBodyIn = TBody> = {
|
|
38
|
+
query: Simplify<UnwrapPart<TQuery>>;
|
|
39
|
+
params: Simplify<UnwrapPart<TParams>>;
|
|
40
|
+
body: Simplify<UnwrapPart<TBody>>;
|
|
41
|
+
state: Simplify<UnwrapPart<TState>>;
|
|
42
|
+
ctx: Simplify<UnwrapPart<TCtx>>;
|
|
43
|
+
/** Pre-parse shapes merged into route `$Infer.Input`. */
|
|
44
|
+
input: {
|
|
45
|
+
query: RequestShape<TQueryIn, TQuery>;
|
|
46
|
+
params: RequestShape<TParamsIn, TParams>;
|
|
47
|
+
body: RequestShape<TBodyIn, TBody>;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
type UnitRuntimeContext = RuntimeContextFields<NativeContext>;
|
|
51
|
+
export type StandaloneMiddlewareContext<TQuery = unknown, TParams = unknown, TBody = unknown, TAppContext extends Record<string, unknown> = AppContext> = Simplify<TAppContext & UnitRuntimeContext & {
|
|
52
|
+
query: Simplify<UnwrapPart<TQuery>>;
|
|
53
|
+
params: Simplify<UnwrapPart<TParams>>;
|
|
54
|
+
body: Simplify<UnwrapPart<TBody>>;
|
|
55
|
+
state: {};
|
|
56
|
+
headers: TaserHeaders;
|
|
57
|
+
cookies: TaserCookieJar;
|
|
58
|
+
}>;
|
|
59
|
+
export type HandlerContext<Acc extends readonly unknown[], Validators extends ValidatorParts, TAppContext extends Record<string, unknown> = AppContext> = Simplify<TAppContext & UnitRuntimeContext & MergeMiddlewareField<Acc, 'ctx'> & {
|
|
60
|
+
query: Simplify<MergePart<Validators extends {
|
|
61
|
+
query?: infer Q;
|
|
62
|
+
} ? Q : unknown, MergeMiddlewareField<Acc, 'query'>>>;
|
|
63
|
+
params: Simplify<MergePart<Validators extends {
|
|
64
|
+
params?: infer P;
|
|
65
|
+
} ? P : unknown, MergeMiddlewareField<Acc, 'params'>>>;
|
|
66
|
+
body: Simplify<MergePart<Validators extends {
|
|
67
|
+
body?: infer B;
|
|
68
|
+
} ? B : unknown, MergeMiddlewareField<Acc, 'body'>>>;
|
|
69
|
+
state: Simplify<MergeMiddlewareField<Acc, 'state'>>;
|
|
70
|
+
headers: TaserHeaders;
|
|
71
|
+
cookies: TaserCookieJar;
|
|
72
|
+
}>;
|
|
73
|
+
export type MiddlewareUnit<TAcc = unknown, TReturns extends ReturnsMap = {}> = MiddlewareDefinition & {
|
|
74
|
+
readonly __middlewareAcc: TAcc;
|
|
75
|
+
readonly __returns?: TReturns;
|
|
76
|
+
};
|
|
77
|
+
export type HandlerUnit<_Acc extends readonly unknown[], _Validators extends ValidatorParts, TReturns extends ReturnsMap = {}, TOutput = Response> = {
|
|
78
|
+
readonly __returns?: TReturns;
|
|
79
|
+
readonly $Infer: {
|
|
80
|
+
Output: TOutput;
|
|
81
|
+
};
|
|
82
|
+
middlewares: readonly MiddlewareDefinition[];
|
|
83
|
+
handler: (ctx: unknown) => Awaitable<ReplyResult | Response>;
|
|
84
|
+
returns?: ReturnsMap;
|
|
85
|
+
query?: Schema<unknown>;
|
|
86
|
+
params?: Schema<unknown>;
|
|
87
|
+
body?: Schema<unknown>;
|
|
88
|
+
};
|
|
89
|
+
export type InlineMiddlewareOptions = {
|
|
90
|
+
state?: Schema<unknown>;
|
|
91
|
+
ctx?: Schema<unknown>;
|
|
92
|
+
query?: Schema<unknown>;
|
|
93
|
+
params?: Schema<unknown>;
|
|
94
|
+
body?: Schema<unknown>;
|
|
95
|
+
returns?: ReturnsMap;
|
|
96
|
+
handler: (ctx: unknown, next: MiddlewareNext) => Awaitable<unknown>;
|
|
97
|
+
};
|
|
98
|
+
export declare function isHandlerUnit(value: unknown): value is HandlerUnit<readonly unknown[], ValidatorParts>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
//#region src/types/units.ts
|
|
2
|
+
function isHandlerUnit(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && "handler" in value && "middlewares" in value && Array.isArray(value.middlewares);
|
|
4
|
+
}
|
|
5
|
+
//#endregion
|
|
6
|
+
export { isHandlerUnit };
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=units.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"units.js","names":[],"sources":["../../../src/types/units.ts"],"sourcesContent":["import type { Awaitable, TaserCookieJar, TaserHeaders } from '@taserjs/router-core'\nimport type { ReplyResult } from '@taserjs/router-utils'\n\nimport type { RouterRegister } from '../register.js'\nimport type {\n MergeMiddlewareField,\n MergePart,\n RequestShape,\n RuntimeContextFields,\n Simplify,\n UnwrapPart,\n} from './type-utils.js'\nimport type { ReturnsMap } from './returns.js'\nimport type { Schema } from './schema.js'\n\nexport type { HttpMethod, Method } from './type-utils.js'\nexport type { EmptyReturns, MergeReturns, ReturnsMap, HandlerReply, HasReturns, StatusCode, ValidHandlerReply, EnforceHandlerReply } from './returns.js'\n\n/** Default empty app context for standalone units without a bound router instance. */\nexport type AppContext = Record<never, never>\n\nexport type NativeContext = RouterRegister extends { NativeContext: infer N } ? N : unknown\n\nexport type ValidatorParts = {\n query?: unknown\n params?: unknown\n body?: unknown\n queryIn?: unknown\n paramsIn?: unknown\n bodyIn?: unknown\n}\n\nexport type MiddlewareNext = {\n (args?: { state?: unknown, ctx?: unknown }): Promise<ReplyResult>\n}\n\nexport type MiddlewareDefinition = {\n state?: unknown\n ctx?: unknown\n query?: unknown\n params?: unknown\n body?: unknown\n returns?: ReturnsMap\n handler: (\n ctx: unknown,\n next: MiddlewareNext,\n ) => Awaitable<ReplyResult | Response | unknown>\n}\n\nexport type MiddlewareReturnFromParts<\n TQuery,\n TParams,\n TBody,\n TState,\n TCtx = unknown,\n TQueryIn = TQuery,\n TParamsIn = TParams,\n TBodyIn = TBody,\n> = {\n query: Simplify<UnwrapPart<TQuery>>\n params: Simplify<UnwrapPart<TParams>>\n body: Simplify<UnwrapPart<TBody>>\n state: Simplify<UnwrapPart<TState>>\n ctx: Simplify<UnwrapPart<TCtx>>\n /** Pre-parse shapes merged into route `$Infer.Input`. */\n input: {\n query: RequestShape<TQueryIn, TQuery>\n params: RequestShape<TParamsIn, TParams>\n body: RequestShape<TBodyIn, TBody>\n }\n}\n\ntype UnitRuntimeContext = RuntimeContextFields<NativeContext>\n\nexport type StandaloneMiddlewareContext<\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n> = Simplify<\n TAppContext & UnitRuntimeContext & {\n query: Simplify<UnwrapPart<TQuery>>\n params: Simplify<UnwrapPart<TParams>>\n body: Simplify<UnwrapPart<TBody>>\n state: {}\n headers: TaserHeaders\n cookies: TaserCookieJar\n }\n>\n\nexport type HandlerContext<\n Acc extends readonly unknown[],\n Validators extends ValidatorParts,\n TAppContext extends Record<string, unknown> = AppContext,\n> = Simplify<\n TAppContext\n & UnitRuntimeContext\n & MergeMiddlewareField<Acc, 'ctx'>\n & {\n query: Simplify<MergePart<\n Validators extends { query?: infer Q } ? Q : unknown,\n MergeMiddlewareField<Acc, 'query'>\n >>\n params: Simplify<MergePart<\n Validators extends { params?: infer P } ? P : unknown,\n MergeMiddlewareField<Acc, 'params'>\n >>\n body: Simplify<MergePart<\n Validators extends { body?: infer B } ? B : unknown,\n MergeMiddlewareField<Acc, 'body'>\n >>\n state: Simplify<MergeMiddlewareField<Acc, 'state'>>\n headers: TaserHeaders\n cookies: TaserCookieJar\n }\n>\n\nexport type MiddlewareUnit<\n TAcc = unknown,\n TReturns extends ReturnsMap = {},\n> = MiddlewareDefinition & {\n readonly __middlewareAcc: TAcc\n readonly __returns?: TReturns\n}\n\nexport type HandlerUnit<\n _Acc extends readonly unknown[],\n _Validators extends ValidatorParts,\n TReturns extends ReturnsMap = {},\n TOutput = Response,\n> = {\n readonly __returns?: TReturns\n readonly $Infer: {\n Output: TOutput\n }\n middlewares: readonly MiddlewareDefinition[]\n handler: (ctx: unknown) => Awaitable<ReplyResult | Response>\n returns?: ReturnsMap\n query?: Schema<unknown>\n params?: Schema<unknown>\n body?: Schema<unknown>\n}\n\nexport type InlineMiddlewareOptions = {\n state?: Schema<unknown>\n ctx?: Schema<unknown>\n query?: Schema<unknown>\n params?: Schema<unknown>\n body?: Schema<unknown>\n returns?: ReturnsMap\n handler: (\n ctx: unknown,\n next: MiddlewareNext,\n ) => Awaitable<unknown>\n}\n\nexport function isHandlerUnit(\n value: unknown,\n): value is HandlerUnit<readonly unknown[], ValidatorParts> {\n return (\n typeof value === 'object'\n && value !== null\n && 'handler' in value\n && 'middlewares' in value\n && Array.isArray((value as HandlerUnit<readonly unknown[], ValidatorParts>).middlewares)\n )\n}\n"],"mappings":";AA4JA,SAAgB,cACd,OAC0D;CAC1D,OACE,OAAO,UAAU,YACd,UAAU,QACV,aAAa,SACb,iBAAiB,SACjB,MAAM,QAAS,MAA0D,WAAW;AAE3F"}
|