@taserjs/router 0.0.4 → 0.0.5
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/dist/cjs/builder/app.cjs +22 -0
- package/dist/cjs/builder/app.cjs.map +1 -0
- package/dist/cjs/builder/app.d.cts +11 -0
- package/dist/cjs/{constants.cjs → builder/constants.cjs} +1 -1
- package/dist/cjs/builder/constants.cjs.map +1 -0
- package/dist/cjs/{factories.cjs → builder/factories.cjs} +1 -1
- package/dist/cjs/builder/factories.cjs.map +1 -0
- package/dist/cjs/{factories.d.cts → builder/factories.d.cts} +2 -2
- package/dist/cjs/{middleware.cjs → builder/middleware.cjs} +1 -1
- package/dist/cjs/builder/middleware.cjs.map +1 -0
- package/dist/cjs/{middleware.d.cts → builder/middleware.d.cts} +1 -1
- package/dist/cjs/{route.cjs → builder/route.cjs} +19 -33
- package/dist/cjs/builder/route.cjs.map +1 -0
- package/dist/cjs/{route.d.cts → builder/route.d.cts} +3 -3
- package/dist/cjs/{taser-router.cjs → builder/router.cjs} +38 -11
- package/dist/cjs/builder/router.cjs.map +1 -0
- package/dist/cjs/builder/router.d.cts +48 -0
- package/dist/cjs/{create-context.cjs → context/create-context.cjs} +1 -1
- package/dist/cjs/context/create-context.cjs.map +1 -0
- package/dist/cjs/{create-context.d.cts → context/create-context.d.cts} +2 -2
- package/dist/cjs/define/validators.cjs +1 -1
- package/dist/cjs/define/validators.cjs.map +1 -1
- package/dist/cjs/define/validators.d.cts +1 -1
- package/dist/cjs/index.cjs +6 -16
- package/dist/cjs/index.d.cts +6 -7
- package/dist/cjs/middleware/body-limit.cjs +2 -2
- package/dist/cjs/middleware/body-limit.cjs.map +1 -1
- package/dist/cjs/middleware/compress.cjs +2 -2
- package/dist/cjs/middleware/compress.cjs.map +1 -1
- package/dist/cjs/middleware/cors.cjs +2 -2
- package/dist/cjs/middleware/cors.cjs.map +1 -1
- package/dist/cjs/middleware/csrf.cjs +2 -2
- package/dist/cjs/middleware/csrf.cjs.map +1 -1
- package/dist/cjs/middleware/etag.cjs +2 -2
- package/dist/cjs/middleware/etag.cjs.map +1 -1
- package/dist/cjs/middleware/jwk.cjs +2 -8
- package/dist/cjs/middleware/jwk.cjs.map +1 -1
- package/dist/cjs/middleware/jwt.cjs +2 -8
- package/dist/cjs/middleware/jwt.cjs.map +1 -1
- package/dist/cjs/middleware/secure-headers.cjs +2 -2
- package/dist/cjs/middleware/secure-headers.cjs.map +1 -1
- package/dist/cjs/middleware/timing.cjs +2 -2
- package/dist/cjs/middleware/timing.cjs.map +1 -1
- package/dist/cjs/middleware/wrap-hono.cjs +10 -0
- package/dist/cjs/middleware/wrap-hono.cjs.map +1 -1
- package/dist/cjs/middleware/wrap-hono.d.cts +4 -0
- package/dist/cjs/types/app.d.cts +7 -7
- package/dist/cjs/types/index.d.cts +32 -13
- package/dist/cjs/types/returns.d.cts +2 -2
- package/dist/cjs/types/type-utils.d.cts +1 -2
- package/dist/cjs/types/units.cjs.map +1 -1
- package/dist/cjs/types/units.d.cts +4 -9
- package/dist/esm/builder/app.d.ts +11 -0
- package/dist/esm/builder/app.js +22 -0
- package/dist/esm/builder/app.js.map +1 -0
- package/dist/esm/{constants.js → builder/constants.js} +1 -1
- package/dist/esm/builder/constants.js.map +1 -0
- package/dist/esm/{factories.d.ts → builder/factories.d.ts} +2 -2
- package/dist/esm/{factories.js → builder/factories.js} +1 -1
- package/dist/esm/builder/factories.js.map +1 -0
- package/dist/esm/{middleware.d.ts → builder/middleware.d.ts} +1 -1
- package/dist/esm/{middleware.js → builder/middleware.js} +1 -1
- package/dist/esm/builder/middleware.js.map +1 -0
- package/dist/esm/{route.d.ts → builder/route.d.ts} +3 -3
- package/dist/esm/{route.js → builder/route.js} +19 -33
- package/dist/esm/builder/route.js.map +1 -0
- package/dist/esm/builder/router.d.ts +48 -0
- package/dist/esm/{taser-router.js → builder/router.js} +37 -10
- package/dist/esm/builder/router.js.map +1 -0
- package/dist/esm/{create-context.d.ts → context/create-context.d.ts} +2 -2
- package/dist/esm/{create-context.js → context/create-context.js} +1 -1
- package/dist/esm/context/create-context.js.map +1 -0
- package/dist/esm/define/validators.d.ts +1 -1
- package/dist/esm/define/validators.js +1 -1
- package/dist/esm/define/validators.js.map +1 -1
- package/dist/esm/index.d.ts +6 -7
- package/dist/esm/index.js +5 -6
- package/dist/esm/middleware/body-limit.js +2 -2
- package/dist/esm/middleware/body-limit.js.map +1 -1
- package/dist/esm/middleware/compress.js +2 -2
- package/dist/esm/middleware/compress.js.map +1 -1
- package/dist/esm/middleware/cors.js +2 -2
- package/dist/esm/middleware/cors.js.map +1 -1
- package/dist/esm/middleware/csrf.js +2 -2
- package/dist/esm/middleware/csrf.js.map +1 -1
- package/dist/esm/middleware/etag.js +2 -2
- package/dist/esm/middleware/etag.js.map +1 -1
- package/dist/esm/middleware/jwk.js +2 -8
- package/dist/esm/middleware/jwk.js.map +1 -1
- package/dist/esm/middleware/jwt.js +2 -8
- package/dist/esm/middleware/jwt.js.map +1 -1
- package/dist/esm/middleware/secure-headers.js +2 -2
- package/dist/esm/middleware/secure-headers.js.map +1 -1
- package/dist/esm/middleware/timing.js +2 -2
- package/dist/esm/middleware/timing.js.map +1 -1
- package/dist/esm/middleware/wrap-hono.d.ts +4 -0
- package/dist/esm/middleware/wrap-hono.js +10 -1
- package/dist/esm/middleware/wrap-hono.js.map +1 -1
- package/dist/esm/types/app.d.ts +7 -7
- package/dist/esm/types/index.d.ts +32 -13
- package/dist/esm/types/returns.d.ts +2 -2
- package/dist/esm/types/type-utils.d.ts +1 -2
- package/dist/esm/types/units.d.ts +4 -9
- package/dist/esm/types/units.js.map +1 -1
- package/package.json +3 -3
- package/src/builder/app.ts +28 -0
- package/src/{factories.ts → builder/factories.ts} +2 -2
- package/src/{middleware.ts → builder/middleware.ts} +2 -2
- package/src/{route.ts → builder/route.ts} +30 -34
- package/src/builder/router.ts +347 -0
- package/src/{create-context.ts → context/create-context.ts} +2 -2
- package/src/define/validators.ts +1 -1
- package/src/index.ts +9 -12
- package/src/middleware/body-limit.ts +2 -2
- package/src/middleware/compress.ts +2 -2
- package/src/middleware/cors.ts +2 -2
- package/src/middleware/csrf.ts +2 -2
- package/src/middleware/etag.ts +2 -2
- package/src/middleware/jwk.ts +2 -7
- package/src/middleware/jwt.ts +2 -7
- package/src/middleware/secure-headers.ts +2 -2
- package/src/middleware/timing.ts +2 -2
- package/src/middleware/wrap-hono.ts +13 -0
- package/src/types/app.ts +12 -8
- package/src/types/index.ts +44 -10
- package/src/types/returns.ts +2 -2
- package/src/types/type-utils.ts +1 -2
- package/src/types/units.ts +4 -8
- package/dist/cjs/constants.cjs.map +0 -1
- package/dist/cjs/create-context.cjs.map +0 -1
- package/dist/cjs/factories.cjs.map +0 -1
- package/dist/cjs/middleware.cjs.map +0 -1
- package/dist/cjs/route.cjs.map +0 -1
- package/dist/cjs/taser-app.cjs +0 -36
- package/dist/cjs/taser-app.cjs.map +0 -1
- package/dist/cjs/taser-app.d.cts +0 -18
- package/dist/cjs/taser-router.cjs.map +0 -1
- package/dist/cjs/taser-router.d.cts +0 -42
- package/dist/esm/constants.js.map +0 -1
- package/dist/esm/create-context.js.map +0 -1
- package/dist/esm/factories.js.map +0 -1
- package/dist/esm/middleware.js.map +0 -1
- package/dist/esm/route.js.map +0 -1
- package/dist/esm/taser-app.d.ts +0 -18
- package/dist/esm/taser-app.js +0 -35
- package/dist/esm/taser-app.js.map +0 -1
- package/dist/esm/taser-router.d.ts +0 -42
- package/dist/esm/taser-router.js.map +0 -1
- package/src/taser-app.ts +0 -38
- package/src/taser-router.ts +0 -165
- /package/dist/cjs/{constants.d.cts → builder/constants.d.cts} +0 -0
- /package/dist/esm/{constants.d.ts → builder/constants.d.ts} +0 -0
- /package/src/{constants.ts → builder/constants.ts} +0 -0
|
@@ -3,13 +3,13 @@ import { RouterRegister } from '../register.js';
|
|
|
3
3
|
import { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape, RuntimeContextFields, Simplify, UnionToIntersection } from './type-utils.js';
|
|
4
4
|
import { ReturnsMap, ValidHandlerReply } from './returns.js';
|
|
5
5
|
import { Schema } from './schema.js';
|
|
6
|
-
import { AppContext, ExtractState, HandlerContext, HandlerUnit, InlineMiddlewareOptions, Method, MiddlewareDefinition, MiddlewareReturnFromParts, MiddlewareUnit,
|
|
6
|
+
import { AppContext, ExtractState, HandlerContext, HandlerUnit, InlineMiddlewareOptions, Method, MiddlewareDefinition, MiddlewareReturnFromParts, MiddlewareUnit, ValidatorParts } from './units.js';
|
|
7
7
|
export type { Awaitable } from '@taserjs/router-core';
|
|
8
8
|
export type { SchemaRequestShape, InferInput, InferOutput, Schema, StandardSchemaV1, } from './schema.js';
|
|
9
9
|
export type { RouterRegister } from '../register.js';
|
|
10
10
|
export type { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape, Simplify, UnwrapPart, } from './type-utils.js';
|
|
11
|
-
export type { EmptyReturns, EnforceHandlerReply, HandlerReply, HasReturns, MergeReturns,
|
|
12
|
-
export type { AppContext, ExtractState, HandlerContext, HandlerUnit, HttpMethod, InlineMiddlewareOptions, IsUnknown, Method, MiddlewareDefinition, MiddlewareNext, MiddlewareReturnFromParts, MiddlewareUnit,
|
|
11
|
+
export type { EmptyReturns, EnforceHandlerReply, HandlerReply, HasReturns, MergeReturns, ReplyFor, ReturnsMap, StatusCode, ValidHandlerReply, } from './returns.js';
|
|
12
|
+
export type { AppContext, ExtractState, HandlerContext, HandlerUnit, HttpMethod, InlineMiddlewareOptions, IsUnknown, Method, MiddlewareDefinition, MiddlewareNext, MiddlewareReturnFromParts, MiddlewareUnit, NextFn, NextResult, StandaloneMiddlewareContext, StateBrand, ValidatorParts, } from './units.js';
|
|
13
13
|
export type RoutePath = RouterRegister extends {
|
|
14
14
|
RoutePath: infer P;
|
|
15
15
|
} ? P : never;
|
|
@@ -29,6 +29,7 @@ type ParseParam<Segment extends string> = Segment extends "*" ? {
|
|
|
29
29
|
} : {};
|
|
30
30
|
type ParseParamsFromSegments<S extends string> = S extends `${infer Segment}/${infer Rest}` ? ParseParam<Segment> & ParseParamsFromSegments<Rest> : ParseParam<S>;
|
|
31
31
|
export type PathParams<Path extends string> = Path extends `/${infer Rest}` ? ParseParamsFromSegments<Rest> : {};
|
|
32
|
+
export type ResolveParams<TPathParams, TSchemaParams> = TSchemaParams extends Record<string, unknown> ? [keyof TSchemaParams] extends [never] ? TPathParams : Simplify<Omit<TPathParams, keyof TSchemaParams> & TSchemaParams> : TPathParams;
|
|
32
33
|
type LayoutParent<Layout extends LayoutId> = LayoutTree[Layout] extends {
|
|
33
34
|
parent: infer Parent extends LayoutId | null;
|
|
34
35
|
} ? Parent : null;
|
|
@@ -58,10 +59,10 @@ type RouteLayoutChain<Path extends RoutePath, TMethod extends Method> = RouteEnt
|
|
|
58
59
|
} ? Chain : RouteParent<Path, TMethod> extends LayoutId ? readonly [RouteParent<Path, TMethod>] : readonly [];
|
|
59
60
|
type RouteResolvedField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends "query" | "params" | "body" | "state"> = MergeMiddlewareField<ResolveLayoutChainMiddlewares<RouteLayoutChain<Path, TMethod>>, Field> & MergeMiddlewareField<Acc, Field>;
|
|
60
61
|
type RouteChainField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends "query" | "params" | "body" | "state"> = RouteResolvedField<Path, TMethod, Acc, Field>;
|
|
61
|
-
type UnitRuntimeContext = Omit<RuntimeContextFields
|
|
62
|
+
type UnitRuntimeContext = Omit<RuntimeContextFields, "var">;
|
|
62
63
|
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 & {
|
|
63
64
|
query: Simplify<MergePart<TQuery, RouteChainField<Path, TMethod, Acc, "query">>>;
|
|
64
|
-
params: Simplify<PathParams<Path
|
|
65
|
+
params: Simplify<ResolveParams<PathParams<Path>, MergePart<TParams, RouteChainField<Path, TMethod, Acc, "params">>>>;
|
|
65
66
|
body: Simplify<MergePart<TBody, RouteChainField<Path, TMethod, Acc, "body">>>;
|
|
66
67
|
state: Simplify<RouteChainField<Path, TMethod, Acc, "state">>;
|
|
67
68
|
headers: TaserHeaders;
|
|
@@ -77,9 +78,9 @@ export type RouteHandleContext<Path extends RoutePath, TMethod extends Method, A
|
|
|
77
78
|
query: Simplify<MergePart<Validators extends {
|
|
78
79
|
query?: infer Q;
|
|
79
80
|
} ? Q : unknown, RouteResolvedField<Path, TMethod, Acc, "query">>>;
|
|
80
|
-
params: Simplify<PathParams<Path
|
|
81
|
+
params: Simplify<ResolveParams<PathParams<Path>, MergePart<Validators extends {
|
|
81
82
|
params?: infer P;
|
|
82
|
-
} ? P : unknown, RouteResolvedField<Path, TMethod, Acc, "params"
|
|
83
|
+
} ? P : unknown, RouteResolvedField<Path, TMethod, Acc, "params">>>>;
|
|
83
84
|
body: Simplify<MergePart<Validators extends {
|
|
84
85
|
body?: infer B;
|
|
85
86
|
} ? B : unknown, RouteResolvedField<Path, TMethod, Acc, "body">>>;
|
|
@@ -96,9 +97,9 @@ export type RouteHandleContextWithoutBody<Path extends RoutePath, TMethod extend
|
|
|
96
97
|
query: Simplify<MergePart<Validators extends {
|
|
97
98
|
query?: infer Q;
|
|
98
99
|
} ? Q : unknown, RouteResolvedField<Path, TMethod, Acc, "query">>>;
|
|
99
|
-
params: Simplify<PathParams<Path
|
|
100
|
+
params: Simplify<ResolveParams<PathParams<Path>, MergePart<Validators extends {
|
|
100
101
|
params?: infer P;
|
|
101
|
-
} ? P : unknown, RouteResolvedField<Path, TMethod, Acc, "params"
|
|
102
|
+
} ? P : unknown, RouteResolvedField<Path, TMethod, Acc, "params">>>>;
|
|
102
103
|
body: never;
|
|
103
104
|
state: Simplify<RouteResolvedField<Path, TMethod, Acc, "state">>;
|
|
104
105
|
headers: TaserHeaders;
|
|
@@ -119,11 +120,11 @@ type RouteQueryInput<Path extends RoutePath, TMethod extends Method, Acc extends
|
|
|
119
120
|
} ? QI : unknown, Validators extends {
|
|
120
121
|
query?: infer Q;
|
|
121
122
|
} ? Q : unknown>, RouteResolvedInputField<Path, TMethod, Acc, "query">>>;
|
|
122
|
-
type RouteParamsInput<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts> = Simplify<PathParams<Path
|
|
123
|
+
type RouteParamsInput<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts> = Simplify<ResolveParams<PathParams<Path>, MergePart<RequestShape<Validators extends {
|
|
123
124
|
paramsIn?: infer PI;
|
|
124
125
|
} ? PI : unknown, Validators extends {
|
|
125
126
|
params?: infer P;
|
|
126
|
-
} ? P : unknown>, RouteResolvedInputField<Path, TMethod, Acc, "params"
|
|
127
|
+
} ? P : unknown>, RouteResolvedInputField<Path, TMethod, Acc, "params">>>>;
|
|
127
128
|
type RouteBodyInput<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Validators extends ValidatorParts> = Simplify<MergePart<RequestShape<Validators extends {
|
|
128
129
|
bodyIn?: infer BI;
|
|
129
130
|
} ? BI : unknown, Validators extends {
|
|
@@ -131,7 +132,7 @@ type RouteBodyInput<Path extends RoutePath, TMethod extends Method, Acc extends
|
|
|
131
132
|
} ? B : unknown>, RouteResolvedInputField<Path, TMethod, Acc, "body">>>;
|
|
132
133
|
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">>;
|
|
133
134
|
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">>;
|
|
134
|
-
export type
|
|
135
|
+
export type InferRouteInputFromPath<Path extends RoutePath, TMethod extends Method> = RouteEntry<Path, TMethod> extends {
|
|
135
136
|
route: infer Route;
|
|
136
137
|
} ? Route extends {
|
|
137
138
|
$Infer: {
|
|
@@ -180,7 +181,7 @@ export type RouteExport<Path extends RoutePath, TMethod extends Method, Acc exte
|
|
|
180
181
|
handler: (ctx: unknown) => Awaitable<Response>;
|
|
181
182
|
readonly $Infer: {
|
|
182
183
|
Context: TMethod extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? RouteHandleContextWithoutBody<Path, TMethod, Acc, {}> : RouteHandleContext<Path, TMethod, Acc, {}>;
|
|
183
|
-
|
|
184
|
+
Input: TMethod extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? RouteHandleInputWithoutBody<Path, TMethod, Acc, {}> : RouteHandleInput<Path, TMethod, Acc, {}>;
|
|
184
185
|
Output: TOutput;
|
|
185
186
|
};
|
|
186
187
|
query?: Schema<unknown>;
|
|
@@ -202,6 +203,8 @@ export type RouteBuilder<Path extends RoutePath, TMethod extends Method, Acc ext
|
|
|
202
203
|
readonly method: TMethod;
|
|
203
204
|
readonly $Infer: {
|
|
204
205
|
Context: TMethod extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? RouteHandleContextWithoutBody<Path, TMethod, Acc, Validators, TAppContext> : RouteHandleContext<Path, TMethod, Acc, Validators, TAppContext>;
|
|
206
|
+
Input: TMethod extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? RouteHandleInputWithoutBody<Path, TMethod, Acc, Validators> : RouteHandleInput<Path, TMethod, Acc, Validators>;
|
|
207
|
+
Output: TReturns;
|
|
205
208
|
};
|
|
206
209
|
returns<const M extends ReturnsMap>(map: M): RouteBuilder<Path, TMethod, Acc, Validators, Omit<TReturns, keyof M> & M, TAppContext>;
|
|
207
210
|
use<TAcc, UReturns extends ReturnsMap = {}, TRequiredLayouts = unknown, TRequiredState = unknown>(unit: MiddlewareUnit<TAcc, UReturns, TRequiredLayouts, TRequiredState>, ..._assert: [
|
|
@@ -254,3 +257,19 @@ export type HandlerBuilder<Acc extends readonly unknown[] = readonly [], Validat
|
|
|
254
257
|
}
|
|
255
258
|
]): HandlerUnit<Acc, Validators, TReturns, R>;
|
|
256
259
|
};
|
|
260
|
+
export type RouteDefinition<Path extends RoutePath = RoutePath, TMethod extends Method = Method, Acc extends readonly unknown[] = readonly unknown[], Validators extends ValidatorParts = ValidatorParts, TReturns extends ReturnsMap = ReturnsMap, TOutput = Response, TAppContext extends Record<string, unknown> = AppContext> = RouteHandleResult<Path, TMethod, Acc, Validators, TReturns, TOutput, TAppContext>;
|
|
261
|
+
export type InferRouteContext<R> = R extends {
|
|
262
|
+
$Infer: {
|
|
263
|
+
Context: infer C;
|
|
264
|
+
};
|
|
265
|
+
} ? C : never;
|
|
266
|
+
export type InferRouteInput<R> = R extends {
|
|
267
|
+
$Infer: {
|
|
268
|
+
Input: infer I;
|
|
269
|
+
};
|
|
270
|
+
} ? I : never;
|
|
271
|
+
export type InferRouteOutput<R> = R extends {
|
|
272
|
+
$Infer: {
|
|
273
|
+
Output: infer O;
|
|
274
|
+
};
|
|
275
|
+
} ? O : never;
|
|
@@ -13,7 +13,7 @@ export type ReturnsMap = {
|
|
|
13
13
|
/** Later map wins on overlapping status keys. */
|
|
14
14
|
export type MergeReturns<A extends ReturnsMap, B extends ReturnsMap> = Omit<A, keyof B> & B;
|
|
15
15
|
/** Typed reply union for statuses declared in a returns map. */
|
|
16
|
-
export type
|
|
16
|
+
export type ReplyFor<M extends ReturnsMap> = {
|
|
17
17
|
[S in keyof M]-?: S extends StatusCode ? M[S] extends Schema<unknown> ? ReplyOf<S, Simplify<InferOutput<M[S]>>> : never : never;
|
|
18
18
|
}[keyof M];
|
|
19
19
|
export type EmptyReturns = {};
|
|
@@ -34,4 +34,4 @@ export type EnforceHandlerReply<R, M extends ReturnsMap> = [R] extends [ValidHan
|
|
|
34
34
|
* Expected `.handler` return type for a returns map.
|
|
35
35
|
* Prefer constraining handler generics with {@link EnforceHandlerReply}.
|
|
36
36
|
*/
|
|
37
|
-
export type HandlerReply<M extends ReturnsMap> = HasReturns<M> extends true ?
|
|
37
|
+
export type HandlerReply<M extends ReturnsMap> = HasReturns<M> extends true ? ReplyFor<M> | Response : Response;
|
|
@@ -37,13 +37,12 @@ export type MiddlewareInputField<Middleware, Field extends MiddlewareInputFieldN
|
|
|
37
37
|
} ? V : {};
|
|
38
38
|
export type MergeMiddlewareField<Middlewares extends readonly unknown[], Field extends MiddlewareFieldName> = [Middlewares] extends [readonly []] ? {} : UnionToIntersection<MiddlewareField<Middlewares[number], Field>>;
|
|
39
39
|
export type MergeMiddlewareInputField<Middlewares extends readonly unknown[], Field extends MiddlewareInputFieldName> = [Middlewares] extends [readonly []] ? {} : UnionToIntersection<MiddlewareInputField<Middlewares[number], Field>>;
|
|
40
|
-
export type RuntimeContextFields
|
|
40
|
+
export type RuntimeContextFields = {
|
|
41
41
|
request: Request;
|
|
42
42
|
method: Method;
|
|
43
43
|
url: URL;
|
|
44
44
|
headers: import('@taserjs/router-core').TaserHeaders;
|
|
45
45
|
cookies: import('@taserjs/router-core').TaserCookieJar;
|
|
46
46
|
var: Record<string, unknown>;
|
|
47
|
-
native?: TNative;
|
|
48
47
|
};
|
|
49
48
|
export {};
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { Awaitable, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
-
import { ReplyResult } from '@taserjs/router-utils';
|
|
3
|
-
import { RouterRegister } from '../register.js';
|
|
4
2
|
import { MergeMiddlewareField, MergePart, RequestShape, RuntimeContextFields, Simplify, UnwrapPart } from './type-utils.js';
|
|
5
3
|
import { ReturnsMap } from './returns.js';
|
|
6
4
|
import { Schema } from './schema.js';
|
|
@@ -8,9 +6,6 @@ export type { HttpMethod, Method } from './type-utils.js';
|
|
|
8
6
|
export type { EmptyReturns, MergeReturns, ReturnsMap, HandlerReply, HasReturns, StatusCode, ValidHandlerReply, EnforceHandlerReply, } from './returns.js';
|
|
9
7
|
/** Default empty app context for standalone units without a bound router instance. */
|
|
10
8
|
export type AppContext = Record<never, never>;
|
|
11
|
-
export type NativeContext = RouterRegister extends {
|
|
12
|
-
NativeContext: infer N;
|
|
13
|
-
} ? N : unknown;
|
|
14
9
|
export type ValidatorParts = {
|
|
15
10
|
query?: unknown;
|
|
16
11
|
params?: unknown;
|
|
@@ -20,7 +15,7 @@ export type ValidatorParts = {
|
|
|
20
15
|
bodyIn?: unknown;
|
|
21
16
|
};
|
|
22
17
|
export declare const StateBrand: unique symbol;
|
|
23
|
-
export type NextResult<TState = unknown> =
|
|
18
|
+
export type NextResult<TState = unknown> = Response & {
|
|
24
19
|
readonly [StateBrand]?: TState;
|
|
25
20
|
};
|
|
26
21
|
export type ExtractState<T> = [Extract<Awaited<T>, {
|
|
@@ -38,7 +33,7 @@ export type MiddlewareDefinition = {
|
|
|
38
33
|
params?: unknown;
|
|
39
34
|
body?: unknown;
|
|
40
35
|
returns?: ReturnsMap;
|
|
41
|
-
handler: (ctx: unknown, next: MiddlewareNext) => Awaitable<
|
|
36
|
+
handler: (ctx: unknown, next: MiddlewareNext) => Awaitable<Response | unknown>;
|
|
42
37
|
};
|
|
43
38
|
export type MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TQueryIn = TQuery, TParamsIn = TParams, TBodyIn = TBody> = {
|
|
44
39
|
query: Simplify<UnwrapPart<TQuery>>;
|
|
@@ -52,7 +47,7 @@ export type MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TQueryIn =
|
|
|
52
47
|
body: RequestShape<TBodyIn, TBody>;
|
|
53
48
|
};
|
|
54
49
|
};
|
|
55
|
-
type UnitRuntimeContext = Omit<RuntimeContextFields
|
|
50
|
+
type UnitRuntimeContext = Omit<RuntimeContextFields, "var">;
|
|
56
51
|
export type StandaloneMiddlewareContext<TQuery = unknown, TParams = unknown, TBody = unknown, TAppContext extends Record<string, unknown> = AppContext, TState = {}> = Simplify<TAppContext & UnitRuntimeContext & {
|
|
57
52
|
query: Simplify<UnwrapPart<TQuery>>;
|
|
58
53
|
params: Simplify<UnwrapPart<TParams>>;
|
|
@@ -87,7 +82,7 @@ export type HandlerUnit<_Acc extends readonly unknown[], _Validators extends Val
|
|
|
87
82
|
Output: TOutput;
|
|
88
83
|
};
|
|
89
84
|
middlewares: readonly MiddlewareDefinition[];
|
|
90
|
-
handler: (ctx: unknown) => Awaitable<
|
|
85
|
+
handler: (ctx: unknown) => Awaitable<Response>;
|
|
91
86
|
returns?: ReturnsMap;
|
|
92
87
|
query?: Schema<unknown>;
|
|
93
88
|
params?: Schema<unknown>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"units.js","names":[],"sources":["../../../src/types/units.ts"],"sourcesContent":["import type { Awaitable, TaserCookieJar, TaserHeaders } from \"@taserjs/router-core\";\
|
|
1
|
+
{"version":3,"file":"units.js","names":[],"sources":["../../../src/types/units.ts"],"sourcesContent":["import type { Awaitable, TaserCookieJar, TaserHeaders } from \"@taserjs/router-core\";\n\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 {\n EmptyReturns,\n MergeReturns,\n ReturnsMap,\n HandlerReply,\n HasReturns,\n StatusCode,\n ValidHandlerReply,\n EnforceHandlerReply,\n} 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 ValidatorParts = {\n query?: unknown;\n params?: unknown;\n body?: unknown;\n queryIn?: unknown;\n paramsIn?: unknown;\n bodyIn?: unknown;\n};\n\nexport declare const StateBrand: unique symbol;\n\nexport type NextResult<TState = unknown> = Response & {\n readonly [StateBrand]?: TState;\n};\n\nexport type ExtractState<T> = [Extract<Awaited<T>, { [StateBrand]?: unknown }>] extends [never]\n ? {}\n : Extract<Awaited<T>, { [StateBrand]?: unknown }> extends { [StateBrand]?: infer S }\n ? [S] extends [never]\n ? {}\n : S\n : {};\n\nexport type IsUnknown<T> = [unknown] extends [T] ? true : false;\n\nexport type NextFn<TExpectedState = unknown> =\n IsUnknown<TExpectedState> extends true\n ? <S extends Record<string, unknown> = {}>(state?: S) => Promise<NextResult<S>>\n : (state: TExpectedState) => Promise<NextResult<TExpectedState>>;\n\nexport type MiddlewareNext = NextFn;\n\nexport type MiddlewareDefinition = {\n query?: unknown;\n params?: unknown;\n body?: unknown;\n returns?: ReturnsMap;\n handler: (ctx: unknown, next: MiddlewareNext) => Awaitable<Response | unknown>;\n};\n\nexport type MiddlewareReturnFromParts<\n TQuery,\n TParams,\n TBody,\n TState,\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 /** 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 = Omit<RuntimeContextFields, \"var\">;\n\nexport type StandaloneMiddlewareContext<\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n TState = {},\n> = Simplify<\n TAppContext &\n UnitRuntimeContext & {\n query: Simplify<UnwrapPart<TQuery>>;\n params: Simplify<UnwrapPart<TParams>>;\n body: Simplify<UnwrapPart<TBody>>;\n state: Simplify<UnwrapPart<TState>>;\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 query: Simplify<\n MergePart<\n Validators extends { query?: infer Q } ? Q : unknown,\n MergeMiddlewareField<Acc, \"query\">\n >\n >;\n params: Simplify<\n MergePart<\n Validators extends { params?: infer P } ? P : unknown,\n MergeMiddlewareField<Acc, \"params\">\n >\n >;\n body: Simplify<\n MergePart<\n Validators extends { body?: infer B } ? B : unknown,\n MergeMiddlewareField<Acc, \"body\">\n >\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 TRequiredLayouts = unknown,\n TRequiredState = unknown,\n> = MiddlewareDefinition & {\n readonly __middlewareAcc: TAcc;\n readonly __returns?: TReturns;\n readonly __requiredLayouts?: TRequiredLayouts;\n readonly __requiredState?: TRequiredState;\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<Response>;\n returns?: ReturnsMap;\n query?: Schema<unknown>;\n params?: Schema<unknown>;\n body?: Schema<unknown>;\n};\n\nexport type InlineMiddlewareOptions<\n Ctx = unknown,\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n R = unknown,\n> = {\n query?: Schema<TQuery, TQueryIn>;\n params?: Schema<TParams, TParamsIn>;\n body?: Schema<TBody, TBodyIn>;\n returns?: TReturns;\n handler: (ctx: Ctx, next: NextFn) => Awaitable<R>;\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":";AA4LA,SAAgB,cACd,OAC0D;CAC1D,OACE,OAAO,UAAU,YACjB,UAAU,QACV,aAAa,SACb,iBAAiB,SACjB,MAAM,QAAS,MAA0D,WAAW;AAExF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taserjs/router",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Type-safe Node.js router with runtime validation and middleware",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"repository": {
|
|
@@ -147,8 +147,8 @@
|
|
|
147
147
|
"dependencies": {
|
|
148
148
|
"@standard-schema/spec": "^1.1.0",
|
|
149
149
|
"hono": "^4.10.3",
|
|
150
|
-
"@taserjs/router-
|
|
151
|
-
"@taserjs/router-
|
|
150
|
+
"@taserjs/router-utils": "0.0.5",
|
|
151
|
+
"@taserjs/router-core": "0.0.5"
|
|
152
152
|
},
|
|
153
153
|
"devDependencies": {
|
|
154
154
|
"@tanstack/vite-config": "^0.6.0",
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RouteManifestShape, TaserRuntime } from "@taserjs/router-core";
|
|
2
|
+
|
|
3
|
+
class TaserServeView {
|
|
4
|
+
readonly fetch: (
|
|
5
|
+
request: Request,
|
|
6
|
+
env?: unknown,
|
|
7
|
+
executionCtx?: unknown,
|
|
8
|
+
) => Promise<Response> | Response;
|
|
9
|
+
|
|
10
|
+
constructor(protected readonly runtime: TaserRuntime) {
|
|
11
|
+
this.fetch = (request: Request, env?: unknown, executionCtx?: unknown) => {
|
|
12
|
+
return this.runtime.fetch(request, env, executionCtx as never);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class TaserApp<
|
|
18
|
+
TManifest extends RouteManifestShape = RouteManifestShape,
|
|
19
|
+
> extends TaserServeView {
|
|
20
|
+
readonly __manifest?: TManifest;
|
|
21
|
+
|
|
22
|
+
constructor(runtime: TaserRuntime, manifest?: TManifest) {
|
|
23
|
+
super(runtime);
|
|
24
|
+
if (manifest !== undefined) {
|
|
25
|
+
this.__manifest = manifest;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createRouteBuilder } from "./route.js";
|
|
2
|
-
import type { RouteBuilder, RoutePath, Schema } from "
|
|
3
|
-
import type { AppContext, HttpMethod } from "
|
|
2
|
+
import type { RouteBuilder, RoutePath, Schema } from "../types/index.js";
|
|
3
|
+
import type { AppContext, HttpMethod } from "../types/units.js";
|
|
4
4
|
|
|
5
5
|
type WithoutBodyOptions<TQuery, TParams, TQueryIn = unknown, TParamsIn = unknown> = {
|
|
6
6
|
query?: Schema<TQuery, TQueryIn>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { LayoutId, MiddlewareBuilder } from "
|
|
2
|
-
import type { MiddlewareDefinition } from "
|
|
1
|
+
import type { LayoutId, MiddlewareBuilder } from "../types/index.js";
|
|
2
|
+
import type { MiddlewareDefinition } from "../types/units.js";
|
|
3
3
|
|
|
4
4
|
export function createMiddleware<const Layout extends LayoutId>(
|
|
5
5
|
layout: Layout,
|
|
@@ -6,11 +6,17 @@ import {
|
|
|
6
6
|
withAuto422,
|
|
7
7
|
} from "@taserjs/router-utils";
|
|
8
8
|
|
|
9
|
-
import type {
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
import type {
|
|
10
|
+
Method,
|
|
11
|
+
RouteBuilder,
|
|
12
|
+
RoutePath,
|
|
13
|
+
ReturnsMap,
|
|
14
|
+
ValidatorParts,
|
|
15
|
+
} from "../types/index.js";
|
|
16
|
+
import type { HttpMethod, MiddlewareDefinition } from "../types/units.js";
|
|
17
|
+
import { isHandlerUnit } from "../types/units.js";
|
|
12
18
|
import { HANDLER_SCHEMA_KEY_MAP } from "./constants.js";
|
|
13
|
-
import { pickDefinedSchemas, type SchemaValidators } from "
|
|
19
|
+
import { pickDefinedSchemas, type SchemaValidators } from "../define/validators.js";
|
|
14
20
|
|
|
15
21
|
function toUtilsMap(map: ReturnsMap | undefined): Record<number, StandardSchemaV1> {
|
|
16
22
|
if (!map) {
|
|
@@ -90,49 +96,39 @@ export function createRouteBuilder(
|
|
|
90
96
|
const routeSchemas = pickDefinedSchemas(validators);
|
|
91
97
|
const base = buildRouteBase(path, method, methods, middlewares);
|
|
92
98
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
schemas: {
|
|
101
|
-
...validators,
|
|
102
|
-
handlerQuery: fnOrUnit.query,
|
|
103
|
-
handlerParams: fnOrUnit.params,
|
|
104
|
-
handlerBody: fnOrUnit.body,
|
|
105
|
-
middlewares,
|
|
106
|
-
handlerMiddlewares: fnOrUnit.middlewares,
|
|
107
|
-
},
|
|
108
|
-
});
|
|
99
|
+
const unit = isHandlerUnit(fnOrUnit)
|
|
100
|
+
? fnOrUnit
|
|
101
|
+
: {
|
|
102
|
+
handler: fnOrUnit as (ctx: unknown) => Response | Promise<Response>,
|
|
103
|
+
middlewares: [] as MiddlewareDefinition[],
|
|
104
|
+
returns: undefined as ReturnsMap | undefined,
|
|
105
|
+
};
|
|
109
106
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
handler: fnOrUnit.handler,
|
|
114
|
-
...routeSchemas,
|
|
115
|
-
...handlerSchemas,
|
|
116
|
-
...(returns ? { returns } : {}),
|
|
117
|
-
};
|
|
118
|
-
}
|
|
107
|
+
const handlerSchemas = isHandlerUnit(fnOrUnit)
|
|
108
|
+
? pickDefinedSchemas(fnOrUnit, HANDLER_SCHEMA_KEY_MAP)
|
|
109
|
+
: {};
|
|
119
110
|
|
|
120
111
|
const returns = buildEffectiveReturns({
|
|
121
112
|
middlewareReturns: collectReturnsFromDefinitions(middlewares),
|
|
122
|
-
routeReturns
|
|
123
|
-
handlerMiddlewareReturns:
|
|
124
|
-
handlerReturns:
|
|
113
|
+
routeReturns,
|
|
114
|
+
handlerMiddlewareReturns: collectReturnsFromDefinitions(unit.middlewares),
|
|
115
|
+
handlerReturns: unit.returns,
|
|
125
116
|
schemas: {
|
|
126
117
|
...validators,
|
|
118
|
+
handlerQuery: (unit as any).query,
|
|
119
|
+
handlerParams: (unit as any).params,
|
|
120
|
+
handlerBody: (unit as any).body,
|
|
127
121
|
middlewares,
|
|
122
|
+
handlerMiddlewares: unit.middlewares,
|
|
128
123
|
},
|
|
129
124
|
});
|
|
130
125
|
|
|
131
126
|
return {
|
|
132
127
|
...base,
|
|
133
|
-
handlerMiddlewares: [],
|
|
134
|
-
handler:
|
|
128
|
+
handlerMiddlewares: [...unit.middlewares],
|
|
129
|
+
handler: unit.handler,
|
|
135
130
|
...routeSchemas,
|
|
131
|
+
...handlerSchemas,
|
|
136
132
|
...(returns ? { returns } : {}),
|
|
137
133
|
};
|
|
138
134
|
},
|