react-query-lightbase-codegen 0.0.13 → 0.0.16
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 +72 -37
- package/lib/esm/import-open-api.js +72 -37
- 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 & {
|
|
@@ -483,7 +485,6 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
483
485
|
}
|
|
484
486
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
485
487
|
output += `
|
|
486
|
-
// USE AS EXAMPLE
|
|
487
488
|
type ${componentName}Variables = {
|
|
488
489
|
${queryParamsType}
|
|
489
490
|
}
|
|
@@ -492,7 +493,7 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
492
493
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
493
494
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
494
495
|
const params = queryString.stringify({${queryParams
|
|
495
|
-
.map((param) =>
|
|
496
|
+
.map((param) => `"${param.name}": props["${param.name}"]`)
|
|
496
497
|
.join(',')}});
|
|
497
498
|
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
498
499
|
return result.data;
|
|
@@ -511,28 +512,25 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
511
512
|
}
|
|
512
513
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
513
514
|
output += `
|
|
514
|
-
type ${componentName}
|
|
515
|
+
type ${componentName}Variables = ${requestBodyComponent};
|
|
515
516
|
|
|
516
|
-
use${componentName}Query.fetch = async (body:
|
|
517
|
+
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
517
518
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
518
519
|
return result.data
|
|
519
520
|
}
|
|
520
521
|
|
|
521
522
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
522
|
-
use${componentName}Query.queryKey = (params:
|
|
523
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
523
524
|
|
|
524
|
-
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}
|
|
525
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
525
526
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
526
527
|
}
|
|
527
528
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
528
529
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
529
530
|
);}
|
|
530
531
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
type ${componentName}MutationVariables = ${requestBodyComponent};
|
|
534
532
|
type ${componentName}MutationProps<T> = {
|
|
535
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
533
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
536
534
|
}
|
|
537
535
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
538
536
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -541,49 +539,87 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
541
539
|
}
|
|
542
540
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
543
541
|
output += `
|
|
544
|
-
type ${componentName}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
${paramsTypes};
|
|
542
|
+
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
543
|
+
|
|
544
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
548
545
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
549
546
|
}
|
|
550
547
|
|
|
551
|
-
use${componentName}Query.fetch = async (props:
|
|
548
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
552
549
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
553
550
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
554
551
|
return result.data
|
|
555
552
|
}
|
|
556
553
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
557
|
-
use${componentName}Query.queryKey = (params:
|
|
554
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
558
555
|
|
|
559
556
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
560
557
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
561
558
|
);}
|
|
562
559
|
|
|
563
|
-
type ${componentName}MutationVariables = {${paramsTypes}} & ${requestBodyComponent}
|
|
564
560
|
type ${componentName}MutationProps<T> = {
|
|
565
|
-
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>
|
|
566
600
|
}
|
|
567
601
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
568
602
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
569
603
|
props?.options
|
|
570
604
|
)};`;
|
|
571
605
|
}
|
|
572
|
-
if (requestBodyComponent && queryParam && !headerParam) {
|
|
573
|
-
output += `// TODO:
|
|
606
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
607
|
+
output += `// TODO: NOT SUPPORTED 2`;
|
|
574
608
|
}
|
|
575
|
-
if (requestBodyComponent && queryParam && headerParam) {
|
|
576
|
-
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`;
|
|
577
614
|
}
|
|
578
615
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
579
|
-
output += `// TODO:
|
|
616
|
+
output += `// TODO: NOT SUPPORTED 5`;
|
|
580
617
|
}
|
|
581
618
|
if (!requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
582
|
-
output += `// TODO:
|
|
619
|
+
output += `// TODO: NOT SUPPORTED 6`;
|
|
583
620
|
}
|
|
584
621
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
585
622
|
output += `
|
|
586
|
-
// DONE
|
|
587
623
|
type ${componentName}Variables = {
|
|
588
624
|
${headerParam}
|
|
589
625
|
};
|
|
@@ -617,7 +653,6 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
617
653
|
}
|
|
618
654
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
619
655
|
output += `
|
|
620
|
-
// DONE
|
|
621
656
|
type ${componentName}Variables = {
|
|
622
657
|
${headerParam}
|
|
623
658
|
${queryParamsType};
|
|
@@ -630,12 +665,11 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
630
665
|
const headers = {
|
|
631
666
|
${headerParams.map((param) => `"${param.name}": data["${param.name}"]`).join(',')}
|
|
632
667
|
}
|
|
633
|
-
// HIHI
|
|
634
668
|
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`, {headers})
|
|
635
669
|
return result.data;
|
|
636
670
|
}
|
|
637
671
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
638
|
-
use${componentName}Query.queryKey = (params:
|
|
672
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
639
673
|
|
|
640
674
|
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
641
675
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
@@ -658,7 +692,6 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
658
692
|
}
|
|
659
693
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
660
694
|
output += `
|
|
661
|
-
// HEHEHEHEHE
|
|
662
695
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
663
696
|
${headerParam}
|
|
664
697
|
};
|
|
@@ -697,7 +730,8 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
697
730
|
return output;
|
|
698
731
|
};
|
|
699
732
|
exports.generateRestfulComponent = generateRestfulComponent;
|
|
700
|
-
const generateQueryHooks = (
|
|
733
|
+
const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
734
|
+
const { paths, components } = spec;
|
|
701
735
|
let output = '';
|
|
702
736
|
Object.entries(paths).forEach(([route, verbs]) => {
|
|
703
737
|
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
@@ -705,10 +739,10 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
705
739
|
output += (0, exports.generateRestfulComponent)({
|
|
706
740
|
operation,
|
|
707
741
|
verb,
|
|
708
|
-
route,
|
|
742
|
+
route: spec.basePath + route,
|
|
709
743
|
operationIds,
|
|
710
744
|
parameters: verbs.parameters,
|
|
711
|
-
schemasComponents,
|
|
745
|
+
schemasComponents: components,
|
|
712
746
|
headerFilters,
|
|
713
747
|
});
|
|
714
748
|
}
|
|
@@ -722,6 +756,7 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
722
756
|
const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
723
757
|
const operationIds = [];
|
|
724
758
|
let specs = await importSpecs(data, format);
|
|
759
|
+
console.log(JSON.stringify(Object.keys(specs), null, 2));
|
|
725
760
|
(0, exports.resolveDiscriminator)(specs);
|
|
726
761
|
let output = '';
|
|
727
762
|
output = `
|
|
@@ -740,7 +775,7 @@ const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
|
740
775
|
${(0, exports.generateRequestBodiesDefinition)(specs.components?.requestBodies)}
|
|
741
776
|
|
|
742
777
|
// HOOKS
|
|
743
|
-
${generateQueryHooks(specs
|
|
778
|
+
${generateQueryHooks(specs, operationIds, headerFilters)}
|
|
744
779
|
`;
|
|
745
780
|
return output;
|
|
746
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 & {
|
|
@@ -463,7 +465,6 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
463
465
|
}
|
|
464
466
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
465
467
|
output += `
|
|
466
|
-
// USE AS EXAMPLE
|
|
467
468
|
type ${componentName}Variables = {
|
|
468
469
|
${queryParamsType}
|
|
469
470
|
}
|
|
@@ -472,7 +473,7 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
472
473
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
473
474
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
474
475
|
const params = queryString.stringify({${queryParams
|
|
475
|
-
.map((param) =>
|
|
476
|
+
.map((param) => `"${param.name}": props["${param.name}"]`)
|
|
476
477
|
.join(',')}});
|
|
477
478
|
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`)
|
|
478
479
|
return result.data;
|
|
@@ -491,28 +492,25 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
491
492
|
}
|
|
492
493
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
493
494
|
output += `
|
|
494
|
-
type ${componentName}
|
|
495
|
+
type ${componentName}Variables = ${requestBodyComponent};
|
|
495
496
|
|
|
496
|
-
use${componentName}Query.fetch = async (body:
|
|
497
|
+
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
497
498
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
498
499
|
return result.data
|
|
499
500
|
}
|
|
500
501
|
|
|
501
502
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
502
|
-
use${componentName}Query.queryKey = (params:
|
|
503
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
503
504
|
|
|
504
|
-
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}
|
|
505
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
505
506
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
506
507
|
}
|
|
507
508
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
508
509
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
509
510
|
);}
|
|
510
511
|
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
type ${componentName}MutationVariables = ${requestBodyComponent};
|
|
514
512
|
type ${componentName}MutationProps<T> = {
|
|
515
|
-
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}
|
|
513
|
+
options?: UseMutationOptions<${genericsTypes}, AxiosError, ${componentName}Variables, T>
|
|
516
514
|
}
|
|
517
515
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
518
516
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -521,49 +519,87 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
521
519
|
}
|
|
522
520
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
523
521
|
output += `
|
|
524
|
-
type ${componentName}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
${paramsTypes};
|
|
522
|
+
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
523
|
+
|
|
524
|
+
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
528
525
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
529
526
|
}
|
|
530
527
|
|
|
531
|
-
use${componentName}Query.fetch = async (props:
|
|
528
|
+
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
532
529
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
533
530
|
const result = await api.${verb}<${genericsTypes}>(\`${route}\`, body)
|
|
534
531
|
return result.data
|
|
535
532
|
}
|
|
536
533
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
537
|
-
use${componentName}Query.queryKey = (params:
|
|
534
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
538
535
|
|
|
539
536
|
export function use${componentName}Query<T = ${genericsTypes}>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
540
537
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
541
538
|
);}
|
|
542
539
|
|
|
543
|
-
type ${componentName}MutationVariables = {${paramsTypes}} & ${requestBodyComponent}
|
|
544
540
|
type ${componentName}MutationProps<T> = {
|
|
545
|
-
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>
|
|
546
580
|
}
|
|
547
581
|
export function use${componentName}Mutation<T = ${genericsTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
548
582
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
549
583
|
props?.options
|
|
550
584
|
)};`;
|
|
551
585
|
}
|
|
552
|
-
if (requestBodyComponent && queryParam && !headerParam) {
|
|
553
|
-
output += `// TODO:
|
|
586
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
587
|
+
output += `// TODO: NOT SUPPORTED 2`;
|
|
554
588
|
}
|
|
555
|
-
if (requestBodyComponent && queryParam && headerParam) {
|
|
556
|
-
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`;
|
|
557
594
|
}
|
|
558
595
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
559
|
-
output += `// TODO:
|
|
596
|
+
output += `// TODO: NOT SUPPORTED 5`;
|
|
560
597
|
}
|
|
561
598
|
if (!requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
562
|
-
output += `// TODO:
|
|
599
|
+
output += `// TODO: NOT SUPPORTED 6`;
|
|
563
600
|
}
|
|
564
601
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
565
602
|
output += `
|
|
566
|
-
// DONE
|
|
567
603
|
type ${componentName}Variables = {
|
|
568
604
|
${headerParam}
|
|
569
605
|
};
|
|
@@ -597,7 +633,6 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
597
633
|
}
|
|
598
634
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
599
635
|
output += `
|
|
600
|
-
// DONE
|
|
601
636
|
type ${componentName}Variables = {
|
|
602
637
|
${headerParam}
|
|
603
638
|
${queryParamsType};
|
|
@@ -610,12 +645,11 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
610
645
|
const headers = {
|
|
611
646
|
${headerParams.map((param) => `"${param.name}": data["${param.name}"]`).join(',')}
|
|
612
647
|
}
|
|
613
|
-
// HIHI
|
|
614
648
|
const result = await api.${verb}<${genericsTypes}>(\`${route}?\${params}\`, {headers})
|
|
615
649
|
return result.data;
|
|
616
650
|
}
|
|
617
651
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
618
|
-
use${componentName}Query.queryKey = (params:
|
|
652
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
619
653
|
|
|
620
654
|
type ${componentName}QueryProps<T = ${genericsTypes}> = ${componentName}Variables & {
|
|
621
655
|
options?: UseQueryOptions<${genericsTypes}, AxiosError, T, any>
|
|
@@ -638,7 +672,6 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
638
672
|
}
|
|
639
673
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
640
674
|
output += `
|
|
641
|
-
// HEHEHEHEHE
|
|
642
675
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
643
676
|
${headerParam}
|
|
644
677
|
};
|
|
@@ -676,7 +709,8 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
676
709
|
}
|
|
677
710
|
return output;
|
|
678
711
|
};
|
|
679
|
-
const generateQueryHooks = (
|
|
712
|
+
const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
713
|
+
const { paths, components } = spec;
|
|
680
714
|
let output = '';
|
|
681
715
|
Object.entries(paths).forEach(([route, verbs]) => {
|
|
682
716
|
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
@@ -684,10 +718,10 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
684
718
|
output += generateRestfulComponent({
|
|
685
719
|
operation,
|
|
686
720
|
verb,
|
|
687
|
-
route,
|
|
721
|
+
route: spec.basePath + route,
|
|
688
722
|
operationIds,
|
|
689
723
|
parameters: verbs.parameters,
|
|
690
|
-
schemasComponents,
|
|
724
|
+
schemasComponents: components,
|
|
691
725
|
headerFilters,
|
|
692
726
|
});
|
|
693
727
|
}
|
|
@@ -701,6 +735,7 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
701
735
|
export const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
702
736
|
const operationIds = [];
|
|
703
737
|
let specs = await importSpecs(data, format);
|
|
738
|
+
console.log(JSON.stringify(Object.keys(specs), null, 2));
|
|
704
739
|
resolveDiscriminator(specs);
|
|
705
740
|
let output = '';
|
|
706
741
|
output = `
|
|
@@ -719,7 +754,7 @@ export const importOpenApi = async ({ data, format, apiDir, headerFilters = [],
|
|
|
719
754
|
${generateRequestBodiesDefinition(specs.components?.requestBodies)}
|
|
720
755
|
|
|
721
756
|
// HOOKS
|
|
722
|
-
${generateQueryHooks(specs
|
|
757
|
+
${generateQueryHooks(specs, operationIds, headerFilters)}
|
|
723
758
|
`;
|
|
724
759
|
return output;
|
|
725
760
|
};
|