swaggie 1.9.0-alpha.2 → 1.9.0-alpha.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/dist/generated/bundledTemplates.js +5 -4
- package/package.json +1 -1
- package/templates/swr-axios/baseClient.ejs +1 -3
- package/templates/swr-axios/client.ejs +24 -2
- package/templates/swr-axios/swrMutationOperation.ejs +32 -0
- package/templates/swr-axios/swrOperation.ejs +6 -36
- package/templates/tsq-xior/client.ejs +13 -3
|
@@ -28,15 +28,16 @@
|
|
|
28
28
|
},
|
|
29
29
|
"swr-axios": {
|
|
30
30
|
"barrel.ejs": "\n/**\n * Serializes a params object into a query string that is compatible with different REST APIs.\n * Implementation from: https://github.com/suhaotian/xior/blob/main/src/utils.ts\n * Kudos to @suhaotian for the original implementation\n */\nfunction encodeParams<T = any>(\n params: T,\n parentKey: string | null = null,\n options?: {\n allowDots?: boolean;\n serializeDate?: (value: Date) => string;\n arrayFormat?: 'indices' | 'repeat' | 'brackets';\n }\n): string {\n if (params === undefined || params === null) return '';\n const encodedParams: string[] = [];\n const paramsIsArray = Array.isArray(params);\n const { arrayFormat, allowDots, serializeDate } = options || {};\n\n const getKey = (key: string) => {\n if (allowDots && !paramsIsArray) return `.${key}`;\n if (paramsIsArray) {\n if (arrayFormat === 'brackets') {\n return '[]';\n }\n if (arrayFormat === 'repeat') {\n return '';\n }\n }\n return `[${key}]`;\n };\n\n for (const key in params) {\n if (Object.prototype.hasOwnProperty.call(params, key)) {\n let value = (params as any)[key];\n if (value !== undefined) {\n const encodedKey = parentKey ? `${parentKey}${getKey(key)}` : (key as string);\n\n // biome-ignore lint/suspicious/noGlobalIsNan: <explanation>\n if (!isNaN(value) && value instanceof Date) {\n value = serializeDate ? serializeDate(value) : value.toISOString();\n }\n if (typeof value === 'object') {\n // If the value is an object or array, recursively encode its contents\n const result = encodeParams(value, encodedKey, options);\n if (result !== '') encodedParams.push(result);\n } else {\n // Otherwise, encode the key-value pair\n encodedParams.push(`${encodeURIComponent(encodedKey)}=${encodeURIComponent(value)}`);\n }\n }\n }\n }\n\n return encodedParams.join('&');\n}\n\n",
|
|
31
|
-
"baseClient.ejs": "import Axios, { type AxiosPromise, type AxiosRequestConfig } from \"axios\";\nimport useSWR, { type SWRConfiguration, type Key } from 'swr';\n\nexport const axios = Axios.create({\n baseURL: '<%= it.baseUrl || '' -%>',\n paramsSerializer: (params: any) =>\n encodeParams(params, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\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
|
|
32
|
-
"client.ejs": "export const <%= it.camelCaseName -%>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n <% }); %>\n};\n\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\n\nfunction toOpName(name) {\n var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;\n return n.charAt(0).toUpperCase() + n.slice(1);\n}\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var swrOperation = Object.assign({\n swrOpName: 'use' + opName,\n clientName: it.camelCaseName,\n }, operation);\n%>\n<%~ include('swrOperation.ejs', swrOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n%>\n <%= keyName %>: (<%
|
|
31
|
+
"baseClient.ejs": "import Axios, { type AxiosPromise, type AxiosRequestConfig } from \"axios\";\nimport useSWR, { type SWRConfiguration, type Key } from 'swr';\nimport useSWRMutation, { type SWRMutationConfiguration } from 'swr/mutation';\n\nexport const axios = Axios.create({\n baseURL: '<%= it.baseUrl || '' -%>',\n paramsSerializer: (params: any) =>\n encodeParams(params, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\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\n",
|
|
32
|
+
"client.ejs": "export const <%= it.camelCaseName -%>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n <% }); %>\n};\n\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n\nfunction toOpName(name) {\n var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;\n return n.charAt(0).toUpperCase() + n.slice(1);\n}\n\nfunction safeOperation(operation, clientName) {\n var safeParams = operation.parameters.map(function(p) {\n return p.name === clientName ? Object.assign({}, p, { name: '_' + p.name }) : p;\n });\n return Object.assign({}, operation, { parameters: safeParams });\n}\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var swrOperation = Object.assign({\n swrOpName: 'use' + opName,\n clientName: it.camelCaseName,\n }, 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 }, safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('swrMutationOperation.ejs', swrMutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = safeOperation(operation, it.camelCaseName);\n%>\n <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => `<%= operation.url %><% if(safeOp.query && safeOp.query.length > 0) { %>?${encodeParams({<% safeOp.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= parameter.name %>, <% }); %>})}<% } %>`,\n<% }); %>\n },\n};\n",
|
|
33
33
|
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n $config?: AxiosRequestConfig\n ): AxiosPromise<<%~ it.returnType %>> {\n const url = `<%= it.url %>`;\n\n return axios.request<<%~ it.returnType %>>({\n url: url,\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'urlencoded') { %>\n data: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n data: <%= it.body.name %>,\n<% } %>\n<% } %>\n<% if(it.query && it.query.length > 0) { %>\n params: {\n <% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>\n },\n<% } %>\n<% if(it.headers && it.headers.length > 0) { %>\n headers: {\n <% it.headers.forEach((parameter) => { %>\n <% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n <% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% } %>\n <% }); %>\n},\n<% } %>\n ...$config,\n });\n },\n",
|
|
34
|
-
"
|
|
34
|
+
"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' : '';\n%>\n<%~ docs %>\n <%= it.mutOpName %>(\n $config?: SWRMutationConfiguration<<%~ it.returnType %>, Error, string, <%~ variablesType %>>,\n $httpConfig?: AxiosRequestConfig\n ) {\n return useSWRMutation<<%~ it.returnType %>, Error, string, <%~ variablesType %>>(\n '<%= swrKey %>',\n (_key: string, { arg }<%= hasParams ? ': { arg: ' + variablesType + ' }' : '' %>) =>\n <%= it.clientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig).then((resp) => resp.data),\n $config\n );\n },\n",
|
|
35
|
+
"swrOperation.ejs": "<% var docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\\n' : ''; %>\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?: AxiosRequestConfig\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 () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig).then((resp) => resp.data),\n config\n );\n\n return { data, isLoading, error, mutate };\n },\n"
|
|
35
36
|
},
|
|
36
37
|
"tsq-xior": {
|
|
37
38
|
"barrel.ejs": "",
|
|
38
39
|
"baseClient.ejs": "import xior, { type XiorResponse, type XiorRequestConfig, encodeParams } from \"xior\";\nimport { type UseQueryOptions, type UseMutationOptions, useQuery, useMutation } from '@tanstack/react-query';\n\nexport const http = xior.create({\n baseURL: '<%= it.baseUrl || '' %>',\n paramsSerializer: (params: any) =>\n encodeParams(params, true, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\n\n",
|
|
39
|
-
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n\n// Helper: strip leading \"get\" and capitalise\nfunction toOpName(name) {\n var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;\n return n.charAt(0).toUpperCase() + n.slice(1);\n}\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var queryOperation = Object.assign({\n rqOpName: 'use' + opName,\n opKey: it.camelCaseName + opName,\n clientName: it.camelCaseName,\n }, operation);\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 }, operation);\n%>\n<%~ include('mutationOperation.ejs', mutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n%>\n <%= keyName %>: (<%
|
|
40
|
+
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n\n// Helper: strip leading \"get\" and capitalise\nfunction toOpName(name) {\n var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;\n return n.charAt(0).toUpperCase() + n.slice(1);\n}\n\n// Returns a deep copy of operation with any parameter whose name collides with\n// the group object name (clientName) renamed to _<name> to avoid TS shadowing.\nfunction safeOperation(operation, clientName) {\n var safeParams = operation.parameters.map(function(p) {\n return p.name === clientName ? Object.assign({}, p, { name: '_' + p.name }) : p;\n });\n return Object.assign({}, operation, { parameters: safeParams });\n}\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var queryOperation = Object.assign({\n rqOpName: 'use' + opName,\n opKey: it.camelCaseName + opName,\n clientName: it.camelCaseName,\n }, 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 }, safeOperation(operation, it.camelCaseName));\n%>\n<%~ include('mutationOperation.ejs', mutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n var safeOp = safeOperation(operation, it.camelCaseName);\n%>\n <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,\n<% }); %>\n },\n};\n",
|
|
40
41
|
"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 configuration for xior request (actually executes the request)';\nvar rawDocs = it.jsDocs\n ? it.jsDocs.replace(/(\\s*)\\*\\/\\s*$/, '\\n' + baseAdditionalParams + '\\n */')\n : '';\nvar docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\\n' : '';\n%>\n<%~ docs %>\n <%= it.mutOpName %><TData = <%~ it.returnType %>, TError = Error>(\n $config?: UseMutationOptions<<%~ it.returnType %>, TError, <%~ variablesType %>>,\n $httpConfig?: XiorRequestConfig\n ) {\n return useMutation<<%~ it.returnType %>, TError, <%~ variablesType %>>({\n mutationFn: (<%= hasParams ? 'vars' : '' %>) => <%= it.clientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig).then(res => res.data),\n ...$config\n });\n },\n",
|
|
41
42
|
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n$config?: XiorRequestConfig\n ): Promise<XiorResponse<<%~ it.returnType %>>> {\n const url = `<%= it.url %>`;\n\n return http.request<<%~ it.returnType %>>({\n url: url,\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'urlencoded') { %>\n data: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n data: <%= it.body.name %>,\n<% } %>\n<% } %>\n<% if(it.query && it.query.length > 0) { %>\n params: {\n <% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>\n },\n<% } %>\n<% if(it.headers && it.headers.length > 0) { %>\n headers: {\n <% it.headers.forEach((parameter) => { %>\n <% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n <% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% } %>\n <% }); %>\n},\n<% } %>\n ...$config,\n });\n },\n",
|
|
42
43
|
"queryOperation.ejs": "<%\nvar baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\\n * @param $httpConfig (optional) Additional configuration for xior request (actually executes the request)';\nvar rawDocs = it.jsDocs\n ? it.jsDocs.replace(/(\\s*)\\*\\/\\s*$/, '\\n' + baseAdditionalParams + '\\n */')\n : '';\nvar docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\\n' : '';\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?: XiorRequestConfig\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: () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig).then(res => res.data),\n ...$config\n });\n },\n"
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Axios, { type AxiosPromise, type AxiosRequestConfig } from "axios";
|
|
2
2
|
import useSWR, { type SWRConfiguration, type Key } from 'swr';
|
|
3
|
+
import useSWRMutation, { type SWRMutationConfiguration } from 'swr/mutation';
|
|
3
4
|
|
|
4
5
|
export const axios = Axios.create({
|
|
5
6
|
baseURL: '<%= it.baseUrl || '' -%>',
|
|
@@ -13,8 +14,5 @@ export const axios = Axios.create({
|
|
|
13
14
|
interface SwrConfig extends SWRConfiguration {
|
|
14
15
|
/* 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. */
|
|
15
16
|
key?: Key;
|
|
16
|
-
|
|
17
|
-
/* Configuration for axios fetcher */
|
|
18
|
-
axios?: AxiosRequestConfig;
|
|
19
17
|
}
|
|
20
18
|
|
|
@@ -7,11 +7,19 @@ export const <%= it.camelCaseName -%>Client = {
|
|
|
7
7
|
|
|
8
8
|
<%
|
|
9
9
|
var getOperations = it.operations.filter((o) => o.method === 'GET');
|
|
10
|
+
var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
|
|
10
11
|
|
|
11
12
|
function toOpName(name) {
|
|
12
13
|
var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;
|
|
13
14
|
return n.charAt(0).toUpperCase() + n.slice(1);
|
|
14
15
|
}
|
|
16
|
+
|
|
17
|
+
function safeOperation(operation, clientName) {
|
|
18
|
+
var safeParams = operation.parameters.map(function(p) {
|
|
19
|
+
return p.name === clientName ? Object.assign({}, p, { name: '_' + p.name }) : p;
|
|
20
|
+
});
|
|
21
|
+
return Object.assign({}, operation, { parameters: safeParams });
|
|
22
|
+
}
|
|
15
23
|
%>
|
|
16
24
|
|
|
17
25
|
export const <%= it.camelCaseName %> = {
|
|
@@ -21,10 +29,23 @@ export const <%= it.camelCaseName %> = {
|
|
|
21
29
|
var swrOperation = Object.assign({
|
|
22
30
|
swrOpName: 'use' + opName,
|
|
23
31
|
clientName: it.camelCaseName,
|
|
24
|
-
}, operation);
|
|
32
|
+
}, safeOperation(operation, it.camelCaseName));
|
|
25
33
|
%>
|
|
26
34
|
<%~ include('swrOperation.ejs', swrOperation); %>
|
|
27
35
|
|
|
36
|
+
<% }); %>
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
mutations: {
|
|
40
|
+
<% mutationOperations.forEach((operation) => {
|
|
41
|
+
var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);
|
|
42
|
+
var swrMutationOperation = Object.assign({
|
|
43
|
+
mutOpName: 'use' + opName,
|
|
44
|
+
clientName: it.camelCaseName,
|
|
45
|
+
}, safeOperation(operation, it.camelCaseName));
|
|
46
|
+
%>
|
|
47
|
+
<%~ include('swrMutationOperation.ejs', swrMutationOperation); %>
|
|
48
|
+
|
|
28
49
|
<% }); %>
|
|
29
50
|
},
|
|
30
51
|
|
|
@@ -32,8 +53,9 @@ export const <%= it.camelCaseName %> = {
|
|
|
32
53
|
<% getOperations.forEach((operation) => {
|
|
33
54
|
var opName = toOpName(operation.name);
|
|
34
55
|
var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);
|
|
56
|
+
var safeOp = safeOperation(operation, it.camelCaseName);
|
|
35
57
|
%>
|
|
36
|
-
<%= keyName %>: (<%
|
|
58
|
+
<%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => `<%= operation.url %><% if(safeOp.query && safeOp.query.length > 0) { %>?${encodeParams({<% safeOp.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= parameter.name %>, <% }); %>})}<% } %>`,
|
|
37
59
|
<% }); %>
|
|
38
60
|
},
|
|
39
61
|
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
<%
|
|
2
|
+
var hasParams = it.parameters.length > 0;
|
|
3
|
+
|
|
4
|
+
var variablesType;
|
|
5
|
+
if (!hasParams) {
|
|
6
|
+
variablesType = 'void';
|
|
7
|
+
} else {
|
|
8
|
+
var parts = it.parameters.map(function(p) {
|
|
9
|
+
return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');
|
|
10
|
+
});
|
|
11
|
+
variablesType = '{ ' + parts.join('; ') + ' }';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
var callArgs = it.parameters.map(function(p) { return 'arg.' + p.name; });
|
|
15
|
+
|
|
16
|
+
// Stable SWR key: replace all ${...} path expressions with *
|
|
17
|
+
var swrKey = it.url.replace(/\$\{(?:[^{}]|\{[^{}]*\})*\}/g, '*');
|
|
18
|
+
|
|
19
|
+
var docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\n' : '';
|
|
20
|
+
%>
|
|
21
|
+
<%~ docs %>
|
|
22
|
+
<%= it.mutOpName %>(
|
|
23
|
+
$config?: SWRMutationConfiguration<<%~ it.returnType %>, Error, string, <%~ variablesType %>>,
|
|
24
|
+
$httpConfig?: AxiosRequestConfig
|
|
25
|
+
) {
|
|
26
|
+
return useSWRMutation<<%~ it.returnType %>, Error, string, <%~ variablesType %>>(
|
|
27
|
+
'<%= swrKey %>',
|
|
28
|
+
(_key: string, { arg }<%= hasParams ? ': { arg: ' + variablesType + ' }' : '' %>) =>
|
|
29
|
+
<%= it.clientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig).then((resp) => resp.data),
|
|
30
|
+
$config
|
|
31
|
+
);
|
|
32
|
+
},
|
|
@@ -2,45 +2,15 @@
|
|
|
2
2
|
<%~ docs %>
|
|
3
3
|
<%= it.swrOpName %>(
|
|
4
4
|
<% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,
|
|
5
|
-
<% }); %> $config?: SwrConfig
|
|
5
|
+
<% }); %> $config?: Omit<SwrConfig, 'key'> & { key?: Key },
|
|
6
|
+
$httpConfig?: AxiosRequestConfig
|
|
6
7
|
) {
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
<% if(it.query && it.query.length > 0) { %>
|
|
11
|
-
const cacheKey = encodeParams({<% it.query.forEach((parameter) => { %>
|
|
12
|
-
'<%= parameter.originalName %>': <%= parameter.name %>,<% }); %>
|
|
13
|
-
});
|
|
14
|
-
const cacheUrl = cacheKey ? `${url}?${cacheKey}` : url;
|
|
15
|
-
<% } else { %>
|
|
16
|
-
const cacheUrl = url;
|
|
17
|
-
<% } %>
|
|
8
|
+
const { key, ...config } = $config || {};
|
|
9
|
+
const cacheUrl = key ?? <%= it.clientName %>.queryKeys.<%= it.swrOpName.charAt(3).toLowerCase() + it.swrOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>);
|
|
18
10
|
|
|
19
11
|
const { data, error, isLoading, mutate } = useSWR<<%~ it.returnType %>>(
|
|
20
|
-
|
|
21
|
-
() =>
|
|
22
|
-
url: url,
|
|
23
|
-
method: '<%= it.method %>',
|
|
24
|
-
<% if(it.query && it.query.length > 0) { %>
|
|
25
|
-
params: {
|
|
26
|
-
<% it.query.forEach((parameter) => { %>
|
|
27
|
-
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
28
|
-
<% }); %>
|
|
29
|
-
},
|
|
30
|
-
<% } %>
|
|
31
|
-
<% if(it.headers && it.headers.length > 0) { %>
|
|
32
|
-
headers: {
|
|
33
|
-
<% it.headers.forEach((parameter) => { %>
|
|
34
|
-
<% if (parameter.value) { %>
|
|
35
|
-
'<%= parameter.originalName %>': '<%= parameter.value %>',
|
|
36
|
-
<% } else { %>
|
|
37
|
-
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
38
|
-
<% } %>
|
|
39
|
-
<% }); %>
|
|
40
|
-
},
|
|
41
|
-
<% } %>
|
|
42
|
-
...$axiosConf,
|
|
43
|
-
}).then((resp) => resp.data),
|
|
12
|
+
cacheUrl,
|
|
13
|
+
() => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig).then((resp) => resp.data),
|
|
44
14
|
config
|
|
45
15
|
);
|
|
46
16
|
|
|
@@ -14,6 +14,15 @@ function toOpName(name) {
|
|
|
14
14
|
var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;
|
|
15
15
|
return n.charAt(0).toUpperCase() + n.slice(1);
|
|
16
16
|
}
|
|
17
|
+
|
|
18
|
+
// Returns a deep copy of operation with any parameter whose name collides with
|
|
19
|
+
// the group object name (clientName) renamed to _<name> to avoid TS shadowing.
|
|
20
|
+
function safeOperation(operation, clientName) {
|
|
21
|
+
var safeParams = operation.parameters.map(function(p) {
|
|
22
|
+
return p.name === clientName ? Object.assign({}, p, { name: '_' + p.name }) : p;
|
|
23
|
+
});
|
|
24
|
+
return Object.assign({}, operation, { parameters: safeParams });
|
|
25
|
+
}
|
|
17
26
|
%>
|
|
18
27
|
|
|
19
28
|
export const <%= it.camelCaseName %> = {
|
|
@@ -24,7 +33,7 @@ export const <%= it.camelCaseName %> = {
|
|
|
24
33
|
rqOpName: 'use' + opName,
|
|
25
34
|
opKey: it.camelCaseName + opName,
|
|
26
35
|
clientName: it.camelCaseName,
|
|
27
|
-
}, operation);
|
|
36
|
+
}, safeOperation(operation, it.camelCaseName));
|
|
28
37
|
%>
|
|
29
38
|
<%~ include('queryOperation.ejs', queryOperation); %>
|
|
30
39
|
|
|
@@ -37,7 +46,7 @@ export const <%= it.camelCaseName %> = {
|
|
|
37
46
|
var mutationOperation = Object.assign({
|
|
38
47
|
mutOpName: 'use' + opName,
|
|
39
48
|
clientName: it.camelCaseName,
|
|
40
|
-
}, operation);
|
|
49
|
+
}, safeOperation(operation, it.camelCaseName));
|
|
41
50
|
%>
|
|
42
51
|
<%~ include('mutationOperation.ejs', mutationOperation); %>
|
|
43
52
|
|
|
@@ -48,8 +57,9 @@ export const <%= it.camelCaseName %> = {
|
|
|
48
57
|
<% getOperations.forEach((operation) => {
|
|
49
58
|
var opName = toOpName(operation.name);
|
|
50
59
|
var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);
|
|
60
|
+
var safeOp = safeOperation(operation, it.camelCaseName);
|
|
51
61
|
%>
|
|
52
|
-
<%= keyName %>: (<%
|
|
62
|
+
<%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,
|
|
53
63
|
<% }); %>
|
|
54
64
|
},
|
|
55
65
|
};
|