@taserjs/router 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/dist/cjs/builder/app.cjs +22 -0
  2. package/dist/cjs/builder/app.cjs.map +1 -0
  3. package/dist/cjs/builder/app.d.cts +11 -0
  4. package/dist/cjs/{constants.cjs → builder/constants.cjs} +1 -1
  5. package/dist/cjs/builder/constants.cjs.map +1 -0
  6. package/dist/cjs/{factories.cjs → builder/factories.cjs} +1 -1
  7. package/dist/cjs/builder/factories.cjs.map +1 -0
  8. package/dist/cjs/{factories.d.cts → builder/factories.d.cts} +2 -2
  9. package/dist/cjs/{middleware.cjs → builder/middleware.cjs} +1 -1
  10. package/dist/cjs/builder/middleware.cjs.map +1 -0
  11. package/dist/cjs/{middleware.d.cts → builder/middleware.d.cts} +1 -1
  12. package/dist/cjs/{route.cjs → builder/route.cjs} +19 -33
  13. package/dist/cjs/builder/route.cjs.map +1 -0
  14. package/dist/cjs/{route.d.cts → builder/route.d.cts} +3 -3
  15. package/dist/cjs/{taser-router.cjs → builder/router.cjs} +38 -11
  16. package/dist/cjs/builder/router.cjs.map +1 -0
  17. package/dist/cjs/builder/router.d.cts +48 -0
  18. package/dist/cjs/{create-context.cjs → context/create-context.cjs} +1 -1
  19. package/dist/cjs/context/create-context.cjs.map +1 -0
  20. package/dist/cjs/{create-context.d.cts → context/create-context.d.cts} +2 -2
  21. package/dist/cjs/define/middleware.cjs +13 -3
  22. package/dist/cjs/define/middleware.cjs.map +1 -1
  23. package/dist/cjs/define/middleware.d.cts +21 -4
  24. package/dist/cjs/define/validators.cjs +1 -1
  25. package/dist/cjs/define/validators.cjs.map +1 -1
  26. package/dist/cjs/define/validators.d.cts +1 -1
  27. package/dist/cjs/index.cjs +6 -16
  28. package/dist/cjs/index.d.cts +6 -7
  29. package/dist/cjs/middleware/body-limit.cjs +2 -2
  30. package/dist/cjs/middleware/body-limit.cjs.map +1 -1
  31. package/dist/cjs/middleware/compress.cjs +2 -2
  32. package/dist/cjs/middleware/compress.cjs.map +1 -1
  33. package/dist/cjs/middleware/cors.cjs +2 -2
  34. package/dist/cjs/middleware/cors.cjs.map +1 -1
  35. package/dist/cjs/middleware/csrf.cjs +2 -2
  36. package/dist/cjs/middleware/csrf.cjs.map +1 -1
  37. package/dist/cjs/middleware/etag.cjs +2 -2
  38. package/dist/cjs/middleware/etag.cjs.map +1 -1
  39. package/dist/cjs/middleware/jwk.cjs +2 -8
  40. package/dist/cjs/middleware/jwk.cjs.map +1 -1
  41. package/dist/cjs/middleware/jwt.cjs +2 -8
  42. package/dist/cjs/middleware/jwt.cjs.map +1 -1
  43. package/dist/cjs/middleware/secure-headers.cjs +2 -2
  44. package/dist/cjs/middleware/secure-headers.cjs.map +1 -1
  45. package/dist/cjs/middleware/timing.cjs +2 -2
  46. package/dist/cjs/middleware/timing.cjs.map +1 -1
  47. package/dist/cjs/middleware/wrap-hono.cjs +10 -0
  48. package/dist/cjs/middleware/wrap-hono.cjs.map +1 -1
  49. package/dist/cjs/middleware/wrap-hono.d.cts +4 -0
  50. package/dist/cjs/types/app.d.cts +7 -7
  51. package/dist/cjs/types/index.d.cts +73 -17
  52. package/dist/cjs/types/returns.d.cts +2 -2
  53. package/dist/cjs/types/type-utils.d.cts +1 -2
  54. package/dist/cjs/types/units.cjs.map +1 -1
  55. package/dist/cjs/types/units.d.cts +9 -12
  56. package/dist/esm/builder/app.d.ts +11 -0
  57. package/dist/esm/builder/app.js +22 -0
  58. package/dist/esm/builder/app.js.map +1 -0
  59. package/dist/esm/{constants.js → builder/constants.js} +1 -1
  60. package/dist/esm/builder/constants.js.map +1 -0
  61. package/dist/esm/{factories.d.ts → builder/factories.d.ts} +2 -2
  62. package/dist/esm/{factories.js → builder/factories.js} +1 -1
  63. package/dist/esm/builder/factories.js.map +1 -0
  64. package/dist/esm/{middleware.d.ts → builder/middleware.d.ts} +1 -1
  65. package/dist/esm/{middleware.js → builder/middleware.js} +1 -1
  66. package/dist/esm/builder/middleware.js.map +1 -0
  67. package/dist/esm/{route.d.ts → builder/route.d.ts} +3 -3
  68. package/dist/esm/{route.js → builder/route.js} +19 -33
  69. package/dist/esm/builder/route.js.map +1 -0
  70. package/dist/esm/builder/router.d.ts +48 -0
  71. package/dist/esm/{taser-router.js → builder/router.js} +37 -10
  72. package/dist/esm/builder/router.js.map +1 -0
  73. package/dist/esm/{create-context.d.ts → context/create-context.d.ts} +2 -2
  74. package/dist/esm/{create-context.js → context/create-context.js} +1 -1
  75. package/dist/esm/context/create-context.js.map +1 -0
  76. package/dist/esm/define/middleware.d.ts +21 -4
  77. package/dist/esm/define/middleware.js +13 -3
  78. package/dist/esm/define/middleware.js.map +1 -1
  79. package/dist/esm/define/validators.d.ts +1 -1
  80. package/dist/esm/define/validators.js +1 -1
  81. package/dist/esm/define/validators.js.map +1 -1
  82. package/dist/esm/index.d.ts +6 -7
  83. package/dist/esm/index.js +5 -6
  84. package/dist/esm/middleware/body-limit.js +2 -2
  85. package/dist/esm/middleware/body-limit.js.map +1 -1
  86. package/dist/esm/middleware/compress.js +2 -2
  87. package/dist/esm/middleware/compress.js.map +1 -1
  88. package/dist/esm/middleware/cors.js +2 -2
  89. package/dist/esm/middleware/cors.js.map +1 -1
  90. package/dist/esm/middleware/csrf.js +2 -2
  91. package/dist/esm/middleware/csrf.js.map +1 -1
  92. package/dist/esm/middleware/etag.js +2 -2
  93. package/dist/esm/middleware/etag.js.map +1 -1
  94. package/dist/esm/middleware/jwk.js +2 -8
  95. package/dist/esm/middleware/jwk.js.map +1 -1
  96. package/dist/esm/middleware/jwt.js +2 -8
  97. package/dist/esm/middleware/jwt.js.map +1 -1
  98. package/dist/esm/middleware/secure-headers.js +2 -2
  99. package/dist/esm/middleware/secure-headers.js.map +1 -1
  100. package/dist/esm/middleware/timing.js +2 -2
  101. package/dist/esm/middleware/timing.js.map +1 -1
  102. package/dist/esm/middleware/wrap-hono.d.ts +4 -0
  103. package/dist/esm/middleware/wrap-hono.js +10 -1
  104. package/dist/esm/middleware/wrap-hono.js.map +1 -1
  105. package/dist/esm/types/app.d.ts +7 -7
  106. package/dist/esm/types/index.d.ts +73 -17
  107. package/dist/esm/types/returns.d.ts +2 -2
  108. package/dist/esm/types/type-utils.d.ts +1 -2
  109. package/dist/esm/types/units.d.ts +9 -12
  110. package/dist/esm/types/units.js.map +1 -1
  111. package/package.json +3 -3
  112. package/src/builder/app.ts +28 -0
  113. package/src/{factories.ts → builder/factories.ts} +2 -2
  114. package/src/{middleware.ts → builder/middleware.ts} +2 -2
  115. package/src/{route.ts → builder/route.ts} +30 -34
  116. package/src/builder/router.ts +347 -0
  117. package/src/{create-context.ts → context/create-context.ts} +2 -2
  118. package/src/define/middleware.ts +197 -7
  119. package/src/define/validators.ts +1 -1
  120. package/src/index.ts +9 -12
  121. package/src/middleware/body-limit.ts +2 -2
  122. package/src/middleware/compress.ts +2 -2
  123. package/src/middleware/cors.ts +2 -2
  124. package/src/middleware/csrf.ts +2 -2
  125. package/src/middleware/etag.ts +2 -2
  126. package/src/middleware/jwk.ts +2 -7
  127. package/src/middleware/jwt.ts +2 -7
  128. package/src/middleware/secure-headers.ts +2 -2
  129. package/src/middleware/timing.ts +2 -2
  130. package/src/middleware/wrap-hono.ts +13 -0
  131. package/src/types/app.ts +12 -8
  132. package/src/types/index.ts +145 -16
  133. package/src/types/returns.ts +2 -2
  134. package/src/types/type-utils.ts +1 -2
  135. package/src/types/units.ts +10 -9
  136. package/dist/cjs/constants.cjs.map +0 -1
  137. package/dist/cjs/create-context.cjs.map +0 -1
  138. package/dist/cjs/factories.cjs.map +0 -1
  139. package/dist/cjs/middleware.cjs.map +0 -1
  140. package/dist/cjs/route.cjs.map +0 -1
  141. package/dist/cjs/taser-app.cjs +0 -36
  142. package/dist/cjs/taser-app.cjs.map +0 -1
  143. package/dist/cjs/taser-app.d.cts +0 -18
  144. package/dist/cjs/taser-router.cjs.map +0 -1
  145. package/dist/cjs/taser-router.d.cts +0 -42
  146. package/dist/esm/constants.js.map +0 -1
  147. package/dist/esm/create-context.js.map +0 -1
  148. package/dist/esm/factories.js.map +0 -1
  149. package/dist/esm/middleware.js.map +0 -1
  150. package/dist/esm/route.js.map +0 -1
  151. package/dist/esm/taser-app.d.ts +0 -18
  152. package/dist/esm/taser-app.js +0 -35
  153. package/dist/esm/taser-app.js.map +0 -1
  154. package/dist/esm/taser-router.d.ts +0 -42
  155. package/dist/esm/taser-router.js.map +0 -1
  156. package/src/taser-app.ts +0 -38
  157. package/src/taser-router.ts +0 -165
  158. /package/dist/cjs/{constants.d.cts → builder/constants.d.cts} +0 -0
  159. /package/dist/esm/{constants.d.ts → builder/constants.d.ts} +0 -0
  160. /package/src/{constants.ts → builder/constants.ts} +0 -0
@@ -0,0 +1,28 @@
1
+ import type { RouteManifestShape, TaserRuntime } from "@taserjs/router-core";
2
+
3
+ class TaserServeView {
4
+ readonly fetch: (
5
+ request: Request,
6
+ env?: unknown,
7
+ executionCtx?: unknown,
8
+ ) => Promise<Response> | Response;
9
+
10
+ constructor(protected readonly runtime: TaserRuntime) {
11
+ this.fetch = (request: Request, env?: unknown, executionCtx?: unknown) => {
12
+ return this.runtime.fetch(request, env, executionCtx as never);
13
+ };
14
+ }
15
+ }
16
+
17
+ export class TaserApp<
18
+ TManifest extends RouteManifestShape = RouteManifestShape,
19
+ > extends TaserServeView {
20
+ readonly __manifest?: TManifest;
21
+
22
+ constructor(runtime: TaserRuntime, manifest?: TManifest) {
23
+ super(runtime);
24
+ if (manifest !== undefined) {
25
+ this.__manifest = manifest;
26
+ }
27
+ }
28
+ }
@@ -1,6 +1,6 @@
1
1
  import { createRouteBuilder } from "./route.js";
2
- import type { RouteBuilder, RoutePath, Schema } from "./types/index.js";
3
- import type { AppContext, HttpMethod } from "./types/units.js";
2
+ import type { RouteBuilder, RoutePath, Schema } from "../types/index.js";
3
+ import type { AppContext, HttpMethod } from "../types/units.js";
4
4
 
5
5
  type WithoutBodyOptions<TQuery, TParams, TQueryIn = unknown, TParamsIn = unknown> = {
6
6
  query?: Schema<TQuery, TQueryIn>;
@@ -1,5 +1,5 @@
1
- import type { LayoutId, MiddlewareBuilder } from "./types/index.js";
2
- import type { MiddlewareDefinition } from "./types/units.js";
1
+ import type { LayoutId, MiddlewareBuilder } from "../types/index.js";
2
+ import type { MiddlewareDefinition } from "../types/units.js";
3
3
 
4
4
  export function createMiddleware<const Layout extends LayoutId>(
5
5
  layout: Layout,
@@ -6,11 +6,17 @@ import {
6
6
  withAuto422,
7
7
  } from "@taserjs/router-utils";
8
8
 
9
- import type { Method, RouteBuilder, RoutePath, ReturnsMap, ValidatorParts } from "./types/index.js";
10
- import type { HttpMethod, MiddlewareDefinition } from "./types/units.js";
11
- import { isHandlerUnit } from "./types/units.js";
9
+ import type {
10
+ Method,
11
+ RouteBuilder,
12
+ RoutePath,
13
+ ReturnsMap,
14
+ ValidatorParts,
15
+ } from "../types/index.js";
16
+ import type { HttpMethod, MiddlewareDefinition } from "../types/units.js";
17
+ import { isHandlerUnit } from "../types/units.js";
12
18
  import { HANDLER_SCHEMA_KEY_MAP } from "./constants.js";
13
- import { pickDefinedSchemas, type SchemaValidators } from "./define/validators.js";
19
+ import { pickDefinedSchemas, type SchemaValidators } from "../define/validators.js";
14
20
 
15
21
  function toUtilsMap(map: ReturnsMap | undefined): Record<number, StandardSchemaV1> {
16
22
  if (!map) {
@@ -90,49 +96,39 @@ export function createRouteBuilder(
90
96
  const routeSchemas = pickDefinedSchemas(validators);
91
97
  const base = buildRouteBase(path, method, methods, middlewares);
92
98
 
93
- if (isHandlerUnit(fnOrUnit)) {
94
- const handlerSchemas = pickDefinedSchemas(fnOrUnit, HANDLER_SCHEMA_KEY_MAP);
95
- const returns = buildEffectiveReturns({
96
- middlewareReturns: collectReturnsFromDefinitions(middlewares),
97
- routeReturns: routeReturns,
98
- handlerMiddlewareReturns: collectReturnsFromDefinitions(fnOrUnit.middlewares),
99
- handlerReturns: fnOrUnit.returns,
100
- schemas: {
101
- ...validators,
102
- handlerQuery: fnOrUnit.query,
103
- handlerParams: fnOrUnit.params,
104
- handlerBody: fnOrUnit.body,
105
- middlewares,
106
- handlerMiddlewares: fnOrUnit.middlewares,
107
- },
108
- });
99
+ const unit = isHandlerUnit(fnOrUnit)
100
+ ? fnOrUnit
101
+ : {
102
+ handler: fnOrUnit as (ctx: unknown) => Response | Promise<Response>,
103
+ middlewares: [] as MiddlewareDefinition[],
104
+ returns: undefined as ReturnsMap | undefined,
105
+ };
109
106
 
110
- return {
111
- ...base,
112
- handlerMiddlewares: [...fnOrUnit.middlewares],
113
- handler: fnOrUnit.handler,
114
- ...routeSchemas,
115
- ...handlerSchemas,
116
- ...(returns ? { returns } : {}),
117
- };
118
- }
107
+ const handlerSchemas = isHandlerUnit(fnOrUnit)
108
+ ? pickDefinedSchemas(fnOrUnit, HANDLER_SCHEMA_KEY_MAP)
109
+ : {};
119
110
 
120
111
  const returns = buildEffectiveReturns({
121
112
  middlewareReturns: collectReturnsFromDefinitions(middlewares),
122
- routeReturns: routeReturns,
123
- handlerMiddlewareReturns: {},
124
- handlerReturns: undefined,
113
+ routeReturns,
114
+ handlerMiddlewareReturns: collectReturnsFromDefinitions(unit.middlewares),
115
+ handlerReturns: unit.returns,
125
116
  schemas: {
126
117
  ...validators,
118
+ handlerQuery: (unit as any).query,
119
+ handlerParams: (unit as any).params,
120
+ handlerBody: (unit as any).body,
127
121
  middlewares,
122
+ handlerMiddlewares: unit.middlewares,
128
123
  },
129
124
  });
130
125
 
131
126
  return {
132
127
  ...base,
133
- handlerMiddlewares: [],
134
- handler: fnOrUnit as (ctx: unknown) => Response | Promise<Response>,
128
+ handlerMiddlewares: [...unit.middlewares],
129
+ handler: unit.handler,
135
130
  ...routeSchemas,
131
+ ...handlerSchemas,
136
132
  ...(returns ? { returns } : {}),
137
133
  };
138
134
  },
@@ -0,0 +1,347 @@
1
+ import {
2
+ createTaserCompatHandler,
3
+ createTaserRuntime,
4
+ type NotFoundHandler,
5
+ type OnErrorHandler,
6
+ type RouteManifestShape,
7
+ } from "@taserjs/router-core";
8
+ import { isPromise, normalizeOnError } from "@taserjs/router-utils";
9
+
10
+ import {
11
+ createAllRoute,
12
+ createAnyRoute,
13
+ createDeleteRoute,
14
+ createGetRoute,
15
+ createHeadRoute,
16
+ createOptionsRoute,
17
+ createPatchRoute,
18
+ createPostRoute,
19
+ createPutRoute,
20
+ } from "./factories.js";
21
+ import { defineHandler } from "../define/handler.js";
22
+ import {
23
+ defineMiddleware,
24
+ type DefineMiddlewareOptions,
25
+ type MultiScopedMiddlewareOptions,
26
+ type ScopedMiddlewareOptions,
27
+ } from "../define/middleware.js";
28
+ import { createMiddleware } from "./middleware.js";
29
+ import { TaserApp } from "./app.js";
30
+ import type { ContextDefinition, CreateTaserAppOptions, OnErrorOptions } from "../types/app.js";
31
+ import type {
32
+ AppContext,
33
+ ExtractState,
34
+ IsUnknown,
35
+ MiddlewareReturnFromParts,
36
+ MiddlewareUnit,
37
+ } from "../types/units.js";
38
+ import type { HandlerBuilder, LayoutId, MiddlewareBuilder } from "../types/index.js";
39
+ import type { ReturnsMap } from "../types/returns.js";
40
+ import type { Schema } from "../types/schema.js";
41
+ import type {
42
+ CreateAllRoute,
43
+ CreateAnyRoute,
44
+ CreateWithBodyRoute,
45
+ CreateWithoutBodyRoute,
46
+ } from "./factories.js";
47
+
48
+ function toOnErrorHandler(
49
+ onErrorOptions: OnErrorOptions | OnErrorOptions["handle"],
50
+ ): OnErrorHandler {
51
+ return normalizeOnError(onErrorOptions);
52
+ }
53
+
54
+ type RouterState = {
55
+ options: CreateTaserAppOptions;
56
+ contextDef: ContextDefinition<Record<string, unknown>, Record<string, unknown>>;
57
+ onError?: OnErrorHandler;
58
+ notFound?: NotFoundHandler;
59
+ };
60
+
61
+ const emptyContext: ContextDefinition<Record<string, unknown>, Record<string, unknown>> = {};
62
+
63
+ export class TaserRouter<TAppContext extends Record<string, unknown> = AppContext> {
64
+ private readonly state: RouterState;
65
+
66
+ constructor(options: CreateTaserAppOptions = {}, state?: Partial<Omit<RouterState, "options">>) {
67
+ this.state = {
68
+ options,
69
+ contextDef: state?.contextDef ?? emptyContext,
70
+ ...(state?.onError !== undefined ? { onError: state.onError } : {}),
71
+ ...(state?.notFound !== undefined ? { notFound: state.notFound } : {}),
72
+ };
73
+ }
74
+
75
+ context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(
76
+ definition: ContextDefinition<TBoot, TReq>,
77
+ ): TaserRouter<TBoot & TReq> {
78
+ this.state.contextDef = definition;
79
+ return this as unknown as TaserRouter<TBoot & TReq>;
80
+ }
81
+
82
+ onError<TResponses extends ReturnsMap = ReturnsMap>(
83
+ options: OnErrorOptions<TResponses> | OnErrorOptions<TResponses>["handle"],
84
+ ): this {
85
+ this.state.onError = toOnErrorHandler(options);
86
+ return this;
87
+ }
88
+
89
+ notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown): this {
90
+ this.state.notFound = (ctx) => handler(ctx);
91
+ return this;
92
+ }
93
+
94
+ get: CreateWithoutBodyRoute<"GET", TAppContext> = createGetRoute as CreateWithoutBodyRoute<
95
+ "GET",
96
+ TAppContext
97
+ >;
98
+ post: CreateWithBodyRoute<"POST", TAppContext> = createPostRoute as CreateWithBodyRoute<
99
+ "POST",
100
+ TAppContext
101
+ >;
102
+ put: CreateWithBodyRoute<"PUT", TAppContext> = createPutRoute as CreateWithBodyRoute<
103
+ "PUT",
104
+ TAppContext
105
+ >;
106
+ patch: CreateWithBodyRoute<"PATCH", TAppContext> = createPatchRoute as CreateWithBodyRoute<
107
+ "PATCH",
108
+ TAppContext
109
+ >;
110
+ delete: CreateWithoutBodyRoute<"DELETE", TAppContext> =
111
+ createDeleteRoute as CreateWithoutBodyRoute<"DELETE", TAppContext>;
112
+ options: CreateWithoutBodyRoute<"OPTIONS", TAppContext> =
113
+ createOptionsRoute as CreateWithoutBodyRoute<"OPTIONS", TAppContext>;
114
+ head: CreateWithoutBodyRoute<"HEAD", TAppContext> = createHeadRoute as CreateWithoutBodyRoute<
115
+ "HEAD",
116
+ TAppContext
117
+ >;
118
+ any: CreateAnyRoute<TAppContext> = createAnyRoute as CreateAnyRoute<TAppContext>;
119
+ all: CreateAllRoute<TAppContext> = createAllRoute as CreateAllRoute<TAppContext>;
120
+
121
+ middleware<const Layout extends LayoutId>(
122
+ layout: Layout,
123
+ ): MiddlewareBuilder<Layout, readonly [], TAppContext> {
124
+ return createMiddleware(layout) as MiddlewareBuilder<Layout, readonly [], TAppContext>;
125
+ }
126
+
127
+ defineHandler<TQuery = unknown, TParams = unknown, TBody = unknown>(options?: {
128
+ query?: Schema<TQuery>;
129
+ params?: Schema<TParams>;
130
+ body?: Schema<TBody>;
131
+ }): HandlerBuilder<
132
+ readonly [],
133
+ { query: TQuery; params: TParams; body: TBody },
134
+ {},
135
+ TAppContext
136
+ > {
137
+ return options === undefined
138
+ ? defineHandler<TAppContext>()
139
+ : defineHandler<TAppContext>(options);
140
+ }
141
+
142
+ defineMiddleware(
143
+ middleware: Parameters<typeof createTaserCompatHandler>[0],
144
+ ): MiddlewareUnit<MiddlewareReturnFromParts<unknown, unknown, unknown, {}>, {}, null, {}>;
145
+
146
+ defineMiddleware<
147
+ const Layout extends LayoutId,
148
+ TState = unknown,
149
+ TRequires = {},
150
+ TQuery = unknown,
151
+ TParams = unknown,
152
+ TBody = unknown,
153
+ TReturns extends ReturnsMap = {},
154
+ TQueryIn = unknown,
155
+ TParamsIn = unknown,
156
+ TBodyIn = unknown,
157
+ R = unknown,
158
+ >(
159
+ layout: Layout,
160
+ options: ScopedMiddlewareOptions<
161
+ Layout,
162
+ TState,
163
+ TRequires,
164
+ TQuery,
165
+ TParams,
166
+ TBody,
167
+ TReturns,
168
+ TQueryIn,
169
+ TParamsIn,
170
+ TBodyIn,
171
+ TAppContext,
172
+ R
173
+ >,
174
+ ): MiddlewareUnit<
175
+ MiddlewareReturnFromParts<
176
+ TQuery,
177
+ TParams,
178
+ TBody,
179
+ IsUnknown<TState> extends true ? ExtractState<R> : TState,
180
+ TQueryIn,
181
+ TParamsIn,
182
+ TBodyIn
183
+ >,
184
+ TReturns,
185
+ Layout,
186
+ TRequires
187
+ >;
188
+
189
+ defineMiddleware<
190
+ const Layouts extends readonly [LayoutId, ...LayoutId[]],
191
+ TState = unknown,
192
+ TRequires = {},
193
+ TQuery = unknown,
194
+ TParams = unknown,
195
+ TBody = unknown,
196
+ TReturns extends ReturnsMap = {},
197
+ TQueryIn = unknown,
198
+ TParamsIn = unknown,
199
+ TBodyIn = unknown,
200
+ R = unknown,
201
+ >(
202
+ layouts: Layouts,
203
+ options: MultiScopedMiddlewareOptions<
204
+ Layouts,
205
+ TState,
206
+ TRequires,
207
+ TQuery,
208
+ TParams,
209
+ TBody,
210
+ TReturns,
211
+ TQueryIn,
212
+ TParamsIn,
213
+ TBodyIn,
214
+ TAppContext,
215
+ R
216
+ >,
217
+ ): MiddlewareUnit<
218
+ MiddlewareReturnFromParts<
219
+ TQuery,
220
+ TParams,
221
+ TBody,
222
+ IsUnknown<TState> extends true ? ExtractState<R> : TState,
223
+ TQueryIn,
224
+ TParamsIn,
225
+ TBodyIn
226
+ >,
227
+ TReturns,
228
+ Layouts,
229
+ TRequires
230
+ >;
231
+
232
+ defineMiddleware<
233
+ TState = unknown,
234
+ TRequires = {},
235
+ TQuery = unknown,
236
+ TParams = unknown,
237
+ TBody = unknown,
238
+ TReturns extends ReturnsMap = {},
239
+ TQueryIn = unknown,
240
+ TParamsIn = unknown,
241
+ TBodyIn = unknown,
242
+ R = unknown,
243
+ >(
244
+ options: DefineMiddlewareOptions<
245
+ TState,
246
+ TRequires,
247
+ TQuery,
248
+ TParams,
249
+ TBody,
250
+ TReturns,
251
+ TQueryIn,
252
+ TParamsIn,
253
+ TBodyIn,
254
+ TAppContext,
255
+ R
256
+ >,
257
+ ): MiddlewareUnit<
258
+ MiddlewareReturnFromParts<
259
+ TQuery,
260
+ TParams,
261
+ TBody,
262
+ IsUnknown<TState> extends true ? ExtractState<R> : TState,
263
+ TQueryIn,
264
+ TParamsIn,
265
+ TBodyIn
266
+ >,
267
+ TReturns,
268
+ null,
269
+ TRequires
270
+ >;
271
+
272
+ defineMiddleware(first: any, second?: any): any {
273
+ return defineMiddleware(first, second);
274
+ }
275
+
276
+ create<const TManifest extends RouteManifestShape>(manifest: TManifest): TaserApp<TManifest> {
277
+ const definition = this.state.contextDef;
278
+ const validateResponse = this.state.options.response?.validate ?? true;
279
+ const onValidationFailure = this.state.options.response?.onValidationFailure;
280
+ const basePath = this.state.options.basePath;
281
+
282
+ const hasCustomContext = definition.boot !== undefined || definition.request !== undefined;
283
+ let bootContextResolved: Record<string, unknown> | undefined;
284
+ let bootPromise: Promise<Record<string, unknown>> | undefined;
285
+
286
+ if (definition.boot !== undefined) {
287
+ const bootResult = definition.boot();
288
+ if (isPromise(bootResult)) {
289
+ bootPromise = bootResult.then((ctx) => {
290
+ bootContextResolved = (ctx ?? {}) as Record<string, unknown>;
291
+ return bootContextResolved;
292
+ });
293
+ } else {
294
+ bootContextResolved = (bootResult ?? {}) as Record<string, unknown>;
295
+ }
296
+ } else if (hasCustomContext) {
297
+ bootContextResolved = {};
298
+ }
299
+
300
+ const contextFactory = hasCustomContext
301
+ ? (req?: Request) => {
302
+ if (bootPromise !== undefined && bootContextResolved === undefined) {
303
+ return bootPromise.then(async (bootContext) => {
304
+ if (!definition.request) {
305
+ return { ...bootContext };
306
+ }
307
+ const requestResult = definition.request(req!);
308
+ const requestContext = isPromise(requestResult) ? await requestResult : requestResult;
309
+ return { ...bootContext, ...requestContext };
310
+ });
311
+ }
312
+
313
+ const bootContext = bootContextResolved ?? {};
314
+ if (!definition.request) {
315
+ return { ...bootContext };
316
+ }
317
+
318
+ const requestResult = definition.request(req!);
319
+ if (isPromise(requestResult)) {
320
+ return requestResult.then((requestContext) => ({
321
+ ...bootContext,
322
+ ...requestContext,
323
+ }));
324
+ }
325
+
326
+ return { ...bootContext, ...(requestResult as Record<string, unknown>) };
327
+ }
328
+ : () => ({});
329
+
330
+ const runtime = createTaserRuntime(manifest, contextFactory, {
331
+ ...(basePath !== undefined ? { basePath } : {}),
332
+ response: {
333
+ validate: validateResponse,
334
+ ...(onValidationFailure !== undefined ? { onValidationFailure } : {}),
335
+ },
336
+ ...(this.state.onError !== undefined ? { onError: this.state.onError } : {}),
337
+ ...(this.state.notFound !== undefined ? { notFound: this.state.notFound } : {}),
338
+ ...(this.state.options.cookies !== undefined ? { cookies: this.state.options.cookies } : {}),
339
+ });
340
+
341
+ return new TaserApp(runtime, manifest);
342
+ }
343
+ }
344
+
345
+ export function createTaserApp(options: CreateTaserAppOptions = {}): TaserRouter {
346
+ return new TaserRouter(options);
347
+ }
@@ -1,6 +1,6 @@
1
1
  import type { ReservedContextKey } from "@taserjs/router-core";
2
2
 
3
- import type { ContextDefinition } from "./types/app.js";
3
+ import type { ContextDefinition } from "../types/app.js";
4
4
 
5
5
  type NoReservedKeysConstraint<
6
6
  TBoot extends Record<string, unknown>,
@@ -16,4 +16,4 @@ export function createContext<
16
16
  return definition;
17
17
  }
18
18
 
19
- export type { ContextDefinition, InferAppContext } from "./types/app.js";
19
+ export type { ContextDefinition, InferAppContext } from "../types/app.js";