@taserjs/router 0.0.5 → 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 +5 -0
- package/dist/cjs/builder/app.cjs.map +1 -1
- package/dist/cjs/builder/app.d.cts +7 -7
- package/dist/cjs/builder/factories.cjs +4 -4
- package/dist/cjs/builder/factories.cjs.map +1 -1
- package/dist/cjs/builder/factories.d.cts +5 -51
- package/dist/cjs/builder/route.cjs +22 -1
- package/dist/cjs/builder/route.cjs.map +1 -1
- package/dist/cjs/builder/route.d.cts +1 -2
- package/dist/cjs/builder/router.cjs +3 -5
- package/dist/cjs/builder/router.cjs.map +1 -1
- package/dist/cjs/builder/router.d.cts +11 -12
- 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/index.cjs +0 -12
- package/dist/cjs/index.d.cts +4 -4
- 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 +3 -21
- package/dist/cjs/types/index.d.cts +29 -7
- package/dist/cjs/types/returns.d.cts +6 -9
- package/dist/cjs/types/type-utils.d.cts +7 -5
- package/dist/cjs/types/units.cjs.map +1 -1
- package/dist/cjs/types/units.d.cts +4 -10
- package/dist/esm/builder/app.d.ts +7 -7
- package/dist/esm/builder/app.js +5 -1
- package/dist/esm/builder/app.js.map +1 -1
- package/dist/esm/builder/factories.d.ts +5 -51
- package/dist/esm/builder/factories.js +4 -4
- package/dist/esm/builder/factories.js.map +1 -1
- package/dist/esm/builder/route.d.ts +1 -2
- package/dist/esm/builder/route.js +22 -1
- package/dist/esm/builder/route.js.map +1 -1
- package/dist/esm/builder/router.d.ts +11 -12
- package/dist/esm/builder/router.js +3 -5
- package/dist/esm/builder/router.js.map +1 -1
- 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/index.d.ts +4 -4
- package/dist/esm/index.js +2 -2
- 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 +3 -21
- package/dist/esm/types/index.d.ts +29 -7
- package/dist/esm/types/returns.d.ts +6 -9
- package/dist/esm/types/type-utils.d.ts +7 -5
- package/dist/esm/types/units.d.ts +4 -10
- package/dist/esm/types/units.js.map +1 -1
- package/package.json +3 -3
- package/src/builder/app.ts +16 -5
- package/src/builder/factories.ts +28 -155
- package/src/builder/route.ts +25 -1
- package/src/builder/router.ts +24 -157
- package/src/define/middleware.ts +115 -123
- package/src/index.ts +5 -9
- package/src/reply.ts +1 -11
- package/src/stream.ts +1 -1
- package/src/types/app.ts +3 -22
- package/src/types/index.ts +83 -13
- package/src/types/returns.ts +7 -76
- package/src/types/type-utils.ts +9 -4
- package/src/types/units.ts +34 -10
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","names":[],"sources":["../../../src/builder/router.ts"],"sourcesContent":["import {\n createTaserCompatHandler,\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 {\n defineMiddleware,\n type DefineMiddlewareOptions,\n type MultiScopedMiddlewareOptions,\n type ScopedMiddlewareOptions,\n} 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 {\n AppContext,\n ExtractState,\n IsUnknown,\n MiddlewareReturnFromParts,\n MiddlewareUnit,\n} 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<TAppContext extends Record<string, unknown> = AppContext> {\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> {\n this.state.contextDef = definition;\n return this as unknown as TaserRouter<TBoot & TReq>;\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(handler: (ctx: unknown) => Response | Promise<Response> | unknown): this {\n this.state.notFound = (ctx) => handler(ctx);\n return this;\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(\n middleware: Parameters<typeof createTaserCompatHandler>[0],\n ): MiddlewareUnit<MiddlewareReturnFromParts<unknown, unknown, unknown, {}>, {}, null, {}>;\n\n defineMiddleware<\n const Layout extends LayoutId,\n TState = unknown,\n TRequires = {},\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 layout: Layout,\n options: ScopedMiddlewareOptions<\n Layout,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\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 Layout,\n TRequires\n >;\n\n defineMiddleware<\n const Layouts extends readonly [LayoutId, ...LayoutId[]],\n TState = unknown,\n TRequires = {},\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 layouts: Layouts,\n options: MultiScopedMiddlewareOptions<\n Layouts,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\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 Layouts,\n TRequires\n >;\n\n defineMiddleware<\n TState = unknown,\n TRequires = {},\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 options: DefineMiddlewareOptions<\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\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 null,\n TRequires\n >;\n\n defineMiddleware(first: any, second?: any): any {\n return defineMiddleware(first, second);\n }\n\n create<const TManifest extends RouteManifestShape>(manifest: TManifest): TaserApp<TManifest> {\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 = this.state.options.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);\n }\n}\n\nexport function createTaserApp(options: CreateTaserAppOptions = {}): TaserRouter {\n return new TaserRouter(options);\n}\n"],"mappings":"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"}
|
|
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":"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"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { createTaserCompatHandler, Awaitable } from '@taserjs/router-core';
|
|
2
2
|
import { ReturnsMap } from '../types/returns.js';
|
|
3
|
-
import { AppContext,
|
|
3
|
+
import { AppContext, DefineMiddlewareResult, MiddlewareReturnFromParts, MiddlewareUnit, NextFn, StandaloneMiddlewareContext } from '../types/units.js';
|
|
4
4
|
import { LayoutId, ResolveLayoutMiddlewaresState, ResolveLayoutsState } from '../types/index.js';
|
|
5
5
|
import { Schema } from '../types/schema.js';
|
|
6
|
-
type HonoMiddlewareHandler = Parameters<typeof createTaserCompatHandler>[0];
|
|
7
|
-
type HonoMiddlewareUnit = MiddlewareUnit<MiddlewareReturnFromParts<unknown, unknown, unknown, {}>, {}, null, {}>;
|
|
6
|
+
export type HonoMiddlewareHandler = Parameters<typeof createTaserCompatHandler>[0];
|
|
7
|
+
export type HonoMiddlewareUnit = MiddlewareUnit<MiddlewareReturnFromParts<unknown, unknown, unknown, {}>, {}, null, {}>;
|
|
8
8
|
export type DefineMiddlewareOptions<TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, TAppContext extends Record<string, unknown> = AppContext, R = unknown> = {
|
|
9
9
|
query?: Schema<TQuery, TQueryIn>;
|
|
10
10
|
params?: Schema<TParams, TParamsIn>;
|
|
@@ -26,8 +26,10 @@ export type MultiScopedMiddlewareOptions<Layouts extends readonly LayoutId[], TS
|
|
|
26
26
|
returns?: TReturns;
|
|
27
27
|
handler: (ctx: StandaloneMiddlewareContext<TQuery, TParams, TBody, TAppContext, ResolveLayoutsState<Layouts> & TRequires>, next: NextFn<NoInfer<TState>>) => Awaitable<R>;
|
|
28
28
|
};
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
29
|
+
export interface DefineMiddlewareFn<TAppContext extends Record<string, unknown> = AppContext> {
|
|
30
|
+
(middleware: HonoMiddlewareHandler): HonoMiddlewareUnit;
|
|
31
|
+
<const Layout extends LayoutId, TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, R = unknown>(layout: Layout, options: ScopedMiddlewareOptions<Layout, TState, TRequires, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn, TAppContext, R>): DefineMiddlewareResult<TQuery, TParams, TBody, TState, R, TQueryIn, TParamsIn, TBodyIn, TReturns, Layout, TRequires>;
|
|
32
|
+
<const Layouts extends readonly [LayoutId, ...LayoutId[]], TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, R = unknown>(layouts: Layouts, options: MultiScopedMiddlewareOptions<Layouts, TState, TRequires, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn, TAppContext, R>): DefineMiddlewareResult<TQuery, TParams, TBody, TState, R, TQueryIn, TParamsIn, TBodyIn, TReturns, Layouts, TRequires>;
|
|
33
|
+
<TState = unknown, TRequires = {}, TQuery = unknown, TParams = unknown, TBody = unknown, TReturns extends ReturnsMap = {}, TQueryIn = unknown, TParamsIn = unknown, TBodyIn = unknown, R = unknown>(options: DefineMiddlewareOptions<TState, TRequires, TQuery, TParams, TBody, TReturns, TQueryIn, TParamsIn, TBodyIn, TAppContext, R>): DefineMiddlewareResult<TQuery, TParams, TBody, TState, R, TQueryIn, TParamsIn, TBodyIn, TReturns, null, TRequires>;
|
|
34
|
+
}
|
|
35
|
+
export declare const defineMiddleware: DefineMiddlewareFn<AppContext>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createTaserCompatHandler } from "@taserjs/router-core";
|
|
2
2
|
//#region src/define/middleware.ts
|
|
3
|
-
function defineMiddleware(first, second) {
|
|
3
|
+
var defineMiddleware = function defineMiddleware(first, second) {
|
|
4
4
|
if (typeof first === "string" || Array.isArray(first)) {
|
|
5
5
|
const options = second;
|
|
6
6
|
return {
|
|
@@ -20,7 +20,7 @@ function defineMiddleware(first, second) {
|
|
|
20
20
|
__middlewareAcc: void 0,
|
|
21
21
|
...options.returns ? { __returns: options.returns } : {}
|
|
22
22
|
};
|
|
23
|
-
}
|
|
23
|
+
};
|
|
24
24
|
//#endregion
|
|
25
25
|
export { defineMiddleware };
|
|
26
26
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","names":[],"sources":["../../../src/define/middleware.ts"],"sourcesContent":["import { createTaserCompatHandler, type Awaitable } from \"@taserjs/router-core\";\n\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type {\n AppContext,\n
|
|
1
|
+
{"version":3,"file":"middleware.js","names":[],"sources":["../../../src/define/middleware.ts"],"sourcesContent":["import { createTaserCompatHandler, type Awaitable } from \"@taserjs/router-core\";\n\nimport type { ReturnsMap } from \"../types/returns.js\";\nimport type {\n AppContext,\n DefineMiddlewareResult,\n MiddlewareReturnFromParts,\n MiddlewareUnit,\n NextFn,\n StandaloneMiddlewareContext,\n} from \"../types/units.js\";\nimport type {\n LayoutId,\n ResolveLayoutMiddlewaresState,\n ResolveLayoutsState,\n} from \"../types/index.js\";\nimport type { Schema } from \"../types/schema.js\";\n\nexport type HonoMiddlewareHandler = Parameters<typeof createTaserCompatHandler>[0];\n\nexport type HonoMiddlewareUnit = MiddlewareUnit<\n MiddlewareReturnFromParts<unknown, unknown, unknown, {}>,\n {},\n null,\n {}\n>;\n\nexport type DefineMiddlewareOptions<\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n R = unknown,\n> = {\n query?: Schema<TQuery, TQueryIn>;\n params?: Schema<TParams, TParamsIn>;\n body?: Schema<TBody, TBodyIn>;\n returns?: TReturns;\n handler: (\n ctx: StandaloneMiddlewareContext<TQuery, TParams, TBody, TAppContext, TRequires>,\n next: NextFn<NoInfer<TState>>,\n ) => Awaitable<R>;\n};\n\nexport type ScopedMiddlewareOptions<\n Layout extends LayoutId,\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n R = unknown,\n> = {\n query?: Schema<TQuery, TQueryIn>;\n params?: Schema<TParams, TParamsIn>;\n body?: Schema<TBody, TBodyIn>;\n returns?: TReturns;\n handler: (\n ctx: StandaloneMiddlewareContext<\n TQuery,\n TParams,\n TBody,\n TAppContext,\n ResolveLayoutMiddlewaresState<Layout> & TRequires\n >,\n next: NextFn<NoInfer<TState>>,\n ) => Awaitable<R>;\n};\n\nexport type MultiScopedMiddlewareOptions<\n Layouts extends readonly LayoutId[],\n TState = unknown,\n TRequires = {},\n TQuery = unknown,\n TParams = unknown,\n TBody = unknown,\n TReturns extends ReturnsMap = {},\n TQueryIn = unknown,\n TParamsIn = unknown,\n TBodyIn = unknown,\n TAppContext extends Record<string, unknown> = AppContext,\n R = unknown,\n> = {\n query?: Schema<TQuery, TQueryIn>;\n params?: Schema<TParams, TParamsIn>;\n body?: Schema<TBody, TBodyIn>;\n returns?: TReturns;\n handler: (\n ctx: StandaloneMiddlewareContext<\n TQuery,\n TParams,\n TBody,\n TAppContext,\n ResolveLayoutsState<Layouts> & TRequires\n >,\n next: NextFn<NoInfer<TState>>,\n ) => Awaitable<R>;\n};\n\nexport interface DefineMiddlewareFn<TAppContext extends Record<string, unknown> = AppContext> {\n (middleware: HonoMiddlewareHandler): HonoMiddlewareUnit;\n\n <\n const Layout extends LayoutId,\n TState = unknown,\n TRequires = {},\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 layout: Layout,\n options: ScopedMiddlewareOptions<\n Layout,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): DefineMiddlewareResult<\n TQuery,\n TParams,\n TBody,\n TState,\n R,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TReturns,\n Layout,\n TRequires\n >;\n\n <\n const Layouts extends readonly [LayoutId, ...LayoutId[]],\n TState = unknown,\n TRequires = {},\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 layouts: Layouts,\n options: MultiScopedMiddlewareOptions<\n Layouts,\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): DefineMiddlewareResult<\n TQuery,\n TParams,\n TBody,\n TState,\n R,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TReturns,\n Layouts,\n TRequires\n >;\n\n <\n TState = unknown,\n TRequires = {},\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 options: DefineMiddlewareOptions<\n TState,\n TRequires,\n TQuery,\n TParams,\n TBody,\n TReturns,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TAppContext,\n R\n >,\n ): DefineMiddlewareResult<\n TQuery,\n TParams,\n TBody,\n TState,\n R,\n TQueryIn,\n TParamsIn,\n TBodyIn,\n TReturns,\n null,\n TRequires\n >;\n}\n\nexport const defineMiddleware: DefineMiddlewareFn<AppContext> = function defineMiddleware(\n first: any,\n second?: any,\n): any {\n if (typeof first === \"string\" || Array.isArray(first)) {\n const options = second!;\n const unit = {\n ...options,\n __middlewareAcc: undefined as unknown,\n ...(options?.returns ? { __returns: options.returns } : {}),\n __requiredLayouts: first,\n };\n return unit;\n }\n\n if (typeof first === \"function\") {\n const honoMiddleware = first as HonoMiddlewareHandler;\n return defineMiddleware({\n handler: (ctx: unknown, next: (state?: Record<string, unknown>) => unknown) =>\n createTaserCompatHandler(honoMiddleware)(ctx, next),\n });\n }\n\n const options = first as DefineMiddlewareOptions<\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any,\n any\n >;\n const unit = {\n ...options,\n __middlewareAcc: undefined as unknown,\n ...(options.returns ? { __returns: options.returns } : {}),\n };\n\n return unit;\n};\n"],"mappings":";;AA6OA,IAAa,mBAAmD,SAAS,iBACvE,OACA,QACK;CACL,IAAI,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;EACrD,MAAM,UAAU;EAOhB,OAAO;GALL,GAAG;GACH,iBAAiB,KAAA;GACjB,GAAI,SAAS,UAAU,EAAE,WAAW,QAAQ,QAAQ,IAAI,CAAC;GACzD,mBAAmB;EAEd;CACT;CAEA,IAAI,OAAO,UAAU,YAAY;EAC/B,MAAM,iBAAiB;EACvB,OAAO,iBAAiB,EACtB,UAAU,KAAc,SACtB,yBAAyB,cAAc,CAAC,CAAC,KAAK,IAAI,EACtD,CAAC;CACH;CAEA,MAAM,UAAU;CAmBhB,OAAO;EALL,GAAG;EACH,iBAAiB,KAAA;EACjB,GAAI,QAAQ,UAAU,EAAE,WAAW,QAAQ,QAAQ,IAAI,CAAC;CAGnD;AACT"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type { RouterRegister } from './register.js';
|
|
2
|
-
export type { InferInput, InferOutput, InferRouteContext, InferRouteInput, InferRouteOutput, Method, PathParams, ReturnsMap, RouteDefinition, RouteExport, RoutePath, Schema, Simplify, } from './types/index.js';
|
|
3
|
-
export type { ContextDefinition, CreateTaserAppOptions, InferAppContext, InferAppManifest, OnErrorOptions, RouteManifestShape,
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
2
|
+
export type { HttpMethod, InferInput, InferOutput, InferRouteContext, InferRouteInput, InferRouteOutput, Method, MiddlewareDefinition, PathParams, ReturnsMap, RouteDefinition, RouteExport, RoutePath, Schema, Simplify, StatusCode, } from './types/index.js';
|
|
3
|
+
export type { ContextDefinition, CreateTaserAppOptions, InferAppContext, InferAppManifest, OnErrorOptions, RouteManifestShape, } from './types/app.js';
|
|
4
|
+
export { ValidationError, validationErrorSchema } from '@taserjs/router-utils';
|
|
5
|
+
export type { ResponseValidationFailureHandler, SuccessStatusCode } from '@taserjs/router-utils';
|
|
6
6
|
export { createTaserApp, TaserRouter } from './builder/router.js';
|
|
7
7
|
export { TaserApp } from './builder/app.js';
|
|
8
8
|
export { createContext } from './context/create-context.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -3,5 +3,5 @@ import { defineMiddleware } from "./define/middleware.js";
|
|
|
3
3
|
import { TaserApp } from "./builder/app.js";
|
|
4
4
|
import { TaserRouter, createTaserApp } from "./builder/router.js";
|
|
5
5
|
import { createContext } from "./context/create-context.js";
|
|
6
|
-
import { ValidationError,
|
|
7
|
-
export { TaserApp, TaserRouter, ValidationError, createContext, createTaserApp, defineHandler, defineMiddleware,
|
|
6
|
+
import { ValidationError, validationErrorSchema } from "@taserjs/router-utils";
|
|
7
|
+
export { TaserApp, TaserRouter, ValidationError, createContext, createTaserApp, defineHandler, defineMiddleware, validationErrorSchema };
|
package/dist/esm/reply.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export type { BinaryBody, BodyKind, FileReplyInit, RedirectInit, ReplyBodyKind, ReplyInit, ReplyOf, SuccessReplyData, } from '@taserjs/router-utils/reply';
|
|
1
|
+
export * from '@taserjs/router-utils/reply';
|
package/dist/esm/reply.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { errorReply, reply, successReply };
|
|
1
|
+
export * from "@taserjs/router-utils/reply";
|
package/dist/esm/stream.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export * from '@taserjs/router-utils/stream';
|
package/dist/esm/stream.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { blob, buffer, file, pipe, stream };
|
|
1
|
+
export * from "@taserjs/router-utils/stream";
|
package/dist/esm/types/app.d.ts
CHANGED
|
@@ -1,26 +1,16 @@
|
|
|
1
|
-
import { RouteManifestShape } from '@taserjs/router-core';
|
|
1
|
+
import { CookieRuntimeConfig, RouteManifestShape } from '@taserjs/router-core';
|
|
2
2
|
import { ResponseValidationFailureHandler } from '@taserjs/router-utils';
|
|
3
3
|
import { TaserApp } from '../builder/app.js';
|
|
4
4
|
import { ReturnsMap } from './returns.js';
|
|
5
5
|
import { Schema } from './schema.js';
|
|
6
6
|
export type CreateTaserAppOptions = {
|
|
7
|
-
basePath?: string;
|
|
8
7
|
response?: {
|
|
9
8
|
/** Validate handler replies against returns maps. Default true. */
|
|
10
9
|
validate?: boolean;
|
|
11
10
|
onValidationFailure?: ResponseValidationFailureHandler;
|
|
12
11
|
};
|
|
13
12
|
/** Global cookie defaults and signing secret. Per-call `ctx.cookies.set` options override defaults. */
|
|
14
|
-
cookies?:
|
|
15
|
-
secret?: string | BufferSource;
|
|
16
|
-
path?: string;
|
|
17
|
-
httpOnly?: boolean;
|
|
18
|
-
sameSite?: "Strict" | "Lax" | "None" | "strict" | "lax" | "none";
|
|
19
|
-
secure?: boolean;
|
|
20
|
-
domain?: string;
|
|
21
|
-
maxAge?: number;
|
|
22
|
-
expires?: Date;
|
|
23
|
-
};
|
|
13
|
+
cookies?: CookieRuntimeConfig;
|
|
24
14
|
};
|
|
25
15
|
export type ContextDefinition<TBoot extends Record<string, unknown> = Record<string, never>, TReq extends Record<string, unknown> = Record<string, never>> = {
|
|
26
16
|
boot?: () => TBoot | Promise<TBoot>;
|
|
@@ -31,17 +21,9 @@ export type OnErrorOptions<TResponses extends ReturnsMap = ReturnsMap> = {
|
|
|
31
21
|
responses?: TResponses;
|
|
32
22
|
handle: (error: unknown, ctx?: unknown) => Response | Promise<Response>;
|
|
33
23
|
};
|
|
34
|
-
export type InferAppManifest<TApp> = TApp extends TaserApp<infer TManifest> ? TManifest : TApp extends {
|
|
24
|
+
export type InferAppManifest<TApp> = TApp extends TaserApp<infer TManifest, any> ? TManifest : TApp extends {
|
|
35
25
|
manifest: infer TManifest;
|
|
36
26
|
} ? TManifest : TApp extends RouteManifestShape ? TApp : never;
|
|
37
|
-
export type TaserHandler<TFrameworkApp> = {
|
|
38
|
-
/**
|
|
39
|
-
* Mount the Taser handler on a framework app.
|
|
40
|
-
* Use universal wildcard patterns (`/*`, `/prefix/*`) for Hono/Fastify.
|
|
41
|
-
* Use framework-native splat patterns for Express (e.g. `/{*splat}`, `/*splat`).
|
|
42
|
-
*/
|
|
43
|
-
mount(pattern: string, frameworkApp: TFrameworkApp): void;
|
|
44
|
-
};
|
|
45
27
|
export type { RouteManifestShape } from '@taserjs/router-core';
|
|
46
28
|
export type { Schema };
|
|
47
29
|
export type { TaserApp } from '../builder/app.js';
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Awaitable, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
2
|
import { RouterRegister } from '../register.js';
|
|
3
|
-
import { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape,
|
|
3
|
+
import { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape, Simplify, UnionToIntersection, UnitRuntimeContext } from './type-utils.js';
|
|
4
4
|
import { ReturnsMap, ValidHandlerReply } from './returns.js';
|
|
5
5
|
import { Schema } from './schema.js';
|
|
6
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
|
-
export type { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape, Simplify, UnwrapPart, } from './type-utils.js';
|
|
10
|
+
export type { MergeMiddlewareField, MergeMiddlewareInputField, MergePart, RequestShape, Simplify, UnitRuntimeContext, UnwrapPart, } from './type-utils.js';
|
|
11
11
|
export type { EmptyReturns, EnforceHandlerReply, HandlerReply, HasReturns, MergeReturns, ReplyFor, ReturnsMap, StatusCode, ValidHandlerReply, } from './returns.js';
|
|
12
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 {
|
|
@@ -57,9 +57,11 @@ type RouteParent<Path extends RoutePath, TMethod extends Method> = RouteEntry<Pa
|
|
|
57
57
|
type RouteLayoutChain<Path extends RoutePath, TMethod extends Method> = RouteEntry<Path, TMethod> extends {
|
|
58
58
|
layoutChain: infer Chain extends readonly LayoutId[];
|
|
59
59
|
} ? Chain : RouteParent<Path, TMethod> extends LayoutId ? readonly [RouteParent<Path, TMethod>] : readonly [];
|
|
60
|
-
type
|
|
60
|
+
export type RouteLayoutMiddlewares<Path extends RoutePath, TMethod extends Method> = ResolveLayoutChainMiddlewares<RouteLayoutChain<Path, TMethod>>;
|
|
61
|
+
export type RouteLayoutField<Path extends RoutePath, TMethod extends Method, Field extends "query" | "params" | "body" | "state"> = MergeMiddlewareField<RouteLayoutMiddlewares<Path, TMethod>, Field>;
|
|
62
|
+
export type RouteLayoutInputField<Path extends RoutePath, TMethod extends Method, Field extends "query" | "params" | "body"> = MergeMiddlewareInputField<RouteLayoutMiddlewares<Path, TMethod>, Field>;
|
|
63
|
+
type RouteResolvedField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends "query" | "params" | "body" | "state"> = RouteLayoutField<Path, TMethod, Field> & MergeMiddlewareField<Acc, Field>;
|
|
61
64
|
type RouteChainField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends "query" | "params" | "body" | "state"> = RouteResolvedField<Path, TMethod, Acc, Field>;
|
|
62
|
-
type UnitRuntimeContext = Omit<RuntimeContextFields, "var">;
|
|
63
65
|
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 & {
|
|
64
66
|
query: Simplify<MergePart<TQuery, RouteChainField<Path, TMethod, Acc, "query">>>;
|
|
65
67
|
params: Simplify<ResolveParams<PathParams<Path>, MergePart<TParams, RouteChainField<Path, TMethod, Acc, "params">>>>;
|
|
@@ -105,7 +107,7 @@ export type RouteHandleContextWithoutBody<Path extends RoutePath, TMethod extend
|
|
|
105
107
|
headers: TaserHeaders;
|
|
106
108
|
cookies: TaserCookieJar;
|
|
107
109
|
}>;
|
|
108
|
-
type RouteResolvedInputField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends "query" | "params" | "body"> =
|
|
110
|
+
type RouteResolvedInputField<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[], Field extends "query" | "params" | "body"> = RouteLayoutInputField<Path, TMethod, Field> & MergeMiddlewareInputField<Acc, Field>;
|
|
109
111
|
type IsNever<T> = [T] extends [never] ? true : false;
|
|
110
112
|
type IsEmptyObject<T> = [keyof T] extends [never] ? true : false;
|
|
111
113
|
export type IsLayoutAllowed<TRequiredLayouts, TChain extends readonly LayoutId[]> = [
|
|
@@ -139,7 +141,8 @@ export type InferRouteInputFromPath<Path extends RoutePath, TMethod extends Meth
|
|
|
139
141
|
Input: infer I;
|
|
140
142
|
};
|
|
141
143
|
} ? I : never : never;
|
|
142
|
-
type
|
|
144
|
+
export type MiddlewareLayoutField<Layout extends LayoutId, Field extends "query" | "params" | "body" | "state"> = MergeMiddlewareField<ResolveLayoutMiddlewares<LayoutParent<Layout>>, Field>;
|
|
145
|
+
type MiddlewareChainField<Layout extends LayoutId, Acc extends readonly unknown[], Field extends "query" | "params" | "body" | "state"> = MiddlewareLayoutField<Layout, Field> & MergeMiddlewareField<Acc, Field>;
|
|
143
146
|
export type MiddlewareChainContext<Layout extends LayoutId, Acc extends readonly unknown[], TQuery, TParams, TBody, TAppContext extends Record<string, unknown> = AppContext> = Simplify<TAppContext & UnitRuntimeContext & {
|
|
144
147
|
query: Simplify<MergePart<TQuery, MiddlewareChainField<Layout, Acc, "query">>>;
|
|
145
148
|
params: Simplify<MergePart<TParams, MiddlewareChainField<Layout, Acc, "params">>>;
|
|
@@ -178,6 +181,7 @@ export type RouteExport<Path extends RoutePath, TMethod extends Method, Acc exte
|
|
|
178
181
|
readonly middlewares: readonly MiddlewareDefinition[];
|
|
179
182
|
readonly handlerMiddlewares: readonly MiddlewareDefinition[];
|
|
180
183
|
readonly returns?: TReturns;
|
|
184
|
+
readonly bodyMode?: "json" | "form" | "urlencoded";
|
|
181
185
|
handler: (ctx: unknown) => Awaitable<Response>;
|
|
182
186
|
readonly $Infer: {
|
|
183
187
|
Context: TMethod extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? RouteHandleContextWithoutBody<Path, TMethod, Acc, {}> : RouteHandleContext<Path, TMethod, Acc, {}>;
|
|
@@ -198,7 +202,7 @@ type RouteHandleResult<Path extends RoutePath, TMethod extends Method, Acc exten
|
|
|
198
202
|
Output: TOutput;
|
|
199
203
|
};
|
|
200
204
|
};
|
|
201
|
-
export type
|
|
205
|
+
export type RouteBuilderBase<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[] = readonly [], Validators extends ValidatorParts = {}, TReturns extends ReturnsMap = {}, TAppContext extends Record<string, unknown> = AppContext> = {
|
|
202
206
|
readonly path: Path;
|
|
203
207
|
readonly method: TMethod;
|
|
204
208
|
readonly $Infer: {
|
|
@@ -206,6 +210,14 @@ export type RouteBuilder<Path extends RoutePath, TMethod extends Method, Acc ext
|
|
|
206
210
|
Input: TMethod extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? RouteHandleInputWithoutBody<Path, TMethod, Acc, Validators> : RouteHandleInput<Path, TMethod, Acc, Validators>;
|
|
207
211
|
Output: TReturns;
|
|
208
212
|
};
|
|
213
|
+
query<TQuery, TQueryIn = unknown>(schema: Schema<TQuery, TQueryIn>): RouteBuilder<Path, TMethod, Acc, Omit<Validators, "query" | "queryIn"> & {
|
|
214
|
+
query: TQuery;
|
|
215
|
+
queryIn: TQueryIn;
|
|
216
|
+
}, TReturns, TAppContext>;
|
|
217
|
+
params<TParams, TParamsIn = unknown>(schema: Schema<TParams, TParamsIn>): RouteBuilder<Path, TMethod, Acc, Omit<Validators, "params" | "paramsIn"> & {
|
|
218
|
+
params: TParams;
|
|
219
|
+
paramsIn: TParamsIn;
|
|
220
|
+
}, TReturns, TAppContext>;
|
|
209
221
|
returns<const M extends ReturnsMap>(map: M): RouteBuilder<Path, TMethod, Acc, Validators, Omit<TReturns, keyof M> & M, TAppContext>;
|
|
210
222
|
use<TAcc, UReturns extends ReturnsMap = {}, TRequiredLayouts = unknown, TRequiredState = unknown>(unit: MiddlewareUnit<TAcc, UReturns, TRequiredLayouts, TRequiredState>, ..._assert: [
|
|
211
223
|
IsLayoutAllowed<TRequiredLayouts, RouteLayoutChain<Path, TMethod>>,
|
|
@@ -231,6 +243,16 @@ export type RouteBuilder<Path extends RoutePath, TMethod extends Method, Acc ext
|
|
|
231
243
|
]): RouteHandleResult<Path, TMethod, Acc, Validators, TReturns, R, TAppContext>;
|
|
232
244
|
handler<HandlerAcc extends readonly unknown[] = readonly [], HandlerValidators extends ValidatorParts = ValidatorParts, HReturns extends ReturnsMap = {}, HOutput = Response>(unit: HandlerUnit<HandlerAcc, HandlerValidators, HReturns, HOutput>): RouteHandleResult<Path, TMethod, readonly [...Acc, ...HandlerAcc], Validators, Omit<TReturns, keyof HReturns> & HReturns, HOutput, TAppContext>;
|
|
233
245
|
};
|
|
246
|
+
export type RouteBuilder<Path extends RoutePath, TMethod extends Method, Acc extends readonly unknown[] = readonly [], Validators extends ValidatorParts = {}, TReturns extends ReturnsMap = {}, TAppContext extends Record<string, unknown> = AppContext> = RouteBuilderBase<Path, TMethod, Acc, Validators, TReturns, TAppContext> & (TMethod extends "GET" | "DELETE" | "HEAD" | "OPTIONS" ? {} : {
|
|
247
|
+
body<TBody, TBodyIn = unknown>(schema: Schema<TBody, TBodyIn>): RouteBuilder<Path, TMethod, Acc, Omit<Validators, "body" | "bodyIn"> & {
|
|
248
|
+
body: TBody;
|
|
249
|
+
bodyIn: TBodyIn;
|
|
250
|
+
}, TReturns, TAppContext>;
|
|
251
|
+
body<Mode extends "json" | "form" | "urlencoded", TBody, TBodyIn = unknown>(mode: Mode, schema: Schema<TBody, TBodyIn>): RouteBuilder<Path, TMethod, Acc, Omit<Validators, "body" | "bodyIn"> & {
|
|
252
|
+
body: TBody;
|
|
253
|
+
bodyIn: TBodyIn;
|
|
254
|
+
}, TReturns, TAppContext>;
|
|
255
|
+
});
|
|
234
256
|
export type HandlerBuilder<Acc extends readonly unknown[] = readonly [], Validators extends ValidatorParts = {}, TReturns extends ReturnsMap = {}, TAppContext extends Record<string, unknown> = AppContext> = {
|
|
235
257
|
returns<const M extends ReturnsMap>(map: M): HandlerBuilder<Acc, Validators, Omit<TReturns, keyof M> & M, TAppContext>;
|
|
236
258
|
use<TAcc, UReturns extends ReturnsMap = {}, TRequiredLayouts = unknown, TRequiredState = unknown>(unit: MiddlewareUnit<TAcc, UReturns, TRequiredLayouts, TRequiredState>, ..._assert: [IsStateSatisfied<MergeMiddlewareField<Acc, "state">, TRequiredState>] extends [
|
|
@@ -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
13
|
export type ReplyFor<M extends ReturnsMap> = {
|
|
17
|
-
[S in keyof M
|
|
18
|
-
}[keyof M];
|
|
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;
|
|
@@ -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;
|
|
@@ -41,8 +43,8 @@ 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
49
|
};
|
|
48
|
-
export
|
|
50
|
+
export type UnitRuntimeContext = Omit<RuntimeContextFields, "var">;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Awaitable, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
-
import { MergeMiddlewareField, MergePart, RequestShape,
|
|
1
|
+
import { Awaitable, MiddlewareDefinition as CoreMiddlewareDefinition, TaserCookieJar, TaserHeaders } from '@taserjs/router-core';
|
|
2
|
+
import { MergeMiddlewareField, MergePart, RequestShape, Simplify, UnitRuntimeContext, UnwrapPart } from './type-utils.js';
|
|
3
3
|
import { ReturnsMap } from './returns.js';
|
|
4
4
|
import { Schema } from './schema.js';
|
|
5
5
|
export type { HttpMethod, Method } from './type-utils.js';
|
|
@@ -28,13 +28,7 @@ export type ExtractState<T> = [Extract<Awaited<T>, {
|
|
|
28
28
|
export type IsUnknown<T> = [unknown] extends [T] ? true : false;
|
|
29
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>>;
|
|
30
30
|
export type MiddlewareNext = NextFn;
|
|
31
|
-
export type MiddlewareDefinition =
|
|
32
|
-
query?: unknown;
|
|
33
|
-
params?: unknown;
|
|
34
|
-
body?: unknown;
|
|
35
|
-
returns?: ReturnsMap;
|
|
36
|
-
handler: (ctx: unknown, next: MiddlewareNext) => Awaitable<Response | unknown>;
|
|
37
|
-
};
|
|
31
|
+
export type MiddlewareDefinition = CoreMiddlewareDefinition<ReturnsMap>;
|
|
38
32
|
export type MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TQueryIn = TQuery, TParamsIn = TParams, TBodyIn = TBody> = {
|
|
39
33
|
query: Simplify<UnwrapPart<TQuery>>;
|
|
40
34
|
params: Simplify<UnwrapPart<TParams>>;
|
|
@@ -47,7 +41,7 @@ export type MiddlewareReturnFromParts<TQuery, TParams, TBody, TState, TQueryIn =
|
|
|
47
41
|
body: RequestShape<TBodyIn, TBody>;
|
|
48
42
|
};
|
|
49
43
|
};
|
|
50
|
-
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>;
|
|
51
45
|
export type StandaloneMiddlewareContext<TQuery = unknown, TParams = unknown, TBody = unknown, TAppContext extends Record<string, unknown> = AppContext, TState = {}> = Simplify<TAppContext & UnitRuntimeContext & {
|
|
52
46
|
query: Simplify<UnwrapPart<TQuery>>;
|
|
53
47
|
params: Simplify<UnwrapPart<TParams>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"units.js","names":[],"sources":["../../../src/types/units.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"units.js","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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taserjs/router",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
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-core": "0.1.0",
|
|
151
|
+
"@taserjs/router-utils": "0.1.0"
|
|
152
152
|
},
|
|
153
153
|
"devDependencies": {
|
|
154
154
|
"@tanstack/vite-config": "^0.6.0",
|
package/src/builder/app.ts
CHANGED
|
@@ -1,25 +1,36 @@
|
|
|
1
1
|
import type { RouteManifestShape, TaserRuntime } from "@taserjs/router-core";
|
|
2
2
|
|
|
3
|
-
class TaserServeView {
|
|
3
|
+
export class TaserServeView<THasNotFound extends boolean = boolean> {
|
|
4
4
|
readonly fetch: (
|
|
5
5
|
request: Request,
|
|
6
6
|
env?: unknown,
|
|
7
7
|
executionCtx?: unknown,
|
|
8
|
-
) =>
|
|
8
|
+
) => THasNotFound extends true
|
|
9
|
+
? Promise<Response> | Response
|
|
10
|
+
: Promise<Response | undefined> | Response | undefined;
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
readonly request: (
|
|
13
|
+
path: string,
|
|
14
|
+
init?: RequestInit,
|
|
15
|
+
) => THasNotFound extends true ? Promise<Response> : Promise<Response | undefined>;
|
|
16
|
+
|
|
17
|
+
constructor(protected readonly runtime: TaserRuntime<THasNotFound>) {
|
|
11
18
|
this.fetch = (request: Request, env?: unknown, executionCtx?: unknown) => {
|
|
12
19
|
return this.runtime.fetch(request, env, executionCtx as never);
|
|
13
20
|
};
|
|
21
|
+
this.request = (path: string, init?: RequestInit) => {
|
|
22
|
+
return this.runtime.request(path, init);
|
|
23
|
+
};
|
|
14
24
|
}
|
|
15
25
|
}
|
|
16
26
|
|
|
17
27
|
export class TaserApp<
|
|
18
28
|
TManifest extends RouteManifestShape = RouteManifestShape,
|
|
19
|
-
|
|
29
|
+
THasNotFound extends boolean = boolean,
|
|
30
|
+
> extends TaserServeView<THasNotFound> {
|
|
20
31
|
readonly __manifest?: TManifest;
|
|
21
32
|
|
|
22
|
-
constructor(runtime: TaserRuntime
|
|
33
|
+
constructor(runtime: TaserRuntime<THasNotFound>, manifest?: TManifest) {
|
|
23
34
|
super(runtime);
|
|
24
35
|
if (manifest !== undefined) {
|
|
25
36
|
this.__manifest = manifest;
|