react-query-lightbase-codegen 0.0.9 → 0.0.12
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/lib/cjs/import-open-api.js +212 -217
- package/lib/cjs/index.js +2 -1
- package/lib/cjs/react-query-codegen-import.js +3 -3
- package/lib/esm/import-open-api.js +191 -196
- package/lib/esm/index.js +1 -1
- package/lib/esm/react-query-codegen-import.js +3 -3
- package/package.json +10 -12
|
@@ -4,18 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.importOpenApi = exports.generateRestfulComponent = exports.formatDescription = exports.generateResponsesDefinition = exports.generateRequestBodiesDefinition = exports.generateSchemasDefinition = exports.resolveDiscriminator = exports.generateInterface = exports.getParamsInPath = exports.getResReqTypes = exports.resolveValue = exports.getObject = exports.getArray = exports.getRef = exports.getScalar = exports.isReference = void 0;
|
|
7
|
-
const case_1 =
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
7
|
+
const case_1 = require("case");
|
|
8
|
+
const get_1 = __importDefault(require("lodash/get"));
|
|
9
|
+
const groupBy_1 = __importDefault(require("lodash/groupBy"));
|
|
10
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
11
|
+
const set_1 = __importDefault(require("lodash/set"));
|
|
12
|
+
const uniq_1 = __importDefault(require("lodash/uniq"));
|
|
11
13
|
const swagger2openapi_1 = __importDefault(require("swagger2openapi"));
|
|
12
|
-
const
|
|
14
|
+
const yaml = require('js-yaml');
|
|
13
15
|
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
14
16
|
/**
|
|
15
17
|
* Import and parse the openapi spec from a yaml/json
|
|
16
18
|
*/
|
|
17
19
|
const importSpecs = (data, extension) => {
|
|
18
|
-
const schema = extension === 'yaml' ?
|
|
20
|
+
const schema = extension === 'yaml' ? yaml.load(data) : JSON.parse(data);
|
|
19
21
|
return new Promise((resolve, reject) => {
|
|
20
22
|
if (!schema.openapi || !schema.openapi.startsWith('3.')) {
|
|
21
23
|
swagger2openapi_1.default.convertObj(schema, {}, (err, convertedObj) => {
|
|
@@ -65,16 +67,16 @@ exports.getScalar = getScalar;
|
|
|
65
67
|
*/
|
|
66
68
|
const getRef = ($ref) => {
|
|
67
69
|
if ($ref.startsWith('#/components/schemas')) {
|
|
68
|
-
return pascal($ref.replace('#/components/schemas/', ''));
|
|
70
|
+
return (0, case_1.pascal)($ref.replace('#/components/schemas/', ''));
|
|
69
71
|
}
|
|
70
72
|
else if ($ref.startsWith('#/components/responses')) {
|
|
71
|
-
return pascal($ref.replace('#/components/responses/', '')) + 'Response';
|
|
73
|
+
return (0, case_1.pascal)($ref.replace('#/components/responses/', '')) + 'Response';
|
|
72
74
|
}
|
|
73
75
|
else if ($ref.startsWith('#/components/parameters')) {
|
|
74
|
-
return pascal($ref.replace('#/components/parameters/', '')) + 'Parameter';
|
|
76
|
+
return (0, case_1.pascal)($ref.replace('#/components/parameters/', '')) + 'Parameter';
|
|
75
77
|
}
|
|
76
78
|
else if ($ref.startsWith('#/components/requestBodies')) {
|
|
77
|
-
return pascal($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
|
|
79
|
+
return (0, case_1.pascal)($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
|
|
78
80
|
}
|
|
79
81
|
else {
|
|
80
82
|
throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
|
|
@@ -117,7 +119,7 @@ const getObject = (item) => {
|
|
|
117
119
|
// Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
|
|
118
120
|
if (item.type === 'object' &&
|
|
119
121
|
!item.properties &&
|
|
120
|
-
(!item.additionalProperties || item.additionalProperties === true ||
|
|
122
|
+
(!item.additionalProperties || item.additionalProperties === true || (0, isEmpty_1.default)(item.additionalProperties))) {
|
|
121
123
|
return '{[key: string]: any}';
|
|
122
124
|
}
|
|
123
125
|
// Consolidation of item.properties & item.additionalProperties
|
|
@@ -155,7 +157,7 @@ exports.resolveValue = resolveValue;
|
|
|
155
157
|
/**
|
|
156
158
|
* Extract responses / request types from open-api specs
|
|
157
159
|
*/
|
|
158
|
-
const getResReqTypes = (responsesOrRequests) =>
|
|
160
|
+
const getResReqTypes = (responsesOrRequests) => (0, uniq_1.default)(responsesOrRequests.map(([_, res]) => {
|
|
159
161
|
if (!res) {
|
|
160
162
|
return 'void';
|
|
161
163
|
}
|
|
@@ -199,7 +201,7 @@ exports.getParamsInPath = getParamsInPath;
|
|
|
199
201
|
*/
|
|
200
202
|
const generateInterface = (name, schema) => {
|
|
201
203
|
const scalar = (0, exports.getScalar)(schema);
|
|
202
|
-
return `${(0, exports.formatDescription)(schema.description)}export interface ${pascal(name)} ${scalar}`;
|
|
204
|
+
return `${(0, exports.formatDescription)(schema.description)}export interface ${(0, case_1.pascal)(name)} ${scalar}`;
|
|
203
205
|
};
|
|
204
206
|
exports.generateInterface = generateInterface;
|
|
205
207
|
/**
|
|
@@ -218,7 +220,7 @@ const resolveDiscriminator = (specs) => {
|
|
|
218
220
|
if (!ref.startsWith('#/components/schemas/')) {
|
|
219
221
|
throw new Error('Discriminator mapping outside of `#/components/schemas` is not supported');
|
|
220
222
|
}
|
|
221
|
-
|
|
223
|
+
(0, set_1.default)(specs, `components.schemas.${ref.slice('#/components/schemas/'.length)}.properties.${propertyName}.enum`, [name]);
|
|
222
224
|
});
|
|
223
225
|
});
|
|
224
226
|
}
|
|
@@ -228,7 +230,7 @@ exports.resolveDiscriminator = resolveDiscriminator;
|
|
|
228
230
|
* Extract all types from #/components/schemas
|
|
229
231
|
*/
|
|
230
232
|
const generateSchemasDefinition = (schemas = {}) => {
|
|
231
|
-
if (
|
|
233
|
+
if ((0, isEmpty_1.default)(schemas)) {
|
|
232
234
|
return '';
|
|
233
235
|
}
|
|
234
236
|
return (Object.entries(schemas)
|
|
@@ -239,7 +241,7 @@ const generateSchemasDefinition = (schemas = {}) => {
|
|
|
239
241
|
!(0, exports.isReference)(schema) &&
|
|
240
242
|
!schema.nullable
|
|
241
243
|
? (0, exports.generateInterface)(name, schema)
|
|
242
|
-
: `${(0, exports.formatDescription)((0, exports.isReference)(schema) ? undefined : schema.description)}export type ${pascal(name)} = ${(0, exports.resolveValue)(schema)};`)
|
|
244
|
+
: `${(0, exports.formatDescription)((0, exports.isReference)(schema) ? undefined : schema.description)}export type ${(0, case_1.pascal)(name)} = ${(0, exports.resolveValue)(schema)};`)
|
|
243
245
|
.join('\n\n') + '\n');
|
|
244
246
|
};
|
|
245
247
|
exports.generateSchemasDefinition = generateSchemasDefinition;
|
|
@@ -247,7 +249,7 @@ exports.generateSchemasDefinition = generateSchemasDefinition;
|
|
|
247
249
|
* Extract all types from #/components/requestBodies
|
|
248
250
|
*/
|
|
249
251
|
const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
250
|
-
if (
|
|
252
|
+
if ((0, isEmpty_1.default)(requestBodies)) {
|
|
251
253
|
return '';
|
|
252
254
|
}
|
|
253
255
|
return ('\n' +
|
|
@@ -257,13 +259,13 @@ const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
|
257
259
|
const type = (0, exports.getResReqTypes)([['', requestBody]]);
|
|
258
260
|
const isEmptyInterface = type === '{}';
|
|
259
261
|
if (isEmptyInterface) {
|
|
260
|
-
return `export interface ${pascal(name)}RequestBody ${type}`;
|
|
262
|
+
return `export interface ${(0, case_1.pascal)(name)}RequestBody ${type}`;
|
|
261
263
|
}
|
|
262
264
|
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
263
|
-
return `${doc}export interface ${pascal(name)}RequestBody ${type}`;
|
|
265
|
+
return `${doc}export interface ${(0, case_1.pascal)(name)}RequestBody ${type}`;
|
|
264
266
|
}
|
|
265
267
|
else {
|
|
266
|
-
return `${doc}export type ${pascal(name)}RequestBody = ${type};`;
|
|
268
|
+
return `${doc}export type ${(0, case_1.pascal)(name)}RequestBody = ${type};`;
|
|
267
269
|
}
|
|
268
270
|
})
|
|
269
271
|
.join('\n\n') +
|
|
@@ -274,7 +276,7 @@ exports.generateRequestBodiesDefinition = generateRequestBodiesDefinition;
|
|
|
274
276
|
* Extract all types from #/components/responses
|
|
275
277
|
*/
|
|
276
278
|
const generateResponsesDefinition = (responses = {}) => {
|
|
277
|
-
if (
|
|
279
|
+
if ((0, isEmpty_1.default)(responses)) {
|
|
278
280
|
return '';
|
|
279
281
|
}
|
|
280
282
|
return ('\n' +
|
|
@@ -284,13 +286,13 @@ const generateResponsesDefinition = (responses = {}) => {
|
|
|
284
286
|
const type = (0, exports.getResReqTypes)([['', response]]);
|
|
285
287
|
const isEmptyInterface = type === '{}';
|
|
286
288
|
if (isEmptyInterface) {
|
|
287
|
-
return `export interface RQ${pascal(name)}Response ${type}`;
|
|
289
|
+
return `export interface RQ${(0, case_1.pascal)(name)}Response ${type}`;
|
|
288
290
|
}
|
|
289
291
|
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
290
|
-
return `${doc}export interface RQ${pascal(name)}Response ${type}`;
|
|
292
|
+
return `${doc}export interface RQ${(0, case_1.pascal)(name)}Response ${type}`;
|
|
291
293
|
}
|
|
292
294
|
else {
|
|
293
|
-
return `${doc}export type RQ${pascal(name)}Response = ${type};`;
|
|
295
|
+
return `${doc}export type RQ${(0, case_1.pascal)(name)}Response = ${type};`;
|
|
294
296
|
}
|
|
295
297
|
})
|
|
296
298
|
.join('\n\n') +
|
|
@@ -304,13 +306,13 @@ const formatDescription = (description, tabSize = 0) => description
|
|
|
304
306
|
? `/**\n${description
|
|
305
307
|
.split('\n')
|
|
306
308
|
.map((i) => `${' '.repeat(tabSize)} * ${i}`)
|
|
307
|
-
.join('\n')}\n${' '.repeat(tabSize)}
|
|
309
|
+
.join('\n')}\n${' '.repeat(tabSize)} */${' '.repeat(tabSize)}`
|
|
308
310
|
: '';
|
|
309
311
|
exports.formatDescription = formatDescription;
|
|
310
312
|
/**
|
|
311
313
|
* Generate a react-query component from openapi operation specs
|
|
312
314
|
*/
|
|
313
|
-
const generateRestfulComponent = (operation, verb, route, operationIds, parameters
|
|
315
|
+
const generateRestfulComponent = ({ operation, verb, route, operationIds, parameters, schemasComponents, headerFilters, }) => {
|
|
314
316
|
const { operationId = route.replace('/', '') } = operation;
|
|
315
317
|
if (operationId === '*') {
|
|
316
318
|
throw new Error(`Invalid operationId/Route set for ${verb} ${route}`);
|
|
@@ -322,20 +324,21 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
322
324
|
route = route.replace(/\{/g, '${'); // `/pet/{id}` => `/pet/${id}`
|
|
323
325
|
// Remove the last param of the route if we are in the DELETE case
|
|
324
326
|
let lastParamInTheRoute = null;
|
|
325
|
-
const componentName = pascal(operationId);
|
|
327
|
+
const componentName = (0, case_1.pascal)(operationId);
|
|
326
328
|
const isOk = ([statusCode]) => statusCode.toString().startsWith('2');
|
|
327
329
|
const responseTypes = (0, exports.getResReqTypes)(Object.entries(operation.responses).filter(isOk)) || 'void';
|
|
328
330
|
const requestBodyTypes = (0, exports.getResReqTypes)([['body', operation.requestBody]]);
|
|
329
331
|
const needAResponseComponent = true;
|
|
330
332
|
const paramsInPath = (0, exports.getParamsInPath)(route).filter((param) => !(verb === 'delete' && param === lastParamInTheRoute));
|
|
331
|
-
const { query: queryParams = [], path: pathParams = [], header
|
|
333
|
+
const { query: queryParams = [], path: pathParams = [], header = [], } = (0, groupBy_1.default)([...(parameters || []), ...(operation.parameters || [])].map((p) => {
|
|
332
334
|
if ((0, exports.isReference)(p)) {
|
|
333
|
-
return
|
|
335
|
+
return (0, get_1.default)(schemasComponents, p.$ref.replace('#/components/', '').replace('/', '.'));
|
|
334
336
|
}
|
|
335
337
|
else {
|
|
336
338
|
return p;
|
|
337
339
|
}
|
|
338
340
|
}), 'in');
|
|
341
|
+
const headerParams = header.filter((p) => !headerFilters?.includes(p.name));
|
|
339
342
|
const paramsTypes = paramsInPath
|
|
340
343
|
.map((p) => {
|
|
341
344
|
try {
|
|
@@ -391,52 +394,43 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
391
394
|
const headerParam = headerType && headerType !== 'void' ? `${headerType};` : '';
|
|
392
395
|
const queryParam = queryParamsType && queryParamsType !== 'void' ? `${queryParamsType}` : '';
|
|
393
396
|
const requestBodyComponent = requestBodyTypes && requestBodyTypes !== 'void' ? `${requestBodyTypes}` : '';
|
|
394
|
-
console.log(headerParam);
|
|
395
397
|
// QUERIES
|
|
396
398
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
397
|
-
output += `
|
|
398
|
-
|
|
399
|
-
|
|
399
|
+
output += `
|
|
400
|
+
type ${componentName}Variables = {
|
|
401
|
+
${paramsTypes}
|
|
400
402
|
}
|
|
401
|
-
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
402
|
-
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
403
|
-
{ enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
404
|
-
);}
|
|
405
403
|
|
|
406
404
|
use${componentName}Query.fetch = async (props: Omit<${componentName}QueryProps, 'options'> ) => {
|
|
407
405
|
const result = await api.${verb}<${genericsTypes}>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
408
406
|
return result.data;
|
|
409
407
|
}
|
|
410
|
-
|
|
411
408
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
409
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
412
410
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
`;
|
|
416
|
-
output += `interface ${componentName}MutationVariables {
|
|
417
|
-
${paramsTypes}
|
|
411
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
412
|
+
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
418
413
|
}
|
|
419
|
-
|
|
414
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
415
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
416
|
+
{ enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
417
|
+
);}
|
|
418
|
+
|
|
419
|
+
type ${componentName}MutationProps<T> = {
|
|
420
420
|
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}MutationVariables, T>
|
|
421
421
|
}
|
|
422
422
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
423
|
-
|
|
424
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}\`)
|
|
425
|
-
return result.data
|
|
426
|
-
},
|
|
423
|
+
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
427
424
|
props?.options
|
|
428
425
|
)};`;
|
|
429
426
|
}
|
|
430
427
|
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
431
|
-
output += `
|
|
432
|
-
|
|
428
|
+
output += `
|
|
429
|
+
type ${componentName}Variables = {
|
|
430
|
+
${paramsTypes}
|
|
433
431
|
${queryParamsType};
|
|
434
|
-
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
435
432
|
}
|
|
436
|
-
|
|
437
|
-
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
438
|
-
);}
|
|
439
|
-
|
|
433
|
+
|
|
440
434
|
use${componentName}Query.fetch = async (props: Omit<${componentName}QueryProps, 'options'>) => {
|
|
441
435
|
const {${paramsInPath.join(', ')}, ...queryParams} = props
|
|
442
436
|
const params = queryString.stringify(queryParams);
|
|
@@ -445,67 +439,58 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
445
439
|
}
|
|
446
440
|
|
|
447
441
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
448
|
-
|
|
449
442
|
use${componentName}Query.queryKey = (params: Omit<${componentName}QueryProps, 'options'>): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
${queryParamsType}
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> ${componentName}Variables & {
|
|
446
|
+
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
455
447
|
}
|
|
456
|
-
|
|
457
|
-
|
|
448
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
449
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
450
|
+
);}
|
|
451
|
+
|
|
452
|
+
type ${componentName}MutationProps<T> = {
|
|
453
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
458
454
|
}
|
|
459
455
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
460
|
-
return useMutation(async (data) => {
|
|
461
|
-
const {${paramsInPath.join(', ')}, ...queryParams} = data
|
|
462
|
-
const params = queryString.stringify(queryParams);
|
|
463
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
464
|
-
return result.data
|
|
465
|
-
},
|
|
456
|
+
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
466
457
|
props?.options
|
|
467
458
|
)};`;
|
|
468
459
|
}
|
|
469
460
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
470
|
-
output += `
|
|
461
|
+
output += `
|
|
462
|
+
use${componentName}Query.fetch = async () => {
|
|
463
|
+
const result = await api.${verb}<${genericsTypes}>(\`${route}\`);
|
|
464
|
+
return result.data;
|
|
465
|
+
}
|
|
466
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
467
|
+
use${componentName}Query.queryKey = (): QueryKey => use${componentName}Query.baseKey()
|
|
468
|
+
|
|
469
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = {
|
|
471
470
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
472
471
|
}
|
|
473
472
|
export function use${componentName}Query<T = ${genericsTypes}>(props?: ${componentName}QueryProps<T>) {
|
|
474
473
|
return useQuery(use${componentName}Query.queryKey(), use${componentName}Query.fetch, props?.options
|
|
475
|
-
);}
|
|
476
|
-
|
|
477
|
-
use${componentName}Query.fetch = async () => {
|
|
478
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}\`);
|
|
479
|
-
return result.data;
|
|
480
|
-
}
|
|
474
|
+
);}
|
|
481
475
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
use${componentName}Query.queryKey = (): QueryKey => use${componentName}Query.baseKey()
|
|
485
|
-
|
|
486
|
-
`;
|
|
487
|
-
output += `interface ${componentName}MutationProps<T> {
|
|
476
|
+
type ${componentName}MutationProps<T> = {
|
|
488
477
|
options?: UseMutationOptions<${genericsTypes}, AxiosError, void, T>
|
|
489
478
|
}
|
|
490
479
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
491
|
-
return useMutation(async () => {
|
|
492
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}\`);
|
|
493
|
-
return result.data;
|
|
494
|
-
},
|
|
480
|
+
return useMutation(async () => use${componentName}Query.fetch(),
|
|
495
481
|
props?.options
|
|
496
482
|
)};`;
|
|
497
483
|
}
|
|
498
484
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
499
|
-
output += `
|
|
500
|
-
|
|
501
|
-
|
|
485
|
+
output += `
|
|
486
|
+
// USE AS EXAMPLE
|
|
487
|
+
type ${componentName}Variables = {
|
|
488
|
+
${queryParamsType}
|
|
502
489
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
{
|
|
506
|
-
)
|
|
507
|
-
|
|
508
|
-
use${componentName}Query.fetch = async (props: Omit<${componentName}QueryProps, 'options'>) => {
|
|
490
|
+
|
|
491
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
492
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
493
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
509
494
|
const params = queryString.stringify({${queryParams
|
|
510
495
|
.map((param) => `${param.name}: props.${param.name}`)
|
|
511
496
|
.join(',')}});
|
|
@@ -513,37 +498,20 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
513
498
|
return result.data;
|
|
514
499
|
}
|
|
515
500
|
|
|
516
|
-
use${componentName}Query
|
|
517
|
-
|
|
518
|
-
|
|
501
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}Variables & { options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>}) {
|
|
502
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
503
|
+
{ enabled: !!props${queryParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
504
|
+
);}
|
|
519
505
|
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}MutationVariables, T>
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
529
|
-
return useMutation(async (data) => {
|
|
530
|
-
const params = queryString.stringify({${queryParams
|
|
531
|
-
.map((param) => `${param.name}: data.${param.name}`)
|
|
532
|
-
.join(',')}});
|
|
533
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
534
|
-
return result.data;
|
|
535
|
-
},
|
|
536
|
-
props?.options
|
|
537
|
-
)};`;
|
|
506
|
+
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: { options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>}) {
|
|
507
|
+
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
508
|
+
props?.options
|
|
509
|
+
)};
|
|
510
|
+
`;
|
|
538
511
|
}
|
|
539
512
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
540
|
-
output += `
|
|
541
|
-
|
|
542
|
-
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
543
|
-
}
|
|
544
|
-
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
545
|
-
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
546
|
-
);}
|
|
513
|
+
output += `
|
|
514
|
+
type ${componentName}QueryVariables = ${requestBodyComponent};
|
|
547
515
|
|
|
548
516
|
use${componentName}Query.fetch = async (body: Omit<${componentName}QueryProps, 'options'>) => {
|
|
549
517
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
@@ -551,46 +519,49 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
551
519
|
}
|
|
552
520
|
|
|
553
521
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
554
|
-
|
|
555
522
|
use${componentName}Query.queryKey = (params: Omit<${componentName}QueryProps, 'options'> ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
556
523
|
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
524
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}QueryVariables & {
|
|
525
|
+
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
526
|
+
}
|
|
527
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
528
|
+
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
529
|
+
);}
|
|
530
|
+
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
type ${componentName}MutationVariables = ${requestBodyComponent};
|
|
534
|
+
type ${componentName}MutationProps<T> = {
|
|
560
535
|
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}MutationVariables, T>
|
|
561
536
|
}
|
|
562
537
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
563
|
-
|
|
564
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
565
|
-
return result.data
|
|
566
|
-
},
|
|
538
|
+
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
567
539
|
props?.options
|
|
568
540
|
)};`;
|
|
569
541
|
}
|
|
570
542
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
571
|
-
output += `
|
|
543
|
+
output += `
|
|
544
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${requestBodyComponent
|
|
572
545
|
.replace('{', '')
|
|
573
|
-
.replace('}', '')} {
|
|
546
|
+
.replace('}', '')} & {
|
|
574
547
|
${paramsTypes};
|
|
575
548
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
576
549
|
}
|
|
577
|
-
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
578
|
-
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
579
|
-
);}
|
|
580
550
|
|
|
581
551
|
use${componentName}Query.fetch = async (props: Omit<${componentName}QueryProps, 'options'>) => {
|
|
582
552
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
583
553
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
584
554
|
return result.data
|
|
585
555
|
}
|
|
586
|
-
|
|
587
556
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
588
|
-
|
|
589
557
|
use${componentName}Query.queryKey = (params: Omit<${componentName}QueryProps, 'options'> ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
590
558
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
559
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
560
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
561
|
+
);}
|
|
562
|
+
|
|
563
|
+
type ${componentName}MutationVariables = {${paramsTypes}} & ${requestBodyComponent}
|
|
564
|
+
type ${componentName}MutationProps<T> = {
|
|
594
565
|
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}MutationVariables, T>
|
|
595
566
|
}
|
|
596
567
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
@@ -612,9 +583,21 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
612
583
|
}
|
|
613
584
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
614
585
|
output += `
|
|
615
|
-
//
|
|
616
|
-
type ${componentName}
|
|
617
|
-
|
|
586
|
+
// DONE
|
|
587
|
+
type ${componentName}Variables = {
|
|
588
|
+
${headerParam}
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
use${componentName}Query.fetch = async (headers: ${componentName}Variables) => {
|
|
592
|
+
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, {headers: headers});
|
|
593
|
+
return result.data;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
597
|
+
|
|
598
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
599
|
+
|
|
600
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
618
601
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
619
602
|
}
|
|
620
603
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
@@ -622,95 +605,89 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
622
605
|
{ enabled: !!props${headerParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
623
606
|
);}
|
|
624
607
|
|
|
625
|
-
use${componentName}Query.fetch = async (headers: ${componentName}HeaderVariables) => {
|
|
626
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, {headers: headers});
|
|
627
|
-
return result.data;
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
631
608
|
|
|
632
|
-
use${componentName}Query.queryKey = (params: Omit<${componentName}QueryProps, 'options'> ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
633
609
|
|
|
634
610
|
type ${componentName}MutationProps<T> = {
|
|
635
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError,
|
|
611
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
636
612
|
}
|
|
637
613
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
638
|
-
return useMutation(async () => {
|
|
639
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}\`);
|
|
640
|
-
return result.data;
|
|
641
|
-
},
|
|
614
|
+
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
642
615
|
props?.options
|
|
643
616
|
)};`;
|
|
644
617
|
}
|
|
645
618
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
646
|
-
output += `
|
|
619
|
+
output += `
|
|
620
|
+
// DONE
|
|
621
|
+
type ${componentName}Variables = {
|
|
622
|
+
${headerParam}
|
|
647
623
|
${queryParamsType};
|
|
624
|
+
};
|
|
625
|
+
|
|
626
|
+
use${componentName}Query.fetch = async (data: ${componentName}Variables) => {
|
|
627
|
+
const params = queryString.stringify({
|
|
628
|
+
${queryParams.map((param) => `${param.name}: data.${param.name}`).join(',')}
|
|
629
|
+
});
|
|
630
|
+
const headers = {
|
|
631
|
+
${headerParams.map((param) => `"${param.name}": data["${param.name}"]`).join(',')}
|
|
632
|
+
}
|
|
633
|
+
// HIHI
|
|
634
|
+
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`, {headers})
|
|
635
|
+
return result.data;
|
|
636
|
+
}
|
|
637
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
638
|
+
use${componentName}Query.queryKey = (params: Omit<${componentName}QueryProps, 'options'> ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
639
|
+
|
|
640
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
648
641
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
649
642
|
}
|
|
650
643
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
651
644
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
652
|
-
{ enabled: !!props
|
|
645
|
+
{ enabled: !!props${[...queryParams, ...headerParams]
|
|
646
|
+
.map((param) => `["${param.name}"]`)
|
|
647
|
+
.join(' && !!props')}, ...options }
|
|
653
648
|
);}
|
|
654
649
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
.map((param) => `${param.name}: props.${param.name}`)
|
|
658
|
-
.join(',')}});
|
|
659
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
660
|
-
return result.data;
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
664
|
-
|
|
665
|
-
use${componentName}Query.queryKey = (params: Omit<${componentName}QueryProps, 'options'> ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
666
|
-
|
|
667
|
-
interface ${componentName}MutationVariables {
|
|
668
|
-
${queryParamsType}
|
|
669
|
-
}
|
|
670
|
-
interface ${componentName}MutationProps<T> {
|
|
671
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}MutationVariables, T>
|
|
650
|
+
type ${componentName}MutationProps<T> = {
|
|
651
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
672
652
|
}
|
|
673
653
|
|
|
674
654
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
675
|
-
return useMutation(async (data) => {
|
|
676
|
-
const params = queryString.stringify({${queryParams
|
|
677
|
-
.map((param) => `${param.name}: data.${param.name}`)
|
|
678
|
-
.join(',')}});
|
|
679
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
680
|
-
return result.data;
|
|
681
|
-
},
|
|
655
|
+
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
682
656
|
props?.options
|
|
683
657
|
)};`;
|
|
684
658
|
}
|
|
685
659
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
686
660
|
output += `
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
}
|
|
691
|
-
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
692
|
-
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
693
|
-
);}
|
|
661
|
+
// HEHEHEHEHE
|
|
662
|
+
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
663
|
+
${headerParam}
|
|
664
|
+
};
|
|
694
665
|
|
|
695
|
-
use${componentName}Query.fetch = async (body:
|
|
696
|
-
const
|
|
666
|
+
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
667
|
+
const headers = {
|
|
668
|
+
${headerParams.map((param) => `"${param.name}": body["${param.name}"]`).join(',')}
|
|
669
|
+
}
|
|
670
|
+
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body, {headers})
|
|
697
671
|
return result.data
|
|
698
672
|
}
|
|
699
|
-
|
|
700
673
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
674
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
675
|
+
|
|
676
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
677
|
+
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
678
|
+
}
|
|
679
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
680
|
+
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body),{ enabled: !!body${headerParams
|
|
681
|
+
.map((param) => `["${param.name}"]`)
|
|
682
|
+
.join(' && !!props')}, ...options }
|
|
683
|
+
);}
|
|
701
684
|
|
|
702
|
-
use${componentName}Query.queryKey = (params: Omit<${componentName}QueryProps, 'options'> ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
703
|
-
|
|
704
|
-
type ${componentName}MutationVariables = ${requestBodyComponent};
|
|
705
685
|
|
|
706
|
-
|
|
707
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
686
|
+
type ${componentName}MutationProps<T> = {
|
|
687
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
708
688
|
}
|
|
709
689
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
710
|
-
return useMutation(async (body) => {
|
|
711
|
-
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
712
|
-
return result.data
|
|
713
|
-
},
|
|
690
|
+
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
714
691
|
props?.options
|
|
715
692
|
)};`;
|
|
716
693
|
}
|
|
@@ -720,33 +697,51 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
720
697
|
return output;
|
|
721
698
|
};
|
|
722
699
|
exports.generateRestfulComponent = generateRestfulComponent;
|
|
700
|
+
const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilters) => {
|
|
701
|
+
let output = '';
|
|
702
|
+
Object.entries(paths).forEach(([route, verbs]) => {
|
|
703
|
+
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
704
|
+
if (['get', 'post', 'patch', 'put', 'delete'].includes(verb) && !operation.deprecated) {
|
|
705
|
+
output += (0, exports.generateRestfulComponent)({
|
|
706
|
+
operation,
|
|
707
|
+
verb,
|
|
708
|
+
route,
|
|
709
|
+
operationIds,
|
|
710
|
+
parameters: verbs.parameters,
|
|
711
|
+
schemasComponents,
|
|
712
|
+
headerFilters,
|
|
713
|
+
});
|
|
714
|
+
}
|
|
715
|
+
});
|
|
716
|
+
});
|
|
717
|
+
return output;
|
|
718
|
+
};
|
|
723
719
|
/**
|
|
724
720
|
* Main entry of the generator. Generate react-query component from openAPI.
|
|
725
721
|
*/
|
|
726
|
-
const importOpenApi = async ({ data, format, apiDir, }) => {
|
|
722
|
+
const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
727
723
|
const operationIds = [];
|
|
728
724
|
let specs = await importSpecs(data, format);
|
|
729
725
|
(0, exports.resolveDiscriminator)(specs);
|
|
730
|
-
let output =
|
|
726
|
+
let output = '';
|
|
727
|
+
output = `
|
|
731
728
|
import { useQuery, useMutation, UseQueryOptions, UseMutationOptions, QueryKey } from 'react-query';
|
|
732
729
|
import queryString from 'query-string';
|
|
733
730
|
import {AxiosError} from 'axios';
|
|
734
731
|
import { api } from '${apiDir}';
|
|
732
|
+
|
|
733
|
+
// SCEHMAS
|
|
734
|
+
${(0, exports.generateSchemasDefinition)(specs.components?.schemas)}
|
|
735
|
+
|
|
736
|
+
// RESPONSES
|
|
737
|
+
${(0, exports.generateResponsesDefinition)(specs.components?.responses)}
|
|
738
|
+
|
|
739
|
+
// REQUEST BODIES
|
|
740
|
+
${(0, exports.generateRequestBodiesDefinition)(specs.components?.requestBodies)}
|
|
741
|
+
|
|
742
|
+
// HOOKS
|
|
743
|
+
${generateQueryHooks(specs.paths, operationIds, specs.components, headerFilters)}
|
|
735
744
|
`;
|
|
736
|
-
output += '\n\n// SCEHMAS\n';
|
|
737
|
-
output += (0, exports.generateSchemasDefinition)(specs.components && specs.components.schemas);
|
|
738
|
-
output += '\n\n// RESPONSES\n';
|
|
739
|
-
output += (0, exports.generateResponsesDefinition)(specs.components && specs.components.responses);
|
|
740
|
-
output += '\n\n// REQUEST BODIES\n';
|
|
741
|
-
output += (0, exports.generateRequestBodiesDefinition)(specs.components && specs.components.requestBodies);
|
|
742
|
-
output += '\n\n// HOOKS\n';
|
|
743
|
-
Object.entries(specs.paths).forEach(([route, verbs]) => {
|
|
744
|
-
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
745
|
-
if (['get', 'post', 'patch', 'put', 'delete'].includes(verb) && !operation.deprecated) {
|
|
746
|
-
output += (0, exports.generateRestfulComponent)(operation, verb, route, operationIds, verbs.parameters, specs.components);
|
|
747
|
-
}
|
|
748
|
-
});
|
|
749
|
-
});
|
|
750
745
|
return output;
|
|
751
746
|
};
|
|
752
747
|
exports.importOpenApi = importOpenApi;
|