react-query-lightbase-codegen 0.0.19 → 0.1.0
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.
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import program from 'commander';
|
|
3
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
4
|
+
import { join, parse } from 'path';
|
|
5
|
+
import importOpenApi from '../scripts/import-open-api';
|
|
6
|
+
const log = console.log; // tslint:disable-line:no-console
|
|
7
|
+
program.option('-o, --output [value]', 'output file destination');
|
|
8
|
+
program.option('-f, --file [value]', 'input file (yaml or json openapi specs)');
|
|
9
|
+
program.parse(process.argv);
|
|
10
|
+
const createSuccessMessage = (backend) => chalk.green(`${backend ? `[${backend}] ` : ''}🎉 Your OpenAPI spec has been converted into react query hooks`);
|
|
11
|
+
const successWithoutOutputMessage = chalk.yellow('Success! No output path specified; printed to standard output.');
|
|
12
|
+
const importSpecs = async (options) => {
|
|
13
|
+
if (!options.file) {
|
|
14
|
+
throw new Error("You need to provide an input specification with `--file`, '--url', or `--github`");
|
|
15
|
+
}
|
|
16
|
+
const data = readFileSync(join(process.cwd(), options.file), 'utf-8');
|
|
17
|
+
const { ext } = parse(options.file);
|
|
18
|
+
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
19
|
+
return importOpenApi({
|
|
20
|
+
data,
|
|
21
|
+
format,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
// Use flags as configuration
|
|
25
|
+
importSpecs(program)
|
|
26
|
+
.then((data) => {
|
|
27
|
+
if (program.output) {
|
|
28
|
+
writeFileSync(join(process.cwd(), program.output), data);
|
|
29
|
+
log(createSuccessMessage());
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
log(data);
|
|
33
|
+
log(successWithoutOutputMessage);
|
|
34
|
+
}
|
|
35
|
+
})
|
|
36
|
+
.catch((err) => {
|
|
37
|
+
log(chalk.red(err));
|
|
38
|
+
process.exit(1);
|
|
39
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import program from 'commander';
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
const { version } = JSON.parse(readFileSync(join(__dirname, '../../package.json'), 'utf-8'));
|
|
5
|
+
program
|
|
6
|
+
.version(version)
|
|
7
|
+
.command('import [open-api-file]', 'generate react-query hooks from OpenAPI specs')
|
|
8
|
+
.parse(process.argv);
|
|
@@ -341,6 +341,13 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
341
341
|
}
|
|
342
342
|
}), 'in');
|
|
343
343
|
const headerParams = header.filter((p) => !headerFilters?.includes(p.name));
|
|
344
|
+
let enabled = [];
|
|
345
|
+
[...queryParams, ...pathParams, ...headerParams].forEach((item) => {
|
|
346
|
+
if (item.required) {
|
|
347
|
+
enabled.push(`["${item.name}"]`);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
const enabledParam = `!!props${enabled.join(' && !!props')}`;
|
|
344
351
|
const paramsTypes = paramsInPath
|
|
345
352
|
.map((p) => {
|
|
346
353
|
try {
|
|
@@ -396,7 +403,6 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
396
403
|
const headerParam = headerType && headerType !== 'void' ? `${headerType};` : '';
|
|
397
404
|
const queryParam = queryParamsType && queryParamsType !== 'void' ? `${queryParamsType}` : '';
|
|
398
405
|
const requestBodyComponent = requestBodyTypes && requestBodyTypes !== 'void' ? `${requestBodyTypes}` : '';
|
|
399
|
-
let AxiosErrorType = true ? 'AxiosError<AxiosErrorType | unknown>' : `AxiosError<any>`;
|
|
400
406
|
// QUERIES
|
|
401
407
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
402
408
|
output += `
|
|
@@ -421,19 +427,20 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
421
427
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
422
428
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
423
429
|
|
|
430
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
424
431
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
425
432
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
426
433
|
|
|
427
434
|
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
428
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
435
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
429
436
|
}
|
|
430
437
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
431
438
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
432
|
-
{ enabled:
|
|
439
|
+
{ enabled: ${enabledParam}, ...options }
|
|
433
440
|
);}
|
|
434
441
|
|
|
435
442
|
type ${componentName}MutationProps<T> = {
|
|
436
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
443
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
437
444
|
}
|
|
438
445
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
439
446
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
@@ -467,18 +474,20 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
467
474
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
468
475
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
469
476
|
|
|
477
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
470
478
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
471
479
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
472
480
|
|
|
473
481
|
type ${componentName}QueryProps<T = ${componentName}Response> ${componentName}Variables & {
|
|
474
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
482
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
475
483
|
}
|
|
476
484
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
477
|
-
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), {
|
|
485
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), {
|
|
486
|
+
enabled: ${enabledParam}, ...options }
|
|
478
487
|
);}
|
|
479
488
|
|
|
480
489
|
type ${componentName}MutationProps<T> = {
|
|
481
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
490
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
482
491
|
}
|
|
483
492
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
484
493
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
@@ -496,27 +505,28 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
496
505
|
use${componentName}Query.queryKey = (): QueryKey => use${componentName}Query.baseKey()
|
|
497
506
|
|
|
498
507
|
use${componentName}Query.updateCache = (
|
|
499
|
-
params:
|
|
508
|
+
params: undefined,
|
|
500
509
|
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
501
510
|
options?: SetDataOptions | undefined
|
|
502
|
-
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(
|
|
511
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(), updater, options);
|
|
503
512
|
|
|
504
|
-
use${componentName}Query.prefetch = (
|
|
505
|
-
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(
|
|
513
|
+
use${componentName}Query.prefetch = () =>
|
|
514
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(), ()=> use${componentName}Query.fetch());
|
|
506
515
|
|
|
507
|
-
use${componentName}Query.
|
|
508
|
-
|
|
516
|
+
use${componentName}Query.cancelQueries = () => queryClient.cancelQueries(use${componentName}Query.queryKey())
|
|
517
|
+
use${componentName}Query.invalidate = () =>
|
|
518
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey());
|
|
509
519
|
|
|
510
520
|
|
|
511
521
|
type ${componentName}QueryProps<T = ${componentName}Response> = {
|
|
512
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
522
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
513
523
|
}
|
|
514
524
|
export function use${componentName}Query<T = ${componentName}Response>(props?: ${componentName}QueryProps<T>) {
|
|
515
525
|
return useQuery(use${componentName}Query.queryKey(), use${componentName}Query.fetch, props?.options
|
|
516
526
|
);}
|
|
517
527
|
|
|
518
528
|
type ${componentName}MutationProps<T> = {
|
|
519
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
529
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, void, T>
|
|
520
530
|
}
|
|
521
531
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
522
532
|
return useMutation(async () => use${componentName}Query.fetch(),
|
|
@@ -550,15 +560,16 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
550
560
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
551
561
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
552
562
|
|
|
563
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
553
564
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
554
565
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
555
566
|
|
|
556
|
-
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}Variables & { options?: UseQueryOptions<${componentName}Response,
|
|
567
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}Variables & { options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>}) {
|
|
557
568
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
558
|
-
{ enabled:
|
|
569
|
+
{ enabled: ${enabledParam}, ...options }
|
|
559
570
|
);}
|
|
560
571
|
|
|
561
|
-
export function use${componentName}Mutation<T = ${componentName}Response>(props?: { options?: UseMutationOptions<${componentName}Response,
|
|
572
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: { options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>}) {
|
|
562
573
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
563
574
|
props?.options
|
|
564
575
|
)};
|
|
@@ -586,19 +597,20 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
586
597
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
587
598
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
588
599
|
|
|
600
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
589
601
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
590
602
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
591
603
|
|
|
592
604
|
|
|
593
605
|
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
594
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
606
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
595
607
|
}
|
|
596
608
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
597
609
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
598
610
|
);}
|
|
599
611
|
|
|
600
612
|
type ${componentName}MutationProps<T> = {
|
|
601
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
613
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
602
614
|
}
|
|
603
615
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
604
616
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -608,10 +620,13 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
608
620
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
609
621
|
output += `
|
|
610
622
|
type ${componentName}Response = ${genericsTypes}
|
|
611
|
-
type ${componentName}Variables =
|
|
623
|
+
type ${componentName}Variables = {
|
|
624
|
+
body: ${requestBodyComponent}
|
|
625
|
+
${paramsTypes}
|
|
626
|
+
}
|
|
612
627
|
|
|
613
628
|
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
614
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
629
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
615
630
|
}
|
|
616
631
|
|
|
617
632
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
@@ -631,16 +646,18 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
631
646
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
632
647
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
633
648
|
|
|
649
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
634
650
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
635
651
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
636
652
|
|
|
637
653
|
|
|
638
654
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
639
|
-
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), {
|
|
655
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), {
|
|
656
|
+
enabled: ${enabledParam}, ...options }
|
|
640
657
|
);}
|
|
641
658
|
|
|
642
659
|
type ${componentName}MutationProps<T> = {
|
|
643
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
660
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
644
661
|
}
|
|
645
662
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
646
663
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -650,22 +667,20 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
650
667
|
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
651
668
|
output += `
|
|
652
669
|
type ${componentName}Response = ${genericsTypes}
|
|
653
|
-
type ${componentName}Variables =
|
|
670
|
+
type ${componentName}Variables = {
|
|
671
|
+
body: ${requestBodyComponent}
|
|
654
672
|
${queryParamsType}
|
|
655
673
|
}
|
|
656
674
|
|
|
657
675
|
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
658
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
676
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
659
677
|
}
|
|
660
678
|
|
|
661
679
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
662
|
-
const {${queryParams
|
|
663
|
-
.map((param) => `["${param.name}"]: ${param.name.replaceAll('-', '')}`)
|
|
664
|
-
.join(',')}, ...body} = props
|
|
665
680
|
const params = queryString.stringify({
|
|
666
681
|
${queryParams.map((param) => `["${param.name}"]: props["${param.name}"]`).join(',')}
|
|
667
682
|
});
|
|
668
|
-
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, body)
|
|
683
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, props.body)
|
|
669
684
|
return result.data
|
|
670
685
|
}
|
|
671
686
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
@@ -680,19 +695,19 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
680
695
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
681
696
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
682
697
|
|
|
698
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
683
699
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
684
700
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
685
701
|
|
|
686
702
|
|
|
687
703
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
688
|
-
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), {
|
|
689
|
-
|
|
690
|
-
.join(' && !!props')}, ...options }
|
|
704
|
+
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), {
|
|
705
|
+
enabled: ${enabledParam}, ...options }
|
|
691
706
|
);}
|
|
692
707
|
|
|
693
708
|
|
|
694
709
|
type ${componentName}MutationProps<T> = {
|
|
695
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
710
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
696
711
|
}
|
|
697
712
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
698
713
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -738,22 +753,23 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
738
753
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
739
754
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
740
755
|
|
|
756
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
741
757
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
742
758
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
743
759
|
|
|
744
760
|
|
|
745
761
|
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
746
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
762
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
747
763
|
}
|
|
748
764
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
749
765
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
750
|
-
{ enabled:
|
|
766
|
+
{ enabled: ${enabledParam}, ...options }
|
|
751
767
|
);}
|
|
752
768
|
|
|
753
769
|
|
|
754
770
|
|
|
755
771
|
type ${componentName}MutationProps<T> = {
|
|
756
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
772
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
757
773
|
}
|
|
758
774
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
759
775
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
@@ -791,21 +807,20 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
791
807
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
792
808
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
793
809
|
|
|
794
|
-
use${componentName}Query.
|
|
810
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
811
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
795
812
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
796
813
|
|
|
797
814
|
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
798
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
815
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
799
816
|
}
|
|
800
817
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
801
818
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
802
|
-
{
|
|
803
|
-
.map((param) => `["${param.name}"]`)
|
|
804
|
-
.join(' && !!props')}, ...options }
|
|
819
|
+
{ enabled: ${enabledParam}, ...options }
|
|
805
820
|
);}
|
|
806
821
|
|
|
807
822
|
type ${componentName}MutationProps<T> = {
|
|
808
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
823
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
809
824
|
}
|
|
810
825
|
|
|
811
826
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
@@ -817,7 +832,8 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
817
832
|
output += `
|
|
818
833
|
type ${componentName}Response = ${genericsTypes}
|
|
819
834
|
|
|
820
|
-
type ${componentName}Variables =
|
|
835
|
+
type ${componentName}Variables = {
|
|
836
|
+
body: ${requestBodyComponent}
|
|
821
837
|
${headerParam}
|
|
822
838
|
};
|
|
823
839
|
|
|
@@ -841,21 +857,21 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
841
857
|
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
842
858
|
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
843
859
|
|
|
860
|
+
use${componentName}Query.cancelQueries = (params: ${componentName}Variables) => queryClient.cancelQueries(use${componentName}Query.queryKey(params))
|
|
844
861
|
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
845
862
|
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
846
863
|
|
|
847
864
|
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
848
|
-
options?: UseQueryOptions<${componentName}Response,
|
|
865
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
849
866
|
}
|
|
850
867
|
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
851
|
-
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body),{
|
|
852
|
-
|
|
853
|
-
.join(' && !!props')}, ...options }
|
|
868
|
+
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body),{
|
|
869
|
+
enabled: ${enabledParam}, ...options }
|
|
854
870
|
);}
|
|
855
871
|
|
|
856
872
|
|
|
857
873
|
type ${componentName}MutationProps<T> = {
|
|
858
|
-
options?: UseMutationOptions<${componentName}Response,
|
|
874
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
859
875
|
}
|
|
860
876
|
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
861
877
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
@@ -908,8 +924,8 @@ const importOpenApi = async ({ data, format, apiDirectory, queryClientDir, heade
|
|
|
908
924
|
import { Updater } from 'react-query/types/core/utils';
|
|
909
925
|
|
|
910
926
|
import queryString from 'query-string';
|
|
911
|
-
import {AxiosError} from 'axios';
|
|
912
|
-
import { api
|
|
927
|
+
import { AxiosError } from 'axios';
|
|
928
|
+
import { api } from '${apiDirectory}';
|
|
913
929
|
import { queryClient } from '${queryClientDir}';
|
|
914
930
|
|
|
915
931
|
// SCEHMAS
|