react-query-lightbase-codegen 0.0.17 → 0.0.18
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.
|
@@ -399,35 +399,49 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
399
399
|
// QUERIES
|
|
400
400
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
401
401
|
output += `
|
|
402
|
+
type ${componentName}Response = ${genericsTypes}
|
|
402
403
|
type ${componentName}Variables = {
|
|
403
404
|
${paramsTypes}
|
|
404
405
|
}
|
|
405
406
|
|
|
406
407
|
use${componentName}Query.fetch = async (props: ${componentName}Variables ) => {
|
|
407
|
-
const result = await api.${verb}<${
|
|
408
|
+
const result = await api.${verb}<${componentName}Response>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
408
409
|
return result.data;
|
|
409
410
|
}
|
|
410
411
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
411
412
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
412
413
|
|
|
413
|
-
|
|
414
|
-
|
|
414
|
+
use${componentName}Query.updateCache = (
|
|
415
|
+
params: ${componentName}Variables,
|
|
416
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
417
|
+
options?: SetDataOptions | undefined
|
|
418
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
419
|
+
|
|
420
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
421
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
422
|
+
|
|
423
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
424
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
425
|
+
|
|
426
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
427
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
415
428
|
}
|
|
416
|
-
export function use${componentName}Query<T = ${
|
|
429
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
417
430
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
418
431
|
{ enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
419
432
|
);}
|
|
420
433
|
|
|
421
434
|
type ${componentName}MutationProps<T> = {
|
|
422
|
-
options?: UseMutationOptions<${
|
|
435
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
423
436
|
}
|
|
424
|
-
export function use${componentName}Mutation<T = ${
|
|
437
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
425
438
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
426
439
|
props?.options
|
|
427
440
|
)};`;
|
|
428
441
|
}
|
|
429
442
|
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
430
443
|
output += `
|
|
444
|
+
type ${componentName}Response = ${genericsTypes}
|
|
431
445
|
type ${componentName}Variables = {
|
|
432
446
|
${paramsTypes}
|
|
433
447
|
${queryParamsType};
|
|
@@ -436,75 +450,114 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
436
450
|
use${componentName}Query.fetch = async (props:${componentName}Variables) => {
|
|
437
451
|
const {${paramsInPath.join(', ')}, ...queryParams} = props
|
|
438
452
|
const params = queryString.stringify(queryParams);
|
|
439
|
-
const result = await api.${verb}<${
|
|
453
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
440
454
|
return result.data;
|
|
441
455
|
}
|
|
442
456
|
|
|
443
457
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
444
|
-
use${componentName}Query.queryKey = (params
|
|
458
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
445
459
|
|
|
460
|
+
use${componentName}Query.updateCache = (
|
|
461
|
+
params: ${componentName}Variables,
|
|
462
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
463
|
+
options?: SetDataOptions | undefined
|
|
464
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
465
|
+
|
|
466
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
467
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
446
468
|
|
|
447
|
-
|
|
448
|
-
|
|
469
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
470
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
471
|
+
|
|
472
|
+
type ${componentName}QueryProps<T = ${componentName}Response> ${componentName}Variables & {
|
|
473
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
449
474
|
}
|
|
450
|
-
export function use${componentName}Query<T = ${
|
|
475
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
451
476
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
452
477
|
);}
|
|
453
478
|
|
|
454
479
|
type ${componentName}MutationProps<T> = {
|
|
455
|
-
options?: UseMutationOptions<${
|
|
480
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
456
481
|
}
|
|
457
|
-
export function use${componentName}Mutation<T = ${
|
|
482
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
458
483
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
459
484
|
props?.options
|
|
460
485
|
)};`;
|
|
461
486
|
}
|
|
462
487
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
463
488
|
output += `
|
|
489
|
+
type ${componentName}Response = ${genericsTypes}
|
|
464
490
|
use${componentName}Query.fetch = async () => {
|
|
465
|
-
const result = await api.${verb}<${
|
|
491
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`);
|
|
466
492
|
return result.data;
|
|
467
493
|
}
|
|
468
494
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
469
495
|
use${componentName}Query.queryKey = (): QueryKey => use${componentName}Query.baseKey()
|
|
470
496
|
|
|
471
|
-
|
|
472
|
-
|
|
497
|
+
use${componentName}Query.updateCache = (
|
|
498
|
+
params: ${componentName}Variables,
|
|
499
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
500
|
+
options?: SetDataOptions | undefined
|
|
501
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
502
|
+
|
|
503
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
504
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
505
|
+
|
|
506
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
507
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = {
|
|
511
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
473
512
|
}
|
|
474
|
-
export function use${componentName}Query<T = ${
|
|
513
|
+
export function use${componentName}Query<T = ${componentName}Response>(props?: ${componentName}QueryProps<T>) {
|
|
475
514
|
return useQuery(use${componentName}Query.queryKey(), use${componentName}Query.fetch, props?.options
|
|
476
515
|
);}
|
|
477
516
|
|
|
478
517
|
type ${componentName}MutationProps<T> = {
|
|
479
|
-
options?: UseMutationOptions<${
|
|
518
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, void, T>
|
|
480
519
|
}
|
|
481
|
-
export function use${componentName}Mutation<T = ${
|
|
520
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
482
521
|
return useMutation(async () => use${componentName}Query.fetch(),
|
|
483
522
|
props?.options
|
|
484
523
|
)};`;
|
|
485
524
|
}
|
|
486
525
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
487
526
|
output += `
|
|
527
|
+
type ${componentName}Response = ${genericsTypes}
|
|
488
528
|
type ${componentName}Variables = {
|
|
489
529
|
${queryParamsType}
|
|
490
530
|
}
|
|
491
531
|
|
|
492
|
-
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
493
|
-
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
494
532
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
495
533
|
const params = queryString.stringify({${queryParams
|
|
496
534
|
.map((param) => `"${param.name}": props["${param.name}"]`)
|
|
497
535
|
.join(',')}});
|
|
498
|
-
const result = await api.${verb}<${
|
|
536
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
499
537
|
return result.data;
|
|
500
538
|
}
|
|
501
539
|
|
|
502
|
-
|
|
540
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
541
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
542
|
+
|
|
543
|
+
use${componentName}Query.updateCache = (
|
|
544
|
+
params: ${componentName}Variables,
|
|
545
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
546
|
+
options?: SetDataOptions | undefined
|
|
547
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
548
|
+
|
|
549
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
550
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
551
|
+
|
|
552
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
553
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
554
|
+
|
|
555
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}Variables & { options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>}) {
|
|
503
556
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
504
557
|
{ enabled: !!props${queryParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
505
558
|
);}
|
|
506
559
|
|
|
507
|
-
export function use${componentName}Mutation<T = ${
|
|
560
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: { options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>}) {
|
|
508
561
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
509
562
|
props?.options
|
|
510
563
|
)};
|
|
@@ -512,67 +565,96 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
512
565
|
}
|
|
513
566
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
514
567
|
output += `
|
|
568
|
+
type ${componentName}Response = ${genericsTypes}
|
|
515
569
|
type ${componentName}Variables = ${requestBodyComponent};
|
|
516
570
|
|
|
517
571
|
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
518
|
-
const result = await api.${verb}<${
|
|
572
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
519
573
|
return result.data
|
|
520
574
|
}
|
|
521
575
|
|
|
522
576
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
523
577
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
524
578
|
|
|
525
|
-
|
|
526
|
-
|
|
579
|
+
use${componentName}Query.updateCache = (
|
|
580
|
+
params: ${componentName}Variables,
|
|
581
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
582
|
+
options?: SetDataOptions | undefined
|
|
583
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
584
|
+
|
|
585
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
586
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
587
|
+
|
|
588
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
589
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
593
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
527
594
|
}
|
|
528
|
-
export function use${componentName}Query<T = ${
|
|
595
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
529
596
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
530
597
|
);}
|
|
531
598
|
|
|
532
599
|
type ${componentName}MutationProps<T> = {
|
|
533
|
-
options?: UseMutationOptions<${
|
|
600
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
534
601
|
}
|
|
535
|
-
export function use${componentName}Mutation<T = ${
|
|
602
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
536
603
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
537
604
|
props?.options
|
|
538
605
|
)};`;
|
|
539
606
|
}
|
|
540
607
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
541
608
|
output += `
|
|
609
|
+
type ${componentName}Response = ${genericsTypes}
|
|
542
610
|
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
543
611
|
|
|
544
|
-
type ${componentName}QueryProps<T = ${
|
|
545
|
-
options?: UseQueryOptions<${
|
|
612
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
613
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
546
614
|
}
|
|
547
615
|
|
|
548
616
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
549
617
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
550
|
-
const result = await api.${verb}<${
|
|
618
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
551
619
|
return result.data
|
|
552
620
|
}
|
|
553
621
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
554
622
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
555
623
|
|
|
556
|
-
|
|
624
|
+
use${componentName}Query.updateCache = (
|
|
625
|
+
params: ${componentName}Variables,
|
|
626
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
627
|
+
options?: SetDataOptions | undefined
|
|
628
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
629
|
+
|
|
630
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
631
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
632
|
+
|
|
633
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
634
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
557
638
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
558
639
|
);}
|
|
559
640
|
|
|
560
641
|
type ${componentName}MutationProps<T> = {
|
|
561
|
-
options?: UseMutationOptions<${
|
|
642
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
562
643
|
}
|
|
563
|
-
export function use${componentName}Mutation<T = ${
|
|
644
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
564
645
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
565
646
|
props?.options
|
|
566
647
|
)};`;
|
|
567
648
|
}
|
|
568
649
|
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
569
650
|
output += `
|
|
651
|
+
type ${componentName}Response = ${genericsTypes}
|
|
570
652
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
571
653
|
${queryParamsType}
|
|
572
654
|
}
|
|
573
655
|
|
|
574
|
-
type ${componentName}QueryProps<T = ${
|
|
575
|
-
options?: UseQueryOptions<${
|
|
656
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
657
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
576
658
|
}
|
|
577
659
|
|
|
578
660
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
@@ -582,13 +664,26 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
582
664
|
const params = queryString.stringify({
|
|
583
665
|
${queryParams.map((param) => `["${param.name}"]: props["${param.name}"]`).join(',')}
|
|
584
666
|
});
|
|
585
|
-
const result = await api.${verb}<${
|
|
667
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, body)
|
|
586
668
|
return result.data
|
|
587
669
|
}
|
|
588
670
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
589
671
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
590
672
|
|
|
591
|
-
|
|
673
|
+
use${componentName}Query.updateCache = (
|
|
674
|
+
params: ${componentName}Variables,
|
|
675
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
676
|
+
options?: SetDataOptions | undefined
|
|
677
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
678
|
+
|
|
679
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
680
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
681
|
+
|
|
682
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
683
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
684
|
+
|
|
685
|
+
|
|
686
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
592
687
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props${queryParams
|
|
593
688
|
.map((param) => `["${param.name}"]`)
|
|
594
689
|
.join(' && !!props')}, ...options }
|
|
@@ -596,9 +691,9 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
596
691
|
|
|
597
692
|
|
|
598
693
|
type ${componentName}MutationProps<T> = {
|
|
599
|
-
options?: UseMutationOptions<${
|
|
694
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
600
695
|
}
|
|
601
|
-
export function use${componentName}Mutation<T = ${
|
|
696
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
602
697
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
603
698
|
props?.options
|
|
604
699
|
)};`;
|
|
@@ -620,23 +715,36 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
620
715
|
}
|
|
621
716
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
622
717
|
output += `
|
|
718
|
+
type ${componentName}Response = ${genericsTypes}
|
|
623
719
|
type ${componentName}Variables = {
|
|
624
720
|
${headerParam}
|
|
625
721
|
};
|
|
626
722
|
|
|
627
723
|
use${componentName}Query.fetch = async (headers: ${componentName}Variables) => {
|
|
628
|
-
const result = await api.${verb}<${
|
|
724
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, {headers: headers});
|
|
629
725
|
return result.data;
|
|
630
726
|
}
|
|
631
727
|
|
|
632
728
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
633
|
-
|
|
634
729
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
635
730
|
|
|
636
|
-
|
|
637
|
-
|
|
731
|
+
use${componentName}Query.updateCache = (
|
|
732
|
+
params: ${componentName}Variables,
|
|
733
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
734
|
+
options?: SetDataOptions | undefined
|
|
735
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
736
|
+
|
|
737
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
738
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
739
|
+
|
|
740
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
741
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
745
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
638
746
|
}
|
|
639
|
-
export function use${componentName}Query<T = ${
|
|
747
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
640
748
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
641
749
|
{ enabled: !!props${headerParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
642
750
|
);}
|
|
@@ -644,15 +752,17 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
644
752
|
|
|
645
753
|
|
|
646
754
|
type ${componentName}MutationProps<T> = {
|
|
647
|
-
options?: UseMutationOptions<${
|
|
755
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
648
756
|
}
|
|
649
|
-
export function use${componentName}Mutation<T = ${
|
|
757
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
650
758
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
651
759
|
props?.options
|
|
652
760
|
)};`;
|
|
653
761
|
}
|
|
654
762
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
655
763
|
output += `
|
|
764
|
+
type ${componentName}Response = ${genericsTypes}
|
|
765
|
+
|
|
656
766
|
type ${componentName}Variables = {
|
|
657
767
|
${headerParam}
|
|
658
768
|
${queryParamsType};
|
|
@@ -660,21 +770,33 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
660
770
|
|
|
661
771
|
use${componentName}Query.fetch = async (data: ${componentName}Variables) => {
|
|
662
772
|
const params = queryString.stringify({
|
|
663
|
-
${queryParams.map((
|
|
773
|
+
${queryParams.map((p) => `"${p.name}": data["${p.name}"]`).join(',')}
|
|
664
774
|
});
|
|
665
775
|
const headers = {
|
|
666
|
-
${headerParams.map((
|
|
776
|
+
${headerParams.map((p) => `"${p.name}": data["${p.name}"]`).join(',')}
|
|
667
777
|
}
|
|
668
|
-
const result = await api.${verb}<${
|
|
778
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, {headers})
|
|
669
779
|
return result.data;
|
|
670
780
|
}
|
|
671
781
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
672
782
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
673
783
|
|
|
674
|
-
|
|
675
|
-
|
|
784
|
+
use${componentName}Query.updateCache = (
|
|
785
|
+
params: ${componentName}Variables,
|
|
786
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
787
|
+
options?: SetDataOptions | undefined
|
|
788
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
789
|
+
|
|
790
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
791
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
792
|
+
|
|
793
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
794
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
795
|
+
|
|
796
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
797
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
676
798
|
}
|
|
677
|
-
export function use${componentName}Query<T = ${
|
|
799
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
678
800
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
679
801
|
{ enabled: !!props${[...queryParams, ...headerParams]
|
|
680
802
|
.map((param) => `["${param.name}"]`)
|
|
@@ -682,16 +804,18 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
682
804
|
);}
|
|
683
805
|
|
|
684
806
|
type ${componentName}MutationProps<T> = {
|
|
685
|
-
options?: UseMutationOptions<${
|
|
807
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
686
808
|
}
|
|
687
809
|
|
|
688
|
-
export function use${componentName}Mutation<T = ${
|
|
810
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
689
811
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
690
812
|
props?.options
|
|
691
813
|
)};`;
|
|
692
814
|
}
|
|
693
815
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
694
816
|
output += `
|
|
817
|
+
type ${componentName}Response = ${genericsTypes}
|
|
818
|
+
|
|
695
819
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
696
820
|
${headerParam}
|
|
697
821
|
};
|
|
@@ -700,16 +824,29 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
700
824
|
const headers = {
|
|
701
825
|
${headerParams.map((param) => `"${param.name}": body["${param.name}"]`).join(',')}
|
|
702
826
|
}
|
|
703
|
-
const result = await api.${verb}<${
|
|
827
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body, {headers})
|
|
704
828
|
return result.data
|
|
705
829
|
}
|
|
830
|
+
|
|
706
831
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
707
832
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
708
833
|
|
|
709
|
-
|
|
710
|
-
|
|
834
|
+
use${componentName}Query.updateCache = (
|
|
835
|
+
params: ${componentName}Variables,
|
|
836
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
837
|
+
options?: SetDataOptions | undefined
|
|
838
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
839
|
+
|
|
840
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
841
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
842
|
+
|
|
843
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
844
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
845
|
+
|
|
846
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
847
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
711
848
|
}
|
|
712
|
-
export function use${componentName}Query<T = ${
|
|
849
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
713
850
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body),{ enabled: !!body${headerParams
|
|
714
851
|
.map((param) => `["${param.name}"]`)
|
|
715
852
|
.join(' && !!props')}, ...options }
|
|
@@ -717,9 +854,9 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
717
854
|
|
|
718
855
|
|
|
719
856
|
type ${componentName}MutationProps<T> = {
|
|
720
|
-
options?: UseMutationOptions<${
|
|
857
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
721
858
|
}
|
|
722
|
-
export function use${componentName}Mutation<T = ${
|
|
859
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
723
860
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
724
861
|
props?.options
|
|
725
862
|
)};`;
|
|
@@ -753,17 +890,26 @@ const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
|
753
890
|
/**
|
|
754
891
|
* Main entry of the generator. Generate react-query component from openAPI.
|
|
755
892
|
*/
|
|
756
|
-
const importOpenApi = async ({ data, format,
|
|
893
|
+
const importOpenApi = async ({ data, format, apiDirectory, queryClientDir, headerFilters = [], }) => {
|
|
757
894
|
const operationIds = [];
|
|
758
895
|
let specs = await importSpecs(data, format);
|
|
759
|
-
console.log(JSON.stringify(Object.keys(specs), null, 2));
|
|
760
896
|
(0, exports.resolveDiscriminator)(specs);
|
|
761
897
|
let output = '';
|
|
762
898
|
output = `
|
|
763
|
-
import {
|
|
899
|
+
import {
|
|
900
|
+
useQuery,
|
|
901
|
+
useMutation,
|
|
902
|
+
UseQueryOptions,
|
|
903
|
+
UseMutationOptions,
|
|
904
|
+
QueryKey,
|
|
905
|
+
SetDataOptions,
|
|
906
|
+
} from 'react-query';
|
|
907
|
+
import { Updater } from 'react-query/types/core/utils';
|
|
908
|
+
|
|
764
909
|
import queryString from 'query-string';
|
|
765
910
|
import {AxiosError} from 'axios';
|
|
766
|
-
import { api } from '${
|
|
911
|
+
import { api } from '${apiDirectory}';
|
|
912
|
+
import { queryClient } from '${queryClientDir}';
|
|
767
913
|
|
|
768
914
|
// SCEHMAS
|
|
769
915
|
${(0, exports.generateSchemasDefinition)(specs.components?.schemas)}
|
|
@@ -10,7 +10,7 @@ const path_1 = require("path");
|
|
|
10
10
|
const import_open_api_1 = require("./import-open-api");
|
|
11
11
|
const log = console.log; // tslint:disable-line:no-console
|
|
12
12
|
const createSuccessMessage = (backend) => chalk_1.default.green(`🎉 ${backend ? `[${backend}] ` : ''} Your OpenAPI spec has been converted into react query hooks`);
|
|
13
|
-
function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, headerFilters, }) {
|
|
13
|
+
function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, queryClientDir, headerFilters, }) {
|
|
14
14
|
(0, fs_1.readdir)(sourceDirectory, function (err, filenames) {
|
|
15
15
|
if (err) {
|
|
16
16
|
throw err;
|
|
@@ -21,7 +21,13 @@ function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, headerFil
|
|
|
21
21
|
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
22
22
|
try {
|
|
23
23
|
const name = `useQueries${filename.split('.')[0]}.tsx`;
|
|
24
|
-
const fileExports = await (0, import_open_api_1.importOpenApi)({
|
|
24
|
+
const fileExports = await (0, import_open_api_1.importOpenApi)({
|
|
25
|
+
data,
|
|
26
|
+
format,
|
|
27
|
+
apiDirectory,
|
|
28
|
+
headerFilters,
|
|
29
|
+
queryClientDir,
|
|
30
|
+
});
|
|
25
31
|
(0, fs_1.writeFileSync)((0, path_1.join)(process.cwd(), `${exportDirectory}/${name}`), fileExports);
|
|
26
32
|
log(createSuccessMessage(filename));
|
|
27
33
|
}
|
|
@@ -379,35 +379,49 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
379
379
|
// QUERIES
|
|
380
380
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
381
381
|
output += `
|
|
382
|
+
type ${componentName}Response = ${genericsTypes}
|
|
382
383
|
type ${componentName}Variables = {
|
|
383
384
|
${paramsTypes}
|
|
384
385
|
}
|
|
385
386
|
|
|
386
387
|
use${componentName}Query.fetch = async (props: ${componentName}Variables ) => {
|
|
387
|
-
const result = await api.${verb}<${
|
|
388
|
+
const result = await api.${verb}<${componentName}Response>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
388
389
|
return result.data;
|
|
389
390
|
}
|
|
390
391
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
391
392
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
392
393
|
|
|
393
|
-
|
|
394
|
-
|
|
394
|
+
use${componentName}Query.updateCache = (
|
|
395
|
+
params: ${componentName}Variables,
|
|
396
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
397
|
+
options?: SetDataOptions | undefined
|
|
398
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
399
|
+
|
|
400
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
401
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
402
|
+
|
|
403
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
404
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
405
|
+
|
|
406
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
407
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
395
408
|
}
|
|
396
|
-
export function use${componentName}Query<T = ${
|
|
409
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
397
410
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
398
411
|
{ enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
399
412
|
);}
|
|
400
413
|
|
|
401
414
|
type ${componentName}MutationProps<T> = {
|
|
402
|
-
options?: UseMutationOptions<${
|
|
415
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
403
416
|
}
|
|
404
|
-
export function use${componentName}Mutation<T = ${
|
|
417
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
405
418
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
406
419
|
props?.options
|
|
407
420
|
)};`;
|
|
408
421
|
}
|
|
409
422
|
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
410
423
|
output += `
|
|
424
|
+
type ${componentName}Response = ${genericsTypes}
|
|
411
425
|
type ${componentName}Variables = {
|
|
412
426
|
${paramsTypes}
|
|
413
427
|
${queryParamsType};
|
|
@@ -416,75 +430,114 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
416
430
|
use${componentName}Query.fetch = async (props:${componentName}Variables) => {
|
|
417
431
|
const {${paramsInPath.join(', ')}, ...queryParams} = props
|
|
418
432
|
const params = queryString.stringify(queryParams);
|
|
419
|
-
const result = await api.${verb}<${
|
|
433
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
420
434
|
return result.data;
|
|
421
435
|
}
|
|
422
436
|
|
|
423
437
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
424
|
-
use${componentName}Query.queryKey = (params
|
|
438
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
425
439
|
|
|
440
|
+
use${componentName}Query.updateCache = (
|
|
441
|
+
params: ${componentName}Variables,
|
|
442
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
443
|
+
options?: SetDataOptions | undefined
|
|
444
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
445
|
+
|
|
446
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
447
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
426
448
|
|
|
427
|
-
|
|
428
|
-
|
|
449
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
450
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
451
|
+
|
|
452
|
+
type ${componentName}QueryProps<T = ${componentName}Response> ${componentName}Variables & {
|
|
453
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
429
454
|
}
|
|
430
|
-
export function use${componentName}Query<T = ${
|
|
455
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
431
456
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
432
457
|
);}
|
|
433
458
|
|
|
434
459
|
type ${componentName}MutationProps<T> = {
|
|
435
|
-
options?: UseMutationOptions<${
|
|
460
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
436
461
|
}
|
|
437
|
-
export function use${componentName}Mutation<T = ${
|
|
462
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
438
463
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
439
464
|
props?.options
|
|
440
465
|
)};`;
|
|
441
466
|
}
|
|
442
467
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
443
468
|
output += `
|
|
469
|
+
type ${componentName}Response = ${genericsTypes}
|
|
444
470
|
use${componentName}Query.fetch = async () => {
|
|
445
|
-
const result = await api.${verb}<${
|
|
471
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`);
|
|
446
472
|
return result.data;
|
|
447
473
|
}
|
|
448
474
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
449
475
|
use${componentName}Query.queryKey = (): QueryKey => use${componentName}Query.baseKey()
|
|
450
476
|
|
|
451
|
-
|
|
452
|
-
|
|
477
|
+
use${componentName}Query.updateCache = (
|
|
478
|
+
params: ${componentName}Variables,
|
|
479
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
480
|
+
options?: SetDataOptions | undefined
|
|
481
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
482
|
+
|
|
483
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
484
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
485
|
+
|
|
486
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
487
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = {
|
|
491
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
453
492
|
}
|
|
454
|
-
export function use${componentName}Query<T = ${
|
|
493
|
+
export function use${componentName}Query<T = ${componentName}Response>(props?: ${componentName}QueryProps<T>) {
|
|
455
494
|
return useQuery(use${componentName}Query.queryKey(), use${componentName}Query.fetch, props?.options
|
|
456
495
|
);}
|
|
457
496
|
|
|
458
497
|
type ${componentName}MutationProps<T> = {
|
|
459
|
-
options?: UseMutationOptions<${
|
|
498
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, void, T>
|
|
460
499
|
}
|
|
461
|
-
export function use${componentName}Mutation<T = ${
|
|
500
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
462
501
|
return useMutation(async () => use${componentName}Query.fetch(),
|
|
463
502
|
props?.options
|
|
464
503
|
)};`;
|
|
465
504
|
}
|
|
466
505
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
467
506
|
output += `
|
|
507
|
+
type ${componentName}Response = ${genericsTypes}
|
|
468
508
|
type ${componentName}Variables = {
|
|
469
509
|
${queryParamsType}
|
|
470
510
|
}
|
|
471
511
|
|
|
472
|
-
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
473
|
-
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
474
512
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
475
513
|
const params = queryString.stringify({${queryParams
|
|
476
514
|
.map((param) => `"${param.name}": props["${param.name}"]`)
|
|
477
515
|
.join(',')}});
|
|
478
|
-
const result = await api.${verb}<${
|
|
516
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
479
517
|
return result.data;
|
|
480
518
|
}
|
|
481
519
|
|
|
482
|
-
|
|
520
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
521
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
522
|
+
|
|
523
|
+
use${componentName}Query.updateCache = (
|
|
524
|
+
params: ${componentName}Variables,
|
|
525
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
526
|
+
options?: SetDataOptions | undefined
|
|
527
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
528
|
+
|
|
529
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
530
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
531
|
+
|
|
532
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
533
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
534
|
+
|
|
535
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}Variables & { options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>}) {
|
|
483
536
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
484
537
|
{ enabled: !!props${queryParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
485
538
|
);}
|
|
486
539
|
|
|
487
|
-
export function use${componentName}Mutation<T = ${
|
|
540
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: { options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>}) {
|
|
488
541
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
489
542
|
props?.options
|
|
490
543
|
)};
|
|
@@ -492,67 +545,96 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
492
545
|
}
|
|
493
546
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
494
547
|
output += `
|
|
548
|
+
type ${componentName}Response = ${genericsTypes}
|
|
495
549
|
type ${componentName}Variables = ${requestBodyComponent};
|
|
496
550
|
|
|
497
551
|
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
498
|
-
const result = await api.${verb}<${
|
|
552
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
499
553
|
return result.data
|
|
500
554
|
}
|
|
501
555
|
|
|
502
556
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
503
557
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
504
558
|
|
|
505
|
-
|
|
506
|
-
|
|
559
|
+
use${componentName}Query.updateCache = (
|
|
560
|
+
params: ${componentName}Variables,
|
|
561
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
562
|
+
options?: SetDataOptions | undefined
|
|
563
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
564
|
+
|
|
565
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
566
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
567
|
+
|
|
568
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
569
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
570
|
+
|
|
571
|
+
|
|
572
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
573
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
507
574
|
}
|
|
508
|
-
export function use${componentName}Query<T = ${
|
|
575
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
509
576
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
510
577
|
);}
|
|
511
578
|
|
|
512
579
|
type ${componentName}MutationProps<T> = {
|
|
513
|
-
options?: UseMutationOptions<${
|
|
580
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
514
581
|
}
|
|
515
|
-
export function use${componentName}Mutation<T = ${
|
|
582
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
516
583
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
517
584
|
props?.options
|
|
518
585
|
)};`;
|
|
519
586
|
}
|
|
520
587
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
521
588
|
output += `
|
|
589
|
+
type ${componentName}Response = ${genericsTypes}
|
|
522
590
|
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
523
591
|
|
|
524
|
-
type ${componentName}QueryProps<T = ${
|
|
525
|
-
options?: UseQueryOptions<${
|
|
592
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
593
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
526
594
|
}
|
|
527
595
|
|
|
528
596
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
529
597
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
530
|
-
const result = await api.${verb}<${
|
|
598
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
531
599
|
return result.data
|
|
532
600
|
}
|
|
533
601
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
534
602
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
535
603
|
|
|
536
|
-
|
|
604
|
+
use${componentName}Query.updateCache = (
|
|
605
|
+
params: ${componentName}Variables,
|
|
606
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
607
|
+
options?: SetDataOptions | undefined
|
|
608
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
609
|
+
|
|
610
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
611
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
612
|
+
|
|
613
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
614
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
537
618
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
538
619
|
);}
|
|
539
620
|
|
|
540
621
|
type ${componentName}MutationProps<T> = {
|
|
541
|
-
options?: UseMutationOptions<${
|
|
622
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
542
623
|
}
|
|
543
|
-
export function use${componentName}Mutation<T = ${
|
|
624
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
544
625
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
545
626
|
props?.options
|
|
546
627
|
)};`;
|
|
547
628
|
}
|
|
548
629
|
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
549
630
|
output += `
|
|
631
|
+
type ${componentName}Response = ${genericsTypes}
|
|
550
632
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
551
633
|
${queryParamsType}
|
|
552
634
|
}
|
|
553
635
|
|
|
554
|
-
type ${componentName}QueryProps<T = ${
|
|
555
|
-
options?: UseQueryOptions<${
|
|
636
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
637
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
556
638
|
}
|
|
557
639
|
|
|
558
640
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
@@ -562,13 +644,26 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
562
644
|
const params = queryString.stringify({
|
|
563
645
|
${queryParams.map((param) => `["${param.name}"]: props["${param.name}"]`).join(',')}
|
|
564
646
|
});
|
|
565
|
-
const result = await api.${verb}<${
|
|
647
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, body)
|
|
566
648
|
return result.data
|
|
567
649
|
}
|
|
568
650
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
569
651
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
570
652
|
|
|
571
|
-
|
|
653
|
+
use${componentName}Query.updateCache = (
|
|
654
|
+
params: ${componentName}Variables,
|
|
655
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
656
|
+
options?: SetDataOptions | undefined
|
|
657
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
658
|
+
|
|
659
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
660
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
661
|
+
|
|
662
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
663
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
664
|
+
|
|
665
|
+
|
|
666
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
572
667
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props${queryParams
|
|
573
668
|
.map((param) => `["${param.name}"]`)
|
|
574
669
|
.join(' && !!props')}, ...options }
|
|
@@ -576,9 +671,9 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
576
671
|
|
|
577
672
|
|
|
578
673
|
type ${componentName}MutationProps<T> = {
|
|
579
|
-
options?: UseMutationOptions<${
|
|
674
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
580
675
|
}
|
|
581
|
-
export function use${componentName}Mutation<T = ${
|
|
676
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
582
677
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
583
678
|
props?.options
|
|
584
679
|
)};`;
|
|
@@ -600,23 +695,36 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
600
695
|
}
|
|
601
696
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
602
697
|
output += `
|
|
698
|
+
type ${componentName}Response = ${genericsTypes}
|
|
603
699
|
type ${componentName}Variables = {
|
|
604
700
|
${headerParam}
|
|
605
701
|
};
|
|
606
702
|
|
|
607
703
|
use${componentName}Query.fetch = async (headers: ${componentName}Variables) => {
|
|
608
|
-
const result = await api.${verb}<${
|
|
704
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, {headers: headers});
|
|
609
705
|
return result.data;
|
|
610
706
|
}
|
|
611
707
|
|
|
612
708
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
613
|
-
|
|
614
709
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
615
710
|
|
|
616
|
-
|
|
617
|
-
|
|
711
|
+
use${componentName}Query.updateCache = (
|
|
712
|
+
params: ${componentName}Variables,
|
|
713
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
714
|
+
options?: SetDataOptions | undefined
|
|
715
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
716
|
+
|
|
717
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
718
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
719
|
+
|
|
720
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
721
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
725
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
618
726
|
}
|
|
619
|
-
export function use${componentName}Query<T = ${
|
|
727
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
620
728
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
621
729
|
{ enabled: !!props${headerParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
622
730
|
);}
|
|
@@ -624,15 +732,17 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
624
732
|
|
|
625
733
|
|
|
626
734
|
type ${componentName}MutationProps<T> = {
|
|
627
|
-
options?: UseMutationOptions<${
|
|
735
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
628
736
|
}
|
|
629
|
-
export function use${componentName}Mutation<T = ${
|
|
737
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
630
738
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
631
739
|
props?.options
|
|
632
740
|
)};`;
|
|
633
741
|
}
|
|
634
742
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
635
743
|
output += `
|
|
744
|
+
type ${componentName}Response = ${genericsTypes}
|
|
745
|
+
|
|
636
746
|
type ${componentName}Variables = {
|
|
637
747
|
${headerParam}
|
|
638
748
|
${queryParamsType};
|
|
@@ -640,21 +750,33 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
640
750
|
|
|
641
751
|
use${componentName}Query.fetch = async (data: ${componentName}Variables) => {
|
|
642
752
|
const params = queryString.stringify({
|
|
643
|
-
${queryParams.map((
|
|
753
|
+
${queryParams.map((p) => `"${p.name}": data["${p.name}"]`).join(',')}
|
|
644
754
|
});
|
|
645
755
|
const headers = {
|
|
646
|
-
${headerParams.map((
|
|
756
|
+
${headerParams.map((p) => `"${p.name}": data["${p.name}"]`).join(',')}
|
|
647
757
|
}
|
|
648
|
-
const result = await api.${verb}<${
|
|
758
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, {headers})
|
|
649
759
|
return result.data;
|
|
650
760
|
}
|
|
651
761
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
652
762
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
653
763
|
|
|
654
|
-
|
|
655
|
-
|
|
764
|
+
use${componentName}Query.updateCache = (
|
|
765
|
+
params: ${componentName}Variables,
|
|
766
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
767
|
+
options?: SetDataOptions | undefined
|
|
768
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
769
|
+
|
|
770
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
771
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
772
|
+
|
|
773
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
774
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
775
|
+
|
|
776
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
777
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
656
778
|
}
|
|
657
|
-
export function use${componentName}Query<T = ${
|
|
779
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
658
780
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
659
781
|
{ enabled: !!props${[...queryParams, ...headerParams]
|
|
660
782
|
.map((param) => `["${param.name}"]`)
|
|
@@ -662,16 +784,18 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
662
784
|
);}
|
|
663
785
|
|
|
664
786
|
type ${componentName}MutationProps<T> = {
|
|
665
|
-
options?: UseMutationOptions<${
|
|
787
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
666
788
|
}
|
|
667
789
|
|
|
668
|
-
export function use${componentName}Mutation<T = ${
|
|
790
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
669
791
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
670
792
|
props?.options
|
|
671
793
|
)};`;
|
|
672
794
|
}
|
|
673
795
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
674
796
|
output += `
|
|
797
|
+
type ${componentName}Response = ${genericsTypes}
|
|
798
|
+
|
|
675
799
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
676
800
|
${headerParam}
|
|
677
801
|
};
|
|
@@ -680,16 +804,29 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
680
804
|
const headers = {
|
|
681
805
|
${headerParams.map((param) => `"${param.name}": body["${param.name}"]`).join(',')}
|
|
682
806
|
}
|
|
683
|
-
const result = await api.${verb}<${
|
|
807
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body, {headers})
|
|
684
808
|
return result.data
|
|
685
809
|
}
|
|
810
|
+
|
|
686
811
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
687
812
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
688
813
|
|
|
689
|
-
|
|
690
|
-
|
|
814
|
+
use${componentName}Query.updateCache = (
|
|
815
|
+
params: ${componentName}Variables,
|
|
816
|
+
updater: Updater<${componentName}Response | undefined, ${componentName}Response>,
|
|
817
|
+
options?: SetDataOptions | undefined
|
|
818
|
+
) => queryClient.setQueryData<${componentName}Response>(use${componentName}Query.queryKey(params), updater, options);
|
|
819
|
+
|
|
820
|
+
use${componentName}Query.prefetch = (params: ${componentName}Variables) =>
|
|
821
|
+
queryClient.prefetchQuery<${componentName}Response>(use${componentName}Query.queryKey(params), ()=> use${componentName}Query.fetch(params));
|
|
822
|
+
|
|
823
|
+
use${componentName}Query.invalidate = (params: ${componentName}Variables) =>
|
|
824
|
+
queryClient.invalidateQueries<${componentName}Response>(use${componentName}Query.queryKey(params));
|
|
825
|
+
|
|
826
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
827
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
691
828
|
}
|
|
692
|
-
export function use${componentName}Query<T = ${
|
|
829
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
693
830
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body),{ enabled: !!body${headerParams
|
|
694
831
|
.map((param) => `["${param.name}"]`)
|
|
695
832
|
.join(' && !!props')}, ...options }
|
|
@@ -697,9 +834,9 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
697
834
|
|
|
698
835
|
|
|
699
836
|
type ${componentName}MutationProps<T> = {
|
|
700
|
-
options?: UseMutationOptions<${
|
|
837
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
701
838
|
}
|
|
702
|
-
export function use${componentName}Mutation<T = ${
|
|
839
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
703
840
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
704
841
|
props?.options
|
|
705
842
|
)};`;
|
|
@@ -732,17 +869,26 @@ const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
|
732
869
|
/**
|
|
733
870
|
* Main entry of the generator. Generate react-query component from openAPI.
|
|
734
871
|
*/
|
|
735
|
-
export const importOpenApi = async ({ data, format,
|
|
872
|
+
export const importOpenApi = async ({ data, format, apiDirectory, queryClientDir, headerFilters = [], }) => {
|
|
736
873
|
const operationIds = [];
|
|
737
874
|
let specs = await importSpecs(data, format);
|
|
738
|
-
console.log(JSON.stringify(Object.keys(specs), null, 2));
|
|
739
875
|
resolveDiscriminator(specs);
|
|
740
876
|
let output = '';
|
|
741
877
|
output = `
|
|
742
|
-
import {
|
|
878
|
+
import {
|
|
879
|
+
useQuery,
|
|
880
|
+
useMutation,
|
|
881
|
+
UseQueryOptions,
|
|
882
|
+
UseMutationOptions,
|
|
883
|
+
QueryKey,
|
|
884
|
+
SetDataOptions,
|
|
885
|
+
} from 'react-query';
|
|
886
|
+
import { Updater } from 'react-query/types/core/utils';
|
|
887
|
+
|
|
743
888
|
import queryString from 'query-string';
|
|
744
889
|
import {AxiosError} from 'axios';
|
|
745
|
-
import { api } from '${
|
|
890
|
+
import { api } from '${apiDirectory}';
|
|
891
|
+
import { queryClient } from '${queryClientDir}';
|
|
746
892
|
|
|
747
893
|
// SCEHMAS
|
|
748
894
|
${generateSchemasDefinition(specs.components?.schemas)}
|
|
@@ -4,7 +4,7 @@ import { join, parse } from 'path';
|
|
|
4
4
|
import { importOpenApi } from './import-open-api';
|
|
5
5
|
const log = console.log; // tslint:disable-line:no-console
|
|
6
6
|
const createSuccessMessage = (backend) => chalk.green(`🎉 ${backend ? `[${backend}] ` : ''} Your OpenAPI spec has been converted into react query hooks`);
|
|
7
|
-
export function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, headerFilters, }) {
|
|
7
|
+
export function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, queryClientDir, headerFilters, }) {
|
|
8
8
|
readdir(sourceDirectory, function (err, filenames) {
|
|
9
9
|
if (err) {
|
|
10
10
|
throw err;
|
|
@@ -15,7 +15,13 @@ export function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, he
|
|
|
15
15
|
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
16
16
|
try {
|
|
17
17
|
const name = `useQueries${filename.split('.')[0]}.tsx`;
|
|
18
|
-
const fileExports = await importOpenApi({
|
|
18
|
+
const fileExports = await importOpenApi({
|
|
19
|
+
data,
|
|
20
|
+
format,
|
|
21
|
+
apiDirectory,
|
|
22
|
+
headerFilters,
|
|
23
|
+
queryClientDir,
|
|
24
|
+
});
|
|
19
25
|
writeFileSync(join(process.cwd(), `${exportDirectory}/${name}`), fileExports);
|
|
20
26
|
log(createSuccessMessage(filename));
|
|
21
27
|
}
|