@web-ts-toolkit/access-router 0.3.0 → 0.4.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/advanced.d.mts +3 -134
- package/advanced.d.ts +3 -134
- package/advanced.js +323 -17
- package/advanced.mjs +312 -17
- package/index.d.mts +2 -2
- package/index.d.ts +2 -2
- package/index.js +358 -40
- package/index.mjs +347 -40
- package/package.json +3 -3
- package/{service-DLKAswCS.d.mts → parsers-CsyGHYQR.d.mts} +349 -48
- package/{service-DLKAswCS.d.ts → parsers-CsyGHYQR.d.ts} +349 -48
package/advanced.js
CHANGED
|
@@ -47,7 +47,18 @@ __export(advanced_exports, {
|
|
|
47
47
|
dataListBodySchema: () => dataListBodySchema,
|
|
48
48
|
dataReadByIdBodySchema: () => dataReadByIdBodySchema,
|
|
49
49
|
dataReadFilterBodySchema: () => dataReadFilterBodySchema,
|
|
50
|
+
defineRequestSchema: () => defineRequestSchema,
|
|
50
51
|
distinctBodySchema: () => distinctBodySchema,
|
|
52
|
+
fromAjv: () => fromAjv,
|
|
53
|
+
fromArkType: () => fromArkType,
|
|
54
|
+
fromIoTs: () => fromIoTs,
|
|
55
|
+
fromJoi: () => fromJoi,
|
|
56
|
+
fromStandardSchema: () => fromStandardSchema,
|
|
57
|
+
fromSuperstruct: () => fromSuperstruct,
|
|
58
|
+
fromValibot: () => fromValibot,
|
|
59
|
+
fromVine: () => fromVine,
|
|
60
|
+
fromYup: () => fromYup,
|
|
61
|
+
fromZod: () => fromZod,
|
|
51
62
|
listBodySchema: () => listBodySchema,
|
|
52
63
|
listQuerySchema: () => listQuerySchema,
|
|
53
64
|
parseBody: () => parseBody,
|
|
@@ -211,7 +222,7 @@ var clientErrors = import_express_json_router.default.clientErrors;
|
|
|
211
222
|
function parsePathParam(value, parameter) {
|
|
212
223
|
const result = stringOrStringArray.safeParse(value);
|
|
213
224
|
if (!result.success) {
|
|
214
|
-
throwValidationError(result.error.issues, parameter, "parameter");
|
|
225
|
+
throwValidationError(normalizeIssues(result.error.issues), parameter, "parameter");
|
|
215
226
|
}
|
|
216
227
|
const param = Array.isArray(result.data) ? result.data[0] : result.data;
|
|
217
228
|
if (!param) {
|
|
@@ -224,49 +235,333 @@ function parsePathParam(value, parameter) {
|
|
|
224
235
|
function parseQuery(schema, value) {
|
|
225
236
|
const result = schema.safeParse(value);
|
|
226
237
|
if (!result.success) {
|
|
227
|
-
throwValidationError(result.error.issues, void 0, "parameter");
|
|
238
|
+
throwValidationError(normalizeIssues(result.error.issues), void 0, "parameter");
|
|
228
239
|
}
|
|
229
240
|
return result.data;
|
|
230
241
|
}
|
|
231
242
|
function parseBody(schema, value) {
|
|
232
243
|
const result = schema.safeParse(value ?? {});
|
|
233
244
|
if (!result.success) {
|
|
234
|
-
throwValidationError(result.error.issues, void 0, "pointer");
|
|
245
|
+
throwValidationError(normalizeIssues(result.error.issues), void 0, "pointer");
|
|
235
246
|
}
|
|
236
247
|
return result.data;
|
|
237
248
|
}
|
|
238
249
|
function parseBodyWithSchema(schema, value, userSchema) {
|
|
239
250
|
const body = parseBody(schema, value);
|
|
240
|
-
return
|
|
251
|
+
return isRequestSchema(userSchema) ? parseUserSchema(userSchema, body) : Promise.resolve(body);
|
|
241
252
|
}
|
|
242
|
-
function parseNestedBodyWithSchema(schema, value, nestedKey, userSchema) {
|
|
253
|
+
async function parseNestedBodyWithSchema(schema, value, nestedKey, userSchema) {
|
|
243
254
|
const body = parseBody(schema, value);
|
|
244
|
-
if (!
|
|
255
|
+
if (!isRequestSchema(userSchema)) return body;
|
|
245
256
|
return {
|
|
246
257
|
...body,
|
|
247
|
-
[nestedKey]: parseUserSchema(userSchema, body?.[nestedKey], [nestedKey])
|
|
258
|
+
[nestedKey]: await parseUserSchema(userSchema, body?.[nestedKey], [nestedKey])
|
|
248
259
|
};
|
|
249
260
|
}
|
|
250
261
|
function throwValidationError(issues, key, location = "pointer") {
|
|
251
262
|
const errors = issues.map((issue) => formatIssue(issue, key, location));
|
|
252
263
|
throw new clientErrors.BadRequestError("Bad Request", { errors });
|
|
253
264
|
}
|
|
254
|
-
function parseUserSchema(schema, value, prefix = []) {
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
path: prefix.concat(issue.path.map(String))
|
|
260
|
-
}));
|
|
261
|
-
throwValidationError(issues, void 0, "pointer");
|
|
265
|
+
async function parseUserSchema(schema, value, prefix = []) {
|
|
266
|
+
const validator = toRequestSchemaValidator(schema);
|
|
267
|
+
const result = await validator(value);
|
|
268
|
+
if (!isRequestSchemaFailure(result)) {
|
|
269
|
+
return result.data;
|
|
262
270
|
}
|
|
263
|
-
|
|
271
|
+
const issues = result.issues.map((issue) => ({
|
|
272
|
+
...issue,
|
|
273
|
+
path: prefix.concat((issue.path ?? []).map(String))
|
|
274
|
+
}));
|
|
275
|
+
throwValidationError(issues, void 0, "pointer");
|
|
264
276
|
}
|
|
265
277
|
function isZodSchema(schema) {
|
|
266
278
|
return typeof schema === "object" && schema !== null && "safeParse" in schema && typeof schema.safeParse === "function";
|
|
267
279
|
}
|
|
280
|
+
function isRequestSchema(schema) {
|
|
281
|
+
return isZodSchema(schema) || isStandardSchema(schema) || isRequestSchemaValidator(schema) || isRequestSchemaAdapter(schema);
|
|
282
|
+
}
|
|
283
|
+
function isRequestSchemaValidator(schema) {
|
|
284
|
+
return typeof schema === "function";
|
|
285
|
+
}
|
|
286
|
+
function isRequestSchemaAdapter(schema) {
|
|
287
|
+
return typeof schema === "object" && schema !== null && "validate" in schema && typeof schema.validate === "function";
|
|
288
|
+
}
|
|
289
|
+
function isStandardSchema(schema) {
|
|
290
|
+
return typeof schema === "object" && schema !== null && "~standard" in schema && typeof schema["~standard"] === "object" && schema["~standard"] !== null && "validate" in schema["~standard"] && typeof schema["~standard"].validate === "function";
|
|
291
|
+
}
|
|
292
|
+
function isRequestSchemaFailure(result) {
|
|
293
|
+
return result.success === false;
|
|
294
|
+
}
|
|
295
|
+
function toRequestSchemaValidator(schema) {
|
|
296
|
+
if (isZodSchema(schema)) return fromZod(schema);
|
|
297
|
+
if (isStandardSchema(schema)) return fromStandardSchema(schema);
|
|
298
|
+
if (isRequestSchemaValidator(schema)) return schema;
|
|
299
|
+
return schema.validate;
|
|
300
|
+
}
|
|
301
|
+
function defineRequestSchema(validator) {
|
|
302
|
+
return validator;
|
|
303
|
+
}
|
|
304
|
+
function fromZod(schema) {
|
|
305
|
+
return (value) => {
|
|
306
|
+
const result = schema.safeParse(value);
|
|
307
|
+
if (result.success) {
|
|
308
|
+
return {
|
|
309
|
+
success: true,
|
|
310
|
+
data: result.data
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
success: false,
|
|
315
|
+
issues: normalizeIssues(result.error.issues)
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
function fromStandardSchema(schema) {
|
|
320
|
+
return async (value) => {
|
|
321
|
+
const result = await schema["~standard"].validate(value);
|
|
322
|
+
if (!isStandardSchemaFailure(result)) {
|
|
323
|
+
return {
|
|
324
|
+
success: true,
|
|
325
|
+
data: result.value
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
return {
|
|
329
|
+
success: false,
|
|
330
|
+
issues: normalizeIssues(result.issues)
|
|
331
|
+
};
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
function fromYup(schema) {
|
|
335
|
+
return async (value) => {
|
|
336
|
+
try {
|
|
337
|
+
const data = await schema.validate(value, { abortEarly: false });
|
|
338
|
+
return {
|
|
339
|
+
success: true,
|
|
340
|
+
data
|
|
341
|
+
};
|
|
342
|
+
} catch (error) {
|
|
343
|
+
return {
|
|
344
|
+
success: false,
|
|
345
|
+
issues: normalizeYupIssues(error)
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function fromJoi(schema) {
|
|
351
|
+
return async (value) => {
|
|
352
|
+
const result = await schema.validate(value, { abortEarly: false });
|
|
353
|
+
if (!result.error?.details?.length) {
|
|
354
|
+
return {
|
|
355
|
+
success: true,
|
|
356
|
+
data: result.value
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
return {
|
|
360
|
+
success: false,
|
|
361
|
+
issues: normalizeJoiIssues(result.error.details)
|
|
362
|
+
};
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function fromAjv(validator) {
|
|
366
|
+
return async (value) => {
|
|
367
|
+
const valid = await validator(value);
|
|
368
|
+
if (valid) {
|
|
369
|
+
return {
|
|
370
|
+
success: true,
|
|
371
|
+
data: value
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
success: false,
|
|
376
|
+
issues: normalizeAjvIssues(validator.errors ?? [])
|
|
377
|
+
};
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
function fromValibot(schema, safeParse) {
|
|
381
|
+
return async (value) => {
|
|
382
|
+
const result = await safeParse(schema, value, { abortEarly: false });
|
|
383
|
+
if (result.success) {
|
|
384
|
+
return {
|
|
385
|
+
success: true,
|
|
386
|
+
data: result.output
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
success: false,
|
|
391
|
+
issues: normalizeValibotIssues(result.issues)
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
function fromArkType(type) {
|
|
396
|
+
return async (value) => {
|
|
397
|
+
const result = await type(value);
|
|
398
|
+
if (!isArkTypeErrors(result)) {
|
|
399
|
+
return {
|
|
400
|
+
success: true,
|
|
401
|
+
data: result
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
return {
|
|
405
|
+
success: false,
|
|
406
|
+
issues: normalizeArkTypeIssues(result)
|
|
407
|
+
};
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function fromIoTs(decoder) {
|
|
411
|
+
return async (value) => {
|
|
412
|
+
const result = decoder.decode(value);
|
|
413
|
+
if (result._tag === "Right") {
|
|
414
|
+
return {
|
|
415
|
+
success: true,
|
|
416
|
+
data: result.right
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
return {
|
|
420
|
+
success: false,
|
|
421
|
+
issues: normalizeIoTsIssues(result.left)
|
|
422
|
+
};
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
function fromSuperstruct(struct, validate) {
|
|
426
|
+
return async (value) => {
|
|
427
|
+
const [failure, output] = await validate(value, struct);
|
|
428
|
+
if (!failure) {
|
|
429
|
+
return {
|
|
430
|
+
success: true,
|
|
431
|
+
data: output
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
return {
|
|
435
|
+
success: false,
|
|
436
|
+
issues: normalizeSuperstructFailure(failure)
|
|
437
|
+
};
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
function fromVine(validator) {
|
|
441
|
+
return async (value) => {
|
|
442
|
+
try {
|
|
443
|
+
const output = await validator.validate(value);
|
|
444
|
+
return {
|
|
445
|
+
success: true,
|
|
446
|
+
data: output
|
|
447
|
+
};
|
|
448
|
+
} catch (error) {
|
|
449
|
+
return {
|
|
450
|
+
success: false,
|
|
451
|
+
issues: normalizeVineError(error)
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
function isStandardSchemaFailure(result) {
|
|
457
|
+
return Array.isArray(result.issues);
|
|
458
|
+
}
|
|
459
|
+
function normalizeIssues(issues) {
|
|
460
|
+
return issues.map((issue) => ({
|
|
461
|
+
message: issue.message,
|
|
462
|
+
path: issue.path?.flatMap((segment) => normalizePathSegment(segment))
|
|
463
|
+
}));
|
|
464
|
+
}
|
|
465
|
+
function normalizePathSegment(segment) {
|
|
466
|
+
const key = isStandardSchemaPathSegment(segment) ? segment.key : segment;
|
|
467
|
+
return typeof key === "string" || typeof key === "number" ? [key] : [];
|
|
468
|
+
}
|
|
469
|
+
function isStandardSchemaPathSegment(segment) {
|
|
470
|
+
return typeof segment === "object" && segment !== null && "key" in segment;
|
|
471
|
+
}
|
|
472
|
+
function normalizeYupIssues(error) {
|
|
473
|
+
if (!isYupValidationError(error)) {
|
|
474
|
+
return [{ message: "Validation failed" }];
|
|
475
|
+
}
|
|
476
|
+
const issues = error.inner?.length ? error.inner : [error];
|
|
477
|
+
return issues.map((issue) => ({
|
|
478
|
+
message: issue.message,
|
|
479
|
+
path: parsePathString(issue.path)
|
|
480
|
+
}));
|
|
481
|
+
}
|
|
482
|
+
function normalizeJoiIssues(issues) {
|
|
483
|
+
return issues.map((issue) => ({
|
|
484
|
+
message: issue.message,
|
|
485
|
+
path: issue.path ? [...issue.path] : void 0
|
|
486
|
+
}));
|
|
487
|
+
}
|
|
488
|
+
function normalizeAjvIssues(issues) {
|
|
489
|
+
return issues.map((issue) => ({
|
|
490
|
+
message: issue.message ?? "Validation failed",
|
|
491
|
+
path: parseAjvPath(issue)
|
|
492
|
+
}));
|
|
493
|
+
}
|
|
494
|
+
function normalizeValibotIssues(issues) {
|
|
495
|
+
return issues.map((issue) => ({
|
|
496
|
+
message: issue.message,
|
|
497
|
+
path: issue.path?.flatMap(
|
|
498
|
+
(item) => typeof item.key === "string" || typeof item.key === "number" ? [item.key] : []
|
|
499
|
+
)
|
|
500
|
+
}));
|
|
501
|
+
}
|
|
502
|
+
function normalizeArkTypeIssues(issues) {
|
|
503
|
+
const normalized = issues.map((issue) => ({
|
|
504
|
+
message: issue.message ?? issue.problem ?? issues.summary ?? "Validation failed",
|
|
505
|
+
path: issue.path ? [...issue.path] : void 0
|
|
506
|
+
}));
|
|
507
|
+
return normalized.length ? normalized : [{ message: issues.summary ?? "Validation failed" }];
|
|
508
|
+
}
|
|
509
|
+
function normalizeIoTsIssues(issues) {
|
|
510
|
+
return issues.map((issue) => ({
|
|
511
|
+
message: issue.message ?? "Validation failed",
|
|
512
|
+
path: issue.context.map((entry) => entry.key).filter(Boolean)
|
|
513
|
+
}));
|
|
514
|
+
}
|
|
515
|
+
function normalizeSuperstructFailure(failure) {
|
|
516
|
+
const failures = failure.failures?.() ?? [failure];
|
|
517
|
+
return failures.map((entry) => ({
|
|
518
|
+
message: entry.message ?? "Validation failed",
|
|
519
|
+
path: normalizeSuperstructPath(entry)
|
|
520
|
+
}));
|
|
521
|
+
}
|
|
522
|
+
function normalizeSuperstructPath(failure) {
|
|
523
|
+
if (failure.path?.length) return [...failure.path];
|
|
524
|
+
if (typeof failure.key === "string" || typeof failure.key === "number") return [failure.key];
|
|
525
|
+
return void 0;
|
|
526
|
+
}
|
|
527
|
+
function normalizeVineError(error) {
|
|
528
|
+
if (!isVineValidationError(error)) {
|
|
529
|
+
return [{ message: "Validation failed" }];
|
|
530
|
+
}
|
|
531
|
+
return error.messages.map((message) => ({
|
|
532
|
+
message: message.message,
|
|
533
|
+
path: normalizeVineField(message)
|
|
534
|
+
}));
|
|
535
|
+
}
|
|
536
|
+
function normalizeVineField(message) {
|
|
537
|
+
const path = message.field.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean).map((segment) => /^\d+$/.test(segment) ? Number(segment) : segment);
|
|
538
|
+
if (typeof message.index === "number" && path.length === 0) {
|
|
539
|
+
return [message.index];
|
|
540
|
+
}
|
|
541
|
+
return path.length ? path : void 0;
|
|
542
|
+
}
|
|
543
|
+
function parseAjvPath(issue) {
|
|
544
|
+
const path = issue.instancePath?.split("/").filter(Boolean).map((segment) => /^\d+$/.test(segment) ? Number(segment) : segment);
|
|
545
|
+
if (issue.params?.missingProperty) {
|
|
546
|
+
return (path ?? []).concat(issue.params.missingProperty);
|
|
547
|
+
}
|
|
548
|
+
return path;
|
|
549
|
+
}
|
|
550
|
+
function parsePathString(path) {
|
|
551
|
+
if (!path) return void 0;
|
|
552
|
+
return path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean).map((segment) => /^\d+$/.test(segment) ? Number(segment) : segment);
|
|
553
|
+
}
|
|
554
|
+
function isYupValidationError(error) {
|
|
555
|
+
return typeof error === "object" && error !== null && "message" in error;
|
|
556
|
+
}
|
|
557
|
+
function isArkTypeErrors(value) {
|
|
558
|
+
return Array.isArray(value);
|
|
559
|
+
}
|
|
560
|
+
function isVineValidationError(error) {
|
|
561
|
+
return typeof error === "object" && error !== null && "messages" in error && Array.isArray(error.messages);
|
|
562
|
+
}
|
|
268
563
|
function formatIssue(issue, key, location = "pointer") {
|
|
269
|
-
const path = issue.path.map(String);
|
|
564
|
+
const path = (issue.path ?? []).map(String);
|
|
270
565
|
const joinedPath = path.join(".");
|
|
271
566
|
if (location === "parameter") {
|
|
272
567
|
return {
|
|
@@ -696,7 +991,18 @@ var rootQuerySchema = import_zod4.z.array(import_zod4.z.union([rootModelQueryEnt
|
|
|
696
991
|
dataListBodySchema,
|
|
697
992
|
dataReadByIdBodySchema,
|
|
698
993
|
dataReadFilterBodySchema,
|
|
994
|
+
defineRequestSchema,
|
|
699
995
|
distinctBodySchema,
|
|
996
|
+
fromAjv,
|
|
997
|
+
fromArkType,
|
|
998
|
+
fromIoTs,
|
|
999
|
+
fromJoi,
|
|
1000
|
+
fromStandardSchema,
|
|
1001
|
+
fromSuperstruct,
|
|
1002
|
+
fromValibot,
|
|
1003
|
+
fromVine,
|
|
1004
|
+
fromYup,
|
|
1005
|
+
fromZod,
|
|
700
1006
|
listBodySchema,
|
|
701
1007
|
listQuerySchema,
|
|
702
1008
|
parseBody,
|