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

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 CHANGED
@@ -161,6 +161,22 @@ const reservedWords = /* @__PURE__ */ new Set([
161
161
  */
162
162
  function isValidVarName(name) {
163
163
  if (!name || reservedWords.has(name)) return false;
164
+ return isIdentifier(name);
165
+ }
166
+ /**
167
+ * Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
168
+ *
169
+ * Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
170
+ * even though they are not valid variable names, so use this (not {@link isValidVarName}) when
171
+ * deciding whether an object key needs quoting.
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * isIdentifier('name') // true
176
+ * isIdentifier('x-total')// false
177
+ * ```
178
+ */
179
+ function isIdentifier(name) {
164
180
  return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
165
181
  }
166
182
  //#endregion
@@ -339,8 +355,11 @@ function getOperationLink(node, link) {
339
355
  if (link === "urlPath") return node.path ? `{@link ${Url.toPath(node.path)}}` : null;
340
356
  return node.path ? `{@link ${node.path.replaceAll("{", ":").replaceAll("}", "")}}` : null;
341
357
  }
342
- function getContentTypeInfo(node) {
343
- const contentTypes = node.requestBody?.content?.map((e) => e.contentType) ?? [];
358
+ /**
359
+ * Derives the shared `ContentTypeInfo` shape from a list of content types, tracking whether several
360
+ * are present and the union, default, and form-data flags the client uses to pick one.
361
+ */
362
+ function buildContentTypeInfo(contentTypes) {
344
363
  const isMultipleContentTypes = contentTypes.length > 1;
345
364
  return {
346
365
  contentTypes,
@@ -350,20 +369,15 @@ function getContentTypeInfo(node) {
350
369
  hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
351
370
  };
352
371
  }
372
+ function getContentTypeInfo(node) {
373
+ return buildContentTypeInfo(node.requestBody?.content?.map((e) => e.contentType) ?? []);
374
+ }
353
375
  /**
354
376
  * The request-body counterpart for the primary success response: the content types it documents and
355
377
  * whether several are present, so the client can let a caller pick which one to accept.
356
378
  */
357
379
  function getResponseContentTypeInfo(node) {
358
- const contentTypes = getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? [];
359
- const isMultipleContentTypes = contentTypes.length > 1;
360
- return {
361
- contentTypes,
362
- isMultipleContentTypes,
363
- contentTypeUnion: isMultipleContentTypes ? contentTypes.map((ct) => JSON.stringify(ct)).join(" | ") : "",
364
- defaultContentType: contentTypes[0] ?? "application/json",
365
- hasFormData: contentTypes.some((ct) => ct === "multipart/form-data")
366
- };
380
+ return buildContentTypeInfo(getPrimarySuccessResponse(node)?.content?.map((e) => e.contentType) ?? []);
367
381
  }
368
382
  function buildRequestConfigType(node) {
369
383
  const request = getContentTypeInfo(node);
@@ -903,40 +917,8 @@ function MutationKey({ name, typeName, node, transformer }) {
903
917
  })] });
904
918
  }
905
919
  //#endregion
906
- //#region src/components/QueryOptions.tsx
907
- const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
908
- function getQueryOptionsParams(node, options) {
909
- return buildQueryOptionsParams(node, options);
910
- }
911
- function QueryOptions({ name, clientName, node, tsResolver }) {
912
- const paramsNode = getQueryOptionsParams(node, { resolver: tsResolver });
913
- const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
914
- const fetcherBody = `const { data } = await ${buildClientCall(node, {
915
- clientName,
916
- signal: false
917
- })}
918
- return data`;
919
- return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
920
- name,
921
- isExportable: true,
922
- isIndexable: true,
923
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
924
- name,
925
- export: true,
926
- params: paramsSignature,
927
- children: `
928
- return {
929
- fetcher: async () => {
930
- ${fetcherBody}
931
- },
932
- }
933
- `
934
- })
935
- });
936
- }
937
- //#endregion
938
920
  //#region src/components/Query.tsx
939
- const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
921
+ const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
940
922
  const callPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "call" });
941
923
  function buildQueryParamsNode(node, options) {
942
924
  const { resolver } = options;
@@ -964,10 +946,10 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
964
946
  ];
965
947
  const queryKeyParamsNode = buildQueryKeyParams(node, { resolver: tsResolver });
966
948
  const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? "";
967
- const queryOptionsParamsNode = getQueryOptionsParams(node, { resolver: tsResolver });
949
+ const queryOptionsParamsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
968
950
  const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? "";
969
951
  const paramsNode = buildQueryParamsNode(node, { resolver: tsResolver });
970
- const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
952
+ const paramsSignature = declarationPrinter$1.print(paramsNode) ?? "";
971
953
  return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
972
954
  name,
973
955
  isExportable: true,
@@ -999,6 +981,35 @@ function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, node, t
999
981
  });
1000
982
  }
1001
983
  //#endregion
984
+ //#region src/components/QueryOptions.tsx
985
+ const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
986
+ function QueryOptions({ name, clientName, node, tsResolver }) {
987
+ const paramsNode = buildQueryOptionsParams(node, { resolver: tsResolver });
988
+ const paramsSignature = declarationPrinter.print(paramsNode) ?? "";
989
+ const fetcherBody = `const { data } = await ${buildClientCall(node, {
990
+ clientName,
991
+ signal: false
992
+ })}
993
+ return data`;
994
+ return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
995
+ name,
996
+ isExportable: true,
997
+ isIndexable: true,
998
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
999
+ name,
1000
+ export: true,
1001
+ params: paramsSignature,
1002
+ children: `
1003
+ return {
1004
+ fetcher: async () => {
1005
+ ${fetcherBody}
1006
+ },
1007
+ }
1008
+ `
1009
+ })
1010
+ });
1011
+ }
1012
+ //#endregion
1002
1013
  //#region src/generators/mutationGenerator.tsx
1003
1014
  /**
1004
1015
  * Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
@@ -1136,14 +1147,11 @@ const queryGenerator = (0, kubb_kit.defineGenerator)({
1136
1147
  operation(node, ctx) {
1137
1148
  if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
1138
1149
  const { config, driver, resolver, root } = ctx;
1139
- const { output, query, mutation, client, group } = ctx.options;
1150
+ const { output, query, client, group } = ctx.options;
1140
1151
  const pluginTs = driver.getPlugin(_kubb_plugin_ts.pluginTsName);
1141
1152
  if (!pluginTs) return null;
1142
1153
  const tsResolver = driver.getResolver(_kubb_plugin_ts.pluginTsName);
1143
- const isQuery = query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase());
1144
- const queryMethods = new Set(query ? query.methods : []);
1145
- const isMutation = mutation !== false && !isQuery && (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase());
1146
- if (!isQuery || isMutation) return null;
1154
+ if (!(query === false || !!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))) return null;
1147
1155
  const importPath = query ? query.importPath : "swr";
1148
1156
  const contractOp = resolveClientOperation({
1149
1157
  clientPlugin: { pluginName: client.pluginName },
@@ -1345,7 +1353,7 @@ const pluginSwr = (0, kubb_kit.definePlugin)((options) => {
1345
1353
  path: "hooks",
1346
1354
  barrel: { type: "named" }
1347
1355
  }, group, exclude = [], include, override = [], mutation = {}, query = {}, mutationKey = mutationKeyTransformer, queryKey = queryKeyTransformer, client, resolver: userResolver, macros: userMacros } = options;
1348
- const selectedGenerators = [queryGenerator, mutationGenerator].filter((generator) => Boolean(generator));
1356
+ const selectedGenerators = [queryGenerator, mutationGenerator];
1349
1357
  const groupConfig = createGroupConfig(group) ?? void 0;
1350
1358
  return {
1351
1359
  name: pluginSwrName,