@kubb/plugin-fetch 5.0.0-beta.81 → 5.0.0-beta.85
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 +115 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +80 -18
- package/dist/index.js.map +1 -1
- package/package.json +5 -7
- package/src/generators/clientGenerator.tsx +2 -2
- package/src/plugin.ts +2 -2
- package/src/types.ts +1 -1
- package/templates/fetch.ts +20 -10
- package/templates/serializers.ts +4 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
|
|
2
|
-
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "
|
|
2
|
+
import { Exclude, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "kubb/kit";
|
|
3
3
|
//#region ../../internals/client/src/types.d.ts
|
|
4
4
|
/**
|
|
5
5
|
* Validator applied to request and response bodies using schemas from `@kubb/plugin-zod`.
|
|
@@ -179,7 +179,7 @@ declare global {
|
|
|
179
179
|
* operation using the shared `Operation` component: a grouped `<Name>Request` type and a function that
|
|
180
180
|
* forwards a single `options` object to the bundled `client` and returns the `RequestResult`.
|
|
181
181
|
*/
|
|
182
|
-
declare const clientGenerator: import("
|
|
182
|
+
declare const clientGenerator: import("kubb/kit").Generator<PluginFetch, unknown>;
|
|
183
183
|
//#endregion
|
|
184
184
|
//#region src/plugin.d.ts
|
|
185
185
|
/**
|
|
@@ -195,7 +195,7 @@ declare const pluginFetchName = "plugin-fetch";
|
|
|
195
195
|
*
|
|
196
196
|
* @example
|
|
197
197
|
* ```ts
|
|
198
|
-
* import { defineConfig } from 'kubb'
|
|
198
|
+
* import { defineConfig } from 'kubb/config'
|
|
199
199
|
* import { pluginTs } from '@kubb/plugin-ts'
|
|
200
200
|
* import { pluginFetch } from '@kubb/plugin-fetch'
|
|
201
201
|
*
|
|
@@ -209,7 +209,7 @@ declare const pluginFetchName = "plugin-fetch";
|
|
|
209
209
|
* })
|
|
210
210
|
* ```
|
|
211
211
|
*/
|
|
212
|
-
declare const pluginFetch: (options?: Options | undefined) => import("
|
|
212
|
+
declare const pluginFetch: (options?: Options | undefined) => import("kubb/kit").Plugin<PluginFetch>;
|
|
213
213
|
//#endregion
|
|
214
214
|
export { type Options, type PluginFetch, type ResolvedOptions, type ResolverClient, clientGenerator, pluginFetch as default, pluginFetch, pluginFetchName };
|
|
215
215
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import "./rolldown-runtime-C0LytTxp.js";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { ast, defineGenerator, definePlugin, defineResolver } from "
|
|
4
|
-
import { File, Function, jsxRenderer } from "
|
|
3
|
+
import { ast, defineGenerator, definePlugin, defineResolver } from "kubb/kit";
|
|
4
|
+
import { File, Function, jsxRenderer } from "kubb/jsx";
|
|
5
5
|
import { createFunctionParameter, createFunctionParameters, functionPrinter, pluginTsName } from "@kubb/plugin-ts";
|
|
6
|
-
import { Fragment, jsx, jsxs } from "
|
|
6
|
+
import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
|
|
7
7
|
import { pluginZodName } from "@kubb/plugin-zod";
|
|
8
|
-
import { buildJSDoc } from "@kubb/ast/utils";
|
|
9
|
-
import { macroSimplifyUnion } from "@kubb/ast/macros";
|
|
10
8
|
import { fileURLToPath } from "node:url";
|
|
11
9
|
//#region ../../internals/utils/src/casing.ts
|
|
12
10
|
/**
|
|
@@ -343,6 +341,34 @@ function dedupeByCasedName(params) {
|
|
|
343
341
|
return true;
|
|
344
342
|
});
|
|
345
343
|
}
|
|
344
|
+
function buildParamsMapping(originalParams, mappedParams) {
|
|
345
|
+
const mapping = {};
|
|
346
|
+
let hasChanged = false;
|
|
347
|
+
originalParams.forEach((param, i) => {
|
|
348
|
+
const mappedName = mappedParams[i]?.name ?? param.name;
|
|
349
|
+
mapping[param.name] = mappedName;
|
|
350
|
+
if (param.name !== mappedName) hasChanged = true;
|
|
351
|
+
});
|
|
352
|
+
return hasChanged ? mapping : null;
|
|
353
|
+
}
|
|
354
|
+
function toAccess(object, name) {
|
|
355
|
+
return isValidVarName(name) ? `${object}.${name}` : `${object}[${JSON.stringify(name)}]`;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Renders the object-literal expression that renames the camelCased keys of a grouped request
|
|
359
|
+
* option back to the names the OpenAPI document declares, guarded so an omitted optional group
|
|
360
|
+
* stays omitted. Shared by the client and cypress generators, which pass a `buildParamsMapping`
|
|
361
|
+
* result and the source expression to read the keys from.
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* buildParamsRemapExpression({ source: 'config.query', mapping: { include_deleted: 'includeDeleted' } })
|
|
366
|
+
* // 'config.query ? { "include_deleted": config.query.includeDeleted } : config.query'
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
function buildParamsRemapExpression({ source, mapping }) {
|
|
370
|
+
return `${source} ? { ${Object.entries(mapping).map(([originalName, casedName]) => `${JSON.stringify(originalName)}: ${toAccess(source, casedName)}`).join(", ")} } : ${source}`;
|
|
371
|
+
}
|
|
346
372
|
//#endregion
|
|
347
373
|
//#region ../../internals/shared/src/operation.ts
|
|
348
374
|
/**
|
|
@@ -664,6 +690,38 @@ function buildSecurityMetadata({ security }) {
|
|
|
664
690
|
return `[${security.map(serializeAuth).join(", ")}]`;
|
|
665
691
|
}
|
|
666
692
|
//#endregion
|
|
693
|
+
//#region ../../internals/client/src/builders/paramsRemap.ts
|
|
694
|
+
/**
|
|
695
|
+
* Builds the call-config entries that rename the camelCased `query` and `headers` keys back to the
|
|
696
|
+
* names the OpenAPI document declares, so the wire format follows the spec while the generated
|
|
697
|
+
* types keep camelCase keys. Returns an empty array when no name changes. Path parameters need no
|
|
698
|
+
* remap because the URL template placeholders are renamed in sync with the `path` keys. Emit the
|
|
699
|
+
* entries after the `...config` spread so they override the camelCased groups the caller passes in.
|
|
700
|
+
*
|
|
701
|
+
* @example
|
|
702
|
+
* ```ts
|
|
703
|
+
* // a query param named include_deleted in the spec
|
|
704
|
+
* buildParamsRemap({ node }) // ['query: config.query ? { "include_deleted": config.query.includeDeleted } : config.query']
|
|
705
|
+
* ```
|
|
706
|
+
*/
|
|
707
|
+
function buildParamsRemap({ node }) {
|
|
708
|
+
if (!ast.isHttpOperationNode(node)) return [];
|
|
709
|
+
const original = getOperationParameters(node, { paramsCasing: "original" });
|
|
710
|
+
const cased = getOperationParameters(node);
|
|
711
|
+
const queryMapping = buildParamsMapping(original.query, cased.query);
|
|
712
|
+
const headerMapping = buildParamsMapping(original.header, cased.header);
|
|
713
|
+
const entries = [];
|
|
714
|
+
if (queryMapping) entries.push(`query: ${buildParamsRemapExpression({
|
|
715
|
+
source: "config.query",
|
|
716
|
+
mapping: queryMapping
|
|
717
|
+
})}`);
|
|
718
|
+
if (headerMapping) entries.push(`headers: ${buildParamsRemapExpression({
|
|
719
|
+
source: "config.headers",
|
|
720
|
+
mapping: headerMapping
|
|
721
|
+
})}`);
|
|
722
|
+
return entries;
|
|
723
|
+
}
|
|
724
|
+
//#endregion
|
|
667
725
|
//#region ../../internals/client/src/builders/generics.ts
|
|
668
726
|
/**
|
|
669
727
|
* Builds the `RequestResult` generic arguments for one operation: the plugin-ts per-status responses
|
|
@@ -726,12 +784,13 @@ function buildGroupedOptionsSignature({ node, tsResolver }) {
|
|
|
726
784
|
//#endregion
|
|
727
785
|
//#region ../../internals/client/src/builders/styles.ts
|
|
728
786
|
/**
|
|
729
|
-
* Renders a parameter name as an object-literal key,
|
|
730
|
-
*
|
|
787
|
+
* Renders a parameter name as an object-literal key, quoted when it is not a bare identifier.
|
|
788
|
+
* Path keys are camelCased to match the URL template placeholders. Query, header, and cookie keys
|
|
789
|
+
* keep the spec name, matching the remapped keys the runtime serializes.
|
|
731
790
|
*/
|
|
732
|
-
function toKey(name) {
|
|
733
|
-
const
|
|
734
|
-
return isValidVarName(
|
|
791
|
+
function toKey(name, location) {
|
|
792
|
+
const key = location === "path" ? camelCase(name) : name;
|
|
793
|
+
return isValidVarName(key) ? key : JSON.stringify(key);
|
|
735
794
|
}
|
|
736
795
|
/**
|
|
737
796
|
* Serializes one parameter's metadata into a `{ style, explode }` literal, or `null` when the
|
|
@@ -745,8 +804,9 @@ function serializeParameter(parameter) {
|
|
|
745
804
|
return parts.length > 0 ? `{ ${parts.join(", ")} }` : null;
|
|
746
805
|
}
|
|
747
806
|
/**
|
|
748
|
-
* Builds the per-operation `styles` literal from the operation's parameters, grouped by location
|
|
749
|
-
* keyed by the camelCased
|
|
807
|
+
* Builds the per-operation `styles` literal from the operation's parameters, grouped by location.
|
|
808
|
+
* Path entries are keyed by the camelCased name to match the URL template placeholders; query,
|
|
809
|
+
* header, and cookie entries keep the spec name to match the keys the runtime serializes.
|
|
750
810
|
* Only parameters whose source defines `style` or `explode` are emitted, so calls without
|
|
751
811
|
* serialization metadata keep the runtime defaults and existing output is unchanged. Returns `null`
|
|
752
812
|
* when no parameter carries metadata.
|
|
@@ -768,7 +828,7 @@ function buildStyles({ node }) {
|
|
|
768
828
|
for (const parameter of node.parameters) {
|
|
769
829
|
const literal = serializeParameter(parameter);
|
|
770
830
|
if (!literal) continue;
|
|
771
|
-
groups[parameter.in].push(`${toKey(parameter.name)}: ${literal}`);
|
|
831
|
+
groups[parameter.in].push(`${toKey(parameter.name, parameter.in)}: ${literal}`);
|
|
772
832
|
}
|
|
773
833
|
const locations = Object.keys(groups).filter((location) => groups[location].length > 0);
|
|
774
834
|
if (locations.length === 0) return null;
|
|
@@ -841,7 +901,8 @@ function Operation({ name, node, tsResolver, zodResolver, validator, security, i
|
|
|
841
901
|
validatorLiteral,
|
|
842
902
|
contentTypeLiteral,
|
|
843
903
|
responseTypeLiteral,
|
|
844
|
-
"...config"
|
|
904
|
+
"...config",
|
|
905
|
+
...buildParamsRemap({ node })
|
|
845
906
|
].filter(Boolean).join(", ")} }`;
|
|
846
907
|
const eventType = `SuccessOf<${tsResolver.resolveResponsesName(node)}>`;
|
|
847
908
|
const returnType = eventStream ? `Promise<EventStreamResult<${eventType}>>` : signature.returnType;
|
|
@@ -894,7 +955,8 @@ function buildCallConfig({ node, validator, zodResolver, security }) {
|
|
|
894
955
|
`url: '${Url.toCasedTemplate(node.path, { casing: "camelcase" })}'`,
|
|
895
956
|
securityLiteral ? `security: ${securityLiteral}` : null,
|
|
896
957
|
validatorLiteral,
|
|
897
|
-
"...config"
|
|
958
|
+
"...config",
|
|
959
|
+
...buildParamsRemap({ node })
|
|
898
960
|
].filter(Boolean).join(", ")} }`;
|
|
899
961
|
}
|
|
900
962
|
/**
|
|
@@ -920,7 +982,7 @@ function buildSdkMethod({ node, name, tsResolver, zodResolver, validator, securi
|
|
|
920
982
|
})
|
|
921
983
|
});
|
|
922
984
|
const generics = signature.generics.length ? `<${signature.generics.join(", ")}>` : "";
|
|
923
|
-
const jsdoc = buildJSDoc(buildOperationComments(node, {
|
|
985
|
+
const jsdoc = ast.buildJSDoc(buildOperationComments(node, {
|
|
924
986
|
link: "urlPath",
|
|
925
987
|
linkPosition: "beforeDeprecated",
|
|
926
988
|
splitLines: true
|
|
@@ -1234,7 +1296,7 @@ function createSdkGenerator() {
|
|
|
1234
1296
|
* drops union members a broader scalar already covers, keeping the generated response and error
|
|
1235
1297
|
* unions tidy. A plugin wires them with `ctx.setMacros([...defaultMacros, ...userMacros])`.
|
|
1236
1298
|
*/
|
|
1237
|
-
const defaultMacros = [macroSimplifyUnion];
|
|
1299
|
+
const defaultMacros = [ast.macroSimplifyUnion];
|
|
1238
1300
|
//#endregion
|
|
1239
1301
|
//#region ../../internals/client/src/resolver.ts
|
|
1240
1302
|
/**
|
|
@@ -1406,7 +1468,7 @@ const pluginFetchName = "plugin-fetch";
|
|
|
1406
1468
|
*
|
|
1407
1469
|
* @example
|
|
1408
1470
|
* ```ts
|
|
1409
|
-
* import { defineConfig } from 'kubb'
|
|
1471
|
+
* import { defineConfig } from 'kubb/config'
|
|
1410
1472
|
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1411
1473
|
* import { pluginFetch } from '@kubb/plugin-fetch'
|
|
1412
1474
|
*
|