@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.
Files changed (68) hide show
  1. package/dist/cjs/builder/app.cjs +5 -0
  2. package/dist/cjs/builder/app.cjs.map +1 -1
  3. package/dist/cjs/builder/app.d.cts +7 -7
  4. package/dist/cjs/builder/factories.cjs +4 -4
  5. package/dist/cjs/builder/factories.cjs.map +1 -1
  6. package/dist/cjs/builder/factories.d.cts +5 -51
  7. package/dist/cjs/builder/route.cjs +22 -1
  8. package/dist/cjs/builder/route.cjs.map +1 -1
  9. package/dist/cjs/builder/route.d.cts +1 -2
  10. package/dist/cjs/builder/router.cjs +3 -5
  11. package/dist/cjs/builder/router.cjs.map +1 -1
  12. package/dist/cjs/builder/router.d.cts +11 -12
  13. package/dist/cjs/define/middleware.cjs +2 -2
  14. package/dist/cjs/define/middleware.cjs.map +1 -1
  15. package/dist/cjs/define/middleware.d.cts +10 -8
  16. package/dist/cjs/index.cjs +0 -12
  17. package/dist/cjs/index.d.cts +4 -4
  18. package/dist/cjs/reply.cjs +8 -19
  19. package/dist/cjs/reply.d.cts +1 -2
  20. package/dist/cjs/stream.cjs +8 -31
  21. package/dist/cjs/stream.d.cts +1 -1
  22. package/dist/cjs/types/app.d.cts +3 -21
  23. package/dist/cjs/types/index.d.cts +29 -7
  24. package/dist/cjs/types/returns.d.cts +6 -9
  25. package/dist/cjs/types/type-utils.d.cts +7 -5
  26. package/dist/cjs/types/units.cjs.map +1 -1
  27. package/dist/cjs/types/units.d.cts +4 -10
  28. package/dist/esm/builder/app.d.ts +7 -7
  29. package/dist/esm/builder/app.js +5 -1
  30. package/dist/esm/builder/app.js.map +1 -1
  31. package/dist/esm/builder/factories.d.ts +5 -51
  32. package/dist/esm/builder/factories.js +4 -4
  33. package/dist/esm/builder/factories.js.map +1 -1
  34. package/dist/esm/builder/route.d.ts +1 -2
  35. package/dist/esm/builder/route.js +22 -1
  36. package/dist/esm/builder/route.js.map +1 -1
  37. package/dist/esm/builder/router.d.ts +11 -12
  38. package/dist/esm/builder/router.js +3 -5
  39. package/dist/esm/builder/router.js.map +1 -1
  40. package/dist/esm/define/middleware.d.ts +10 -8
  41. package/dist/esm/define/middleware.js +2 -2
  42. package/dist/esm/define/middleware.js.map +1 -1
  43. package/dist/esm/index.d.ts +4 -4
  44. package/dist/esm/index.js +2 -2
  45. package/dist/esm/reply.d.ts +1 -2
  46. package/dist/esm/reply.js +1 -2
  47. package/dist/esm/stream.d.ts +1 -1
  48. package/dist/esm/stream.js +1 -2
  49. package/dist/esm/types/app.d.ts +3 -21
  50. package/dist/esm/types/index.d.ts +29 -7
  51. package/dist/esm/types/returns.d.ts +6 -9
  52. package/dist/esm/types/type-utils.d.ts +7 -5
  53. package/dist/esm/types/units.d.ts +4 -10
  54. package/dist/esm/types/units.js.map +1 -1
  55. package/package.json +3 -3
  56. package/src/builder/app.ts +16 -5
  57. package/src/builder/factories.ts +28 -155
  58. package/src/builder/route.ts +25 -1
  59. package/src/builder/router.ts +24 -157
  60. package/src/define/middleware.ts +115 -123
  61. package/src/index.ts +5 -9
  62. package/src/reply.ts +1 -11
  63. package/src/stream.ts +1 -1
  64. package/src/types/app.ts +3 -22
  65. package/src/types/index.ts +83 -13
  66. package/src/types/returns.ts +7 -76
  67. package/src/types/type-utils.ts +9 -4
  68. package/src/types/units.ts +34 -10
@@ -1,168 +1,46 @@
1
1
  import { createRouteBuilder } from "./route.js";
2
- import type { RouteBuilder, RoutePath, Schema } from "../types/index.js";
2
+ import type { RouteBuilder, RoutePath } from "../types/index.js";
3
3
  import type { AppContext, HttpMethod } from "../types/units.js";
4
4
 
5
- type WithoutBodyOptions<TQuery, TParams, TQueryIn = unknown, TParamsIn = unknown> = {
6
- query?: Schema<TQuery, TQueryIn>;
7
- params?: Schema<TParams, TParamsIn>;
8
- };
9
-
10
- type WithBodyOptions<
11
- TQuery,
12
- TParams,
13
- TBody,
14
- TQueryIn = unknown,
15
- TParamsIn = unknown,
16
- TBodyIn = unknown,
17
- > = WithoutBodyOptions<TQuery, TParams, TQueryIn, TParamsIn> & {
18
- body?: Schema<TBody, TBodyIn>;
19
- };
20
-
21
5
  export type CreateWithoutBodyRoute<
22
6
  M extends "GET" | "DELETE" | "OPTIONS" | "HEAD",
23
7
  TAppContext extends Record<string, unknown> = AppContext,
24
- > = {
25
- <const Path extends RoutePath>(
26
- path: Path,
27
- ): RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
28
- <
29
- const Path extends RoutePath,
30
- TQuery = unknown,
31
- TParams = unknown,
32
- TQueryIn = unknown,
33
- TParamsIn = unknown,
34
- >(
35
- path: Path,
36
- options: WithoutBodyOptions<TQuery, TParams, TQueryIn, TParamsIn>,
37
- ): RouteBuilder<
38
- Path,
39
- M,
40
- readonly [],
41
- {
42
- query: TQuery;
43
- params: TParams;
44
- queryIn: TQueryIn;
45
- paramsIn: TParamsIn;
46
- },
47
- {},
48
- TAppContext
49
- >;
50
- };
8
+ > = <const Path extends RoutePath>(
9
+ path: Path,
10
+ ) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
51
11
 
52
12
  export type CreateWithBodyRoute<
53
13
  M extends "POST" | "PUT" | "PATCH",
54
14
  TAppContext extends Record<string, unknown> = AppContext,
55
- > = {
56
- <const Path extends RoutePath>(
57
- path: Path,
58
- ): RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
59
- <
60
- const Path extends RoutePath,
61
- TQuery = unknown,
62
- TParams = unknown,
63
- TBody = unknown,
64
- TQueryIn = unknown,
65
- TParamsIn = unknown,
66
- TBodyIn = unknown,
67
- >(
68
- path: Path,
69
- options: WithBodyOptions<TQuery, TParams, TBody, TQueryIn, TParamsIn, TBodyIn>,
70
- ): RouteBuilder<
71
- Path,
72
- M,
73
- readonly [],
74
- {
75
- query: TQuery;
76
- params: TParams;
77
- body: TBody;
78
- queryIn: TQueryIn;
79
- paramsIn: TParamsIn;
80
- bodyIn: TBodyIn;
81
- },
82
- {},
83
- TAppContext
84
- >;
85
- };
86
-
87
- export type CreateAnyRoute<TAppContext extends Record<string, unknown> = AppContext> = {
88
- <const Path extends RoutePath, const Methods extends readonly HttpMethod[]>(
89
- path: Path,
90
- methods: Methods,
91
- ): RouteBuilder<Path, Methods[number], readonly [], {}, {}, TAppContext>;
92
- <
93
- const Path extends RoutePath,
94
- const Methods extends readonly HttpMethod[],
95
- TQuery = unknown,
96
- TParams = unknown,
97
- TBody = unknown,
98
- TQueryIn = unknown,
99
- TParamsIn = unknown,
100
- TBodyIn = unknown,
101
- >(
102
- path: Path,
103
- methods: Methods,
104
- options: WithBodyOptions<TQuery, TParams, TBody, TQueryIn, TParamsIn, TBodyIn>,
105
- ): RouteBuilder<
106
- Path,
107
- Methods[number],
108
- readonly [],
109
- {
110
- query: TQuery;
111
- params: TParams;
112
- body: TBody;
113
- queryIn: TQueryIn;
114
- paramsIn: TParamsIn;
115
- bodyIn: TBodyIn;
116
- },
117
- {},
118
- TAppContext
119
- >;
120
- };
121
-
122
- export type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = {
123
- <const Path extends RoutePath>(
124
- path: Path,
125
- ): RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;
126
- <
127
- const Path extends RoutePath,
128
- TQuery = unknown,
129
- TParams = unknown,
130
- TBody = unknown,
131
- TQueryIn = unknown,
132
- TParamsIn = unknown,
133
- TBodyIn = unknown,
134
- >(
135
- path: Path,
136
- options: WithBodyOptions<TQuery, TParams, TBody, TQueryIn, TParamsIn, TBodyIn>,
137
- ): RouteBuilder<
138
- Path,
139
- HttpMethod,
140
- readonly [],
141
- {
142
- query: TQuery;
143
- params: TParams;
144
- body: TBody;
145
- queryIn: TQueryIn;
146
- paramsIn: TParamsIn;
147
- bodyIn: TBodyIn;
148
- },
149
- {},
150
- TAppContext
151
- >;
152
- };
15
+ > = <const Path extends RoutePath>(
16
+ path: Path,
17
+ ) => RouteBuilder<Path, M, readonly [], {}, {}, TAppContext>;
18
+
19
+ export type CreateAnyRoute<TAppContext extends Record<string, unknown> = AppContext> = <
20
+ const Path extends RoutePath,
21
+ const Methods extends readonly HttpMethod[],
22
+ >(
23
+ path: Path,
24
+ methods: Methods,
25
+ ) => RouteBuilder<Path, Methods[number], readonly [], {}, {}, TAppContext>;
26
+
27
+ export type CreateAllRoute<TAppContext extends Record<string, unknown> = AppContext> = <
28
+ const Path extends RoutePath,
29
+ >(
30
+ path: Path,
31
+ ) => RouteBuilder<Path, HttpMethod, readonly [], {}, {}, TAppContext>;
153
32
 
154
33
  function createWithoutBodyRoute<M extends "GET" | "DELETE" | "OPTIONS" | "HEAD">(
155
34
  method: M,
156
35
  ): CreateWithoutBodyRoute<M> {
157
- return ((path: string, options?: WithoutBodyOptions<unknown, unknown>) =>
158
- createRouteBuilder(path, method, options)) as unknown as CreateWithoutBodyRoute<M>;
36
+ return ((path: string) =>
37
+ createRouteBuilder(path, method)) as unknown as CreateWithoutBodyRoute<M>;
159
38
  }
160
39
 
161
40
  function createWithBodyRoute<M extends "POST" | "PUT" | "PATCH">(
162
41
  method: M,
163
42
  ): CreateWithBodyRoute<M> {
164
- return ((path: string, options?: WithBodyOptions<unknown, unknown, unknown>) =>
165
- createRouteBuilder(path, method, options)) as unknown as CreateWithBodyRoute<M>;
43
+ return ((path: string) => createRouteBuilder(path, method)) as unknown as CreateWithBodyRoute<M>;
166
44
  }
167
45
 
168
46
  export const createGetRoute = createWithoutBodyRoute("GET");
@@ -173,13 +51,8 @@ export const createPostRoute = createWithBodyRoute("POST");
173
51
  export const createPutRoute = createWithBodyRoute("PUT");
174
52
  export const createPatchRoute = createWithBodyRoute("PATCH");
175
53
 
176
- export const createAnyRoute: CreateAnyRoute = ((
177
- path: string,
178
- methods: readonly HttpMethod[],
179
- options?: WithBodyOptions<unknown, unknown, unknown>,
180
- ) => createRouteBuilder(path, "ANY", options, methods)) as unknown as CreateAnyRoute;
54
+ export const createAnyRoute: CreateAnyRoute = ((path: string, methods: readonly HttpMethod[]) =>
55
+ createRouteBuilder(path, "ANY", methods)) as unknown as CreateAnyRoute;
181
56
 
182
- export const createAllRoute: CreateAllRoute = ((
183
- path: string,
184
- options?: WithBodyOptions<unknown, unknown, unknown>,
185
- ) => createRouteBuilder(path, "ALL", options)) as unknown as CreateAllRoute;
57
+ export const createAllRoute: CreateAllRoute = ((path: string) =>
58
+ createRouteBuilder(path, "ALL")) as unknown as CreateAllRoute;
@@ -5,6 +5,7 @@ import {
5
5
  mergeReturnsMaps,
6
6
  withAuto422,
7
7
  } from "@taserjs/router-utils";
8
+ import type { BodyMode } from "@taserjs/router-core";
8
9
 
9
10
  import type {
10
11
  Method,
@@ -12,6 +13,7 @@ import type {
12
13
  RoutePath,
13
14
  ReturnsMap,
14
15
  ValidatorParts,
16
+ Schema,
15
17
  } from "../types/index.js";
16
18
  import type { HttpMethod, MiddlewareDefinition } from "../types/units.js";
17
19
  import { isHandlerUnit } from "../types/units.js";
@@ -75,15 +77,36 @@ function buildRouteBase(
75
77
  export function createRouteBuilder(
76
78
  path: string,
77
79
  method: HttpMethod | "ANY" | "ALL",
78
- validators: SchemaValidators = {},
79
80
  methods?: readonly HttpMethod[],
80
81
  ): RouteBuilder<RoutePath, Method, readonly [], ValidatorParts> {
81
82
  const middlewares: MiddlewareDefinition[] = [];
82
83
  let routeReturns: Record<number, StandardSchemaV1> = {};
84
+ const validators: SchemaValidators = {};
85
+ let bodyMode: BodyMode | undefined;
83
86
 
84
87
  const builder = {
85
88
  path,
86
89
  method,
90
+ query(schema: Schema<unknown>) {
91
+ validators.query = schema;
92
+ return builder;
93
+ },
94
+ params(schema: Schema<unknown>) {
95
+ validators.params = schema;
96
+ return builder;
97
+ },
98
+ body(modeOrSchema: BodyMode | Schema<unknown>, schema?: Schema<unknown>) {
99
+ if (typeof modeOrSchema === "string") {
100
+ bodyMode = modeOrSchema as BodyMode;
101
+ if (schema !== undefined) {
102
+ validators.body = schema;
103
+ }
104
+ } else {
105
+ bodyMode = "json";
106
+ validators.body = modeOrSchema;
107
+ }
108
+ return builder;
109
+ },
87
110
  returns(map: ReturnsMap) {
88
111
  routeReturns = { ...routeReturns, ...toUtilsMap(map) };
89
112
  return builder;
@@ -129,6 +152,7 @@ export function createRouteBuilder(
129
152
  handler: unit.handler,
130
153
  ...routeSchemas,
131
154
  ...handlerSchemas,
155
+ ...(bodyMode ? { bodyMode } : {}),
132
156
  ...(returns ? { returns } : {}),
133
157
  };
134
158
  },
@@ -1,5 +1,4 @@
1
1
  import {
2
- createTaserCompatHandler,
3
2
  createTaserRuntime,
4
3
  type NotFoundHandler,
5
4
  type OnErrorHandler,
@@ -19,22 +18,11 @@ import {
19
18
  createPutRoute,
20
19
  } from "./factories.js";
21
20
  import { defineHandler } from "../define/handler.js";
22
- import {
23
- defineMiddleware,
24
- type DefineMiddlewareOptions,
25
- type MultiScopedMiddlewareOptions,
26
- type ScopedMiddlewareOptions,
27
- } from "../define/middleware.js";
21
+ import { defineMiddleware, type DefineMiddlewareFn } from "../define/middleware.js";
28
22
  import { createMiddleware } from "./middleware.js";
29
23
  import { TaserApp } from "./app.js";
30
24
  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";
25
+ import type { AppContext } from "../types/units.js";
38
26
  import type { HandlerBuilder, LayoutId, MiddlewareBuilder } from "../types/index.js";
39
27
  import type { ReturnsMap } from "../types/returns.js";
40
28
  import type { Schema } from "../types/schema.js";
@@ -60,7 +48,10 @@ type RouterState = {
60
48
 
61
49
  const emptyContext: ContextDefinition<Record<string, unknown>, Record<string, unknown>> = {};
62
50
 
63
- export class TaserRouter<TAppContext extends Record<string, unknown> = AppContext> {
51
+ export class TaserRouter<
52
+ TAppContext extends Record<string, unknown> = AppContext,
53
+ THasNotFound extends boolean = false,
54
+ > {
64
55
  private readonly state: RouterState;
65
56
 
66
57
  constructor(options: CreateTaserAppOptions = {}, state?: Partial<Omit<RouterState, "options">>) {
@@ -74,9 +65,9 @@ export class TaserRouter<TAppContext extends Record<string, unknown> = AppContex
74
65
 
75
66
  context<TBoot extends Record<string, unknown>, TReq extends Record<string, unknown>>(
76
67
  definition: ContextDefinition<TBoot, TReq>,
77
- ): TaserRouter<TBoot & TReq> {
68
+ ): TaserRouter<TBoot & TReq, THasNotFound> {
78
69
  this.state.contextDef = definition;
79
- return this as unknown as TaserRouter<TBoot & TReq>;
70
+ return this as unknown as TaserRouter<TBoot & TReq, THasNotFound>;
80
71
  }
81
72
 
82
73
  onError<TResponses extends ReturnsMap = ReturnsMap>(
@@ -86,9 +77,11 @@ export class TaserRouter<TAppContext extends Record<string, unknown> = AppContex
86
77
  return this;
87
78
  }
88
79
 
89
- notFound(handler: (ctx: unknown) => Response | Promise<Response> | unknown): this {
80
+ notFound(
81
+ handler: (ctx: unknown) => Response | Promise<Response> | unknown,
82
+ ): TaserRouter<TAppContext, true> {
90
83
  this.state.notFound = (ctx) => handler(ctx);
91
- return this;
84
+ return this as unknown as TaserRouter<TAppContext, true>;
92
85
  }
93
86
 
94
87
  get: CreateWithoutBodyRoute<"GET", TAppContext> = createGetRoute as CreateWithoutBodyRoute<
@@ -139,145 +132,17 @@ export class TaserRouter<TAppContext extends Record<string, unknown> = AppContex
139
132
  : defineHandler<TAppContext>(options);
140
133
  }
141
134
 
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
- }
135
+ defineMiddleware: DefineMiddlewareFn<TAppContext> =
136
+ defineMiddleware as DefineMiddlewareFn<TAppContext>;
275
137
 
276
- create<const TManifest extends RouteManifestShape>(manifest: TManifest): TaserApp<TManifest> {
138
+ create<const TManifest extends RouteManifestShape>(
139
+ manifest: TManifest,
140
+ runtimeOptions?: { basePath?: string },
141
+ ): TaserApp<TManifest, THasNotFound> {
277
142
  const definition = this.state.contextDef;
278
143
  const validateResponse = this.state.options.response?.validate ?? true;
279
144
  const onValidationFailure = this.state.options.response?.onValidationFailure;
280
- const basePath = this.state.options.basePath;
145
+ const basePath = runtimeOptions?.basePath;
281
146
 
282
147
  const hasCustomContext = definition.boot !== undefined || definition.request !== undefined;
283
148
  let bootContextResolved: Record<string, unknown> | undefined;
@@ -338,10 +203,12 @@ export class TaserRouter<TAppContext extends Record<string, unknown> = AppContex
338
203
  ...(this.state.options.cookies !== undefined ? { cookies: this.state.options.cookies } : {}),
339
204
  });
340
205
 
341
- return new TaserApp(runtime, manifest);
206
+ return new TaserApp(runtime, manifest) as unknown as TaserApp<TManifest, THasNotFound>;
342
207
  }
343
208
  }
344
209
 
345
- export function createTaserApp(options: CreateTaserAppOptions = {}): TaserRouter {
346
- return new TaserRouter(options);
210
+ export function createTaserApp(
211
+ options: CreateTaserAppOptions = {},
212
+ ): TaserRouter<AppContext, false> {
213
+ return new TaserRouter<AppContext, false>(options);
347
214
  }