swaggie 2.1.1 → 2.1.3

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/README.md CHANGED
@@ -124,7 +124,7 @@ swaggie -c swaggie.config.json
124
124
 
125
125
  ```json
126
126
  {
127
- "$schema": "https://raw.githubusercontent.com/yhnavein/swaggie/master/schema.json",
127
+ "$schema": "https://yhnavein.github.io/swaggie/schema.json",
128
128
  "src": "https://petstore3.swagger.io/api/v3/openapi.json",
129
129
  "out": "./src/client/petstore.ts",
130
130
  "template": "axios",
@@ -216,9 +216,17 @@ function prepareClient(
216
216
  return null;
217
217
  }
218
218
 
219
+ const camelCaseName = _case.camel.call(void 0, name);
220
+ // `default` is a JS reserved word — it is valid in `defaultClient` (HTTP
221
+ // client export) but not as a standalone `export const default = {}` used by
222
+ // the reactive hooks templates. When the camel-cased name would be `default`,
223
+ // we use `main` for the hooks namespace instead.
224
+ const hooksCamelCaseName = camelCaseName === 'default' ? 'main' : camelCaseName;
225
+
219
226
  return {
220
227
  clientName: name,
221
- camelCaseName: _case.camel.call(void 0, name),
228
+ camelCaseName,
229
+ hooksCamelCaseName,
222
230
  operations: preparedOperations,
223
231
  baseUrl: options.baseUrl,
224
232
  };
@@ -615,23 +623,36 @@ const PRIMITIVES =
615
623
  * and inline object types (including PascalCase names used as property value types
616
624
  * inside `{ key: SomeType }` shapes).
617
625
  *
618
- * @example prefixApiType('Pet') 'API.Pet'
619
- * @example prefixApiType('Pet[]') → 'API.Pet[]'
620
- * @example prefixApiType('Pet | null') → 'API.Pet | null'
621
- * @example prefixApiType('unknown') → 'unknown'
622
- * @example prefixApiType('{ id: number }') → '{ id: number }'
623
- * @example prefixApiType('{ profile: MyEnum; }') → '{ profile: API.MyEnum; }'
624
- * @example prefixApiType('API.Pet') → 'API.Pet' (API itself is in the exclude list)
626
+ * Quoted string literals (single or double) are left entirely untouched so that
627
+ * enum-derived types like `"AZURE" | "AWS"` are never incorrectly rewritten to
628
+ * `"API.AZURE" | "API.AWS"`.
629
+ *
630
+ * @example prefixApiType('Pet') → 'API.Pet'
631
+ * @example prefixApiType('Pet[]') → 'API.Pet[]'
632
+ * @example prefixApiType('Pet | null') → 'API.Pet | null'
633
+ * @example prefixApiType('unknown') → 'unknown'
634
+ * @example prefixApiType('{ id: number }') → '{ id: number }'
635
+ * @example prefixApiType('{ profile: MyEnum; }') → '{ profile: API.MyEnum; }'
636
+ * @example prefixApiType('API.Pet') → 'API.Pet'
637
+ * @example prefixApiType('"AZURE" | "AWS"') → '"AZURE" | "AWS"'
638
+ * @example prefixApiType('"available" | Pet') → '"available" | API.Pet'
625
639
  */
626
640
  function prefixApiType(typeStr) {
627
641
  if (!typeStr) {
628
642
  return typeStr;
629
643
  }
630
- // The negative lookbehind `(?<!\.)` skips identifiers that are already part
631
- // of a namespace reference (e.g. `API.Pet` when the regex reaches `Pet` it
632
- // is preceded by `.`, so we leave it alone).
633
- return typeStr.replace(/(?<!\.)\b([A-Z][A-Za-z0-9_]*)\b/g, (match) =>
634
- PRIMITIVES.test(match) ? match : `API.${match}`
644
+ // The alternation has two branches:
645
+ // Group 1: a full quoted string literal ("..." or '...') returned as-is so
646
+ // that enum-derived values like "AZURE" are never prefixed.
647
+ // Group 2: an uppercase-starting identifier that is NOT already preceded by a
648
+ // dot (negative lookbehind) prefixed with API. unless it is a known
649
+ // primitive / built-in.
650
+ return typeStr.replace(
651
+ /("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')|(?<!\.)\b([A-Z][A-Za-z0-9_]*)\b/g,
652
+ (match, quotedLiteral, ident) => {
653
+ if (quotedLiteral !== undefined) return quotedLiteral;
654
+ return PRIMITIVES.test(ident) ? ident : `API.${ident}`;
655
+ },
635
656
  );
636
657
  } exports.prefixApiType = prefixApiType;
637
658
 
@@ -38,15 +38,15 @@
38
38
  },
39
39
  "swr": {
40
40
  "baseClient.ejs": "import useSWR, { type SWRConfiguration, type Key } from 'swr';\nimport useSWRMutation, { type SWRMutationConfiguration } from 'swr/mutation';\n\ninterface SwrConfig extends SWRConfiguration {\n /* Custom key for SWR. You don't have to worry about this as by default it's the URL. You can use standard SWR Key here if you need more flexibility. */\n key?: Key;\n}\n",
41
- "client.ejs": "export const <%= it.camelCaseName -%>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n <% }); %>\n};\n\n<% if (!it.splitMode) { %>\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var swrOperation = Object.assign({\n swrOpName: 'use' + opName,\n clientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('swrOperation.ejs', swrOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var swrMutationOperation = Object.assign({\n mutOpName: 'use' + opName,\n clientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('swrMutationOperation.ejs', swrMutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n%>\n <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => `<%= operation.url %><% if(safeOp.query && safeOp.query.length > 0) { %>?${encodeParams({<% safeOp.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= safeOp.queryParamObject ? `${safeOp.queryParamObject.name}?.${parameter.name}` : parameter.name %>, <% }); %>})}<% } %>`,\n<% }); %>\n },\n};\n<% } /* end !splitMode */ %>\n",
42
- "hooksClient.ejs": "<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\nvar httpClientPrefix = 'API.' + it.camelCaseName;\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var swrOperation = Object.assign({}, safe, {\n swrOpName: 'use' + opName,\n clientName: it.camelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('swrOperation.ejs', swrOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var swrMutationOperation = Object.assign({}, safe, {\n mutOpName: 'use' + opName,\n clientName: it.camelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('swrMutationOperation.ejs', swrMutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n var prefixedParams = safeOp.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n });\n%>\n <%= keyName %>: (<% prefixedParams.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => `<%= operation.url %><% if(safeOp.query && safeOp.query.length > 0) { %>?${API.encodeParams({<% safeOp.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= safeOp.queryParamObject ? `${safeOp.queryParamObject.name}?.${parameter.name}` : parameter.name %>, <% }); %>})}<% } %>`,\n<% }); %>\n },\n};\n",
43
- "swrMutationOperation.ejs": "<%\nvar hasParams = it.parameters.length > 0;\n\nvar variablesType;\nif (!hasParams) {\n variablesType = 'void';\n} else {\n var parts = it.parameters.map(function(p) {\n return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');\n });\n variablesType = '{ ' + parts.join('; ') + ' }';\n}\n\nvar callArgs = it.parameters.map(function(p) { return 'arg.' + p.name; });\n\n// Stable SWR key: replace all ${...} path expressions with *\nvar swrKey = it.url.replace(/\\$\\{(?:[^{}]|\\{[^{}]*\\})*\\}/g, '*');\n\nvar docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\\n' : '';\nvar httpClientName = it.httpClientName || it.clientName;\n%>\n<%~ docs %>\n <%= it.mutOpName %>(\n $config?: SWRMutationConfiguration<<%~ it.returnType %>, Error, string, <%~ variablesType %>>,\n $httpConfig?: <%= it.httpConfigType %>\n ) {\n return useSWRMutation<<%~ it.returnType %>, Error, string, <%~ variablesType %>>(\n '<%= swrKey %>',\n (_key: string, { arg }<%= hasParams ? ': { arg: ' + variablesType + ' }' : '' %>) =>\n <%= httpClientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig)<%~ it.responseMapper %>,\n $config\n );\n },\n",
41
+ "client.ejs": "export const <%= it.camelCaseName -%>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n <% }); %>\n};\n\n<% if (!it.splitMode) { %>\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n%>\n\nexport const <%= it.hooksCamelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var swrOperation = Object.assign({\n swrOpName: 'use' + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('swrOperation.ejs', swrOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var swrMutationOperation = Object.assign({\n mutOpName: 'use' + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('swrMutationOperation.ejs', swrMutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n%>\n <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => `<%= operation.url %><% if(safeOp.query && safeOp.query.length > 0) { %>?${encodeParams({<% safeOp.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= safeOp.queryParamObject ? `${safeOp.queryParamObject.name}?.${parameter.name}` : parameter.name %>, <% }); %>})}<% } %>`,\n<% }); %>\n },\n};\n<% } /* end !splitMode */ %>\n",
42
+ "hooksClient.ejs": "<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\nvar httpClientPrefix = 'API.' + it.camelCaseName;\n%>\n\nexport const <%= it.hooksCamelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var swrOperation = Object.assign({}, safe, {\n swrOpName: 'use' + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('swrOperation.ejs', swrOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var swrMutationOperation = Object.assign({}, safe, {\n mutOpName: 'use' + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('swrMutationOperation.ejs', swrMutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n var prefixedParams = safeOp.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n });\n%>\n <%= keyName %>: (<% prefixedParams.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => `<%= operation.url %><% if(safeOp.query && safeOp.query.length > 0) { %>?${API.encodeParams({<% safeOp.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= safeOp.queryParamObject ? `${safeOp.queryParamObject.name}?.${parameter.name}` : parameter.name %>, <% }); %>})}<% } %>`,\n<% }); %>\n },\n};\n",
43
+ "swrMutationOperation.ejs": "<%\nvar hasParams = it.parameters.length > 0;\n\nvar variablesType;\nif (!hasParams) {\n variablesType = 'void';\n} else {\n var parts = it.parameters.map(function(p) {\n return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');\n });\n variablesType = '{ ' + parts.join('; ') + ' }';\n}\n\nvar callArgs = it.parameters.map(function(p) { return 'arg.' + p.name; });\n\n// Stable SWR key: replace all ${...} path expressions with *\nvar swrKey = it.url.replace(/\\$\\{(?:[^{}]|\\{[^{}]*\\})*\\}/g, '*');\n\nvar docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\\n' : '';\nvar httpClientName = it.httpClientName || it.clientName;\n%>\n<%~ docs %>\n <%= it.mutOpName %>(\n $config?: SWRMutationConfiguration<<%~ it.returnType %>, Error, string, <%~ variablesType %>>,\n $httpConfig?: <%= it.httpConfigType %>\n ) {\n return useSWRMutation<<%~ it.returnType %>, Error, string, <%~ variablesType %>>(\n '<%= swrKey %>',\n (_key: string, { arg }<%~ hasParams ? ': { arg: ' + variablesType + ' }' : '' %>) =>\n <%= httpClientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig)<%~ it.responseMapper %>,\n $config\n );\n },\n",
44
44
  "swrOperation.ejs": "<% var docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\\n' : ''; %>\n<% var httpClientName = it.httpClientName || it.clientName; %>\n<%~ docs %>\n <%= it.swrOpName %>(\n<% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,\n<% }); %> $config?: Omit<SwrConfig, 'key'> & { key?: Key },\n $httpConfig?: <%= it.httpConfigType %>\n ) {\n const { key, ...config } = $config || {};\n const cacheUrl = key ?? <%= it.clientName %>.queryKeys.<%= it.swrOpName.charAt(3).toLowerCase() + it.swrOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>);\n\n const { data, error, isLoading, mutate } = useSWR<<%~ it.returnType %>>(\n cacheUrl,\n () => <%= httpClientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig)<%~ it.responseMapper %>,\n config\n );\n\n return { data, isLoading, error, mutate };\n },\n"
45
45
  },
46
46
  "tsq": {
47
47
  "baseClient.ejs": "import { type UseQueryOptions, type UseMutationOptions, useQuery, useMutation } from '@tanstack/react-query';\n",
48
- "client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n<% if (!it.splitMode) { %>\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var queryOperation = Object.assign({\n rqOpName: 'use' + opName,\n opKey: it.camelCaseName + opName,\n clientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('queryOperation.ejs', queryOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var mutationOperation = Object.assign({\n mutOpName: 'use' + opName,\n clientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('mutationOperation.ejs', mutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n%>\n <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,\n<% }); %>\n },\n};\n<% } /* end !splitMode */ %>\n",
49
- "hooksClient.ejs": "<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\nvar httpClientPrefix = 'API.' + it.camelCaseName;\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var queryOperation = Object.assign({}, safe, {\n rqOpName: 'use' + opName,\n opKey: it.camelCaseName + opName,\n clientName: it.camelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('queryOperation.ejs', queryOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var mutationOperation = Object.assign({}, safe, {\n mutOpName: 'use' + opName,\n clientName: it.camelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('mutationOperation.ejs', mutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n var prefixedParams = safeOp.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n });\n%>\n <%= keyName %>: (<% prefixedParams.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,\n<% }); %>\n },\n};\n",
48
+ "client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n<% if (!it.splitMode) { %>\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n%>\n\nexport const <%= it.hooksCamelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var queryOperation = Object.assign({\n rqOpName: 'use' + opName,\n opKey: it.hooksCamelCaseName + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('queryOperation.ejs', queryOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var mutationOperation = Object.assign({\n mutOpName: 'use' + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: it.camelCaseName,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n }, it.safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('mutationOperation.ejs', mutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n%>\n <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.hooksCamelCaseName %>', '<%= it.hooksCamelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,\n<% }); %>\n },\n};\n<% } /* end !splitMode */ %>\n",
49
+ "hooksClient.ejs": "<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\nvar httpClientPrefix = 'API.' + it.camelCaseName;\n%>\n\nexport const <%= it.hooksCamelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var queryOperation = Object.assign({}, safe, {\n rqOpName: 'use' + opName,\n opKey: it.hooksCamelCaseName + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('queryOperation.ejs', queryOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var safe = it.safeOperation(operation, it.camelCaseName);\n var mutationOperation = Object.assign({}, safe, {\n mutOpName: 'use' + opName,\n clientName: it.hooksCamelCaseName,\n httpClientName: httpClientPrefix,\n httpConfigType: it.httpConfigType,\n responseMapper: it.responseMapper,\n returnType: it.prefixApiType(safe.returnType),\n parameters: safe.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n }),\n });\n%>\n<%~ include('mutationOperation.ejs', mutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = it.toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = it.safeOperation(operation, it.camelCaseName);\n var prefixedParams = safeOp.parameters.map(function(p) {\n return Object.assign({}, p, { type: it.prefixApiType(p.type) });\n });\n%>\n <%= keyName %>: (<% prefixedParams.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.hooksCamelCaseName %>', '<%= it.hooksCamelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,\n<% }); %>\n },\n};\n",
50
50
  "mutationOperation.ejs": "<%\nvar hasParams = it.parameters.length > 0;\n\nvar variablesType;\nif (!hasParams) {\n variablesType = 'void';\n} else {\n var parts = it.parameters.map(function(p) {\n return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');\n });\n variablesType = '{ ' + parts.join('; ') + ' }';\n}\n\nvar callArgs = it.parameters.map(function(p) { return 'vars.' + p.name; });\n\nvar baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\\n * @param $httpConfig (optional) Additional HTTP client configuration (passed to the underlying ' + it.httpConfigType + ')';\nvar rawDocs = it.jsDocs\n ? it.jsDocs.replace(/(\\s*)\\*\\/\\s*$/, '\\n' + baseAdditionalParams + '\\n */')\n : '';\nvar docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\\n' : '';\nvar httpClientName = it.httpClientName || it.clientName;\n%>\n<%~ docs %>\n <%= it.mutOpName %><TData = <%~ it.returnType %>, TError = Error>(\n $config?: UseMutationOptions<<%~ it.returnType %>, TError, <%~ variablesType %>>,\n $httpConfig?: <%= it.httpConfigType %>\n ) {\n return useMutation<<%~ it.returnType %>, TError, <%~ variablesType %>>({\n mutationFn: (<%= hasParams ? 'vars' : '' %>) => <%= httpClientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig)<%~ it.responseMapper %>,\n ...$config\n });\n },\n",
51
51
  "queryOperation.ejs": "<%\nvar baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\\n * @param $httpConfig (optional) Additional HTTP client configuration (passed to the underlying ' + it.httpConfigType + ')';\nvar rawDocs = it.jsDocs\n ? it.jsDocs.replace(/(\\s*)\\*\\/\\s*$/, '\\n' + baseAdditionalParams + '\\n */')\n : '';\nvar docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\\n' : '';\nvar httpClientName = it.httpClientName || it.clientName;\n%>\n<%~ docs %>\n <%= it.rqOpName %><TData = <%~ it.returnType %>, TError = Error>(\n<% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,\n<% }); %> $config?: Omit<UseQueryOptions<<%~ it.returnType %>, TError, TData>, 'queryKey' | 'queryFn'>,\n $httpConfig?: <%= it.httpConfigType %>\n ) {\n return useQuery<<%~ it.returnType %>, TError, TData>({\n queryKey: <%= it.clientName %>.queryKeys.<%= it.rqOpName.charAt(3).toLowerCase() + it.rqOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>),\n queryFn: () => <%= httpClientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig)<%~ it.responseMapper %>,\n ...$config\n });\n },\n"
52
52
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swaggie",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "Generate a fully typed TypeScript API client from your OpenAPI 3 spec",
5
5
  "author": {
6
6
  "name": "Piotr Dabrowski",
@@ -41,7 +41,7 @@
41
41
  "bundle-templates": "bun scripts/bundle-templates.ts",
42
42
  "rm-tests": "find dist/ \\( -name '*.spec.js' -o -name 'types.js' \\) -type f -delete",
43
43
  "types": "tsc --project tsconfig.types.json && cp test/index.d.ts ./dist/",
44
- "docs:build": "vitepress build docs",
44
+ "docs:build": "cp schema.json docs/public/schema.json && vitepress build docs",
45
45
  "docs:dev": "vitepress dev docs",
46
46
  "docs:preview": "vitepress preview docs"
47
47
  },
@@ -72,16 +72,16 @@
72
72
  "dependencies": {
73
73
  "case": "^1.6.3",
74
74
  "commander": "^14.0.3",
75
- "eta": "^4.5.1",
75
+ "eta": "^4.6.0",
76
76
  "yaml": "^2.8.3",
77
77
  "picocolors": "^1.1.1"
78
78
  },
79
79
  "devDependencies": {
80
- "bun-types": "1.3.11",
80
+ "bun-types": "1.3.13",
81
81
  "openapi-types": "^12.1.3",
82
82
  "sucrase": "3.35.1",
83
- "typescript": "6.0.2",
83
+ "typescript": "6.0.3",
84
84
  "vitepress": "^2.0.0-alpha.17",
85
- "vitepress-plugin-tabs": "0.8.0"
85
+ "vitepress-plugin-tabs": "0.9.0"
86
86
  }
87
87
  }
@@ -11,13 +11,14 @@ var getOperations = it.operations.filter((o) => o.method === 'GET');
11
11
  var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
12
12
  %>
13
13
 
14
- export const <%= it.camelCaseName %> = {
14
+ export const <%= it.hooksCamelCaseName %> = {
15
15
  queries: {
16
16
  <% getOperations.forEach((operation) => {
17
17
  var opName = it.toOpName(operation.name);
18
18
  var swrOperation = Object.assign({
19
19
  swrOpName: 'use' + opName,
20
- clientName: it.camelCaseName,
20
+ clientName: it.hooksCamelCaseName,
21
+ httpClientName: it.camelCaseName,
21
22
  httpConfigType: it.httpConfigType,
22
23
  responseMapper: it.responseMapper,
23
24
  }, it.safeOperation(operation, it.camelCaseName));
@@ -32,7 +33,8 @@ export const <%= it.camelCaseName %> = {
32
33
  var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);
33
34
  var swrMutationOperation = Object.assign({
34
35
  mutOpName: 'use' + opName,
35
- clientName: it.camelCaseName,
36
+ clientName: it.hooksCamelCaseName,
37
+ httpClientName: it.camelCaseName,
36
38
  httpConfigType: it.httpConfigType,
37
39
  responseMapper: it.responseMapper,
38
40
  }, it.safeOperation(operation, it.camelCaseName));
@@ -4,14 +4,14 @@ var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
4
4
  var httpClientPrefix = 'API.' + it.camelCaseName;
5
5
  %>
6
6
 
7
- export const <%= it.camelCaseName %> = {
7
+ export const <%= it.hooksCamelCaseName %> = {
8
8
  queries: {
9
9
  <% getOperations.forEach((operation) => {
10
10
  var opName = it.toOpName(operation.name);
11
11
  var safe = it.safeOperation(operation, it.camelCaseName);
12
12
  var swrOperation = Object.assign({}, safe, {
13
13
  swrOpName: 'use' + opName,
14
- clientName: it.camelCaseName,
14
+ clientName: it.hooksCamelCaseName,
15
15
  httpClientName: httpClientPrefix,
16
16
  httpConfigType: it.httpConfigType,
17
17
  responseMapper: it.responseMapper,
@@ -32,7 +32,7 @@ export const <%= it.camelCaseName %> = {
32
32
  var safe = it.safeOperation(operation, it.camelCaseName);
33
33
  var swrMutationOperation = Object.assign({}, safe, {
34
34
  mutOpName: 'use' + opName,
35
- clientName: it.camelCaseName,
35
+ clientName: it.hooksCamelCaseName,
36
36
  httpClientName: httpClientPrefix,
37
37
  httpConfigType: it.httpConfigType,
38
38
  responseMapper: it.responseMapper,
@@ -26,7 +26,7 @@ var httpClientName = it.httpClientName || it.clientName;
26
26
  ) {
27
27
  return useSWRMutation<<%~ it.returnType %>, Error, string, <%~ variablesType %>>(
28
28
  '<%= swrKey %>',
29
- (_key: string, { arg }<%= hasParams ? ': { arg: ' + variablesType + ' }' : '' %>) =>
29
+ (_key: string, { arg }<%~ hasParams ? ': { arg: ' + variablesType + ' }' : '' %>) =>
30
30
  <%= httpClientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig)<%~ it.responseMapper %>,
31
31
  $config
32
32
  );
@@ -11,14 +11,15 @@ var getOperations = it.operations.filter((o) => o.method === 'GET');
11
11
  var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
12
12
  %>
13
13
 
14
- export const <%= it.camelCaseName %> = {
14
+ export const <%= it.hooksCamelCaseName %> = {
15
15
  queries: {
16
16
  <% getOperations.forEach((operation) => {
17
17
  var opName = it.toOpName(operation.name);
18
18
  var queryOperation = Object.assign({
19
19
  rqOpName: 'use' + opName,
20
- opKey: it.camelCaseName + opName,
21
- clientName: it.camelCaseName,
20
+ opKey: it.hooksCamelCaseName + opName,
21
+ clientName: it.hooksCamelCaseName,
22
+ httpClientName: it.camelCaseName,
22
23
  httpConfigType: it.httpConfigType,
23
24
  responseMapper: it.responseMapper,
24
25
  }, it.safeOperation(operation, it.camelCaseName));
@@ -33,7 +34,8 @@ export const <%= it.camelCaseName %> = {
33
34
  var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);
34
35
  var mutationOperation = Object.assign({
35
36
  mutOpName: 'use' + opName,
36
- clientName: it.camelCaseName,
37
+ clientName: it.hooksCamelCaseName,
38
+ httpClientName: it.camelCaseName,
37
39
  httpConfigType: it.httpConfigType,
38
40
  responseMapper: it.responseMapper,
39
41
  }, it.safeOperation(operation, it.camelCaseName));
@@ -49,7 +51,7 @@ export const <%= it.camelCaseName %> = {
49
51
  var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);
50
52
  var safeOp = it.safeOperation(operation, it.camelCaseName);
51
53
  %>
52
- <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,
54
+ <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.hooksCamelCaseName %>', '<%= it.hooksCamelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,
53
55
  <% }); %>
54
56
  },
55
57
  };
@@ -4,15 +4,15 @@ var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
4
4
  var httpClientPrefix = 'API.' + it.camelCaseName;
5
5
  %>
6
6
 
7
- export const <%= it.camelCaseName %> = {
7
+ export const <%= it.hooksCamelCaseName %> = {
8
8
  queries: {
9
9
  <% getOperations.forEach((operation) => {
10
10
  var opName = it.toOpName(operation.name);
11
11
  var safe = it.safeOperation(operation, it.camelCaseName);
12
12
  var queryOperation = Object.assign({}, safe, {
13
13
  rqOpName: 'use' + opName,
14
- opKey: it.camelCaseName + opName,
15
- clientName: it.camelCaseName,
14
+ opKey: it.hooksCamelCaseName + opName,
15
+ clientName: it.hooksCamelCaseName,
16
16
  httpClientName: httpClientPrefix,
17
17
  httpConfigType: it.httpConfigType,
18
18
  responseMapper: it.responseMapper,
@@ -33,7 +33,7 @@ export const <%= it.camelCaseName %> = {
33
33
  var safe = it.safeOperation(operation, it.camelCaseName);
34
34
  var mutationOperation = Object.assign({}, safe, {
35
35
  mutOpName: 'use' + opName,
36
- clientName: it.camelCaseName,
36
+ clientName: it.hooksCamelCaseName,
37
37
  httpClientName: httpClientPrefix,
38
38
  httpConfigType: it.httpConfigType,
39
39
  responseMapper: it.responseMapper,
@@ -57,7 +57,7 @@ export const <%= it.camelCaseName %> = {
57
57
  return Object.assign({}, p, { type: it.prefixApiType(p.type) });
58
58
  });
59
59
  %>
60
- <%= keyName %>: (<% prefixedParams.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,
60
+ <%= keyName %>: (<% prefixedParams.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.hooksCamelCaseName %>', '<%= it.hooksCamelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,
61
61
  <% }); %>
62
62
  },
63
63
  };