-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
createExports.mts
118 lines (101 loc) · 3.43 KB
/
createExports.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import type ts from "typescript";
import { createPrefetchOrEnsure } from "./createPrefetchOrEnsure.mjs";
import { createUseMutation } from "./createUseMutation.mjs";
import { createUseQuery } from "./createUseQuery.mjs";
import type { Service } from "./service.mjs";
export const createExports = (
service: Service,
pageParam: string,
nextPageParam: string,
initialPageParam: string,
) => {
const { methods } = service;
const allGet = methods.filter((m) =>
m.httpMethodName.toUpperCase().includes("GET"),
);
const allPost = methods.filter((m) =>
m.httpMethodName.toUpperCase().includes("POST"),
);
const allPut = methods.filter((m) =>
m.httpMethodName.toUpperCase().includes("PUT"),
);
const allPatch = methods.filter((m) =>
m.httpMethodName.toUpperCase().includes("PATCH"),
);
const allDelete = methods.filter((m) =>
m.httpMethodName.toUpperCase().includes("DELETE"),
);
const allGetQueries = allGet.map((m) =>
createUseQuery(m, pageParam, nextPageParam, initialPageParam),
);
const allPrefetchQueries = allGet.map((m) =>
createPrefetchOrEnsure({ ...m, functionType: "prefetch" }),
);
const allEnsureQueries = allGet.map((m) =>
createPrefetchOrEnsure({ ...m, functionType: "ensure" }),
);
const allPostMutations = allPost.map((m) => createUseMutation(m));
const allPutMutations = allPut.map((m) => createUseMutation(m));
const allPatchMutations = allPatch.map((m) => createUseMutation(m));
const allDeleteMutations = allDelete.map((m) => createUseMutation(m));
const allQueries = [...allGetQueries];
const allMutations = [
...allPostMutations,
...allPutMutations,
...allPatchMutations,
...allDeleteMutations,
];
const commonInQueries = allQueries.flatMap(
({ apiResponse, returnType, key, queryKeyFn }) => [
apiResponse,
returnType,
key,
queryKeyFn,
],
);
const commonInMutations = allMutations.flatMap(({ mutationResult }) => [
mutationResult,
]);
const allCommon = [...commonInQueries, ...commonInMutations];
const mainQueries = allQueries.flatMap(({ queryHook }) => [queryHook]);
const mainMutations = allMutations.flatMap(({ mutationHook }) => [
mutationHook,
]);
const mainExports = [...mainQueries, ...mainMutations];
const infiniteQueriesExports = allQueries
.flatMap(({ infiniteQueryHook }) => [infiniteQueryHook])
.filter(Boolean) as ts.VariableStatement[];
const suspenseQueries = allQueries.flatMap(({ suspenseQueryHook }) => [
suspenseQueryHook,
]);
const suspenseExports = [...suspenseQueries];
const allPrefetches = allPrefetchQueries.flatMap(({ hook }) => [hook]);
const allEnsures = allEnsureQueries.flatMap(({ hook }) => [hook]);
const allPrefetchExports = [...allPrefetches];
return {
/**
* Common types and variables between queries (regular and suspense) and mutations
*/
allCommon,
/**
* Main exports are the hooks that are used in the components
*/
mainExports,
/**
* Infinite queries exports are the hooks that are used in the infinite scroll components
*/
infiniteQueriesExports,
/**
* Suspense exports are the hooks that are used in the suspense components
*/
suspenseExports,
/**
* Prefetch exports are the hooks that are used in the prefetch components
*/
allPrefetchExports,
/**
* Ensure exports are the hooks that are used in the loader components
*/
allEnsures,
};
};