@taserjs/router 0.0.4 → 0.1.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/dist/cjs/builder/app.cjs +27 -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} +5 -5
- package/dist/cjs/builder/factories.cjs.map +1 -0
- package/dist/cjs/builder/factories.d.cts +15 -0
- 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} +41 -34
- package/dist/cjs/builder/route.cjs.map +1 -0
- package/dist/cjs/builder/route.d.cts +4 -0
- package/dist/cjs/{taser-router.cjs → builder/router.cjs} +38 -13
- package/dist/cjs/builder/router.cjs.map +1 -0
- package/dist/{esm/taser-router.d.ts → cjs/builder/router.d.cts} +16 -11
- 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/middleware.cjs +2 -2
- package/dist/cjs/define/middleware.cjs.map +1 -1
- package/dist/cjs/define/middleware.d.cts +10 -8
- 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 -28
- package/dist/cjs/index.d.cts +7 -8
- 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/reply.cjs +8 -19
- package/dist/cjs/reply.d.cts +1 -2
- package/dist/cjs/stream.cjs +8 -31
- package/dist/cjs/stream.d.cts +1 -1
- package/dist/cjs/types/app.d.cts +8 -26
- package/dist/cjs/types/index.d.cts +60 -19
- package/dist/cjs/types/returns.d.cts +8 -11
- package/dist/cjs/types/type-utils.d.cts +8 -7
- package/dist/cjs/types/units.cjs.map +1 -1
- package/dist/cjs/types/units.d.cts +6 -17
- package/dist/esm/builder/app.d.ts +11 -0
- package/dist/esm/builder/app.js +26 -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/builder/factories.d.ts +15 -0
- package/dist/esm/{factories.js → builder/factories.js} +5 -5
- 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/builder/route.d.ts +4 -0
- package/dist/esm/{route.js → builder/route.js} +41 -34
- package/dist/esm/builder/route.js.map +1 -0
- package/dist/{cjs/taser-router.d.cts → esm/builder/router.d.ts} +16 -11
- package/dist/esm/{taser-router.js → builder/router.js} +37 -12
- 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/middleware.d.ts +10 -8
- package/dist/esm/define/middleware.js +2 -2
- package/dist/esm/define/middleware.js.map +1 -1
- 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 +7 -8
- 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/reply.d.ts +1 -2
- package/dist/esm/reply.js +1 -2
- package/dist/esm/stream.d.ts +1 -1
- package/dist/esm/stream.js +1 -2
- package/dist/esm/types/app.d.ts +8 -26
- package/dist/esm/types/index.d.ts +60 -19
- package/dist/esm/types/returns.d.ts +8 -11
- package/dist/esm/types/type-utils.d.ts +8 -7
- package/dist/esm/types/units.d.ts +6 -17
- package/dist/esm/types/units.js.map +1 -1
- package/package.json +3 -3
- package/src/builder/app.ts +39 -0
- package/src/builder/factories.ts +58 -0
- package/src/{middleware.ts → builder/middleware.ts} +2 -2
- package/src/{route.ts → builder/route.ts} +55 -35
- package/src/{taser-router.ts → builder/router.ts} +77 -28
- package/src/{create-context.ts → context/create-context.ts} +2 -2
- package/src/define/middleware.ts +115 -123
- package/src/define/validators.ts +1 -1
- package/src/index.ts +13 -20
- 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/reply.ts +1 -11
- package/src/stream.ts +1 -1
- package/src/types/app.ts +13 -28
- package/src/types/index.ts +126 -22
- package/src/types/returns.ts +9 -78
- package/src/types/type-utils.ts +10 -6
- package/src/types/units.ts +36 -16
- 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/factories.d.cts +0 -61
- package/dist/cjs/middleware.cjs.map +0 -1
- package/dist/cjs/route.cjs.map +0 -1
- package/dist/cjs/route.d.cts +0 -5
- 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/esm/constants.js.map +0 -1
- package/dist/esm/create-context.js.map +0 -1
- package/dist/esm/factories.d.ts +0 -61
- package/dist/esm/factories.js.map +0 -1
- package/dist/esm/middleware.js.map +0 -1
- package/dist/esm/route.d.ts +0 -5
- 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.js.map +0 -1
- package/src/factories.ts +0 -185
- package/src/taser-app.ts +0 -38
- /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
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
import { ReplyOf } from '@taserjs/router-utils';
|
|
1
|
+
import { ReplyOf } from '@taserjs/router-utils/reply';
|
|
2
|
+
import { StatusCode } from '@taserjs/router-utils';
|
|
2
3
|
import { Simplify } from './type-utils.js';
|
|
3
4
|
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 & {});
|
|
5
|
+
export type { StatusCode };
|
|
9
6
|
/** Status-code → Standard Schema map for route/middleware/handler outputs. */
|
|
10
7
|
export type ReturnsMap = {
|
|
11
|
-
readonly [
|
|
8
|
+
readonly [status: number]: Schema<unknown> | undefined;
|
|
12
9
|
};
|
|
13
10
|
/** Later map wins on overlapping status keys. */
|
|
14
11
|
export type MergeReturns<A extends ReturnsMap, B extends ReturnsMap> = Omit<A, keyof B> & B;
|
|
15
12
|
/** Typed reply union for statuses declared in a returns map. */
|
|
16
|
-
export type
|
|
17
|
-
[S in keyof M
|
|
18
|
-
}[keyof M];
|
|
13
|
+
export type ReplyFor<M extends ReturnsMap> = {
|
|
14
|
+
[S in keyof M & number]: M[S] extends Schema<unknown> ? ReplyOf<S, Simplify<InferOutput<M[S]>>> : never;
|
|
15
|
+
}[keyof M & number];
|
|
19
16
|
export type EmptyReturns = {};
|
|
20
17
|
/** True when no status schemas have been declared yet. */
|
|
21
18
|
export type HasReturns<M extends ReturnsMap> = [keyof M] extends [never] ? false : true;
|
|
@@ -34,4 +31,4 @@ export type EnforceHandlerReply<R, M extends ReturnsMap> = [R] extends [ValidHan
|
|
|
34
31
|
* Expected `.handler` return type for a returns map.
|
|
35
32
|
* Prefer constraining handler generics with {@link EnforceHandlerReply}.
|
|
36
33
|
*/
|
|
37
|
-
export type HandlerReply<M extends ReturnsMap> = HasReturns<M> extends true ?
|
|
34
|
+
export type HandlerReply<M extends ReturnsMap> = HasReturns<M> extends true ? ReplyFor<M> | Response : Response;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
+
import { HttpMethod, HttpVerb } from '@taserjs/router-utils/http';
|
|
3
|
+
export type Method = HttpMethod;
|
|
4
|
+
export type { HttpMethod, HttpVerb };
|
|
3
5
|
export type MiddlewareFieldName = "query" | "params" | "body" | "state";
|
|
4
6
|
export type MiddlewareInputFieldName = "query" | "params" | "body";
|
|
5
7
|
export type UnwrapPart<T> = unknown extends T ? {} : T;
|
|
@@ -37,13 +39,12 @@ export type MiddlewareInputField<Middleware, Field extends MiddlewareInputFieldN
|
|
|
37
39
|
} ? V : {};
|
|
38
40
|
export type MergeMiddlewareField<Middlewares extends readonly unknown[], Field extends MiddlewareFieldName> = [Middlewares] extends [readonly []] ? {} : UnionToIntersection<MiddlewareField<Middlewares[number], Field>>;
|
|
39
41
|
export type MergeMiddlewareInputField<Middlewares extends readonly unknown[], Field extends MiddlewareInputFieldName> = [Middlewares] extends [readonly []] ? {} : UnionToIntersection<MiddlewareInputField<Middlewares[number], Field>>;
|
|
40
|
-
export type RuntimeContextFields
|
|
42
|
+
export type RuntimeContextFields = {
|
|
41
43
|
request: Request;
|
|
42
44
|
method: Method;
|
|
43
45
|
url: URL;
|
|
44
|
-
headers:
|
|
45
|
-
cookies:
|
|
46
|
+
headers: TaserHeaders;
|
|
47
|
+
cookies: TaserCookieJar;
|
|
46
48
|
var: Record<string, unknown>;
|
|
47
|
-
native?: TNative;
|
|
48
49
|
};
|
|
49
|
-
export
|
|
50
|
+
export type UnitRuntimeContext = Omit<RuntimeContextFields, "var">;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"units.cjs","names":[],"sources":["../../../src/types/units.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"units.cjs","names":[],"sources":["../../../src/types/units.ts"],"sourcesContent":["import type {\n Awaitable,\n MiddlewareDefinition as CoreMiddlewareDefinition,\n TaserCookieJar,\n TaserHeaders,\n} from \"@taserjs/router-core\";\n\nimport type {\n MergeMiddlewareField,\n MergePart,\n RequestShape,\n Simplify,\n UnitRuntimeContext,\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 = CoreMiddlewareDefinition<ReturnsMap>;\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\nexport type DefineMiddlewareResult<\n TQuery,\n TParams,\n TBody,\n TState,\n R,\n TQueryIn = TQuery,\n TParamsIn = TParams,\n TBodyIn = TBody,\n TReturns extends ReturnsMap = {},\n TLayout = null,\n TRequires = {},\n> = MiddlewareUnit<\n MiddlewareReturnFromParts<\n TQuery,\n TParams,\n TBody,\n IsUnknown<TState> extends true ? ExtractState<R> : TState,\n TQueryIn,\n TParamsIn,\n TBodyIn\n >,\n TReturns,\n TLayout,\n TRequires\n>;\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":";AAoNA,SAAgB,cACd,OAC0D;CAC1D,OACE,OAAO,UAAU,YACjB,UAAU,QACV,aAAa,SACb,iBAAiB,SACjB,MAAM,QAAS,MAA0D,WAAW;AAExF"}
|
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
import { Awaitable, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
-
import {
|
|
3
|
-
import { RouterRegister } from '../register.js';
|
|
4
|
-
import { MergeMiddlewareField, MergePart, RequestShape, RuntimeContextFields, Simplify, UnwrapPart } from './type-utils.js';
|
|
1
|
+
import { Awaitable, MiddlewareDefinition as CoreMiddlewareDefinition, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
+
import { MergeMiddlewareField, MergePart, RequestShape, Simplify, UnitRuntimeContext, UnwrapPart } from './type-utils.js';
|
|
5
3
|
import { ReturnsMap } from './returns.js';
|
|
6
4
|
import { Schema } from './schema.js';
|
|
7
5
|
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>, {
|
|
@@ -33,13 +28,7 @@ export type ExtractState<T> = [Extract<Awaited<T>, {
|
|
|
33
28
|
export type IsUnknown<T> = [unknown] extends [T] ? true : false;
|
|
34
29
|
export type NextFn<TExpectedState = unknown> = IsUnknown<TExpectedState> extends true ? <S extends Record<string, unknown> = {}>(state?: S) => Promise<NextResult<S>> : (state: TExpectedState) => Promise<NextResult<TExpectedState>>;
|
|
35
30
|
export type MiddlewareNext = NextFn;
|
|
36
|
-
export type MiddlewareDefinition =
|
|
37
|
-
query?: unknown;
|
|
38
|
-
params?: unknown;
|
|
39
|
-
body?: unknown;
|
|
40
|
-
returns?: ReturnsMap;
|
|
41
|
-
handler: (ctx: unknown, next: MiddlewareNext) => Awaitable<ReplyResult | Response | unknown>;
|
|
42
|
-
};
|
|
31
|
+
export type MiddlewareDefinition = CoreMiddlewareDefinition<ReturnsMap>;
|
|
43
32
|
export type MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TQueryIn = TQuery, TParamsIn = TParams, TBodyIn = TBody> = {
|
|
44
33
|
query: Simplify<UnwrapPart<TQuery>>;
|
|
45
34
|
params: Simplify<UnwrapPart<TParams>>;
|
|
@@ -52,7 +41,7 @@ export type MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TQueryIn =
|
|
|
52
41
|
body: RequestShape<TBodyIn, TBody>;
|
|
53
42
|
};
|
|
54
43
|
};
|
|
55
|
-
type
|
|
44
|
+
export type DefineMiddlewareResult<TQuery, TParams, TBody, TState, R, TQueryIn = TQuery, TParamsIn = TParams, TBodyIn = TBody, TReturns extends ReturnsMap = {}, TLayout = null, TRequires = {}> = MiddlewareUnit<MiddlewareReturnFromParts<TQuery, TParams, TBody, IsUnknown<TState> extends true ? ExtractState<R> : TState, TQueryIn, TParamsIn, TBodyIn>, TReturns, TLayout, TRequires>;
|
|
56
45
|
export type StandaloneMiddlewareContext<TQuery = unknown, TParams = unknown, TBody = unknown, TAppContext extends Record<string, unknown> = AppContext, TState = {}> = Simplify<TAppContext & UnitRuntimeContext & {
|
|
57
46
|
query: Simplify<UnwrapPart<TQuery>>;
|
|
58
47
|
params: Simplify<UnwrapPart<TParams>>;
|
|
@@ -87,7 +76,7 @@ export type HandlerUnit<_Acc extends readonly unknown[], _Validators extends Val
|
|
|
87
76
|
Output: TOutput;
|
|
88
77
|
};
|
|
89
78
|
middlewares: readonly MiddlewareDefinition[];
|
|
90
|
-
handler: (ctx: unknown) => Awaitable<
|
|
79
|
+
handler: (ctx: unknown) => Awaitable<Response>;
|
|
91
80
|
returns?: ReturnsMap;
|
|
92
81
|
query?: Schema<unknown>;
|
|
93
82
|
params?: Schema<unknown>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { RouteManifestShape, TaserRuntime } from '@taserjs/router-core';
|
|
2
|
+
export declare class TaserServeView<THasNotFound extends boolean = boolean> {
|
|
3
|
+
protected readonly runtime: TaserRuntime<THasNotFound>;
|
|
4
|
+
readonly fetch: (request: Request, env?: unknown, executionCtx?: unknown) => THasNotFound extends true ? Promise<Response> | Response : Promise<Response | undefined> | Response | undefined;
|
|
5
|
+
readonly request: (path: string, init?: RequestInit) => THasNotFound extends true ? Promise<Response> : Promise<Response | undefined>;
|
|
6
|
+
constructor(runtime: TaserRuntime<THasNotFound>);
|
|
7
|
+
}
|
|
8
|
+
export declare class TaserApp<TManifest extends RouteManifestShape = RouteManifestShape, THasNotFound extends boolean = boolean> extends TaserServeView<THasNotFound> {
|
|
9
|
+
readonly __manifest?: TManifest;
|
|
10
|
+
constructor(runtime: TaserRuntime<THasNotFound>, manifest?: TManifest);
|
|
11
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//#region src/builder/app.ts
|
|
2
|
+
var TaserServeView = class {
|
|
3
|
+
runtime;
|
|
4
|
+
fetch;
|
|
5
|
+
request;
|
|
6
|
+
constructor(runtime) {
|
|
7
|
+
this.runtime = runtime;
|
|
8
|
+
this.fetch = (request, env, executionCtx) => {
|
|
9
|
+
return this.runtime.fetch(request, env, executionCtx);
|
|
10
|
+
};
|
|
11
|
+
this.request = (path, init) => {
|
|
12
|
+
return this.runtime.request(path, init);
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var TaserApp = class extends TaserServeView {
|
|
17
|
+
__manifest;
|
|
18
|
+
constructor(runtime, manifest) {
|
|
19
|
+
super(runtime);
|
|
20
|
+
if (manifest !== void 0) this.__manifest = manifest;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
export { TaserApp, TaserServeView };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=app.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app.js","names":[],"sources":["../../../src/builder/app.ts"],"sourcesContent":["import type { RouteManifestShape, TaserRuntime } from \"@taserjs/router-core\";\n\nexport class TaserServeView<THasNotFound extends boolean = boolean> {\n readonly fetch: (\n request: Request,\n env?: unknown,\n executionCtx?: unknown,\n ) => THasNotFound extends true\n ? Promise<Response> | Response\n : Promise<Response | undefined> | Response | undefined;\n\n readonly request: (\n path: string,\n init?: RequestInit,\n ) => THasNotFound extends true ? Promise<Response> : Promise<Response | undefined>;\n\n constructor(protected readonly runtime: TaserRuntime<THasNotFound>) {\n this.fetch = (request: Request, env?: unknown, executionCtx?: unknown) => {\n return this.runtime.fetch(request, env, executionCtx as never);\n };\n this.request = (path: string, init?: RequestInit) => {\n return this.runtime.request(path, init);\n };\n }\n}\n\nexport class TaserApp<\n TManifest extends RouteManifestShape = RouteManifestShape,\n THasNotFound extends boolean = boolean,\n> extends TaserServeView<THasNotFound> {\n readonly __manifest?: TManifest;\n\n constructor(runtime: TaserRuntime<THasNotFound>, manifest?: TManifest) {\n super(runtime);\n if (manifest !== undefined) {\n this.__manifest = manifest;\n }\n }\n}\n"],"mappings":";AAEA,IAAa,iBAAb,MAAoE;CAcnC;CAb/B;CAQA;CAKA,YAAY,SAAwD;EAArC,KAAA,UAAA;EAC7B,KAAK,SAAS,SAAkB,KAAe,iBAA2B;GACxE,OAAO,KAAK,QAAQ,MAAM,SAAS,KAAK,YAAqB;EAC/D;EACA,KAAK,WAAW,MAAc,SAAuB;GACnD,OAAO,KAAK,QAAQ,QAAQ,MAAM,IAAI;EACxC;CACF;AACF;AAEA,IAAa,WAAb,cAGU,eAA6B;CACrC;CAEA,YAAY,SAAqC,UAAsB;EACrE,MAAM,OAAO;EACb,IAAI,aAAa,KAAA,GACf,KAAK,aAAa;CAEtB;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","names":[],"sources":["../../../src/builder/constants.ts"],"sourcesContent":["/** Input schema keys validated by middleware and route handlers. Headers/cookies are parsed only. */\nexport const SCHEMA_KEYS = [\"query\", \"params\", \"body\"] as const;\n\nexport type SchemaKey = (typeof SCHEMA_KEYS)[number];\n\nexport const HANDLER_SCHEMA_KEY_MAP = {\n query: \"handlerQuery\",\n params: \"handlerParams\",\n body: \"handlerBody\",\n} as const satisfies Record<SchemaKey, string>;\n"],"mappings":";;AACA,IAAa,cAAc;CAAC;CAAS;CAAU;AAAM;AAIrD,IAAa,yBAAyB;CACpC,OAAO;CACP,QAAQ;CACR,MAAM;AACR"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { RouteBuilder, RoutePath } from '../types/index.js';
|
|
2
|
+
import { AppContext, HttpMethod } from '../types/units.js';
|
|
3
|
+
export type CreateWithoutBodyRoute<M extends "GET" | "DELETE" | "OPTIONS" | "HEAD", TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
|
|
4
|
+
export type CreateWithBodyRoute<M extends "POST" | "PUT" | "PATCH", TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
|
|
5
|
+
export type CreateAnyRoute<TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath, const Methods extends readonly HttpMethod[]>(path: Path, methods: Methods) => RouteBuilder<Path, Methods[number], readonly [], {}, {}, TAppContext>;
|
|
6
|
+
export type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = <const Path extends RoutePath>(path: Path) => RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;
|
|
7
|
+
export declare const createGetRoute: CreateWithoutBodyRoute<"GET", AppContext>;
|
|
8
|
+
export declare const createDeleteRoute: CreateWithoutBodyRoute<"DELETE", AppContext>;
|
|
9
|
+
export declare const createOptionsRoute: CreateWithoutBodyRoute<"OPTIONS", AppContext>;
|
|
10
|
+
export declare const createHeadRoute: CreateWithoutBodyRoute<"HEAD", AppContext>;
|
|
11
|
+
export declare const createPostRoute: CreateWithBodyRoute<"POST", AppContext>;
|
|
12
|
+
export declare const createPutRoute: CreateWithBodyRoute<"PUT", AppContext>;
|
|
13
|
+
export declare const createPatchRoute: CreateWithBodyRoute<"PATCH", AppContext>;
|
|
14
|
+
export declare const createAnyRoute: CreateAnyRoute;
|
|
15
|
+
export declare const createAllRoute: CreateAllRoute;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createRouteBuilder } from "./route.js";
|
|
2
|
-
//#region src/factories.ts
|
|
2
|
+
//#region src/builder/factories.ts
|
|
3
3
|
function createWithoutBodyRoute(method) {
|
|
4
|
-
return ((path
|
|
4
|
+
return ((path) => createRouteBuilder(path, method));
|
|
5
5
|
}
|
|
6
6
|
function createWithBodyRoute(method) {
|
|
7
|
-
return ((path
|
|
7
|
+
return ((path) => createRouteBuilder(path, method));
|
|
8
8
|
}
|
|
9
9
|
var createGetRoute = createWithoutBodyRoute("GET");
|
|
10
10
|
var createDeleteRoute = createWithoutBodyRoute("DELETE");
|
|
@@ -13,8 +13,8 @@ var createHeadRoute = createWithoutBodyRoute("HEAD");
|
|
|
13
13
|
var createPostRoute = createWithBodyRoute("POST");
|
|
14
14
|
var createPutRoute = createWithBodyRoute("PUT");
|
|
15
15
|
var createPatchRoute = createWithBodyRoute("PATCH");
|
|
16
|
-
var createAnyRoute = ((path, methods
|
|
17
|
-
var createAllRoute = ((path
|
|
16
|
+
var createAnyRoute = ((path, methods) => createRouteBuilder(path, "ANY", methods));
|
|
17
|
+
var createAllRoute = ((path) => createRouteBuilder(path, "ALL"));
|
|
18
18
|
//#endregion
|
|
19
19
|
export { createAllRoute, createAnyRoute, createDeleteRoute, createGetRoute, createHeadRoute, createOptionsRoute, createPatchRoute, createPostRoute, createPutRoute };
|
|
20
20
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factories.js","names":[],"sources":["../../../src/builder/factories.ts"],"sourcesContent":["import { createRouteBuilder } from \"./route.js\";\nimport type { RouteBuilder, RoutePath } from \"../types/index.js\";\nimport type { AppContext, HttpMethod } from \"../types/units.js\";\n\nexport type CreateWithoutBodyRoute<\n M extends \"GET\" | \"DELETE\" | \"OPTIONS\" | \"HEAD\",\n TAppContext extends Record<string, unknown> = AppContext,\n> = <const Path extends RoutePath>(\n path: Path,\n) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;\n\nexport type CreateWithBodyRoute<\n M extends \"POST\" | \"PUT\" | \"PATCH\",\n TAppContext extends Record<string, unknown> = AppContext,\n> = <const Path extends RoutePath>(\n path: Path,\n) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;\n\nexport type CreateAnyRoute<TAppContext extends Record<string, unknown> = AppContext> = <\n const Path extends RoutePath,\n const Methods extends readonly HttpMethod[],\n>(\n path: Path,\n methods: Methods,\n) => RouteBuilder<Path, Methods[number], readonly [], {}, {}, TAppContext>;\n\nexport type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = <\n const Path extends RoutePath,\n>(\n path: Path,\n) => RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;\n\nfunction createWithoutBodyRoute<M extends \"GET\" | \"DELETE\" | \"OPTIONS\" | \"HEAD\">(\n method: M,\n): CreateWithoutBodyRoute<M> {\n return ((path: string) =>\n createRouteBuilder(path, method)) as unknown as CreateWithoutBodyRoute<M>;\n}\n\nfunction createWithBodyRoute<M extends \"POST\" | \"PUT\" | \"PATCH\">(\n method: M,\n): CreateWithBodyRoute<M> {\n return ((path: string) => createRouteBuilder(path, method)) as unknown as CreateWithBodyRoute<M>;\n}\n\nexport const createGetRoute = createWithoutBodyRoute(\"GET\");\nexport const createDeleteRoute = createWithoutBodyRoute(\"DELETE\");\nexport const createOptionsRoute = createWithoutBodyRoute(\"OPTIONS\");\nexport const createHeadRoute = createWithoutBodyRoute(\"HEAD\");\nexport const createPostRoute = createWithBodyRoute(\"POST\");\nexport const createPutRoute = createWithBodyRoute(\"PUT\");\nexport const createPatchRoute = createWithBodyRoute(\"PATCH\");\n\nexport const createAnyRoute: CreateAnyRoute = ((path: string, methods: readonly HttpMethod[]) =>\n createRouteBuilder(path, \"ANY\", methods)) as unknown as CreateAnyRoute;\n\nexport const createAllRoute: CreateAllRoute = ((path: string) =>\n createRouteBuilder(path, \"ALL\")) as unknown as CreateAllRoute;\n"],"mappings":";;AAgCA,SAAS,uBACP,QAC2B;CAC3B,SAAS,SACP,mBAAmB,MAAM,MAAM;AACnC;AAEA,SAAS,oBACP,QACwB;CACxB,SAAS,SAAiB,mBAAmB,MAAM,MAAM;AAC3D;AAEA,IAAa,iBAAiB,uBAAuB,KAAK;AAC1D,IAAa,oBAAoB,uBAAuB,QAAQ;AAChE,IAAa,qBAAqB,uBAAuB,SAAS;AAClE,IAAa,kBAAkB,uBAAuB,MAAM;AAC5D,IAAa,kBAAkB,oBAAoB,MAAM;AACzD,IAAa,iBAAiB,oBAAoB,KAAK;AACvD,IAAa,mBAAmB,oBAAoB,OAAO;AAE3D,IAAa,mBAAmC,MAAc,YAC5D,mBAAmB,MAAM,OAAO,OAAO;AAEzC,IAAa,mBAAmC,SAC9C,mBAAmB,MAAM,KAAK"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { LayoutId, MiddlewareBuilder } from '
|
|
1
|
+
import { LayoutId, MiddlewareBuilder } from '../types/index.js';
|
|
2
2
|
export declare function createMiddleware<const Layout extends LayoutId>(layout: Layout): MiddlewareBuilder<Layout, readonly []>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","names":[],"sources":["../../../src/builder/middleware.ts"],"sourcesContent":["import type { LayoutId, MiddlewareBuilder } from \"../types/index.js\";\nimport type { MiddlewareDefinition } from \"../types/units.js\";\n\nexport function createMiddleware<const Layout extends LayoutId>(\n layout: Layout,\n): MiddlewareBuilder<Layout, readonly []> {\n const entries: MiddlewareDefinition[] = [];\n\n const chain = {\n layout,\n middlewares: entries,\n use(definition: MiddlewareDefinition) {\n entries.push(definition);\n return chain;\n },\n };\n\n return chain as unknown as MiddlewareBuilder<Layout, readonly []>;\n}\n"],"mappings":";AAGA,SAAgB,iBACd,QACwC;CACxC,MAAM,UAAkC,CAAC;CAEzC,MAAM,QAAQ;EACZ;EACA,aAAa;EACb,IAAI,YAAkC;GACpC,QAAQ,KAAK,UAAU;GACvB,OAAO;EACT;CACF;CAEA,OAAO;AACT"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Method, RouteBuilder, RoutePath, ValidatorParts } from '../types/index.js';
|
|
2
|
+
import { HttpMethod } from '../types/units.js';
|
|
3
|
+
/** Shared internal builder — not part of the public `@taserjs/router` API. */
|
|
4
|
+
export declare function createRouteBuilder(path: string, method: HttpMethod | "ANY" | "ALL", methods?: readonly HttpMethod[]): RouteBuilder<RoutePath, Method, readonly [], ValidatorParts>;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { isHandlerUnit } from "
|
|
1
|
+
import { isHandlerUnit } from "../types/units.js";
|
|
2
2
|
import { HANDLER_SCHEMA_KEY_MAP } from "./constants.js";
|
|
3
|
-
import { pickDefinedSchemas } from "
|
|
3
|
+
import { pickDefinedSchemas } from "../define/validators.js";
|
|
4
4
|
import { collectReturnsFromDefinitions, hasInputSchemas, mergeReturnsMaps, withAuto422 } from "@taserjs/router-utils";
|
|
5
|
-
//#region src/route.ts
|
|
5
|
+
//#region src/builder/route.ts
|
|
6
6
|
function toUtilsMap(map) {
|
|
7
7
|
if (!map) return {};
|
|
8
8
|
const out = {};
|
|
@@ -22,12 +22,32 @@ function buildRouteBase(path, method, methods, middlewares) {
|
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
24
|
/** Shared internal builder — not part of the public `@taserjs/router` API. */
|
|
25
|
-
function createRouteBuilder(path, method,
|
|
25
|
+
function createRouteBuilder(path, method, methods) {
|
|
26
26
|
const middlewares = [];
|
|
27
27
|
let routeReturns = {};
|
|
28
|
+
const validators = {};
|
|
29
|
+
let bodyMode;
|
|
28
30
|
const builder = {
|
|
29
31
|
path,
|
|
30
32
|
method,
|
|
33
|
+
query(schema) {
|
|
34
|
+
validators.query = schema;
|
|
35
|
+
return builder;
|
|
36
|
+
},
|
|
37
|
+
params(schema) {
|
|
38
|
+
validators.params = schema;
|
|
39
|
+
return builder;
|
|
40
|
+
},
|
|
41
|
+
body(modeOrSchema, schema) {
|
|
42
|
+
if (typeof modeOrSchema === "string") {
|
|
43
|
+
bodyMode = modeOrSchema;
|
|
44
|
+
if (schema !== void 0) validators.body = schema;
|
|
45
|
+
} else {
|
|
46
|
+
bodyMode = "json";
|
|
47
|
+
validators.body = modeOrSchema;
|
|
48
|
+
}
|
|
49
|
+
return builder;
|
|
50
|
+
},
|
|
31
51
|
returns(map) {
|
|
32
52
|
routeReturns = {
|
|
33
53
|
...routeReturns,
|
|
@@ -42,46 +62,33 @@ function createRouteBuilder(path, method, validators = {}, methods) {
|
|
|
42
62
|
handler(fnOrUnit) {
|
|
43
63
|
const routeSchemas = pickDefinedSchemas(validators);
|
|
44
64
|
const base = buildRouteBase(path, method, methods, middlewares);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
handlerReturns: fnOrUnit.returns,
|
|
52
|
-
schemas: {
|
|
53
|
-
...validators,
|
|
54
|
-
handlerQuery: fnOrUnit.query,
|
|
55
|
-
handlerParams: fnOrUnit.params,
|
|
56
|
-
handlerBody: fnOrUnit.body,
|
|
57
|
-
middlewares,
|
|
58
|
-
handlerMiddlewares: fnOrUnit.middlewares
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
return {
|
|
62
|
-
...base,
|
|
63
|
-
handlerMiddlewares: [...fnOrUnit.middlewares],
|
|
64
|
-
handler: fnOrUnit.handler,
|
|
65
|
-
...routeSchemas,
|
|
66
|
-
...handlerSchemas,
|
|
67
|
-
...returns ? { returns } : {}
|
|
68
|
-
};
|
|
69
|
-
}
|
|
65
|
+
const unit = isHandlerUnit(fnOrUnit) ? fnOrUnit : {
|
|
66
|
+
handler: fnOrUnit,
|
|
67
|
+
middlewares: [],
|
|
68
|
+
returns: void 0
|
|
69
|
+
};
|
|
70
|
+
const handlerSchemas = isHandlerUnit(fnOrUnit) ? pickDefinedSchemas(fnOrUnit, HANDLER_SCHEMA_KEY_MAP) : {};
|
|
70
71
|
const returns = buildEffectiveReturns({
|
|
71
72
|
middlewareReturns: collectReturnsFromDefinitions(middlewares),
|
|
72
73
|
routeReturns,
|
|
73
|
-
handlerMiddlewareReturns:
|
|
74
|
-
handlerReturns:
|
|
74
|
+
handlerMiddlewareReturns: collectReturnsFromDefinitions(unit.middlewares),
|
|
75
|
+
handlerReturns: unit.returns,
|
|
75
76
|
schemas: {
|
|
76
77
|
...validators,
|
|
77
|
-
|
|
78
|
+
handlerQuery: unit.query,
|
|
79
|
+
handlerParams: unit.params,
|
|
80
|
+
handlerBody: unit.body,
|
|
81
|
+
middlewares,
|
|
82
|
+
handlerMiddlewares: unit.middlewares
|
|
78
83
|
}
|
|
79
84
|
});
|
|
80
85
|
return {
|
|
81
86
|
...base,
|
|
82
|
-
handlerMiddlewares: [],
|
|
83
|
-
handler:
|
|
87
|
+
handlerMiddlewares: [...unit.middlewares],
|
|
88
|
+
handler: unit.handler,
|
|
84
89
|
...routeSchemas,
|
|
90
|
+
...handlerSchemas,
|
|
91
|
+
...bodyMode ? { bodyMode } : {},
|
|
85
92
|
...returns ? { returns } : {}
|
|
86
93
|
};
|
|
87
94
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.js","names":[],"sources":["../../../src/builder/route.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport {\n collectReturnsFromDefinitions,\n hasInputSchemas,\n mergeReturnsMaps,\n withAuto422,\n} from \"@taserjs/router-utils\";\nimport type { BodyMode } from \"@taserjs/router-core\";\n\nimport type {\n Method,\n RouteBuilder,\n RoutePath,\n ReturnsMap,\n ValidatorParts,\n Schema,\n} from \"../types/index.js\";\nimport type { HttpMethod, MiddlewareDefinition } from \"../types/units.js\";\nimport { isHandlerUnit } from \"../types/units.js\";\nimport { HANDLER_SCHEMA_KEY_MAP } from \"./constants.js\";\nimport { pickDefinedSchemas, type SchemaValidators } from \"../define/validators.js\";\n\nfunction toUtilsMap(map: ReturnsMap | undefined): Record<number, StandardSchemaV1> {\n if (!map) {\n return {};\n }\n const out: Record<number, StandardSchemaV1> = {};\n for (const [key, schema] of Object.entries(map)) {\n if (schema !== undefined) {\n out[Number(key)] = schema;\n }\n }\n return out;\n}\n\nfunction buildEffectiveReturns(args: {\n middlewareReturns: ReturnsMap;\n routeReturns: ReturnsMap;\n handlerMiddlewareReturns: ReturnsMap;\n handlerReturns: ReturnsMap | undefined;\n schemas: {\n query?: unknown;\n params?: unknown;\n body?: unknown;\n handlerQuery?: unknown;\n handlerParams?: unknown;\n handlerBody?: unknown;\n middlewares?: readonly MiddlewareDefinition[];\n handlerMiddlewares?: readonly MiddlewareDefinition[];\n };\n}): Record<number, StandardSchemaV1> | undefined {\n const merged = mergeReturnsMaps(\n toUtilsMap(args.middlewareReturns),\n toUtilsMap(args.routeReturns),\n toUtilsMap(args.handlerMiddlewareReturns),\n toUtilsMap(args.handlerReturns),\n );\n const with422 = withAuto422(merged, hasInputSchemas(args.schemas));\n return Object.keys(with422).length > 0 ? with422 : undefined;\n}\n\nfunction buildRouteBase(\n path: string,\n method: HttpMethod | \"ANY\" | \"ALL\",\n methods: readonly HttpMethod[] | undefined,\n middlewares: MiddlewareDefinition[],\n) {\n return {\n path,\n method,\n ...(methods ? { methods: [...methods] } : {}),\n middlewares: [...middlewares],\n };\n}\n\n/** Shared internal builder — not part of the public `@taserjs/router` API. */\nexport function createRouteBuilder(\n path: string,\n method: HttpMethod | \"ANY\" | \"ALL\",\n methods?: readonly HttpMethod[],\n): RouteBuilder<RoutePath, Method, readonly [], ValidatorParts> {\n const middlewares: MiddlewareDefinition[] = [];\n let routeReturns: Record<number, StandardSchemaV1> = {};\n const validators: SchemaValidators = {};\n let bodyMode: BodyMode | undefined;\n\n const builder = {\n path,\n method,\n query(schema: Schema<unknown>) {\n validators.query = schema;\n return builder;\n },\n params(schema: Schema<unknown>) {\n validators.params = schema;\n return builder;\n },\n body(modeOrSchema: BodyMode | Schema<unknown>, schema?: Schema<unknown>) {\n if (typeof modeOrSchema === \"string\") {\n bodyMode = modeOrSchema as BodyMode;\n if (schema !== undefined) {\n validators.body = schema;\n }\n } else {\n bodyMode = \"json\";\n validators.body = modeOrSchema;\n }\n return builder;\n },\n returns(map: ReturnsMap) {\n routeReturns = { ...routeReturns, ...toUtilsMap(map) };\n return builder;\n },\n use(definition: MiddlewareDefinition) {\n middlewares.push(definition);\n return builder;\n },\n handler(fnOrUnit: ((ctx: unknown) => unknown) | unknown) {\n const routeSchemas = pickDefinedSchemas(validators);\n const base = buildRouteBase(path, method, methods, middlewares);\n\n const unit = isHandlerUnit(fnOrUnit)\n ? fnOrUnit\n : {\n handler: fnOrUnit as (ctx: unknown) => Response | Promise<Response>,\n middlewares: [] as MiddlewareDefinition[],\n returns: undefined as ReturnsMap | undefined,\n };\n\n const handlerSchemas = isHandlerUnit(fnOrUnit)\n ? pickDefinedSchemas(fnOrUnit, HANDLER_SCHEMA_KEY_MAP)\n : {};\n\n const returns = buildEffectiveReturns({\n middlewareReturns: collectReturnsFromDefinitions(middlewares),\n routeReturns,\n handlerMiddlewareReturns: collectReturnsFromDefinitions(unit.middlewares),\n handlerReturns: unit.returns,\n schemas: {\n ...validators,\n handlerQuery: (unit as any).query,\n handlerParams: (unit as any).params,\n handlerBody: (unit as any).body,\n middlewares,\n handlerMiddlewares: unit.middlewares,\n },\n });\n\n return {\n ...base,\n handlerMiddlewares: [...unit.middlewares],\n handler: unit.handler,\n ...routeSchemas,\n ...handlerSchemas,\n ...(bodyMode ? { bodyMode } : {}),\n ...(returns ? { returns } : {}),\n };\n },\n };\n\n return builder as unknown as RouteBuilder<RoutePath, Method, readonly [], ValidatorParts>;\n}\n"],"mappings":";;;;;AAsBA,SAAS,WAAW,KAA+D;CACjF,IAAI,CAAC,KACH,OAAO,CAAC;CAEV,MAAM,MAAwC,CAAC;CAC/C,KAAK,MAAM,CAAC,KAAK,WAAW,OAAO,QAAQ,GAAG,GAC5C,IAAI,WAAW,KAAA,GACb,IAAI,OAAO,GAAG,KAAK;CAGvB,OAAO;AACT;AAEA,SAAS,sBAAsB,MAekB;CAO/C,MAAM,UAAU,YAND,iBACb,WAAW,KAAK,iBAAiB,GACjC,WAAW,KAAK,YAAY,GAC5B,WAAW,KAAK,wBAAwB,GACxC,WAAW,KAAK,cAAc,CAEJ,GAAQ,gBAAgB,KAAK,OAAO,CAAC;CACjE,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,IAAI,UAAU,KAAA;AACrD;AAEA,SAAS,eACP,MACA,QACA,SACA,aACA;CACA,OAAO;EACL;EACA;EACA,GAAI,UAAU,EAAE,SAAS,CAAC,GAAG,OAAO,EAAE,IAAI,CAAC;EAC3C,aAAa,CAAC,GAAG,WAAW;CAC9B;AACF;;AAGA,SAAgB,mBACd,MACA,QACA,SAC8D;CAC9D,MAAM,cAAsC,CAAC;CAC7C,IAAI,eAAiD,CAAC;CACtD,MAAM,aAA+B,CAAC;CACtC,IAAI;CAEJ,MAAM,UAAU;EACd;EACA;EACA,MAAM,QAAyB;GAC7B,WAAW,QAAQ;GACnB,OAAO;EACT;EACA,OAAO,QAAyB;GAC9B,WAAW,SAAS;GACpB,OAAO;EACT;EACA,KAAK,cAA0C,QAA0B;GACvE,IAAI,OAAO,iBAAiB,UAAU;IACpC,WAAW;IACX,IAAI,WAAW,KAAA,GACb,WAAW,OAAO;GAEtB,OAAO;IACL,WAAW;IACX,WAAW,OAAO;GACpB;GACA,OAAO;EACT;EACA,QAAQ,KAAiB;GACvB,eAAe;IAAE,GAAG;IAAc,GAAG,WAAW,GAAG;GAAE;GACrD,OAAO;EACT;EACA,IAAI,YAAkC;GACpC,YAAY,KAAK,UAAU;GAC3B,OAAO;EACT;EACA,QAAQ,UAAiD;GACvD,MAAM,eAAe,mBAAmB,UAAU;GAClD,MAAM,OAAO,eAAe,MAAM,QAAQ,SAAS,WAAW;GAE9D,MAAM,OAAO,cAAc,QAAQ,IAC/B,WACA;IACE,SAAS;IACT,aAAa,CAAC;IACd,SAAS,KAAA;GACX;GAEJ,MAAM,iBAAiB,cAAc,QAAQ,IACzC,mBAAmB,UAAU,sBAAsB,IACnD,CAAC;GAEL,MAAM,UAAU,sBAAsB;IACpC,mBAAmB,8BAA8B,WAAW;IAC5D;IACA,0BAA0B,8BAA8B,KAAK,WAAW;IACxE,gBAAgB,KAAK;IACrB,SAAS;KACP,GAAG;KACH,cAAe,KAAa;KAC5B,eAAgB,KAAa;KAC7B,aAAc,KAAa;KAC3B;KACA,oBAAoB,KAAK;IAC3B;GACF,CAAC;GAED,OAAO;IACL,GAAG;IACH,oBAAoB,CAAC,GAAG,KAAK,WAAW;IACxC,SAAS,KAAK;IACd,GAAG;IACH,GAAG;IACH,GAAI,WAAW,EAAE,SAAS,IAAI,CAAC;IAC/B,GAAI,UAAU,EAAE,QAAQ,IAAI,CAAC;GAC/B;EACF;CACF;CAEA,OAAO;AACT"}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { NotFoundHandler, OnErrorHandler, RouteManifestShape } from '@taserjs/router-core';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
2
|
+
import { DefineMiddlewareFn } from '../define/middleware.js';
|
|
3
|
+
import { TaserApp } from './app.js';
|
|
4
|
+
import { ContextDefinition, CreateTaserAppOptions, OnErrorOptions } from '../types/app.js';
|
|
5
|
+
import { AppContext } from '../types/units.js';
|
|
6
|
+
import { HandlerBuilder, LayoutId, MiddlewareBuilder } from '../types/index.js';
|
|
7
|
+
import { ReturnsMap } from '../types/returns.js';
|
|
8
|
+
import { Schema } from '../types/schema.js';
|
|
7
9
|
import { CreateAllRoute, CreateAnyRoute, CreateWithBodyRoute, CreateWithoutBodyRoute } from './factories.js';
|
|
8
10
|
type RouterState = {
|
|
9
11
|
options: CreateTaserAppOptions;
|
|
@@ -11,12 +13,12 @@ type RouterState = {
|
|
|
11
13
|
onError?: OnErrorHandler;
|
|
12
14
|
notFound?: NotFoundHandler;
|
|
13
15
|
};
|
|
14
|
-
export declare class TaserRouter<TAppContext extends Record<string, unknown> = AppContext> {
|
|
16
|
+
export declare class TaserRouter<TAppContext extends Record<string, unknown> = AppContext, THasNotFound extends boolean = false> {
|
|
15
17
|
private readonly state;
|
|
16
18
|
constructor(options?: CreateTaserAppOptions, state?: Partial<Omit<RouterState, "options">>);
|
|
17
|
-
context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(definition: ContextDefinition<TBoot, TReq>): TaserRouter<TBoot & TReq>;
|
|
18
|
-
onError<TResponses extends
|
|
19
|
-
notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown):
|
|
19
|
+
context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(definition: ContextDefinition<TBoot, TReq>): TaserRouter<TBoot & TReq, THasNotFound>;
|
|
20
|
+
onError<TResponses extends ReturnsMap = ReturnsMap>(options: OnErrorOptions<TResponses> | OnErrorOptions<TResponses>["handle"]): this;
|
|
21
|
+
notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown): TaserRouter<TAppContext, true>;
|
|
20
22
|
get: CreateWithoutBodyRoute<"GET", TAppContext>;
|
|
21
23
|
post: CreateWithBodyRoute<"POST", TAppContext>;
|
|
22
24
|
put: CreateWithBodyRoute<"PUT", TAppContext>;
|
|
@@ -36,7 +38,10 @@ export declare class TaserRouter<TAppContext extends Record<string, unknown> = A
|
|
|
36
38
|
params: TParams;
|
|
37
39
|
body: TBody;
|
|
38
40
|
}, {}, TAppContext>;
|
|
39
|
-
|
|
41
|
+
defineMiddleware: DefineMiddlewareFn<TAppContext>;
|
|
42
|
+
create<const TManifest extends RouteManifestShape>(manifest: TManifest, runtimeOptions?: {
|
|
43
|
+
basePath?: string;
|
|
44
|
+
}): TaserApp<TManifest, THasNotFound>;
|
|
40
45
|
}
|
|
41
|
-
export declare function createTaserApp(options?: CreateTaserAppOptions): TaserRouter
|
|
46
|
+
export declare function createTaserApp(options?: CreateTaserAppOptions): TaserRouter<AppContext, false>;
|
|
42
47
|
export {};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { createAllRoute, createAnyRoute, createDeleteRoute, createGetRoute, createHeadRoute, createOptionsRoute, createPatchRoute, createPostRoute, createPutRoute } from "./factories.js";
|
|
2
|
-
import { defineHandler } from "
|
|
2
|
+
import { defineHandler } from "../define/handler.js";
|
|
3
|
+
import { defineMiddleware } from "../define/middleware.js";
|
|
3
4
|
import { createMiddleware } from "./middleware.js";
|
|
4
|
-
import { TaserApp } from "./
|
|
5
|
-
import { normalizeOnError } from "@taserjs/router-utils";
|
|
5
|
+
import { TaserApp } from "./app.js";
|
|
6
|
+
import { isPromise, normalizeOnError } from "@taserjs/router-utils";
|
|
6
7
|
import { createTaserRuntime } from "@taserjs/router-core";
|
|
7
|
-
//#region src/
|
|
8
|
+
//#region src/builder/router.ts
|
|
8
9
|
function toOnErrorHandler(onErrorOptions) {
|
|
9
10
|
return normalizeOnError(onErrorOptions);
|
|
10
11
|
}
|
|
@@ -46,19 +47,43 @@ var TaserRouter = class {
|
|
|
46
47
|
defineHandler(options) {
|
|
47
48
|
return options === void 0 ? defineHandler() : defineHandler(options);
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
+
defineMiddleware = defineMiddleware;
|
|
51
|
+
create(manifest, runtimeOptions) {
|
|
50
52
|
const definition = this.state.contextDef;
|
|
51
53
|
const validateResponse = this.state.options.response?.validate ?? true;
|
|
52
54
|
const onValidationFailure = this.state.options.response?.onValidationFailure;
|
|
53
|
-
const basePath =
|
|
55
|
+
const basePath = runtimeOptions?.basePath;
|
|
54
56
|
const hasCustomContext = definition.boot !== void 0 || definition.request !== void 0;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
|
|
57
|
+
let bootContextResolved;
|
|
58
|
+
let bootPromise;
|
|
59
|
+
if (definition.boot !== void 0) {
|
|
60
|
+
const bootResult = definition.boot();
|
|
61
|
+
if (isPromise(bootResult)) bootPromise = bootResult.then((ctx) => {
|
|
62
|
+
bootContextResolved = ctx ?? {};
|
|
63
|
+
return bootContextResolved;
|
|
64
|
+
});
|
|
65
|
+
else bootContextResolved = bootResult ?? {};
|
|
66
|
+
} else if (hasCustomContext) bootContextResolved = {};
|
|
67
|
+
return new TaserApp(createTaserRuntime(manifest, hasCustomContext ? (req) => {
|
|
68
|
+
if (bootPromise !== void 0 && bootContextResolved === void 0) return bootPromise.then(async (bootContext) => {
|
|
69
|
+
if (!definition.request) return { ...bootContext };
|
|
70
|
+
const requestResult = definition.request(req);
|
|
71
|
+
const requestContext = isPromise(requestResult) ? await requestResult : requestResult;
|
|
72
|
+
return {
|
|
73
|
+
...bootContext,
|
|
74
|
+
...requestContext
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
const bootContext = bootContextResolved ?? {};
|
|
78
|
+
if (!definition.request) return { ...bootContext };
|
|
79
|
+
const requestResult = definition.request(req);
|
|
80
|
+
if (isPromise(requestResult)) return requestResult.then((requestContext) => ({
|
|
60
81
|
...bootContext,
|
|
61
82
|
...requestContext
|
|
83
|
+
}));
|
|
84
|
+
return {
|
|
85
|
+
...bootContext,
|
|
86
|
+
...requestResult
|
|
62
87
|
};
|
|
63
88
|
} : () => ({}), {
|
|
64
89
|
...basePath !== void 0 ? { basePath } : {},
|
|
@@ -78,4 +103,4 @@ function createTaserApp(options = {}) {
|
|
|
78
103
|
//#endregion
|
|
79
104
|
export { TaserRouter, createTaserApp };
|
|
80
105
|
|
|
81
|
-
//# sourceMappingURL=
|
|
106
|
+
//# sourceMappingURL=router.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.js","names":[],"sources":["../../../src/builder/router.ts"],"sourcesContent":["import {\n createTaserRuntime,\n type NotFoundHandler,\n type OnErrorHandler,\n type RouteManifestShape,\n} from \"@taserjs/router-core\";\nimport { isPromise, normalizeOnError } from \"@taserjs/router-utils\";\n\nimport {\n createAllRoute,\n createAnyRoute,\n createDeleteRoute,\n createGetRoute,\n createHeadRoute,\n createOptionsRoute,\n createPatchRoute,\n createPostRoute,\n createPutRoute,\n} from \"./factories.js\";\nimport { defineHandler } from \"../define/handler.js\";\nimport { defineMiddleware, type DefineMiddlewareFn } from \"../define/middleware.js\";\nimport { createMiddleware } from \"./middleware.js\";\nimport { TaserApp } from \"./app.js\";\nimport type { ContextDefinition, CreateTaserAppOptions, OnErrorOptions } from \"../types/app.js\";\nimport type { AppContext } from \"../types/units.js\";\nimport type { HandlerBuilder, LayoutId, MiddlewareBuilder } from \"../types/index.js\";\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type { Schema } from \"../types/schema.js\";\nimport type {\n CreateAllRoute,\n CreateAnyRoute,\n CreateWithBodyRoute,\n CreateWithoutBodyRoute,\n} from \"./factories.js\";\n\nfunction toOnErrorHandler(\n onErrorOptions: OnErrorOptions | OnErrorOptions[\"handle\"],\n): OnErrorHandler {\n return normalizeOnError(onErrorOptions);\n}\n\ntype RouterState = {\n options: CreateTaserAppOptions;\n contextDef: ContextDefinition<Record<string, unknown>, Record<string, unknown>>;\n onError?: OnErrorHandler;\n notFound?: NotFoundHandler;\n};\n\nconst emptyContext: ContextDefinition<Record<string, unknown>, Record<string, unknown>> = {};\n\nexport class TaserRouter<\n TAppContext extends Record<string, unknown> = AppContext,\n THasNotFound extends boolean = false,\n> {\n private readonly state: RouterState;\n\n constructor(options: CreateTaserAppOptions = {}, state?: Partial<Omit<RouterState, \"options\">>) {\n this.state = {\n options,\n contextDef: state?.contextDef ?? emptyContext,\n ...(state?.onError !== undefined ? { onError: state.onError } : {}),\n ...(state?.notFound !== undefined ? { notFound: state.notFound } : {}),\n };\n }\n\n context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(\n definition: ContextDefinition<TBoot, TReq>,\n ): TaserRouter<TBoot & TReq, THasNotFound> {\n this.state.contextDef = definition;\n return this as unknown as TaserRouter<TBoot & TReq, THasNotFound>;\n }\n\n onError<TResponses extends ReturnsMap = ReturnsMap>(\n options: OnErrorOptions<TResponses> | OnErrorOptions<TResponses>[\"handle\"],\n ): this {\n this.state.onError = toOnErrorHandler(options);\n return this;\n }\n\n notFound(\n handler: (ctx: unknown) => Response | Promise<Response> | unknown,\n ): TaserRouter<TAppContext, true> {\n this.state.notFound = (ctx) => handler(ctx);\n return this as unknown as TaserRouter<TAppContext, true>;\n }\n\n get: CreateWithoutBodyRoute<\"GET\", TAppContext> = createGetRoute as CreateWithoutBodyRoute<\n \"GET\",\n TAppContext\n >;\n post: CreateWithBodyRoute<\"POST\", TAppContext> = createPostRoute as CreateWithBodyRoute<\n \"POST\",\n TAppContext\n >;\n put: CreateWithBodyRoute<\"PUT\", TAppContext> = createPutRoute as CreateWithBodyRoute<\n \"PUT\",\n TAppContext\n >;\n patch: CreateWithBodyRoute<\"PATCH\", TAppContext> = createPatchRoute as CreateWithBodyRoute<\n \"PATCH\",\n TAppContext\n >;\n delete: CreateWithoutBodyRoute<\"DELETE\", TAppContext> =\n createDeleteRoute as CreateWithoutBodyRoute<\"DELETE\", TAppContext>;\n options: CreateWithoutBodyRoute<\"OPTIONS\", TAppContext> =\n createOptionsRoute as CreateWithoutBodyRoute<\"OPTIONS\", TAppContext>;\n head: CreateWithoutBodyRoute<\"HEAD\", TAppContext> = createHeadRoute as CreateWithoutBodyRoute<\n \"HEAD\",\n TAppContext\n >;\n any: CreateAnyRoute<TAppContext> = createAnyRoute as CreateAnyRoute<TAppContext>;\n all: CreateAllRoute<TAppContext> = createAllRoute as CreateAllRoute<TAppContext>;\n\n middleware<const Layout extends LayoutId>(\n layout: Layout,\n ): MiddlewareBuilder<Layout, readonly [], TAppContext> {\n return createMiddleware(layout) as MiddlewareBuilder<Layout, readonly [], TAppContext>;\n }\n\n defineHandler<TQuery = unknown, TParams = unknown, TBody = unknown>(options?: {\n query?: Schema<TQuery>;\n params?: Schema<TParams>;\n body?: Schema<TBody>;\n }): HandlerBuilder<\n readonly [],\n { query: TQuery; params: TParams; body: TBody },\n {},\n TAppContext\n > {\n return options === undefined\n ? defineHandler<TAppContext>()\n : defineHandler<TAppContext>(options);\n }\n\n defineMiddleware: DefineMiddlewareFn<TAppContext> =\n defineMiddleware as DefineMiddlewareFn<TAppContext>;\n\n create<const TManifest extends RouteManifestShape>(\n manifest: TManifest,\n runtimeOptions?: { basePath?: string },\n ): TaserApp<TManifest, THasNotFound> {\n const definition = this.state.contextDef;\n const validateResponse = this.state.options.response?.validate ?? true;\n const onValidationFailure = this.state.options.response?.onValidationFailure;\n const basePath = runtimeOptions?.basePath;\n\n const hasCustomContext = definition.boot !== undefined || definition.request !== undefined;\n let bootContextResolved: Record<string, unknown> | undefined;\n let bootPromise: Promise<Record<string, unknown>> | undefined;\n\n if (definition.boot !== undefined) {\n const bootResult = definition.boot();\n if (isPromise(bootResult)) {\n bootPromise = bootResult.then((ctx) => {\n bootContextResolved = (ctx ?? {}) as Record<string, unknown>;\n return bootContextResolved;\n });\n } else {\n bootContextResolved = (bootResult ?? {}) as Record<string, unknown>;\n }\n } else if (hasCustomContext) {\n bootContextResolved = {};\n }\n\n const contextFactory = hasCustomContext\n ? (req?: Request) => {\n if (bootPromise !== undefined && bootContextResolved === undefined) {\n return bootPromise.then(async (bootContext) => {\n if (!definition.request) {\n return { ...bootContext };\n }\n const requestResult = definition.request(req!);\n const requestContext = isPromise(requestResult) ? await requestResult : requestResult;\n return { ...bootContext, ...requestContext };\n });\n }\n\n const bootContext = bootContextResolved ?? {};\n if (!definition.request) {\n return { ...bootContext };\n }\n\n const requestResult = definition.request(req!);\n if (isPromise(requestResult)) {\n return requestResult.then((requestContext) => ({\n ...bootContext,\n ...requestContext,\n }));\n }\n\n return { ...bootContext, ...(requestResult as Record<string, unknown>) };\n }\n : () => ({});\n\n const runtime = createTaserRuntime(manifest, contextFactory, {\n ...(basePath !== undefined ? { basePath } : {}),\n response: {\n validate: validateResponse,\n ...(onValidationFailure !== undefined ? { onValidationFailure } : {}),\n },\n ...(this.state.onError !== undefined ? { onError: this.state.onError } : {}),\n ...(this.state.notFound !== undefined ? { notFound: this.state.notFound } : {}),\n ...(this.state.options.cookies !== undefined ? { cookies: this.state.options.cookies } : {}),\n });\n\n return new TaserApp(runtime, manifest) as unknown as TaserApp<TManifest, THasNotFound>;\n }\n}\n\nexport function createTaserApp(\n options: CreateTaserAppOptions = {},\n): TaserRouter<AppContext, false> {\n return new TaserRouter<AppContext, false>(options);\n}\n"],"mappings":";;;;;;;;AAmCA,SAAS,iBACP,gBACgB;CAChB,OAAO,iBAAiB,cAAc;AACxC;AASA,IAAM,eAAoF,CAAC;AAE3F,IAAa,cAAb,MAGE;CACA;CAEA,YAAY,UAAiC,CAAC,GAAG,OAA+C;EAC9F,KAAK,QAAQ;GACX;GACA,YAAY,OAAO,cAAc;GACjC,GAAI,OAAO,YAAY,KAAA,IAAY,EAAE,SAAS,MAAM,QAAQ,IAAI,CAAC;GACjE,GAAI,OAAO,aAAa,KAAA,IAAY,EAAE,UAAU,MAAM,SAAS,IAAI,CAAC;EACtE;CACF;CAEA,QACE,YACyC;EACzC,KAAK,MAAM,aAAa;EACxB,OAAO;CACT;CAEA,QACE,SACM;EACN,KAAK,MAAM,UAAU,iBAAiB,OAAO;EAC7C,OAAO;CACT;CAEA,SACE,SACgC;EAChC,KAAK,MAAM,YAAY,QAAQ,QAAQ,GAAG;EAC1C,OAAO;CACT;CAEA,MAAkD;CAIlD,OAAiD;CAIjD,MAA+C;CAI/C,QAAmD;CAInD,SACE;CACF,UACE;CACF,OAAoD;CAIpD,MAAmC;CACnC,MAAmC;CAEnC,WACE,QACqD;EACrD,OAAO,iBAAiB,MAAM;CAChC;CAEA,cAAoE,SASlE;EACA,OAAO,YAAY,KAAA,IACf,cAA2B,IAC3B,cAA2B,OAAO;CACxC;CAEA,mBACE;CAEF,OACE,UACA,gBACmC;EACnC,MAAM,aAAa,KAAK,MAAM;EAC9B,MAAM,mBAAmB,KAAK,MAAM,QAAQ,UAAU,YAAY;EAClE,MAAM,sBAAsB,KAAK,MAAM,QAAQ,UAAU;EACzD,MAAM,WAAW,gBAAgB;EAEjC,MAAM,mBAAmB,WAAW,SAAS,KAAA,KAAa,WAAW,YAAY,KAAA;EACjF,IAAI;EACJ,IAAI;EAEJ,IAAI,WAAW,SAAS,KAAA,GAAW;GACjC,MAAM,aAAa,WAAW,KAAK;GACnC,IAAI,UAAU,UAAU,GACtB,cAAc,WAAW,MAAM,QAAQ;IACrC,sBAAuB,OAAO,CAAC;IAC/B,OAAO;GACT,CAAC;QAED,sBAAuB,cAAc,CAAC;EAE1C,OAAO,IAAI,kBACT,sBAAsB,CAAC;EA4CzB,OAAO,IAAI,SAXK,mBAAmB,UA9BZ,oBAClB,QAAkB;GACjB,IAAI,gBAAgB,KAAA,KAAa,wBAAwB,KAAA,GACvD,OAAO,YAAY,KAAK,OAAO,gBAAgB;IAC7C,IAAI,CAAC,WAAW,SACd,OAAO,EAAE,GAAG,YAAY;IAE1B,MAAM,gBAAgB,WAAW,QAAQ,GAAI;IAC7C,MAAM,iBAAiB,UAAU,aAAa,IAAI,MAAM,gBAAgB;IACxE,OAAO;KAAE,GAAG;KAAa,GAAG;IAAe;GAC7C,CAAC;GAGH,MAAM,cAAc,uBAAuB,CAAC;GAC5C,IAAI,CAAC,WAAW,SACd,OAAO,EAAE,GAAG,YAAY;GAG1B,MAAM,gBAAgB,WAAW,QAAQ,GAAI;GAC7C,IAAI,UAAU,aAAa,GACzB,OAAO,cAAc,MAAM,oBAAoB;IAC7C,GAAG;IACH,GAAG;GACL,EAAE;GAGJ,OAAO;IAAE,GAAG;IAAa,GAAI;GAA0C;EACzE,WACO,CAAC,IAEiD;GAC3D,GAAI,aAAa,KAAA,IAAY,EAAE,SAAS,IAAI,CAAC;GAC7C,UAAU;IACR,UAAU;IACV,GAAI,wBAAwB,KAAA,IAAY,EAAE,oBAAoB,IAAI,CAAC;GACrE;GACA,GAAI,KAAK,MAAM,YAAY,KAAA,IAAY,EAAE,SAAS,KAAK,MAAM,QAAQ,IAAI,CAAC;GAC1E,GAAI,KAAK,MAAM,aAAa,KAAA,IAAY,EAAE,UAAU,KAAK,MAAM,SAAS,IAAI,CAAC;GAC7E,GAAI,KAAK,MAAM,QAAQ,YAAY,KAAA,IAAY,EAAE,SAAS,KAAK,MAAM,QAAQ,QAAQ,IAAI,CAAC;EAC5F,CAEoB,GAAS,QAAQ;CACvC;AACF;AAEA,SAAgB,eACd,UAAiC,CAAC,GACF;CAChC,OAAO,IAAI,YAA+B,OAAO;AACnD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReservedContextKey } from '@taserjs/router-core';
|
|
2
|
-
import { ContextDefinition } from '
|
|
2
|
+
import { ContextDefinition } from '../types/app.js';
|
|
3
3
|
type NoReservedKeysConstraint<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>> = Extract<keyof TBoot | keyof TReq, ReservedContextKey> extends never ? unknown : never;
|
|
4
4
|
export declare function createContext<TBoot extends Record<string, unknown> = Record<string, never>, TReq extends Record<string, unknown> = Record<string, never>>(definition: ContextDefinition<TBoot, TReq> & NoReservedKeysConstraint<TBoot, TReq>): ContextDefinition<TBoot, TReq>;
|
|
5
|
-
export type { ContextDefinition, InferAppContext } from '
|
|
5
|
+
export type { ContextDefinition, InferAppContext } from '../types/app.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-context.js","names":[],"sources":["../../../src/context/create-context.ts"],"sourcesContent":["import type { ReservedContextKey } from \"@taserjs/router-core\";\n\nimport type { ContextDefinition } from \"../types/app.js\";\n\ntype NoReservedKeysConstraint<\n TBoot extends Record<string, unknown>,\n TReq extends Record<string, unknown>,\n> = Extract<keyof TBoot | keyof TReq, ReservedContextKey> extends never ? unknown : never;\n\nexport function createContext<\n TBoot extends Record<string, unknown> = Record<string, never>,\n TReq extends Record<string, unknown> = Record<string, never>,\n>(\n definition: ContextDefinition<TBoot, TReq> & NoReservedKeysConstraint<TBoot, TReq>,\n): ContextDefinition<TBoot, TReq> {\n return definition;\n}\n\nexport type { ContextDefinition, InferAppContext } from \"../types/app.js\";\n"],"mappings":";AASA,SAAgB,cAId,YACgC;CAChC,OAAO;AACT"}
|