@mpen/routekit 0.1.0 → 0.1.1

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 (133) hide show
  1. package/dist/bin.d.mts +4 -0
  2. package/dist/client/react.d.mts +178 -0
  3. package/dist/client/react.mjs +142 -0
  4. package/dist/client.d.mts +433 -0
  5. package/dist/client.mjs +264 -0
  6. package/dist/content-BuDOmhH_.mjs +102 -0
  7. package/dist/core-CzUCxvGk.d.mts +140 -0
  8. package/dist/core-DbmQauwS.mjs +81 -0
  9. package/dist/handlers.d.mts +72 -0
  10. package/dist/handlers.mjs +153 -0
  11. package/dist/index.d.mts +3 -0
  12. package/dist/index.mjs +1152 -0
  13. package/dist/middleware.d.mts +388 -0
  14. package/dist/middleware.mjs +1222 -0
  15. package/dist/request-Dn0zc-xm.mjs +1025 -0
  16. package/dist/response/content.d.mts +79 -0
  17. package/dist/response/content.mjs +2 -0
  18. package/dist/response/json-rpc.d.mts +1 -0
  19. package/dist/response/json-rpc.mjs +1 -0
  20. package/dist/response/problem/valibot.d.mts +230 -0
  21. package/dist/response/problem/valibot.mjs +258 -0
  22. package/dist/response/problem.d.mts +415 -0
  23. package/dist/response/problem.mjs +183 -0
  24. package/dist/response/status.d.mts +45 -0
  25. package/dist/response/status.mjs +2 -0
  26. package/dist/responses-B379Ep9Y.d.mts +296 -0
  27. package/dist/responses-BpVrgeYi.mjs +101 -0
  28. package/dist/router-Cwb7ak0J.d.mts +1819 -0
  29. package/dist/routes.d.mts +282 -0
  30. package/dist/routes.mjs +311 -0
  31. package/dist/status-C-8mw-FB.mjs +59 -0
  32. package/dist/valibot-D7liFYyB.d.mts +290 -0
  33. package/dist/valibot-Du97X-TS.mjs +326 -0
  34. package/package.json +8 -2
  35. package/src/bin/gen-api-client.test.ts +0 -70
  36. package/src/bin/gen-api-client.ts +0 -986
  37. package/src/client/headers.ts +0 -31
  38. package/src/client/index.ts +0 -8
  39. package/src/client/promise.ts +0 -11
  40. package/src/client/react/index.test.tsx +0 -266
  41. package/src/client/react/index.ts +0 -431
  42. package/src/client/responses.test.ts +0 -151
  43. package/src/client/responses.ts +0 -278
  44. package/src/client/transport.ts +0 -74
  45. package/src/client/transports/body-codec.ts +0 -61
  46. package/src/client/transports/fetch.ts +0 -113
  47. package/src/client/tsconfig.json +0 -9
  48. package/src/client/types.ts +0 -15
  49. package/src/client/url.ts +0 -31
  50. package/src/index.ts +0 -63
  51. package/src/router/fetch-types.ts +0 -13
  52. package/src/router/handlers/index.ts +0 -2
  53. package/src/router/handlers/openapi/index.ts +0 -2
  54. package/src/router/handlers/openapi/openapi.ts +0 -293
  55. package/src/router/integration/zod-openapi.test.ts +0 -74
  56. package/src/router/lib/charset.test.ts +0 -22
  57. package/src/router/lib/charset.ts +0 -133
  58. package/src/router/lib/collections.ts +0 -3
  59. package/src/router/lib/format.test.ts +0 -67
  60. package/src/router/lib/format.ts +0 -35
  61. package/src/router/lib/host.ts +0 -4
  62. package/src/router/lib/json-schema.ts +0 -6
  63. package/src/router/lib/media-type.test.ts +0 -122
  64. package/src/router/lib/media-type.ts +0 -289
  65. package/src/router/lib/pathname.test.ts +0 -18
  66. package/src/router/lib/pathname.ts +0 -19
  67. package/src/router/lib/route-names.ts +0 -70
  68. package/src/router/lib/route-normalize.test.ts +0 -36
  69. package/src/router/lib/route-normalize.ts +0 -67
  70. package/src/router/lib/schema-merge.ts +0 -56
  71. package/src/router/middleware/accept-ctx.test.ts +0 -33
  72. package/src/router/middleware/accept-ctx.ts +0 -12
  73. package/src/router/middleware/body-limit.test.ts +0 -112
  74. package/src/router/middleware/body-limit.ts +0 -121
  75. package/src/router/middleware/content-type-context.ts +0 -0
  76. package/src/router/middleware/cors.test.ts +0 -269
  77. package/src/router/middleware/cors.ts +0 -490
  78. package/src/router/middleware/csrf.test.ts +0 -106
  79. package/src/router/middleware/csrf.ts +0 -192
  80. package/src/router/middleware/define.ts +0 -249
  81. package/src/router/middleware/index.ts +0 -34
  82. package/src/router/middleware/jsxhtml-response.ts +0 -0
  83. package/src/router/middleware/oas-swagger.ts +0 -0
  84. package/src/router/middleware/rate-limit.test.ts +0 -886
  85. package/src/router/middleware/rate-limit.ts +0 -920
  86. package/src/router/middleware/request-id-ctx.test.ts +0 -183
  87. package/src/router/middleware/request-id-ctx.ts +0 -135
  88. package/src/router/middleware/request-logger-format.test.ts +0 -16
  89. package/src/router/middleware/request-logger-format.ts +0 -269
  90. package/src/router/middleware/request-logger.test.ts +0 -267
  91. package/src/router/middleware/request-logger.ts +0 -131
  92. package/src/router/middleware/start-time-ctx.ts +0 -5
  93. package/src/router/request.ts +0 -611
  94. package/src/router/response/core.ts +0 -181
  95. package/src/router/response/directives.ts +0 -233
  96. package/src/router/response/formats/content/bodyless.ts +0 -54
  97. package/src/router/response/formats/content/content.ts +0 -79
  98. package/src/router/response/formats/content/index.ts +0 -2
  99. package/src/router/response/formats/json-rpc/index.ts +0 -2
  100. package/src/router/response/formats/problem/badRequest.ts +0 -90
  101. package/src/router/response/formats/problem/conflict.ts +0 -90
  102. package/src/router/response/formats/problem/created.ts +0 -40
  103. package/src/router/response/formats/problem/index.ts +0 -27
  104. package/src/router/response/formats/problem/notFound.ts +0 -90
  105. package/src/router/response/formats/problem/permissionDenied.ts +0 -90
  106. package/src/router/response/formats/problem/problem.test.ts +0 -888
  107. package/src/router/response/formats/problem/rateLimited.ts +0 -90
  108. package/src/router/response/formats/problem/responses.ts +0 -219
  109. package/src/router/response/formats/problem/root-errors.ts +0 -48
  110. package/src/router/response/formats/problem/sessionExpired.ts +0 -90
  111. package/src/router/response/formats/problem/types.ts +0 -170
  112. package/src/router/response/formats/problem/unauthenticated.ts +0 -90
  113. package/src/router/response/formats/problem/valibot.ts +0 -410
  114. package/src/router/response/formats/status/index.ts +0 -1
  115. package/src/router/response/formats/status/responses.ts +0 -59
  116. package/src/router/response/formats/status/status.test.ts +0 -21
  117. package/src/router/response/framers.ts +0 -85
  118. package/src/router/response/index.ts +0 -28
  119. package/src/router/response/openapi.test.ts +0 -96
  120. package/src/router/response/openapi.ts +0 -1
  121. package/src/router/response/serializers.ts +0 -66
  122. package/src/router/response/stream.ts +0 -35
  123. package/src/router/router.test.ts +0 -1571
  124. package/src/router/router.ts +0 -1965
  125. package/src/router/routes/index.ts +0 -46
  126. package/src/router/routes/valibot/index.ts +0 -18
  127. package/src/router/routes/valibot/valibot.ts +0 -1393
  128. package/src/router/routes/valibot.test.ts +0 -286
  129. package/src/router/routes/zod/index.ts +0 -18
  130. package/src/router/routes/zod/zod.ts +0 -1318
  131. package/src/router/routes/zod.test.ts +0 -280
  132. package/src/router/server-interface.ts +0 -31
  133. package/src/router/types.ts +0 -657
@@ -0,0 +1,296 @@
1
+ import { n as RoutekitResponse, r as RoutekitResponseInit } from "./core-CzUCxvGk.mjs";
2
+ import { HttpStatus } from "@mpen/http";
3
+
4
+ //#region src/router/response/formats/problem/types.d.ts
5
+ /**
6
+ * Path segment used to locate a validation or business-rule issue.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const path: ProblemIssuePathSegment[] = ['body', 'users', 0, 'email']
11
+ * ```
12
+ */
13
+ type ProblemIssuePathSegment = string | number;
14
+ /**
15
+ * A single issue associated with a problem response.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const issue: ProblemIssue<'required'> = {
20
+ * code: 'required',
21
+ * message: 'Email is required',
22
+ * path: ['body', 'email'],
23
+ * }
24
+ * ```
25
+ *
26
+ * @typeParam Code - Machine-readable issue code.
27
+ */
28
+ interface ProblemIssue<Code extends string = string> {
29
+ /**
30
+ * Machine-readable issue code.
31
+ */
32
+ code: Code;
33
+ /**
34
+ * Human-readable issue message.
35
+ */
36
+ message: string;
37
+ /**
38
+ * Structured path to the invalid value.
39
+ */
40
+ path?: readonly ProblemIssuePathSegment[];
41
+ /**
42
+ * Expected value description.
43
+ */
44
+ expected?: string;
45
+ /**
46
+ * Received value description.
47
+ */
48
+ received?: string;
49
+ }
50
+ /**
51
+ * Primary problem metadata that clients can switch on.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const error: ProblemError<'not_found'> = {
56
+ * code: 'not_found',
57
+ * message: 'No user exists for the provided id.',
58
+ * title: 'User not found',
59
+ * }
60
+ * ```
61
+ *
62
+ * @typeParam Code - Machine-readable error code.
63
+ */
64
+ interface ProblemError<Code extends string = string> {
65
+ /**
66
+ * Machine-readable error code.
67
+ */
68
+ code: Code;
69
+ /**
70
+ * Human-readable problem message clients can display by default.
71
+ */
72
+ message: string;
73
+ /**
74
+ * Optional short problem heading.
75
+ */
76
+ title?: string;
77
+ }
78
+ /**
79
+ * Successful standard response envelope.
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * const response: SuccessResponse<{ id: string }> = {
84
+ * success: true,
85
+ * data: { id: 'user_123' },
86
+ * }
87
+ * ```
88
+ *
89
+ * @typeParam Data - Successful response payload type.
90
+ * @typeParam Meta - Optional metadata payload type.
91
+ */
92
+ interface SuccessResponse<Data = unknown, Meta = unknown> {
93
+ /**
94
+ * Discriminator for successful responses.
95
+ */
96
+ success: true;
97
+ /**
98
+ * Successful response payload.
99
+ */
100
+ data: Data;
101
+ /**
102
+ * Optional response metadata.
103
+ */
104
+ meta?: Meta;
105
+ }
106
+ /**
107
+ * Error standard response envelope.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const response: ProblemResponse<'validation_failed'> = {
112
+ * success: false,
113
+ * error: {
114
+ * code: 'validation_failed',
115
+ * message: 'Validation failed',
116
+ * },
117
+ * issues: [{ code: 'required', message: 'Email is required', path: ['body', 'email'] }],
118
+ * }
119
+ * ```
120
+ *
121
+ * @typeParam Code - Machine-readable primary error code.
122
+ * @typeParam Issue - Issue shape used for validation or business-rule details.
123
+ */
124
+ interface ProblemResponse<Code extends string = string, Issue extends ProblemIssue = ProblemIssue> {
125
+ /**
126
+ * Discriminator for problem responses.
127
+ */
128
+ success: false;
129
+ /**
130
+ * Primary error clients can switch on.
131
+ */
132
+ error: ProblemError<Code>;
133
+ /**
134
+ * Optional validation or business-rule issues.
135
+ */
136
+ issues?: readonly Issue[];
137
+ }
138
+ /**
139
+ * Standard response union for successful and problem responses.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * type GetUserResponse = StandardResponse<{ id: string }, 'not_found'>
144
+ * ```
145
+ *
146
+ * @typeParam Data - Successful response payload type.
147
+ * @typeParam Code - Machine-readable primary error code.
148
+ * @typeParam Issue - Issue shape used for validation or business-rule details.
149
+ * @typeParam Meta - Optional metadata payload type.
150
+ */
151
+ type StandardResponse<Data = unknown, Code extends string = string, Issue extends ProblemIssue = ProblemIssue, Meta = unknown> = SuccessResponse<Data, Meta> | ProblemResponse<Code, Issue>;
152
+ //#endregion
153
+ //#region src/router/response/formats/problem/responses.d.ts
154
+ /**
155
+ * Default primary error code for validation problem responses.
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * validationProblem([{ code: 'required', message: 'Name is required' }])
160
+ * ```
161
+ */
162
+ declare const VALIDATION_PROBLEM_CODE = "validation_failed";
163
+ /**
164
+ * Options accepted by [`ok`]{@link ok}.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * ok({ id: 'user_123' }, { meta: { requestId: 'req_123' } })
169
+ * ```
170
+ *
171
+ * @typeParam Meta - Optional metadata payload type.
172
+ */
173
+ interface SuccessResponseInit<Meta = unknown> extends Omit<RoutekitResponseInit, 'status'> {
174
+ /**
175
+ * Optional response metadata.
176
+ */
177
+ meta?: Meta;
178
+ }
179
+ /**
180
+ * Options accepted by [`problem`]{@link problem}.
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * problem({
185
+ * code: 'not_found',
186
+ * status: 404,
187
+ * message: 'No user exists for the provided id.',
188
+ * title: 'User not found',
189
+ * })
190
+ * ```
191
+ *
192
+ * @typeParam Code - Machine-readable primary error code.
193
+ * @typeParam Issue - Issue shape used for validation or business-rule details.
194
+ */
195
+ interface ProblemResponseInit<Code extends string = string, Issue extends ProblemIssue = ProblemIssue, Status extends number = number> extends Omit<RoutekitResponseInit, 'status'> {
196
+ /**
197
+ * Machine-readable error code.
198
+ */
199
+ code: Code;
200
+ /**
201
+ * Human-readable problem message clients can display by default.
202
+ */
203
+ message: string;
204
+ /**
205
+ * HTTP status code for this response.
206
+ */
207
+ status: Status;
208
+ /**
209
+ * Optional short problem heading.
210
+ */
211
+ title?: string;
212
+ /**
213
+ * Optional validation or business-rule issues.
214
+ */
215
+ issues?: readonly Issue[];
216
+ }
217
+ /**
218
+ * Options accepted by [`validationProblem`]{@link validationProblem}.
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * validationProblem([{ code: 'invalid_type', message: 'Expected a string' }], {
223
+ * message: 'The request body was invalid.',
224
+ * })
225
+ * ```
226
+ *
227
+ * @typeParam Code - Machine-readable primary error code.
228
+ * @typeParam Issue - Issue shape used for validation details.
229
+ */
230
+ interface ValidationProblemInit<Code extends string = typeof VALIDATION_PROBLEM_CODE, Issue extends ProblemIssue = ProblemIssue, Status extends number = number> extends Omit<ProblemResponseInit<Code, Issue, Status>, 'code' | 'message' | 'status' | 'issues'> {
231
+ /**
232
+ * Machine-readable validation error code.
233
+ */
234
+ code?: Code;
235
+ /**
236
+ * Human-readable validation summary.
237
+ */
238
+ message?: string;
239
+ /**
240
+ * HTTP status code for this response. Defaults to `400`.
241
+ */
242
+ status?: Status;
243
+ }
244
+ /**
245
+ * Create a `200 OK` standard response envelope.
246
+ *
247
+ * @example
248
+ * ```ts
249
+ * router.get('/users/:id', () => ok({ id: 'user_123' }))
250
+ * ```
251
+ *
252
+ * @param data - Successful response payload.
253
+ * @param init - Response headers and optional metadata.
254
+ * @returns Routekit logical response with an [`SuccessResponse`]{@link SuccessResponse} body.
255
+ * @typeParam Data - Successful response payload type.
256
+ * @typeParam Meta - Optional metadata payload type.
257
+ */
258
+ declare function ok<const Data, const Meta = unknown>(data: Data, init?: SuccessResponseInit<Meta>): RoutekitResponse<SuccessResponse<Data, Meta>, HttpStatus.OK>;
259
+ /**
260
+ * Create a standard problem response envelope.
261
+ *
262
+ * @example
263
+ * ```ts
264
+ * return problem({
265
+ * code: 'not_found',
266
+ * status: 404,
267
+ * message: 'No user exists for the provided id.',
268
+ * title: 'User not found',
269
+ * })
270
+ * ```
271
+ *
272
+ * @param init - Problem response data, headers, and status.
273
+ * @returns Routekit logical response with a [`ProblemResponse`]{@link ProblemResponse} body.
274
+ * @typeParam Code - Machine-readable primary error code.
275
+ * @typeParam Issue - Issue shape used for validation or business-rule details.
276
+ */
277
+ declare function problem<const Code extends string, const Issue extends ProblemIssue = ProblemIssue, const Status extends number = number>(init: ProblemResponseInit<Code, Issue, Status>): RoutekitResponse<ProblemResponse<Code, Issue>, Status>;
278
+ /**
279
+ * Create a standard validation problem response envelope.
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * return validationProblem([
284
+ * { code: 'required', message: 'Email is required', path: ['body', 'email'] },
285
+ * ])
286
+ * ```
287
+ *
288
+ * @param issues - Validation issues to include in the response.
289
+ * @param init - Optional problem response overrides and headers.
290
+ * @returns Routekit logical response with a validation [`ProblemResponse`]{@link ProblemResponse}.
291
+ * @typeParam Issue - Issue shape used for validation details.
292
+ * @typeParam Code - Machine-readable primary error code.
293
+ */
294
+ declare function validationProblem<const Issue extends ProblemIssue, const Code extends string = typeof VALIDATION_PROBLEM_CODE, const Status extends number = HttpStatus.BAD_REQUEST>(issues: readonly Issue[], init?: ValidationProblemInit<Code, Issue, Status>): RoutekitResponse<ProblemResponse<Code, Issue>, Status | HttpStatus.BAD_REQUEST>;
295
+ //#endregion
296
+ export { ok as a, ProblemError as c, ProblemResponse as d, StandardResponse as f, ValidationProblemInit as i, ProblemIssue as l, SuccessResponseInit as n, problem as o, SuccessResponse as p, VALIDATION_PROBLEM_CODE as r, validationProblem as s, ProblemResponseInit as t, ProblemIssuePathSegment as u };
@@ -0,0 +1,101 @@
1
+ import { a as response } from "./core-DbmQauwS.mjs";
2
+ import { HttpStatus } from "@mpen/http";
3
+ //#region src/router/response/formats/problem/responses.ts
4
+ /**
5
+ * Default primary error code for validation problem responses.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * validationProblem([{ code: 'required', message: 'Name is required' }])
10
+ * ```
11
+ */
12
+ const VALIDATION_PROBLEM_CODE = "validation_failed";
13
+ function withDefinedOptional(value, key, field) {
14
+ if (field === void 0) return value;
15
+ return {
16
+ ...value,
17
+ [key]: field
18
+ };
19
+ }
20
+ /**
21
+ * Create a `200 OK` standard response envelope.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * router.get('/users/:id', () => ok({ id: 'user_123' }))
26
+ * ```
27
+ *
28
+ * @param data - Successful response payload.
29
+ * @param init - Response headers and optional metadata.
30
+ * @returns Routekit logical response with an [`SuccessResponse`]{@link SuccessResponse} body.
31
+ * @typeParam Data - Successful response payload type.
32
+ * @typeParam Meta - Optional metadata payload type.
33
+ */
34
+ function ok(data, init = {}) {
35
+ const { meta, ...responseInit } = init;
36
+ return response(withDefinedOptional({
37
+ success: true,
38
+ data
39
+ }, "meta", meta), {
40
+ ...responseInit,
41
+ status: HttpStatus.OK
42
+ });
43
+ }
44
+ /**
45
+ * Create a standard problem response envelope.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * return problem({
50
+ * code: 'not_found',
51
+ * status: 404,
52
+ * message: 'No user exists for the provided id.',
53
+ * title: 'User not found',
54
+ * })
55
+ * ```
56
+ *
57
+ * @param init - Problem response data, headers, and status.
58
+ * @returns Routekit logical response with a [`ProblemResponse`]{@link ProblemResponse} body.
59
+ * @typeParam Code - Machine-readable primary error code.
60
+ * @typeParam Issue - Issue shape used for validation or business-rule details.
61
+ */
62
+ function problem(init) {
63
+ const { code, message, status, title, issues, ...responseInit } = init;
64
+ return response(withDefinedOptional({
65
+ success: false,
66
+ error: withDefinedOptional({
67
+ code,
68
+ message
69
+ }, "title", title)
70
+ }, "issues", issues), {
71
+ ...responseInit,
72
+ status
73
+ });
74
+ }
75
+ /**
76
+ * Create a standard validation problem response envelope.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * return validationProblem([
81
+ * { code: 'required', message: 'Email is required', path: ['body', 'email'] },
82
+ * ])
83
+ * ```
84
+ *
85
+ * @param issues - Validation issues to include in the response.
86
+ * @param init - Optional problem response overrides and headers.
87
+ * @returns Routekit logical response with a validation [`ProblemResponse`]{@link ProblemResponse}.
88
+ * @typeParam Issue - Issue shape used for validation details.
89
+ * @typeParam Code - Machine-readable primary error code.
90
+ */
91
+ function validationProblem(issues, init = {}) {
92
+ return problem({
93
+ ...init,
94
+ code: init.code ?? "validation_failed",
95
+ message: init.message ?? "Validation failed",
96
+ status: init.status ?? HttpStatus.BAD_REQUEST,
97
+ issues
98
+ });
99
+ }
100
+ //#endregion
101
+ export { validationProblem as i, ok as n, problem as r, VALIDATION_PROBLEM_CODE as t };