@kubb/plugin-client 5.7.0-canary.20261024T172532 → 5.7.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/index.cjs +77 -37
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +77 -37
- package/dist/index.js.map +1 -1
- package/docs/guides/validate-requests-and-responses.md +23 -0
- package/docs/guides/write-your-client.md +1 -1
- package/docs/options.md +9 -7
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -428,7 +428,7 @@ var compatibility = {
|
|
|
428
428
|
};
|
|
429
429
|
//#endregion
|
|
430
430
|
//#region package.json
|
|
431
|
-
var version = "5.7.0
|
|
431
|
+
var version = "5.7.0";
|
|
432
432
|
//#endregion
|
|
433
433
|
//#region ../../internals/client/src/builders/validatorOptions.ts
|
|
434
434
|
/**
|
|
@@ -437,7 +437,7 @@ var version = "5.7.0-canary.20261024T172532";
|
|
|
437
437
|
function isValidatorEnabled(validator) {
|
|
438
438
|
if (!validator) return false;
|
|
439
439
|
if (validator === "zod") return true;
|
|
440
|
-
return Boolean(validator.request || validator.response);
|
|
440
|
+
return Boolean(validator.request || validator.params || validator.response);
|
|
441
441
|
}
|
|
442
442
|
/**
|
|
443
443
|
* Returns `'zod'` when request body parsing is enabled, `null` otherwise. The string shorthand
|
|
@@ -448,6 +448,14 @@ function resolveRequestValidator(validator) {
|
|
|
448
448
|
return validator.request ?? null;
|
|
449
449
|
}
|
|
450
450
|
/**
|
|
451
|
+
* Returns `'zod'` when path, query, and header params parsing is enabled, `null` otherwise. Only the
|
|
452
|
+
* object form `{ params: 'zod' }` enables it.
|
|
453
|
+
*/
|
|
454
|
+
function resolveParamsValidator(validator) {
|
|
455
|
+
if (!validator || validator === "zod") return null;
|
|
456
|
+
return validator.params ?? null;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
451
459
|
* Returns `'zod'` when response parsing is enabled, `null` otherwise. The string shorthand `'zod'`
|
|
452
460
|
* maps to response parsing.
|
|
453
461
|
*/
|
|
@@ -466,36 +474,13 @@ function buildZodErrorParse(node, zodResolver) {
|
|
|
466
474
|
return zodResolver.response.error?.(node) || null;
|
|
467
475
|
}
|
|
468
476
|
//#endregion
|
|
469
|
-
//#region ../../internals/client/src/builders/validator.ts
|
|
470
|
-
/**
|
|
471
|
-
* Builds the validator-hook references for one operation. Request validation runs before the send;
|
|
472
|
-
* response validation runs on the success body only. Returns `null` references when the matching
|
|
473
|
-
* direction is disabled or the schema is absent.
|
|
474
|
-
*/
|
|
475
|
-
function buildValidatorHooks({ node, validator, zodResolver }) {
|
|
476
|
-
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
477
|
-
const parsesResponse = zodResolver && resolveResponseValidator(validator) === "zod";
|
|
478
|
-
const response = parsesResponse ? zodResolver.response.response(node) || null : null;
|
|
479
|
-
const error = parsesResponse ? buildZodErrorParse(node, zodResolver) : null;
|
|
480
|
-
const request = zodResolver && resolveRequestValidator(validator) === "zod" && hasRequestBody ? zodResolver.response.body(node) || null : null;
|
|
481
|
-
return {
|
|
482
|
-
request,
|
|
483
|
-
response,
|
|
484
|
-
error,
|
|
485
|
-
importedZodNames: [
|
|
486
|
-
response,
|
|
487
|
-
error,
|
|
488
|
-
request
|
|
489
|
-
].filter((name) => Boolean(name))
|
|
490
|
-
};
|
|
491
|
-
}
|
|
492
|
-
//#endregion
|
|
493
477
|
//#region ../../internals/client/src/builders/security.ts
|
|
494
478
|
function serializeAuth(auth) {
|
|
495
479
|
const parts = [`type: '${auth.type}'`];
|
|
496
480
|
if (auth.scheme) parts.push(`scheme: '${auth.scheme}'`);
|
|
497
481
|
if (auth.name) parts.push(`name: '${auth.name}'`);
|
|
498
482
|
if (auth.in) parts.push(`in: '${auth.in}'`);
|
|
483
|
+
if (auth.scopes?.length) parts.push(`scopes: [${auth.scopes.map((scope) => `'${scope}'`).join(", ")}]`);
|
|
499
484
|
return `{ ${parts.join(", ")} }`;
|
|
500
485
|
}
|
|
501
486
|
/**
|
|
@@ -538,11 +523,14 @@ function getOperationSecurity({ document, method, path }) {
|
|
|
538
523
|
const definitions = document.components?.securitySchemes ?? {};
|
|
539
524
|
const security = [];
|
|
540
525
|
const seen = /* @__PURE__ */ new Set();
|
|
541
|
-
for (const requirement of requirements) for (const schemeName of Object.
|
|
526
|
+
for (const requirement of requirements) for (const [schemeName, scopes] of Object.entries(requirement)) {
|
|
542
527
|
if (seen.has(schemeName)) continue;
|
|
543
528
|
seen.add(schemeName);
|
|
544
529
|
const auth = resolveSecurityScheme(definitions[schemeName]);
|
|
545
|
-
if (auth) security.push(
|
|
530
|
+
if (auth) security.push(scopes.length ? {
|
|
531
|
+
...auth,
|
|
532
|
+
scopes
|
|
533
|
+
} : auth);
|
|
546
534
|
}
|
|
547
535
|
return security.length ? security : void 0;
|
|
548
536
|
}
|
|
@@ -558,6 +546,49 @@ function buildSecurityMetadata({ security }) {
|
|
|
558
546
|
return `[${security.map(serializeAuth).join(", ")}]`;
|
|
559
547
|
}
|
|
560
548
|
//#endregion
|
|
549
|
+
//#region ../../internals/client/src/builders/validator.ts
|
|
550
|
+
/**
|
|
551
|
+
* Builds the validator-hook references for one operation. Request body and params validation run before the send.
|
|
552
|
+
* Response validation runs on the success body only. Returns `null` references when the matching
|
|
553
|
+
* direction is disabled or the schema is absent.
|
|
554
|
+
*/
|
|
555
|
+
function buildValidatorHooks({ node, validator, zodResolver }) {
|
|
556
|
+
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
557
|
+
const parsesResponse = zodResolver && resolveResponseValidator(validator) === "zod";
|
|
558
|
+
const response = parsesResponse ? zodResolver.response.response(node) || null : null;
|
|
559
|
+
const error = parsesResponse ? buildZodErrorParse(node, zodResolver) : null;
|
|
560
|
+
const request = zodResolver && resolveRequestValidator(validator) === "zod" && hasRequestBody ? zodResolver.response.body(node) || null : null;
|
|
561
|
+
const paramGroups = getOperationParameters(node);
|
|
562
|
+
const resolveParamsName = (kind, params) => zodResolver && resolveParamsValidator(validator) === "zod" && params.length > 0 ? zodResolver.param[kind](node, params[0]) : null;
|
|
563
|
+
const path = resolveParamsName("path", paramGroups.path);
|
|
564
|
+
const query = resolveParamsName("query", paramGroups.query);
|
|
565
|
+
const headers = resolveParamsName("headers", paramGroups.header);
|
|
566
|
+
const importedZodNames = [
|
|
567
|
+
response,
|
|
568
|
+
error,
|
|
569
|
+
request,
|
|
570
|
+
path,
|
|
571
|
+
query,
|
|
572
|
+
headers
|
|
573
|
+
].filter((name) => Boolean(name));
|
|
574
|
+
return {
|
|
575
|
+
request,
|
|
576
|
+
path,
|
|
577
|
+
query,
|
|
578
|
+
headers: headers ? `toHeadersStandardSchema(${headers}, ${headers}.shape)` : null,
|
|
579
|
+
response,
|
|
580
|
+
error,
|
|
581
|
+
importedZodNames
|
|
582
|
+
};
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* Prints the `validator: { … }` entry of a call config from the hooks named in `keys`, or `null` when none is set.
|
|
586
|
+
*/
|
|
587
|
+
function buildValidatorLiteral(hooks, keys) {
|
|
588
|
+
const entries = keys.flatMap((key) => hooks?.[key] ? [`${key}: ${hooks[key]}`] : []);
|
|
589
|
+
return entries.length ? `validator: { ${entries.join(", ")} }` : null;
|
|
590
|
+
}
|
|
591
|
+
//#endregion
|
|
561
592
|
//#region ../../internals/client/src/builders/contentType.ts
|
|
562
593
|
/**
|
|
563
594
|
* Builds the `contentType` entry baked into the call config, or `null` for the JSON default.
|
|
@@ -762,12 +793,14 @@ function Operation({ name, node, types, zodResolver, validator, returnType, thro
|
|
|
762
793
|
const eventStream = isEventStream(node);
|
|
763
794
|
const responseType = getResponseType(node);
|
|
764
795
|
const responseTypeLiteral = responseType ? `responseType: '${responseType}'` : null;
|
|
765
|
-
const
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
796
|
+
const validatorLiteral = buildValidatorLiteral(validators, [
|
|
797
|
+
"request",
|
|
798
|
+
"path",
|
|
799
|
+
"query",
|
|
800
|
+
"headers",
|
|
801
|
+
"response",
|
|
802
|
+
"error"
|
|
803
|
+
]);
|
|
771
804
|
const callConfig = `{ ${[
|
|
772
805
|
`method: '${node.method.toUpperCase()}'`,
|
|
773
806
|
`url: '${node.path}'`,
|
|
@@ -879,14 +912,15 @@ function createClientGenerator(name, resolveClientPath) {
|
|
|
879
912
|
const { config, driver, resolver, root } = ctx;
|
|
880
913
|
const { output, validator, returnType, throwOnErrorDefault, group } = ctx.options;
|
|
881
914
|
const types = resolveOperationTypes(driver, ctx.plugin.name);
|
|
882
|
-
const pluginZod =
|
|
915
|
+
const pluginZod = isValidatorEnabled(validator) ? driver.getPlugin(pluginZodName) : null;
|
|
883
916
|
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null;
|
|
884
917
|
const importedTypeNames = [types.response.options(node), types.response.responses(node)];
|
|
885
|
-
const
|
|
918
|
+
const validatorHooks = buildValidatorHooks({
|
|
886
919
|
node,
|
|
887
920
|
validator,
|
|
888
921
|
zodResolver
|
|
889
|
-
})
|
|
922
|
+
});
|
|
923
|
+
const importedZodNames = validatorHooks.importedZodNames;
|
|
890
924
|
const meta = {
|
|
891
925
|
name: resolver.name(node.operationId),
|
|
892
926
|
file: resolver.file({
|
|
@@ -918,6 +952,7 @@ function createClientGenerator(name, resolveClientPath) {
|
|
|
918
952
|
const customClientPath = resolveClientPath?.(ctx.options, root);
|
|
919
953
|
const clientPath = customClientPath ?? path.resolve(root, ".kubb/client.ts");
|
|
920
954
|
const clientRoot = customClientPath === void 0 ? meta.file.path : void 0;
|
|
955
|
+
const standardSchemaPath = customClientPath ?? path.resolve(root, ".kubb/standardSchema.ts");
|
|
921
956
|
const eventStream = isEventStream(node);
|
|
922
957
|
const clientValueImports = (() => {
|
|
923
958
|
if (eventStream) return ["client", "toEventStream"];
|
|
@@ -982,6 +1017,11 @@ function createClientGenerator(name, resolveClientPath) {
|
|
|
982
1017
|
root: meta.file.path,
|
|
983
1018
|
path: meta.fileZod.path
|
|
984
1019
|
}),
|
|
1020
|
+
validatorHooks.headers && /* @__PURE__ */ jsx(File.Import, {
|
|
1021
|
+
name: ["toHeadersStandardSchema"],
|
|
1022
|
+
root: clientRoot,
|
|
1023
|
+
path: standardSchemaPath
|
|
1024
|
+
}),
|
|
985
1025
|
/* @__PURE__ */ jsx(Operation, {
|
|
986
1026
|
name: meta.name,
|
|
987
1027
|
node,
|