react-query-lightbase-codegen 0.0.14 → 0.0.17
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 +71 -31
- package/lib/esm/import-open-api.js +71 -31
- package/package.json +1 -1
|
@@ -25,6 +25,8 @@ const importSpecs = (data, extension) => {
|
|
|
25
25
|
reject(err);
|
|
26
26
|
}
|
|
27
27
|
else {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
convertedObj.openapi.basePath = convertedObj.original.basePath;
|
|
28
30
|
resolve(convertedObj.openapi);
|
|
29
31
|
}
|
|
30
32
|
});
|
|
@@ -401,7 +403,7 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
401
403
|
${paramsTypes}
|
|
402
404
|
}
|
|
403
405
|
|
|
404
|
-
use${componentName}Query.fetch = async (props:
|
|
406
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables ) => {
|
|
405
407
|
const result = await api.${verb}<${genericsTypes}>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
406
408
|
return result.data;
|
|
407
409
|
}
|
|
@@ -417,7 +419,7 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
417
419
|
);}
|
|
418
420
|
|
|
419
421
|
type ${componentName}MutationProps<T> = {
|
|
420
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
422
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
421
423
|
}
|
|
422
424
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
423
425
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
@@ -431,7 +433,7 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
431
433
|
${queryParamsType};
|
|
432
434
|
}
|
|
433
435
|
|
|
434
|
-
use${componentName}Query.fetch = async (props
|
|
436
|
+
use${componentName}Query.fetch = async (props:${componentName}Variables) => {
|
|
435
437
|
const {${paramsInPath.join(', ')}, ...queryParams} = props
|
|
436
438
|
const params = queryString.stringify(queryParams);
|
|
437
439
|
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
@@ -439,7 +441,7 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
439
441
|
}
|
|
440
442
|
|
|
441
443
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
442
|
-
use${componentName}Query.queryKey = (params
|
|
444
|
+
use${componentName}Query.queryKey = (params:${componentName}Variables): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
443
445
|
|
|
444
446
|
|
|
445
447
|
type ${componentName}QueryProps<T = ${genericsTypes}> ${componentName}Variables & {
|
|
@@ -510,28 +512,25 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
510
512
|
}
|
|
511
513
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
512
514
|
output += `
|
|
513
|
-
type ${componentName}
|
|
515
|
+
type ${componentName}Variables = ${requestBodyComponent};
|
|
514
516
|
|
|
515
|
-
use${componentName}Query.fetch = async (body:
|
|
517
|
+
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
516
518
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
517
519
|
return result.data
|
|
518
520
|
}
|
|
519
521
|
|
|
520
522
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
521
|
-
use${componentName}Query.queryKey = (params:
|
|
523
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
522
524
|
|
|
523
|
-
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}
|
|
525
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
524
526
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
525
527
|
}
|
|
526
528
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
527
529
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
528
530
|
);}
|
|
529
531
|
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
type ${componentName}MutationVariables = ${requestBodyComponent};
|
|
533
532
|
type ${componentName}MutationProps<T> = {
|
|
534
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
533
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
535
534
|
}
|
|
536
535
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
537
536
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -540,45 +539,84 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
540
539
|
}
|
|
541
540
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
542
541
|
output += `
|
|
543
|
-
type ${componentName}
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
${paramsTypes};
|
|
542
|
+
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
543
|
+
|
|
544
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
547
545
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
548
546
|
}
|
|
549
547
|
|
|
550
|
-
use${componentName}Query.fetch = async (props:
|
|
548
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
551
549
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
552
550
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
553
551
|
return result.data
|
|
554
552
|
}
|
|
555
553
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
556
|
-
use${componentName}Query.queryKey = (params:
|
|
554
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
557
555
|
|
|
558
556
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
559
557
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
560
558
|
);}
|
|
561
559
|
|
|
562
|
-
type ${componentName}MutationVariables = {${paramsTypes}} & ${requestBodyComponent}
|
|
563
560
|
type ${componentName}MutationProps<T> = {
|
|
564
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
561
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
562
|
+
}
|
|
563
|
+
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
564
|
+
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
565
|
+
props?.options
|
|
566
|
+
)};`;
|
|
567
|
+
}
|
|
568
|
+
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
569
|
+
output += `
|
|
570
|
+
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
571
|
+
${queryParamsType}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
575
|
+
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
579
|
+
const {${queryParams
|
|
580
|
+
.map((param) => `["${param.name}"]: ${param.name.replaceAll('-', '')}`)
|
|
581
|
+
.join(',')}, ...body} = props
|
|
582
|
+
const params = queryString.stringify({
|
|
583
|
+
${queryParams.map((param) => `["${param.name}"]: props["${param.name}"]`).join(',')}
|
|
584
|
+
});
|
|
585
|
+
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`, body)
|
|
586
|
+
return result.data
|
|
587
|
+
}
|
|
588
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
589
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
590
|
+
|
|
591
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
592
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props${queryParams
|
|
593
|
+
.map((param) => `["${param.name}"]`)
|
|
594
|
+
.join(' && !!props')}, ...options }
|
|
595
|
+
);}
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
type ${componentName}MutationProps<T> = {
|
|
599
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
565
600
|
}
|
|
566
601
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
567
602
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
568
603
|
props?.options
|
|
569
604
|
)};`;
|
|
570
605
|
}
|
|
571
|
-
if (requestBodyComponent && queryParam && !headerParam) {
|
|
572
|
-
output += `// TODO:
|
|
606
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
607
|
+
output += `// TODO: NOT SUPPORTED 2`;
|
|
573
608
|
}
|
|
574
|
-
if (requestBodyComponent && queryParam && headerParam) {
|
|
575
|
-
output += `// TODO:
|
|
609
|
+
if (requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
610
|
+
output += `// TODO: NOT SUPPORTED 3`;
|
|
611
|
+
}
|
|
612
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
613
|
+
output += `// TODO: NOT SUPPORTED 4`;
|
|
576
614
|
}
|
|
577
615
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
578
|
-
output += `// TODO:
|
|
616
|
+
output += `// TODO: NOT SUPPORTED 5`;
|
|
579
617
|
}
|
|
580
618
|
if (!requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
581
|
-
output += `// TODO:
|
|
619
|
+
output += `// TODO: NOT SUPPORTED 6`;
|
|
582
620
|
}
|
|
583
621
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
584
622
|
output += `
|
|
@@ -631,7 +669,7 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
631
669
|
return result.data;
|
|
632
670
|
}
|
|
633
671
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
634
|
-
use${componentName}Query.queryKey = (params:
|
|
672
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
635
673
|
|
|
636
674
|
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
637
675
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
@@ -692,7 +730,8 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
692
730
|
return output;
|
|
693
731
|
};
|
|
694
732
|
exports.generateRestfulComponent = generateRestfulComponent;
|
|
695
|
-
const generateQueryHooks = (
|
|
733
|
+
const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
734
|
+
const { paths, components } = spec;
|
|
696
735
|
let output = '';
|
|
697
736
|
Object.entries(paths).forEach(([route, verbs]) => {
|
|
698
737
|
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
@@ -700,10 +739,10 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
700
739
|
output += (0, exports.generateRestfulComponent)({
|
|
701
740
|
operation,
|
|
702
741
|
verb,
|
|
703
|
-
route,
|
|
742
|
+
route: spec.basePath + route,
|
|
704
743
|
operationIds,
|
|
705
744
|
parameters: verbs.parameters,
|
|
706
|
-
schemasComponents,
|
|
745
|
+
schemasComponents: components,
|
|
707
746
|
headerFilters,
|
|
708
747
|
});
|
|
709
748
|
}
|
|
@@ -717,6 +756,7 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
717
756
|
const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
718
757
|
const operationIds = [];
|
|
719
758
|
let specs = await importSpecs(data, format);
|
|
759
|
+
console.log(JSON.stringify(Object.keys(specs), null, 2));
|
|
720
760
|
(0, exports.resolveDiscriminator)(specs);
|
|
721
761
|
let output = '';
|
|
722
762
|
output = `
|
|
@@ -735,7 +775,7 @@ const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
|
735
775
|
${(0, exports.generateRequestBodiesDefinition)(specs.components?.requestBodies)}
|
|
736
776
|
|
|
737
777
|
// HOOKS
|
|
738
|
-
${generateQueryHooks(specs
|
|
778
|
+
${generateQueryHooks(specs, operationIds, headerFilters)}
|
|
739
779
|
`;
|
|
740
780
|
return output;
|
|
741
781
|
};
|
|
@@ -19,6 +19,8 @@ const importSpecs = (data, extension) => {
|
|
|
19
19
|
reject(err);
|
|
20
20
|
}
|
|
21
21
|
else {
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
convertedObj.openapi.basePath = convertedObj.original.basePath;
|
|
22
24
|
resolve(convertedObj.openapi);
|
|
23
25
|
}
|
|
24
26
|
});
|
|
@@ -381,7 +383,7 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
381
383
|
${paramsTypes}
|
|
382
384
|
}
|
|
383
385
|
|
|
384
|
-
use${componentName}Query.fetch = async (props:
|
|
386
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables ) => {
|
|
385
387
|
const result = await api.${verb}<${genericsTypes}>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
386
388
|
return result.data;
|
|
387
389
|
}
|
|
@@ -397,7 +399,7 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
397
399
|
);}
|
|
398
400
|
|
|
399
401
|
type ${componentName}MutationProps<T> = {
|
|
400
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
402
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
401
403
|
}
|
|
402
404
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
403
405
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
@@ -411,7 +413,7 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
411
413
|
${queryParamsType};
|
|
412
414
|
}
|
|
413
415
|
|
|
414
|
-
use${componentName}Query.fetch = async (props
|
|
416
|
+
use${componentName}Query.fetch = async (props:${componentName}Variables) => {
|
|
415
417
|
const {${paramsInPath.join(', ')}, ...queryParams} = props
|
|
416
418
|
const params = queryString.stringify(queryParams);
|
|
417
419
|
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
@@ -419,7 +421,7 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
419
421
|
}
|
|
420
422
|
|
|
421
423
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
422
|
-
use${componentName}Query.queryKey = (params
|
|
424
|
+
use${componentName}Query.queryKey = (params:${componentName}Variables): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
423
425
|
|
|
424
426
|
|
|
425
427
|
type ${componentName}QueryProps<T = ${genericsTypes}> ${componentName}Variables & {
|
|
@@ -490,28 +492,25 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
490
492
|
}
|
|
491
493
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
492
494
|
output += `
|
|
493
|
-
type ${componentName}
|
|
495
|
+
type ${componentName}Variables = ${requestBodyComponent};
|
|
494
496
|
|
|
495
|
-
use${componentName}Query.fetch = async (body:
|
|
497
|
+
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
496
498
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
497
499
|
return result.data
|
|
498
500
|
}
|
|
499
501
|
|
|
500
502
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
501
|
-
use${componentName}Query.queryKey = (params:
|
|
503
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
502
504
|
|
|
503
|
-
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}
|
|
505
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
504
506
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
505
507
|
}
|
|
506
508
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
507
509
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
508
510
|
);}
|
|
509
511
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
type ${componentName}MutationVariables = ${requestBodyComponent};
|
|
513
512
|
type ${componentName}MutationProps<T> = {
|
|
514
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
513
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
515
514
|
}
|
|
516
515
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
517
516
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -520,45 +519,84 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
520
519
|
}
|
|
521
520
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
522
521
|
output += `
|
|
523
|
-
type ${componentName}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
${paramsTypes};
|
|
522
|
+
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
523
|
+
|
|
524
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
527
525
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
528
526
|
}
|
|
529
527
|
|
|
530
|
-
use${componentName}Query.fetch = async (props:
|
|
528
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
531
529
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
532
530
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
533
531
|
return result.data
|
|
534
532
|
}
|
|
535
533
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
536
|
-
use${componentName}Query.queryKey = (params:
|
|
534
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
537
535
|
|
|
538
536
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
539
537
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
540
538
|
);}
|
|
541
539
|
|
|
542
|
-
type ${componentName}MutationVariables = {${paramsTypes}} & ${requestBodyComponent}
|
|
543
540
|
type ${componentName}MutationProps<T> = {
|
|
544
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
541
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
542
|
+
}
|
|
543
|
+
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
544
|
+
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
545
|
+
props?.options
|
|
546
|
+
)};`;
|
|
547
|
+
}
|
|
548
|
+
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
549
|
+
output += `
|
|
550
|
+
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
551
|
+
${queryParamsType}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
555
|
+
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
559
|
+
const {${queryParams
|
|
560
|
+
.map((param) => `["${param.name}"]: ${param.name.replaceAll('-', '')}`)
|
|
561
|
+
.join(',')}, ...body} = props
|
|
562
|
+
const params = queryString.stringify({
|
|
563
|
+
${queryParams.map((param) => `["${param.name}"]: props["${param.name}"]`).join(',')}
|
|
564
|
+
});
|
|
565
|
+
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`, body)
|
|
566
|
+
return result.data
|
|
567
|
+
}
|
|
568
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
569
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
570
|
+
|
|
571
|
+
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
572
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props${queryParams
|
|
573
|
+
.map((param) => `["${param.name}"]`)
|
|
574
|
+
.join(' && !!props')}, ...options }
|
|
575
|
+
);}
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
type ${componentName}MutationProps<T> = {
|
|
579
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
545
580
|
}
|
|
546
581
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
547
582
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
548
583
|
props?.options
|
|
549
584
|
)};`;
|
|
550
585
|
}
|
|
551
|
-
if (requestBodyComponent && queryParam && !headerParam) {
|
|
552
|
-
output += `// TODO:
|
|
586
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
587
|
+
output += `// TODO: NOT SUPPORTED 2`;
|
|
553
588
|
}
|
|
554
|
-
if (requestBodyComponent && queryParam && headerParam) {
|
|
555
|
-
output += `// TODO:
|
|
589
|
+
if (requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
590
|
+
output += `// TODO: NOT SUPPORTED 3`;
|
|
591
|
+
}
|
|
592
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
593
|
+
output += `// TODO: NOT SUPPORTED 4`;
|
|
556
594
|
}
|
|
557
595
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
558
|
-
output += `// TODO:
|
|
596
|
+
output += `// TODO: NOT SUPPORTED 5`;
|
|
559
597
|
}
|
|
560
598
|
if (!requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
561
|
-
output += `// TODO:
|
|
599
|
+
output += `// TODO: NOT SUPPORTED 6`;
|
|
562
600
|
}
|
|
563
601
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
564
602
|
output += `
|
|
@@ -611,7 +649,7 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
611
649
|
return result.data;
|
|
612
650
|
}
|
|
613
651
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
614
|
-
use${componentName}Query.queryKey = (params:
|
|
652
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
615
653
|
|
|
616
654
|
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
617
655
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
@@ -671,7 +709,8 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
671
709
|
}
|
|
672
710
|
return output;
|
|
673
711
|
};
|
|
674
|
-
const generateQueryHooks = (
|
|
712
|
+
const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
713
|
+
const { paths, components } = spec;
|
|
675
714
|
let output = '';
|
|
676
715
|
Object.entries(paths).forEach(([route, verbs]) => {
|
|
677
716
|
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
@@ -679,10 +718,10 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
679
718
|
output += generateRestfulComponent({
|
|
680
719
|
operation,
|
|
681
720
|
verb,
|
|
682
|
-
route,
|
|
721
|
+
route: spec.basePath + route,
|
|
683
722
|
operationIds,
|
|
684
723
|
parameters: verbs.parameters,
|
|
685
|
-
schemasComponents,
|
|
724
|
+
schemasComponents: components,
|
|
686
725
|
headerFilters,
|
|
687
726
|
});
|
|
688
727
|
}
|
|
@@ -696,6 +735,7 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
696
735
|
export const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
697
736
|
const operationIds = [];
|
|
698
737
|
let specs = await importSpecs(data, format);
|
|
738
|
+
console.log(JSON.stringify(Object.keys(specs), null, 2));
|
|
699
739
|
resolveDiscriminator(specs);
|
|
700
740
|
let output = '';
|
|
701
741
|
output = `
|
|
@@ -714,7 +754,7 @@ export const importOpenApi = async ({ data, format, apiDir, headerFilters = [],
|
|
|
714
754
|
${generateRequestBodiesDefinition(specs.components?.requestBodies)}
|
|
715
755
|
|
|
716
756
|
// HOOKS
|
|
717
|
-
${generateQueryHooks(specs
|
|
757
|
+
${generateQueryHooks(specs, operationIds, headerFilters)}
|
|
718
758
|
`;
|
|
719
759
|
return output;
|
|
720
760
|
};
|