swaggie 2.2.0 → 2.2.2
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/gen/genMocks.js
CHANGED
|
@@ -48,10 +48,7 @@ const MOCK_FILE_HEADER = `\
|
|
|
48
48
|
const template = options.template;
|
|
49
49
|
|
|
50
50
|
if (template === 'ng1') {
|
|
51
|
-
throw new Error(
|
|
52
|
-
'Mock generation is not supported for the "ng1" template. ' +
|
|
53
|
-
'Use "ng2", "axios", "fetch", "xior", "swr", or "tsq" instead.'
|
|
54
|
-
);
|
|
51
|
+
throw new Error('Mock generation is not supported for the "ng1" template');
|
|
55
52
|
}
|
|
56
53
|
|
|
57
54
|
const isNg2 = template === 'ng2';
|
|
@@ -254,28 +251,34 @@ interface MockSWRReturn {
|
|
|
254
251
|
data: unknown;
|
|
255
252
|
/** Whether to return a loading state (default: false) */
|
|
256
253
|
isLoading?: boolean;
|
|
254
|
+
/** Whether to return a validating state (default: false) */
|
|
255
|
+
isValidating?: boolean;
|
|
257
256
|
/** Whether to return an error (default: undefined) */
|
|
258
257
|
error?: Error | undefined | null;
|
|
258
|
+
/** The mutate function to return (default: empty mock) */
|
|
259
|
+
mutate?: KeyedMutator<any>;
|
|
259
260
|
}
|
|
260
261
|
|
|
261
262
|
interface MockSWRMutationReturn {
|
|
262
263
|
/** The data to return from the mock */
|
|
263
|
-
data
|
|
264
|
+
data?: unknown;
|
|
264
265
|
/** Whether to return a mutating state (default: false) */
|
|
265
266
|
isMutating?: boolean;
|
|
266
267
|
/** Whether to return an error (default: undefined) */
|
|
267
268
|
error?: Error | undefined | null;
|
|
269
|
+
/** The trigger function to return (default: empty mock) */
|
|
270
|
+
trigger?: (...args: unknown[]) => Promise<unknown>;
|
|
271
|
+
/** The reset function to return (default: empty mock) */
|
|
272
|
+
reset?: () => void;
|
|
268
273
|
}
|
|
269
274
|
|
|
270
275
|
/** Augments a spy with a \`mockSWR\` shorthand for useSWR query hooks. */
|
|
271
276
|
function withMockSWR<Fn extends (...args: never[]) => unknown>(spy: ${ref}.SpiedFunction<Fn>) {
|
|
272
277
|
return Object.assign(spy, {
|
|
273
|
-
mockSWR({
|
|
278
|
+
mockSWR({ ...rest }: MockSWRReturn) {
|
|
274
279
|
spy.mockReturnValue({
|
|
275
280
|
...defaultSWRReturn,
|
|
276
|
-
|
|
277
|
-
isLoading: isLoading ?? false,
|
|
278
|
-
error: error ?? undefined,
|
|
281
|
+
...rest,
|
|
279
282
|
} as ReturnType<Fn>);
|
|
280
283
|
},
|
|
281
284
|
});
|
|
@@ -286,15 +289,13 @@ function withMockSWRMutation<Fn extends (...args: never[]) => unknown>(spy: ${re
|
|
|
286
289
|
return Object.assign(spy, {
|
|
287
290
|
mockSWRMutation({
|
|
288
291
|
data,
|
|
289
|
-
|
|
290
|
-
error,
|
|
292
|
+
...rest
|
|
291
293
|
}: MockSWRMutationReturn) {
|
|
292
294
|
spy.mockReturnValue({
|
|
293
295
|
...defaultSWRMutationReturn,
|
|
294
296
|
trigger: ${ref}.fn(() => Promise.resolve(undefined)),
|
|
295
|
-
isMutating: isMutating ?? false,
|
|
296
|
-
error: error ?? undefined,
|
|
297
297
|
data,
|
|
298
|
+
...rest,
|
|
298
299
|
} as ReturnType<Fn>);
|
|
299
300
|
},
|
|
300
301
|
});
|
|
@@ -41,7 +41,7 @@
|
|
|
41
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
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
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
|
-
"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"
|
|
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, isValidating, 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, isValidating, 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",
|
package/package.json
CHANGED
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
const { key, ...config } = $config || {};
|
|
10
10
|
const cacheUrl = key ?? <%= it.clientName %>.queryKeys.<%= it.swrOpName.charAt(3).toLowerCase() + it.swrOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>);
|
|
11
11
|
|
|
12
|
-
const { data, error, isLoading, mutate } = useSWR<<%~ it.returnType %>>(
|
|
12
|
+
const { data, error, isLoading, isValidating, mutate } = useSWR<<%~ it.returnType %>>(
|
|
13
13
|
cacheUrl,
|
|
14
14
|
() => <%= httpClientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig)<%~ it.responseMapper %>,
|
|
15
15
|
config
|
|
16
16
|
);
|
|
17
17
|
|
|
18
|
-
return { data, isLoading, error, mutate };
|
|
18
|
+
return { data, isLoading, isValidating, error, mutate };
|
|
19
19
|
},
|