@kubb/plugin-client 6.1.0-canary.20261109T175349 → 6.1.0-canary.20261109T191216
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/{client-D0IfSe5q.d.ts → client-DaI8VaEz.d.ts} +2 -3
- package/dist/index.cjs +69 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +69 -40
- package/dist/index.js.map +1 -1
- package/dist/templates/client.d.ts +1 -1
- package/dist/templates/core.d.ts +1 -1
- package/dist/templates/sse.d.ts +1 -1
- package/docs/guides/validate-requests-and-responses.md +7 -4
- package/package.json +5 -5
- package/templates/core.ts +9 -9
package/dist/index.d.cts
CHANGED
|
@@ -73,10 +73,10 @@ type Options$1 = OutputOptions & {
|
|
|
73
73
|
*/
|
|
74
74
|
override?: Array<Override<Omit<ResolvedOptions$1, 'throwOnErrorDefault'>>>;
|
|
75
75
|
/**
|
|
76
|
-
* Base URL prepended to every request. When omitted, the generated client sets no base URL, so
|
|
77
|
-
*
|
|
78
|
-
* Values containing a `${...}` interpolation are emitted as template literals in the generated
|
|
79
|
-
*
|
|
76
|
+
* Base URL prepended to every request. When omitted, the generated client sets no base URL, so set one at runtime with
|
|
77
|
+
* `client.setConfig({ baseURL })`. The top-level server URL is not used, but an operation with path-level or operation-level `servers` calls it.
|
|
78
|
+
* Values containing a `${...}` interpolation are emitted as template literals in the generated client config, which keeps
|
|
79
|
+
* runtime environment reads dynamic.
|
|
80
80
|
*/
|
|
81
81
|
baseURL?: string;
|
|
82
82
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -73,10 +73,10 @@ type Options$1 = OutputOptions & {
|
|
|
73
73
|
*/
|
|
74
74
|
override?: Array<Override<Omit<ResolvedOptions$1, 'throwOnErrorDefault'>>>;
|
|
75
75
|
/**
|
|
76
|
-
* Base URL prepended to every request. When omitted, the generated client sets no base URL, so
|
|
77
|
-
*
|
|
78
|
-
* Values containing a `${...}` interpolation are emitted as template literals in the generated
|
|
79
|
-
*
|
|
76
|
+
* Base URL prepended to every request. When omitted, the generated client sets no base URL, so set one at runtime with
|
|
77
|
+
* `client.setConfig({ baseURL })`. The top-level server URL is not used, but an operation with path-level or operation-level `servers` calls it.
|
|
78
|
+
* Values containing a `${...}` interpolation are emitted as template literals in the generated client config, which keeps
|
|
79
|
+
* runtime environment reads dynamic.
|
|
80
80
|
*/
|
|
81
81
|
baseURL?: string;
|
|
82
82
|
/**
|
package/dist/index.js
CHANGED
|
@@ -521,7 +521,7 @@ var compatibility = {
|
|
|
521
521
|
"node": ">=22.22"
|
|
522
522
|
};
|
|
523
523
|
// package.json
|
|
524
|
-
var version = "6.1.0-canary.
|
|
524
|
+
var version = "6.1.0-canary.20261109T191216";
|
|
525
525
|
// ../../internals/client/src/builders/validatorOptions.ts
|
|
526
526
|
/**
|
|
527
527
|
* Returns `true` when any direction of the validator uses zod (used for dependency checks).
|
|
@@ -557,22 +557,19 @@ function resolveResponseValidator(validator) {
|
|
|
557
557
|
return validator.response ?? null;
|
|
558
558
|
}
|
|
559
559
|
/**
|
|
560
|
-
*
|
|
561
|
-
*
|
|
562
|
-
* operation documents no error responses with a schema.
|
|
560
|
+
* Builds the `validator.error` map of a call, one plugin-zod `<operation>StatusNNNSchema` per non-2xx response with a
|
|
561
|
+
* schema, such as `{ 404: getPetStatus404Schema, '5XX': getPetStatus5XXSchema }`. Returns `null` without one.
|
|
563
562
|
*/
|
|
564
563
|
function buildZodErrorParse(node, zodResolver) {
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
}
|
|
574
|
-
function getErrorStatusCodes(node) {
|
|
575
|
-
return node.responses.filter((res) => !isSuccessStatusCode(res.statusCode) && res.content?.some((entry) => entry.schema)).map((res) => String(res.statusCode));
|
|
564
|
+
const entries = node.responses.filter((res) => !isSuccessStatusCode(res.statusCode) && res.content?.some((entry) => entry.schema)).map((res) => ({
|
|
565
|
+
key: String(res.statusCode),
|
|
566
|
+
name: zodResolver.response.status(node, res.statusCode)
|
|
567
|
+
}));
|
|
568
|
+
if (!entries.length) return null;
|
|
569
|
+
return {
|
|
570
|
+
literal: `{ ${entries.map(({ key, name }) => `${/^\d+$/.test(key) ? key : stringify(key)}: ${name}`).join(", ")} }`,
|
|
571
|
+
names: entries.map(({ name }) => name)
|
|
572
|
+
};
|
|
576
573
|
}
|
|
577
574
|
// ../../internals/client/src/builders/generics.ts
|
|
578
575
|
/**
|
|
@@ -767,6 +764,18 @@ function buildSecurityMetadata({ security }) {
|
|
|
767
764
|
if (!security?.length) return null;
|
|
768
765
|
return `[${security.map(serializeAuth).join(", ")}]`;
|
|
769
766
|
}
|
|
767
|
+
// ../../internals/client/src/builders/server.ts
|
|
768
|
+
/**
|
|
769
|
+
* Returns the URL of the operation's own `servers`, else its path item's, picked with `server.index` and falling back to the
|
|
770
|
+
* first entry. Returns `undefined` without `servers`, for a relative URL such as `/v2`, which would drop the client's host, or
|
|
771
|
+
* for a URL with `{variable}` placeholders, which are not supported, so the client's `baseURL` applies.
|
|
772
|
+
*/
|
|
773
|
+
function getOperationBaseURL({ document, method, path, server: { index = 0 } = {} }) {
|
|
774
|
+
const pathItem = document?.paths?.[path];
|
|
775
|
+
const servers = [(pathItem?.[method.toLowerCase()])?.servers, pathItem?.servers].find((list) => list?.length);
|
|
776
|
+
const url = (servers?.[index] ?? servers?.[0])?.url;
|
|
777
|
+
return url && /^[a-z][a-z\d+.-]*:\/\/[^{}]*$/i.test(url) ? url : void 0;
|
|
778
|
+
}
|
|
770
779
|
// ../../internals/client/src/builders/validator.ts
|
|
771
780
|
/**
|
|
772
781
|
* Builds the validator-hook references for one operation. Request body and params validation run before the send.
|
|
@@ -777,8 +786,7 @@ function buildValidatorHooks({ node, validator, zodResolver }) {
|
|
|
777
786
|
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema);
|
|
778
787
|
const parsesResponse = zodResolver && resolveResponseValidator(validator) === "zod";
|
|
779
788
|
const response = parsesResponse ? zodResolver.response.response(node) || null : null;
|
|
780
|
-
const
|
|
781
|
-
const errorStatus = error ? buildErrorStatusLiteral(node) : null;
|
|
789
|
+
const errorParse = parsesResponse ? buildZodErrorParse(node, zodResolver) : null;
|
|
782
790
|
const request = zodResolver && resolveRequestValidator(validator) === "zod" && hasRequestBody ? zodResolver.response.body(node) || null : null;
|
|
783
791
|
const paramGroups = getOperationParameters(node);
|
|
784
792
|
const resolveParamsName = (kind, params) => zodResolver && resolveParamsValidator(validator) === "zod" && params.length > 0 ? zodResolver.param[kind](node, params[0]) : null;
|
|
@@ -788,7 +796,7 @@ function buildValidatorHooks({ node, validator, zodResolver }) {
|
|
|
788
796
|
const cookies = resolveParamsName("cookies", paramGroups.cookie);
|
|
789
797
|
const importedZodNames = [
|
|
790
798
|
response,
|
|
791
|
-
|
|
799
|
+
...errorParse?.names ?? [],
|
|
792
800
|
request,
|
|
793
801
|
path,
|
|
794
802
|
query,
|
|
@@ -802,17 +810,17 @@ function buildValidatorHooks({ node, validator, zodResolver }) {
|
|
|
802
810
|
headers: headers ? `toHeadersStandardSchema(${headers}, ${headers}.shape)` : null,
|
|
803
811
|
cookies,
|
|
804
812
|
response,
|
|
805
|
-
error,
|
|
806
|
-
errorStatus,
|
|
813
|
+
error: errorParse?.literal ?? null,
|
|
807
814
|
importedZodNames
|
|
808
815
|
};
|
|
809
816
|
}
|
|
810
817
|
/**
|
|
811
818
|
* Prints the `validator: { … }` entry of a call config from the hooks named in `keys`, or `null` when none is set.
|
|
819
|
+
* The entry spreads `config.validator` last, so a per-call hook overrides the generated one for its key only.
|
|
812
820
|
*/
|
|
813
821
|
function buildValidatorLiteral(hooks, keys) {
|
|
814
822
|
const entries = keys.flatMap((key) => hooks?.[key] ? [`${key}: ${hooks[key]}`] : []);
|
|
815
|
-
return entries.length ? `validator: { ${entries.join(", ")} }` : null;
|
|
823
|
+
return entries.length ? `validator: { ${entries.join(", ")}, ...config.validator }` : null;
|
|
816
824
|
}
|
|
817
825
|
// ../../internals/client/src/builders/contentType.ts
|
|
818
826
|
/**
|
|
@@ -865,13 +873,13 @@ function serializeParameter(parameter) {
|
|
|
865
873
|
return parts.length > 0 ? `{ ${parts.join(", ")} }` : null;
|
|
866
874
|
}
|
|
867
875
|
/**
|
|
868
|
-
* Serializes the `encoding` of the request body's default content type into
|
|
869
|
-
*
|
|
876
|
+
* Serializes the `encoding` of the request body's default content type into `property: { ... }`
|
|
877
|
+
* entries, empty when it carries none. Only an operation with a single request content type
|
|
870
878
|
* emits it, because the runtime applies `styles.body` to whichever type the caller selects.
|
|
871
879
|
*/
|
|
872
880
|
function serializeBodyEncoding(node) {
|
|
873
881
|
const content = node.requestBody?.content;
|
|
874
|
-
|
|
882
|
+
return Object.entries(content?.length === 1 ? content[0]?.encoding ?? {} : {}).map(([property, { contentType, style, explode, allowReserved }]) => {
|
|
875
883
|
const parts = [
|
|
876
884
|
style && `style: ${JSON.stringify(style)}`,
|
|
877
885
|
explode !== void 0 && `explode: ${explode}`,
|
|
@@ -880,7 +888,6 @@ function serializeBodyEncoding(node) {
|
|
|
880
888
|
].filter(Boolean);
|
|
881
889
|
return `${isValidVarName(property) ? property : JSON.stringify(property)}: { ${parts.join(", ")} }`;
|
|
882
890
|
});
|
|
883
|
-
return entries.length > 0 ? `body: { ${entries.join(", ")} }` : null;
|
|
884
891
|
}
|
|
885
892
|
/**
|
|
886
893
|
* Builds the per-operation `styles` literal from the operation's parameters, grouped by location.
|
|
@@ -890,26 +897,31 @@ function serializeBodyEncoding(node) {
|
|
|
890
897
|
* is unchanged. A form request body adds a `body` group keyed by property name from its `encoding`.
|
|
891
898
|
* Returns `null` when nothing carries metadata.
|
|
892
899
|
*
|
|
900
|
+
* With `merge`, the literal spreads `config.styles` per parameter, so a per-call entry overrides the generated one for
|
|
901
|
+
* that parameter and leaves the others in place.
|
|
902
|
+
*
|
|
893
903
|
* @example
|
|
894
904
|
* ```ts
|
|
895
905
|
* // a path param with { style: 'matrix', explode: true } and a query param with { explode: false }
|
|
896
906
|
* buildStyles({ node }) // "{ path: { id: { style: 'matrix', explode: true } }, query: { tags: { explode: false } } }"
|
|
897
907
|
* ```
|
|
898
908
|
*/
|
|
899
|
-
function buildStyles({ node }) {
|
|
909
|
+
function buildStyles({ node, merge }) {
|
|
900
910
|
if (!ast.isHttpOperationNode(node)) return null;
|
|
901
911
|
const groups = {
|
|
902
912
|
path: [],
|
|
903
913
|
query: [],
|
|
904
914
|
header: [],
|
|
905
|
-
cookie: []
|
|
915
|
+
cookie: [],
|
|
916
|
+
body: serializeBodyEncoding(node)
|
|
906
917
|
};
|
|
907
918
|
for (const [location, parameters] of Object.entries(getOperationParameters(node))) for (const parameter of parameters) {
|
|
908
919
|
const literal = serializeParameter(parameter);
|
|
909
920
|
if (literal) groups[location].push(`${toKey(parameter.name)}: ${literal}`);
|
|
910
921
|
}
|
|
911
|
-
const entries =
|
|
912
|
-
|
|
922
|
+
const entries = Object.entries(groups).filter(([, items]) => items.length > 0).map(([location, items]) => `${location}: { ${(merge ? [...items, `...config.styles?.${location}`] : items).join(", ")} }`);
|
|
923
|
+
if (entries.length === 0) return null;
|
|
924
|
+
return `{ ${(merge ? ["...config.styles", ...entries] : entries).join(", ")} }`;
|
|
913
925
|
}
|
|
914
926
|
// ../../internals/client/src/builders/callConfig.ts
|
|
915
927
|
/**
|
|
@@ -925,37 +937,45 @@ function buildResponseTypeLiteral(node) {
|
|
|
925
937
|
}
|
|
926
938
|
/**
|
|
927
939
|
* Builds the call config literal a generated operation forwards to the client, shared by the standalone
|
|
928
|
-
* functions and the SDK methods. The `
|
|
940
|
+
* functions and the SDK methods. The operation's own `baseURL` comes before the `...config` spread, so a per-call one wins. The
|
|
941
|
+
* `validator` and `throwOnError` entries come after it, so a per-call `validator` is merged per key with the generated one instead
|
|
942
|
+
* of replacing it. `styles` is merged the same way, per parameter, unless `returnType` is `'plain'`.
|
|
929
943
|
*
|
|
930
944
|
* `mergeContentType` is `true` when the caller must destructure `contentType` from `options`, and
|
|
931
945
|
* `eventStream` is `true` when the operation streams `text/event-stream` or JSON Lines. Unless `returnType` is `'plain'`, a
|
|
932
946
|
* stream's literal leaves the `response` validator to `toEventStream`, which runs it per item.
|
|
933
947
|
*/
|
|
934
|
-
function buildCallConfig({ node, validatorHooks, security, throwOnErrorDefault, returnType }) {
|
|
948
|
+
function buildCallConfig({ node, validatorHooks, security, baseURL, throwOnErrorDefault, returnType }) {
|
|
935
949
|
const eventStream = isEventStream(node);
|
|
936
950
|
const securityLiteral = buildSecurityMetadata({ security });
|
|
937
|
-
const
|
|
951
|
+
const ownRuntime = returnType !== "plain";
|
|
952
|
+
const stylesLiteral = buildStyles({
|
|
953
|
+
node,
|
|
954
|
+
merge: ownRuntime
|
|
955
|
+
});
|
|
956
|
+
const styles = stylesLiteral ? `styles: ${stylesLiteral}` : null;
|
|
938
957
|
const validatorLiteral = buildValidatorLiteral(validatorHooks, [
|
|
939
958
|
"request",
|
|
940
959
|
"path",
|
|
941
960
|
"query",
|
|
942
961
|
"headers",
|
|
943
962
|
"cookies",
|
|
944
|
-
...eventStream &&
|
|
945
|
-
"error"
|
|
946
|
-
"errorStatus"
|
|
963
|
+
...eventStream && ownRuntime ? [] : ["response"],
|
|
964
|
+
"error"
|
|
947
965
|
]);
|
|
948
966
|
const { literal: contentTypeLiteral, merge: mergeContentType } = buildContentType({ node });
|
|
949
967
|
return {
|
|
950
968
|
callConfig: `{ ${[
|
|
951
969
|
`method: '${node.method.toUpperCase()}'`,
|
|
952
970
|
`url: '${node.path}'`,
|
|
971
|
+
baseURL ? `baseURL: ${JSON.stringify(baseURL)}` : null,
|
|
953
972
|
securityLiteral ? `security: ${securityLiteral}` : null,
|
|
954
|
-
|
|
955
|
-
validatorLiteral,
|
|
973
|
+
ownRuntime ? null : styles,
|
|
956
974
|
contentTypeLiteral,
|
|
957
975
|
buildResponseTypeLiteral(node),
|
|
958
976
|
"...config",
|
|
977
|
+
ownRuntime ? styles : null,
|
|
978
|
+
validatorLiteral,
|
|
959
979
|
`throwOnError: config.throwOnError ?? ${throwOnErrorDefault}`
|
|
960
980
|
].filter(Boolean).join(", ")} }`,
|
|
961
981
|
mergeContentType,
|
|
@@ -997,11 +1017,12 @@ function buildGroupedOptionsSignature({ node, types, returnType, throwOnErrorDef
|
|
|
997
1017
|
* body destructures `contentType` from `options`. Shared by the standalone functions and the SDK methods, so a
|
|
998
1018
|
* `text/event-stream` operation is wired to `toEventStream` in one place.
|
|
999
1019
|
*/
|
|
1000
|
-
function buildOperationBody({ node, types, validatorHooks, security, returnType, throwOnErrorDefault }) {
|
|
1020
|
+
function buildOperationBody({ node, types, validatorHooks, security, baseURL, returnType, throwOnErrorDefault }) {
|
|
1001
1021
|
const { callConfig, mergeContentType, eventStream } = buildCallConfig({
|
|
1002
1022
|
node,
|
|
1003
1023
|
validatorHooks,
|
|
1004
1024
|
security,
|
|
1025
|
+
baseURL,
|
|
1005
1026
|
throwOnErrorDefault,
|
|
1006
1027
|
returnType
|
|
1007
1028
|
});
|
|
@@ -1041,13 +1062,14 @@ function buildOperationBody({ node, types, validatorHooks, security, returnType,
|
|
|
1041
1062
|
* type, signature, and call config are built with the AST factory, and only the jsx-renderer emits
|
|
1042
1063
|
* the source.
|
|
1043
1064
|
*/
|
|
1044
|
-
function Operation({ name, node, types, validatorHooks, returnType, throwOnErrorDefault, security }) {
|
|
1065
|
+
function Operation({ name, node, types, validatorHooks, returnType, throwOnErrorDefault, security, baseURL }) {
|
|
1045
1066
|
if (!ast.isHttpOperationNode(node)) return null;
|
|
1046
1067
|
const body = buildOperationBody({
|
|
1047
1068
|
node,
|
|
1048
1069
|
types,
|
|
1049
1070
|
validatorHooks,
|
|
1050
1071
|
security,
|
|
1072
|
+
baseURL,
|
|
1051
1073
|
returnType,
|
|
1052
1074
|
throwOnErrorDefault
|
|
1053
1075
|
});
|
|
@@ -1246,6 +1268,12 @@ function createClientGenerator(name, { sse } = {}) {
|
|
|
1246
1268
|
method: node.method,
|
|
1247
1269
|
path: node.path
|
|
1248
1270
|
});
|
|
1271
|
+
const baseURL = getOperationBaseURL({
|
|
1272
|
+
document: ctx.adapter.document,
|
|
1273
|
+
method: node.method,
|
|
1274
|
+
path: node.path,
|
|
1275
|
+
server: ctx.adapter.options?.server
|
|
1276
|
+
});
|
|
1249
1277
|
const clientPath = importPath ?? path.resolve(root, ".kubb/client.ts");
|
|
1250
1278
|
const clientRoot = importPath === void 0 ? meta.file.path : void 0;
|
|
1251
1279
|
const standardSchemaPath = importPath ?? path.resolve(root, ".kubb/standardSchema.ts");
|
|
@@ -1340,7 +1368,8 @@ function createClientGenerator(name, { sse } = {}) {
|
|
|
1340
1368
|
validatorHooks,
|
|
1341
1369
|
returnType,
|
|
1342
1370
|
throwOnErrorDefault,
|
|
1343
|
-
security
|
|
1371
|
+
security,
|
|
1372
|
+
baseURL
|
|
1344
1373
|
})
|
|
1345
1374
|
]
|
|
1346
1375
|
});
|