@kubb/plugin-swr 5.0.0-beta.86 → 5.0.0-beta.94

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.js CHANGED
@@ -131,6 +131,22 @@ const reservedWords = /* @__PURE__ */ new Set([
131
131
  */
132
132
  function isValidVarName(name) {
133
133
  if (!name || reservedWords.has(name)) return false;
134
+ return isIdentifier(name);
135
+ }
136
+ /**
137
+ * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
138
+ *
139
+ * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
140
+ * even though they are not valid variable names, so use this (not {@link isValidVarName}) when
141
+ * deciding whether an object key needs quoting.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * isIdentifier('name') // true
146
+ * isIdentifier('x-total')// false
147
+ * ```
148
+ */
149
+ function isIdentifier(name) {
134
150
  return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
135
151
  }
136
152
  //#endregion
@@ -309,8 +325,11 @@ function getOperationLink(node, link) {
309
325
  if (link === "urlPath") return node.path ? `{@link ${Url.toPath(node.path)}}` : null;
310
326
  return node.path ? `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}` : null;
311
327
  }
312
- function getContentTypeInfo(node) {
313
- const contentTypes = node.requestBody?.content?.map((e) => e.contentType) ?? [];
328
+ /**
329
+ * Derives the shared `ContentTypeInfo` shape from a list of content types, tracking whether several
330
+ * are present and the union, default, and form-data flags the client uses to pick one.
331
+ */
332
+ function buildContentTypeInfo(contentTypes) {
314
333
  const isMultipleContentTypes = contentTypes.length > 1;
315
334
  return {
316
335
  contentTypes,
@@ -320,20 +339,15 @@ function getContentTypeInfo(node) {
320
339
  hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
321
340
  };
322
341
  }
342
+ function getContentTypeInfo(node) {
343
+ return buildContentTypeInfo(node.requestBody?.content?.map((e) => e.contentType) ?? []);
344
+ }
323
345
  /**
324
346
  * The request-body counterpart for the primary success response: the content types it documents and
325
347
  * whether several are present, so the client can let a caller pick which one to accept.
326
348
  */
327
349
  function getResponseContentTypeInfo(node) {
328
- const contentTypes = getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? [];
329
- const isMultipleContentTypes = contentTypes.length > 1;
330
- return {
331
- contentTypes,
332
- isMultipleContentTypes,
333
- contentTypeUnion: isMultipleContentTypes ? contentTypes.map((ct) => JSON.stringify(ct)).join(" | ") : "",
334
- defaultContentType: contentTypes[0] ?? "application/json",
335
- hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
336
- };
350
+ return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
337
351
  }
338
352
  function buildRequestConfigType(node) {
339
353
  const request = getContentTypeInfo(node);
@@ -873,40 +887,8 @@ function MutationKey({ name, typeName, node, transformer }) {
873
887
  })] });
874
888
  }
875
889
  //#endregion
876
- //#region src/components/QueryOptions.tsx
877
- const declarationPrinter$1 = functionPrinter({ mode: "declaration" });
878
- function getQueryOptionsParams(node, options) {
879
- return buildQueryOptionsParams(node, options);
880
- }
881
- function QueryOptions({ name, clientName, node, tsResolver }) {
882
- const paramsNode = getQueryOptionsParams(node, { resolver: tsResolver });
883
- const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
884
- const fetcherBody = `const { data } = await ${buildClientCall(node, {
885
- clientName,
886
- signal: false
887
- })}
888
- return data`;
889
- return /* @__PURE__ */ jsx(File.Source, {
890
- name,
891
- isExportable: true,
892
- isIndexable: true,
893
- children: /* @__PURE__ */ jsx(Function, {
894
- name,
895
- export: true,
896
- params: paramsSignature,
897
- children: `
898
- return {
899
- fetcher: async () => {
900
- ${fetcherBody}
901
- },
902
- }
903
- `
904
- })
905
- });
906
- }
907
- //#endregion
908
890
  //#region src/components/Query.tsx
909
- const declarationPrinter = functionPrinter({ mode: "declaration" });
891
+ const declarationPrinter$1 = functionPrinter({ mode: "declaration" });
910
892
  const callPrinter = functionPrinter({ mode: "call" });
911
893
  function buildQueryParamsNode(node, options) {
912
894
  const { resolver } = options;
@@ -934,10 +916,10 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
934
916
  ];
935
917
  const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
936
918
  const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? "";
937
- const queryOptionsParamsNode = getQueryOptionsParams(node, { resolver: tsResolver });
919
+ const queryOptionsParamsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
938
920
  const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? "";
939
921
  const paramsNode = buildQueryParamsNode(node, { resolver: tsResolver });
940
- const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
922
+ const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
941
923
  return /* @__PURE__ */ jsx(File.Source, {
942
924
  name,
943
925
  isExportable: true,
@@ -969,6 +951,35 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
969
951
  });
970
952
  }
971
953
  //#endregion
954
+ //#region src/components/QueryOptions.tsx
955
+ const declarationPrinter = functionPrinter({ mode: "declaration" });
956
+ function QueryOptions({ name, clientName, node, tsResolver }) {
957
+ const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
958
+ const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
959
+ const fetcherBody = `const { data } = await ${buildClientCall(node, {
960
+ clientName,
961
+ signal: false
962
+ })}
963
+ return data`;
964
+ return /* @__PURE__ */ jsx(File.Source, {
965
+ name,
966
+ isExportable: true,
967
+ isIndexable: true,
968
+ children: /* @__PURE__ */ jsx(Function, {
969
+ name,
970
+ export: true,
971
+ params: paramsSignature,
972
+ children: `
973
+ return {
974
+ fetcher: async () => {
975
+ ${fetcherBody}
976
+ },
977
+ }
978
+ `
979
+ })
980
+ });
981
+ }
982
+ //#endregion
972
983
  //#region src/generators/mutationGenerator.tsx
973
984
  /**
974
985
  * Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
@@ -1106,14 +1117,11 @@ const queryGenerator = defineGenerator({
1106
1117
  operation(node, ctx) {
1107
1118
  if (!ast.isHttpOperationNode(node)) return null;
1108
1119
  const { config, driver, resolver, root } = ctx;
1109
- const { output, query, mutation, client, group } = ctx.options;
1120
+ const { output, query, client, group } = ctx.options;
1110
1121
  const pluginTs = driver.getPlugin(pluginTsName);
1111
1122
  if (!pluginTs) return null;
1112
1123
  const tsResolver = driver.getResolver(pluginTsName);
1113
- const isQuery = query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
1114
- const queryMethods = new Set(query ? query.methods : []);
1115
- const isMutation = mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase());
1116
- if (!isQuery || isMutation) return null;
1124
+ if (!(query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))) return null;
1117
1125
  const importPath = query ? query.importPath : "swr";
1118
1126
  const contractOp = resolveClientOperation({
1119
1127
  clientPlugin: { pluginName: client.pluginName },
@@ -1301,7 +1309,7 @@ const pluginSwrName = "plugin-swr";
1301
1309
  * import { pluginSwr } from '@kubb/plugin-swr'
1302
1310
  *
1303
1311
  * export default defineConfig({
1304
- * input: { path: './petStore.yaml' },
1312
+ * input: './petStore.yaml',
1305
1313
  * output: { path: './src/gen' },
1306
1314
  * plugins: [
1307
1315
  * pluginTs(),
@@ -1315,7 +1323,7 @@ const pluginSwr = definePlugin((options) => {
1315
1323
  path: "hooks",
1316
1324
  barrel: { type: "named" }
1317
1325
  }, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, resolver: userResolver, macros: userMacros } = options;
1318
- const selectedGenerators = [queryGenerator, mutationGenerator].filter((generator) => Boolean(generator));
1326
+ const selectedGenerators = [queryGenerator, mutationGenerator];
1319
1327
  const groupConfig = createGroupConfig(group) ?? void 0;
1320
1328
  return {
1321
1329
  name: pluginSwrName,