react-query-lightbase-codegen 0.0.15 → 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.
|
@@ -25,6 +25,8 @@ const importSpecs = (data, extension) => {
|
|
|
25
25
|
reject(err);
|
|
26
26
|
}
|
|
27
27
|
else {
|
|
28
|
+
// @ts-ignore
|
|
29
|
+
convertedObj.openapi.basePath = convertedObj.original.basePath;
|
|
28
30
|
resolve(convertedObj.openapi);
|
|
29
31
|
}
|
|
30
32
|
});
|
|
@@ -397,35 +399,49 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
397
399
|
// QUERIES
|
|
398
400
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
399
401
|
output += `
|
|
402
|
+
type ${componentName}Response = ${genericsTypes}
|
|
400
403
|
type ${componentName}Variables = {
|
|
401
404
|
${paramsTypes}
|
|
402
405
|
}
|
|
403
406
|
|
|
404
407
|
use${componentName}Query.fetch = async (props: ${componentName}Variables ) => {
|
|
405
|
-
const result = await api.${verb}<${
|
|
408
|
+
const result = await api.${verb}<${componentName}Response>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
406
409
|
return result.data;
|
|
407
410
|
}
|
|
408
411
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
409
412
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
410
413
|
|
|
411
|
-
|
|
412
|
-
|
|
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>
|
|
413
428
|
}
|
|
414
|
-
export function use${componentName}Query<T = ${
|
|
429
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
415
430
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
416
431
|
{ enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
417
432
|
);}
|
|
418
433
|
|
|
419
434
|
type ${componentName}MutationProps<T> = {
|
|
420
|
-
options?: UseMutationOptions<${
|
|
435
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
421
436
|
}
|
|
422
|
-
export function use${componentName}Mutation<T = ${
|
|
437
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
423
438
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
424
439
|
props?.options
|
|
425
440
|
)};`;
|
|
426
441
|
}
|
|
427
442
|
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
428
443
|
output += `
|
|
444
|
+
type ${componentName}Response = ${genericsTypes}
|
|
429
445
|
type ${componentName}Variables = {
|
|
430
446
|
${paramsTypes}
|
|
431
447
|
${queryParamsType};
|
|
@@ -434,75 +450,114 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
434
450
|
use${componentName}Query.fetch = async (props:${componentName}Variables) => {
|
|
435
451
|
const {${paramsInPath.join(', ')}, ...queryParams} = props
|
|
436
452
|
const params = queryString.stringify(queryParams);
|
|
437
|
-
const result = await api.${verb}<${
|
|
453
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
438
454
|
return result.data;
|
|
439
455
|
}
|
|
440
456
|
|
|
441
457
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
442
|
-
use${componentName}Query.queryKey = (params
|
|
458
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
443
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));
|
|
444
468
|
|
|
445
|
-
|
|
446
|
-
|
|
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>
|
|
447
474
|
}
|
|
448
|
-
export function use${componentName}Query<T = ${
|
|
475
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
449
476
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
450
477
|
);}
|
|
451
478
|
|
|
452
479
|
type ${componentName}MutationProps<T> = {
|
|
453
|
-
options?: UseMutationOptions<${
|
|
480
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
454
481
|
}
|
|
455
|
-
export function use${componentName}Mutation<T = ${
|
|
482
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
456
483
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
457
484
|
props?.options
|
|
458
485
|
)};`;
|
|
459
486
|
}
|
|
460
487
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
461
488
|
output += `
|
|
489
|
+
type ${componentName}Response = ${genericsTypes}
|
|
462
490
|
use${componentName}Query.fetch = async () => {
|
|
463
|
-
const result = await api.${verb}<${
|
|
491
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`);
|
|
464
492
|
return result.data;
|
|
465
493
|
}
|
|
466
494
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
467
495
|
use${componentName}Query.queryKey = (): QueryKey => use${componentName}Query.baseKey()
|
|
468
496
|
|
|
469
|
-
|
|
470
|
-
|
|
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>
|
|
471
512
|
}
|
|
472
|
-
export function use${componentName}Query<T = ${
|
|
513
|
+
export function use${componentName}Query<T = ${componentName}Response>(props?: ${componentName}QueryProps<T>) {
|
|
473
514
|
return useQuery(use${componentName}Query.queryKey(), use${componentName}Query.fetch, props?.options
|
|
474
515
|
);}
|
|
475
516
|
|
|
476
517
|
type ${componentName}MutationProps<T> = {
|
|
477
|
-
options?: UseMutationOptions<${
|
|
518
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, void, T>
|
|
478
519
|
}
|
|
479
|
-
export function use${componentName}Mutation<T = ${
|
|
520
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
480
521
|
return useMutation(async () => use${componentName}Query.fetch(),
|
|
481
522
|
props?.options
|
|
482
523
|
)};`;
|
|
483
524
|
}
|
|
484
525
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
485
526
|
output += `
|
|
527
|
+
type ${componentName}Response = ${genericsTypes}
|
|
486
528
|
type ${componentName}Variables = {
|
|
487
529
|
${queryParamsType}
|
|
488
530
|
}
|
|
489
531
|
|
|
490
|
-
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
491
|
-
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
492
532
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
493
533
|
const params = queryString.stringify({${queryParams
|
|
494
534
|
.map((param) => `"${param.name}": props["${param.name}"]`)
|
|
495
535
|
.join(',')}});
|
|
496
|
-
const result = await api.${verb}<${
|
|
536
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
497
537
|
return result.data;
|
|
498
538
|
}
|
|
499
539
|
|
|
500
|
-
|
|
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>}) {
|
|
501
556
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
502
557
|
{ enabled: !!props${queryParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
503
558
|
);}
|
|
504
559
|
|
|
505
|
-
export function use${componentName}Mutation<T = ${
|
|
560
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: { options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>}) {
|
|
506
561
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
507
562
|
props?.options
|
|
508
563
|
)};
|
|
@@ -510,67 +565,96 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
510
565
|
}
|
|
511
566
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
512
567
|
output += `
|
|
568
|
+
type ${componentName}Response = ${genericsTypes}
|
|
513
569
|
type ${componentName}Variables = ${requestBodyComponent};
|
|
514
570
|
|
|
515
571
|
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
516
|
-
const result = await api.${verb}<${
|
|
572
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
517
573
|
return result.data
|
|
518
574
|
}
|
|
519
575
|
|
|
520
576
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
521
577
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
522
578
|
|
|
523
|
-
|
|
524
|
-
|
|
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>
|
|
525
594
|
}
|
|
526
|
-
export function use${componentName}Query<T = ${
|
|
595
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
527
596
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
528
597
|
);}
|
|
529
598
|
|
|
530
599
|
type ${componentName}MutationProps<T> = {
|
|
531
|
-
options?: UseMutationOptions<${
|
|
600
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
532
601
|
}
|
|
533
|
-
export function use${componentName}Mutation<T = ${
|
|
602
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
534
603
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
535
604
|
props?.options
|
|
536
605
|
)};`;
|
|
537
606
|
}
|
|
538
607
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
539
608
|
output += `
|
|
609
|
+
type ${componentName}Response = ${genericsTypes}
|
|
540
610
|
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
541
611
|
|
|
542
|
-
type ${componentName}QueryProps<T = ${
|
|
543
|
-
options?: UseQueryOptions<${
|
|
612
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
613
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
544
614
|
}
|
|
545
615
|
|
|
546
616
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
547
617
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
548
|
-
const result = await api.${verb}<${
|
|
618
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
549
619
|
return result.data
|
|
550
620
|
}
|
|
551
621
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
552
622
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
553
623
|
|
|
554
|
-
|
|
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>) {
|
|
555
638
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
556
639
|
);}
|
|
557
640
|
|
|
558
641
|
type ${componentName}MutationProps<T> = {
|
|
559
|
-
options?: UseMutationOptions<${
|
|
642
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
560
643
|
}
|
|
561
|
-
export function use${componentName}Mutation<T = ${
|
|
644
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
562
645
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
563
646
|
props?.options
|
|
564
647
|
)};`;
|
|
565
648
|
}
|
|
566
649
|
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
567
650
|
output += `
|
|
651
|
+
type ${componentName}Response = ${genericsTypes}
|
|
568
652
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
569
653
|
${queryParamsType}
|
|
570
654
|
}
|
|
571
655
|
|
|
572
|
-
type ${componentName}QueryProps<T = ${
|
|
573
|
-
options?: UseQueryOptions<${
|
|
656
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
657
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
574
658
|
}
|
|
575
659
|
|
|
576
660
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
@@ -580,15 +664,26 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
580
664
|
const params = queryString.stringify({
|
|
581
665
|
${queryParams.map((param) => `["${param.name}"]: props["${param.name}"]`).join(',')}
|
|
582
666
|
});
|
|
583
|
-
const result = await api.${verb}<${
|
|
667
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, body)
|
|
584
668
|
return result.data
|
|
585
669
|
}
|
|
586
670
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
587
671
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
588
672
|
|
|
589
|
-
|
|
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));
|
|
590
684
|
|
|
591
|
-
|
|
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
|
)};`;
|
|
@@ -730,7 +867,8 @@ const generateRestfulComponent = ({ operation, verb, route, operationIds, parame
|
|
|
730
867
|
return output;
|
|
731
868
|
};
|
|
732
869
|
exports.generateRestfulComponent = generateRestfulComponent;
|
|
733
|
-
const generateQueryHooks = (
|
|
870
|
+
const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
871
|
+
const { paths, components } = spec;
|
|
734
872
|
let output = '';
|
|
735
873
|
Object.entries(paths).forEach(([route, verbs]) => {
|
|
736
874
|
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
@@ -738,10 +876,10 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
738
876
|
output += (0, exports.generateRestfulComponent)({
|
|
739
877
|
operation,
|
|
740
878
|
verb,
|
|
741
|
-
route,
|
|
879
|
+
route: spec.basePath + route,
|
|
742
880
|
operationIds,
|
|
743
881
|
parameters: verbs.parameters,
|
|
744
|
-
schemasComponents,
|
|
882
|
+
schemasComponents: components,
|
|
745
883
|
headerFilters,
|
|
746
884
|
});
|
|
747
885
|
}
|
|
@@ -752,16 +890,26 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
752
890
|
/**
|
|
753
891
|
* Main entry of the generator. Generate react-query component from openAPI.
|
|
754
892
|
*/
|
|
755
|
-
const importOpenApi = async ({ data, format,
|
|
893
|
+
const importOpenApi = async ({ data, format, apiDirectory, queryClientDir, headerFilters = [], }) => {
|
|
756
894
|
const operationIds = [];
|
|
757
895
|
let specs = await importSpecs(data, format);
|
|
758
896
|
(0, exports.resolveDiscriminator)(specs);
|
|
759
897
|
let output = '';
|
|
760
898
|
output = `
|
|
761
|
-
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
|
+
|
|
762
909
|
import queryString from 'query-string';
|
|
763
910
|
import {AxiosError} from 'axios';
|
|
764
|
-
import { api } from '${
|
|
911
|
+
import { api } from '${apiDirectory}';
|
|
912
|
+
import { queryClient } from '${queryClientDir}';
|
|
765
913
|
|
|
766
914
|
// SCEHMAS
|
|
767
915
|
${(0, exports.generateSchemasDefinition)(specs.components?.schemas)}
|
|
@@ -773,7 +921,7 @@ const importOpenApi = async ({ data, format, apiDir, headerFilters = [], }) => {
|
|
|
773
921
|
${(0, exports.generateRequestBodiesDefinition)(specs.components?.requestBodies)}
|
|
774
922
|
|
|
775
923
|
// HOOKS
|
|
776
|
-
${generateQueryHooks(specs
|
|
924
|
+
${generateQueryHooks(specs, operationIds, headerFilters)}
|
|
777
925
|
`;
|
|
778
926
|
return output;
|
|
779
927
|
};
|
|
@@ -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
|
}
|
|
@@ -19,6 +19,8 @@ const importSpecs = (data, extension) => {
|
|
|
19
19
|
reject(err);
|
|
20
20
|
}
|
|
21
21
|
else {
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
convertedObj.openapi.basePath = convertedObj.original.basePath;
|
|
22
24
|
resolve(convertedObj.openapi);
|
|
23
25
|
}
|
|
24
26
|
});
|
|
@@ -377,35 +379,49 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
377
379
|
// QUERIES
|
|
378
380
|
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
379
381
|
output += `
|
|
382
|
+
type ${componentName}Response = ${genericsTypes}
|
|
380
383
|
type ${componentName}Variables = {
|
|
381
384
|
${paramsTypes}
|
|
382
385
|
}
|
|
383
386
|
|
|
384
387
|
use${componentName}Query.fetch = async (props: ${componentName}Variables ) => {
|
|
385
|
-
const result = await api.${verb}<${
|
|
388
|
+
const result = await api.${verb}<${componentName}Response>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
386
389
|
return result.data;
|
|
387
390
|
}
|
|
388
391
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
389
392
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
390
393
|
|
|
391
|
-
|
|
392
|
-
|
|
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>
|
|
393
408
|
}
|
|
394
|
-
export function use${componentName}Query<T = ${
|
|
409
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
395
410
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
396
411
|
{ enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
397
412
|
);}
|
|
398
413
|
|
|
399
414
|
type ${componentName}MutationProps<T> = {
|
|
400
|
-
options?: UseMutationOptions<${
|
|
415
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
401
416
|
}
|
|
402
|
-
export function use${componentName}Mutation<T = ${
|
|
417
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
403
418
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
404
419
|
props?.options
|
|
405
420
|
)};`;
|
|
406
421
|
}
|
|
407
422
|
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
408
423
|
output += `
|
|
424
|
+
type ${componentName}Response = ${genericsTypes}
|
|
409
425
|
type ${componentName}Variables = {
|
|
410
426
|
${paramsTypes}
|
|
411
427
|
${queryParamsType};
|
|
@@ -414,75 +430,114 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
414
430
|
use${componentName}Query.fetch = async (props:${componentName}Variables) => {
|
|
415
431
|
const {${paramsInPath.join(', ')}, ...queryParams} = props
|
|
416
432
|
const params = queryString.stringify(queryParams);
|
|
417
|
-
const result = await api.${verb}<${
|
|
433
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
418
434
|
return result.data;
|
|
419
435
|
}
|
|
420
436
|
|
|
421
437
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
422
|
-
use${componentName}Query.queryKey = (params
|
|
438
|
+
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
423
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));
|
|
424
448
|
|
|
425
|
-
|
|
426
|
-
|
|
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>
|
|
427
454
|
}
|
|
428
|
-
export function use${componentName}Query<T = ${
|
|
455
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...props }: ${componentName}QueryProps<T>) {
|
|
429
456
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
430
457
|
);}
|
|
431
458
|
|
|
432
459
|
type ${componentName}MutationProps<T> = {
|
|
433
|
-
options?: UseMutationOptions<${
|
|
460
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
434
461
|
}
|
|
435
|
-
export function use${componentName}Mutation<T = ${
|
|
462
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
436
463
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
437
464
|
props?.options
|
|
438
465
|
)};`;
|
|
439
466
|
}
|
|
440
467
|
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
441
468
|
output += `
|
|
469
|
+
type ${componentName}Response = ${genericsTypes}
|
|
442
470
|
use${componentName}Query.fetch = async () => {
|
|
443
|
-
const result = await api.${verb}<${
|
|
471
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`);
|
|
444
472
|
return result.data;
|
|
445
473
|
}
|
|
446
474
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
447
475
|
use${componentName}Query.queryKey = (): QueryKey => use${componentName}Query.baseKey()
|
|
448
476
|
|
|
449
|
-
|
|
450
|
-
|
|
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>
|
|
451
492
|
}
|
|
452
|
-
export function use${componentName}Query<T = ${
|
|
493
|
+
export function use${componentName}Query<T = ${componentName}Response>(props?: ${componentName}QueryProps<T>) {
|
|
453
494
|
return useQuery(use${componentName}Query.queryKey(), use${componentName}Query.fetch, props?.options
|
|
454
495
|
);}
|
|
455
496
|
|
|
456
497
|
type ${componentName}MutationProps<T> = {
|
|
457
|
-
options?: UseMutationOptions<${
|
|
498
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, void, T>
|
|
458
499
|
}
|
|
459
|
-
export function use${componentName}Mutation<T = ${
|
|
500
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
460
501
|
return useMutation(async () => use${componentName}Query.fetch(),
|
|
461
502
|
props?.options
|
|
462
503
|
)};`;
|
|
463
504
|
}
|
|
464
505
|
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
465
506
|
output += `
|
|
507
|
+
type ${componentName}Response = ${genericsTypes}
|
|
466
508
|
type ${componentName}Variables = {
|
|
467
509
|
${queryParamsType}
|
|
468
510
|
}
|
|
469
511
|
|
|
470
|
-
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
471
|
-
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
472
512
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
473
513
|
const params = queryString.stringify({${queryParams
|
|
474
514
|
.map((param) => `"${param.name}": props["${param.name}"]`)
|
|
475
515
|
.join(',')}});
|
|
476
|
-
const result = await api.${verb}<${
|
|
516
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`)
|
|
477
517
|
return result.data;
|
|
478
518
|
}
|
|
479
519
|
|
|
480
|
-
|
|
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>}) {
|
|
481
536
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props),
|
|
482
537
|
{ enabled: !!props${queryParams.map((param) => `["${param.name}"]`).join(' && !!props')}, ...options }
|
|
483
538
|
);}
|
|
484
539
|
|
|
485
|
-
export function use${componentName}Mutation<T = ${
|
|
540
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: { options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>}) {
|
|
486
541
|
return useMutation(async (data) => use${componentName}Query.fetch(data),
|
|
487
542
|
props?.options
|
|
488
543
|
)};
|
|
@@ -490,67 +545,96 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
490
545
|
}
|
|
491
546
|
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
492
547
|
output += `
|
|
548
|
+
type ${componentName}Response = ${genericsTypes}
|
|
493
549
|
type ${componentName}Variables = ${requestBodyComponent};
|
|
494
550
|
|
|
495
551
|
use${componentName}Query.fetch = async (body: ${componentName}Variables) => {
|
|
496
|
-
const result = await api.${verb}<${
|
|
552
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
497
553
|
return result.data
|
|
498
554
|
}
|
|
499
555
|
|
|
500
556
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
501
557
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
502
558
|
|
|
503
|
-
|
|
504
|
-
|
|
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>
|
|
505
574
|
}
|
|
506
|
-
export function use${componentName}Query<T = ${
|
|
575
|
+
export function use${componentName}Query<T = ${componentName}Response>({ options = {}, ...body }: ${componentName}QueryProps<T>) {
|
|
507
576
|
return useQuery(use${componentName}Query.queryKey(body), async () => use${componentName}Query.fetch(body), options
|
|
508
577
|
);}
|
|
509
578
|
|
|
510
579
|
type ${componentName}MutationProps<T> = {
|
|
511
|
-
options?: UseMutationOptions<${
|
|
580
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
512
581
|
}
|
|
513
|
-
export function use${componentName}Mutation<T = ${
|
|
582
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
514
583
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
515
584
|
props?.options
|
|
516
585
|
)};`;
|
|
517
586
|
}
|
|
518
587
|
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
519
588
|
output += `
|
|
589
|
+
type ${componentName}Response = ${genericsTypes}
|
|
520
590
|
type ${componentName}Variables = ${requestBodyComponent} & {${paramsTypes}}
|
|
521
591
|
|
|
522
|
-
type ${componentName}QueryProps<T = ${
|
|
523
|
-
options?: UseQueryOptions<${
|
|
592
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
593
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
524
594
|
}
|
|
525
595
|
|
|
526
596
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
527
597
|
const {${paramsInPath.join(', ')}, ...body} = props
|
|
528
|
-
const result = await api.${verb}<${
|
|
598
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}\`, body)
|
|
529
599
|
return result.data
|
|
530
600
|
}
|
|
531
601
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
532
602
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
533
603
|
|
|
534
|
-
|
|
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>) {
|
|
535
618
|
return useQuery(use${componentName}Query.queryKey(props), async () => use${componentName}Query.fetch(props), { enabled: !!props.${paramsInPath.join(' && !!props.')}, ...options }
|
|
536
619
|
);}
|
|
537
620
|
|
|
538
621
|
type ${componentName}MutationProps<T> = {
|
|
539
|
-
options?: UseMutationOptions<${
|
|
622
|
+
options?: UseMutationOptions<${componentName}Response, AxiosError, ${componentName}Variables, T>
|
|
540
623
|
}
|
|
541
|
-
export function use${componentName}Mutation<T = ${
|
|
624
|
+
export function use${componentName}Mutation<T = ${componentName}Response>(props?: ${componentName}MutationProps<T>) {
|
|
542
625
|
return useMutation(async (body) => use${componentName}Query.fetch(body),
|
|
543
626
|
props?.options
|
|
544
627
|
)};`;
|
|
545
628
|
}
|
|
546
629
|
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
547
630
|
output += `
|
|
631
|
+
type ${componentName}Response = ${genericsTypes}
|
|
548
632
|
type ${componentName}Variables = ${requestBodyComponent} & {
|
|
549
633
|
${queryParamsType}
|
|
550
634
|
}
|
|
551
635
|
|
|
552
|
-
type ${componentName}QueryProps<T = ${
|
|
553
|
-
options?: UseQueryOptions<${
|
|
636
|
+
type ${componentName}QueryProps<T = ${componentName}Response> = ${componentName}Variables & {
|
|
637
|
+
options?: UseQueryOptions<${componentName}Response, AxiosError, T, any>
|
|
554
638
|
}
|
|
555
639
|
|
|
556
640
|
use${componentName}Query.fetch = async (props: ${componentName}Variables) => {
|
|
@@ -560,15 +644,26 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
560
644
|
const params = queryString.stringify({
|
|
561
645
|
${queryParams.map((param) => `["${param.name}"]: props["${param.name}"]`).join(',')}
|
|
562
646
|
});
|
|
563
|
-
const result = await api.${verb}<${
|
|
647
|
+
const result = await api.${verb}<${componentName}Response>(\`${route}?\${params}\`, body)
|
|
564
648
|
return result.data
|
|
565
649
|
}
|
|
566
650
|
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
567
651
|
use${componentName}Query.queryKey = (params: ${componentName}Variables ): QueryKey => [...use${componentName}Query.baseKey(), params];
|
|
568
652
|
|
|
569
|
-
|
|
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));
|
|
570
664
|
|
|
571
|
-
|
|
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
|
)};`;
|
|
@@ -709,7 +846,8 @@ export const generateRestfulComponent = ({ operation, verb, route, operationIds,
|
|
|
709
846
|
}
|
|
710
847
|
return output;
|
|
711
848
|
};
|
|
712
|
-
const generateQueryHooks = (
|
|
849
|
+
const generateQueryHooks = (spec, operationIds, headerFilters) => {
|
|
850
|
+
const { paths, components } = spec;
|
|
713
851
|
let output = '';
|
|
714
852
|
Object.entries(paths).forEach(([route, verbs]) => {
|
|
715
853
|
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
@@ -717,10 +855,10 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
717
855
|
output += generateRestfulComponent({
|
|
718
856
|
operation,
|
|
719
857
|
verb,
|
|
720
|
-
route,
|
|
858
|
+
route: spec.basePath + route,
|
|
721
859
|
operationIds,
|
|
722
860
|
parameters: verbs.parameters,
|
|
723
|
-
schemasComponents,
|
|
861
|
+
schemasComponents: components,
|
|
724
862
|
headerFilters,
|
|
725
863
|
});
|
|
726
864
|
}
|
|
@@ -731,16 +869,26 @@ const generateQueryHooks = (paths, operationIds, schemasComponents, headerFilter
|
|
|
731
869
|
/**
|
|
732
870
|
* Main entry of the generator. Generate react-query component from openAPI.
|
|
733
871
|
*/
|
|
734
|
-
export const importOpenApi = async ({ data, format,
|
|
872
|
+
export const importOpenApi = async ({ data, format, apiDirectory, queryClientDir, headerFilters = [], }) => {
|
|
735
873
|
const operationIds = [];
|
|
736
874
|
let specs = await importSpecs(data, format);
|
|
737
875
|
resolveDiscriminator(specs);
|
|
738
876
|
let output = '';
|
|
739
877
|
output = `
|
|
740
|
-
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
|
+
|
|
741
888
|
import queryString from 'query-string';
|
|
742
889
|
import {AxiosError} from 'axios';
|
|
743
|
-
import { api } from '${
|
|
890
|
+
import { api } from '${apiDirectory}';
|
|
891
|
+
import { queryClient } from '${queryClientDir}';
|
|
744
892
|
|
|
745
893
|
// SCEHMAS
|
|
746
894
|
${generateSchemasDefinition(specs.components?.schemas)}
|
|
@@ -752,7 +900,7 @@ export const importOpenApi = async ({ data, format, apiDir, headerFilters = [],
|
|
|
752
900
|
${generateRequestBodiesDefinition(specs.components?.requestBodies)}
|
|
753
901
|
|
|
754
902
|
// HOOKS
|
|
755
|
-
${generateQueryHooks(specs
|
|
903
|
+
${generateQueryHooks(specs, operationIds, headerFilters)}
|
|
756
904
|
`;
|
|
757
905
|
return output;
|
|
758
906
|
};
|
|
@@ -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
|
}
|