forked from 7nohe/openapi-react-query-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateExports.mts
More file actions
192 lines (174 loc) · 5.67 KB
/
createExports.mts
File metadata and controls
192 lines (174 loc) · 5.67 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type { Project } from "ts-morph";
import ts from "typescript";
import type { LimitedUserConfig } from "./cli.mjs";
import { capitalizeFirstLetter } from "./common.mjs";
import { modelsFileName } from "./constants.mjs";
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,
client,
project,
pageParam,
nextPageParam,
initialPageParam,
}: {
service: Service;
client: LimitedUserConfig["client"];
project: Project;
pageParam: string;
nextPageParam: string;
initialPageParam: string;
}) => {
const { methods } = service;
const methodDataNames = methods.reduce(
(acc, data) => {
const methodName = data.method.getName();
acc[`${capitalizeFirstLetter(methodName)}Data`] = methodName;
return acc;
},
{} as { [key: string]: string },
);
const modelsFile = project
.getSourceFiles?.()
.find((sourceFile) => sourceFile.getFilePath().includes(modelsFileName));
const modelDeclarations = modelsFile?.getExportedDeclarations();
const entries = modelDeclarations?.entries();
const modelNames: string[] = [];
const paginatableMethods: string[] = [];
for (const [key, value] of entries ?? []) {
modelNames.push(key);
const node = value[0].compilerNode;
if (ts.isTypeAliasDeclaration(node) && methodDataNames[key] !== undefined) {
// get the type alias declaration
const typeAliasDeclaration = node.type;
if (typeAliasDeclaration.kind === ts.SyntaxKind.TypeLiteral) {
const query = (typeAliasDeclaration as ts.TypeLiteralNode).members.find(
(m) =>
m.kind === ts.SyntaxKind.PropertySignature &&
m.name?.getText() === "query",
);
if (query) {
const queryType = (query as ts.PropertySignature).type;
const members =
queryType && ts.isTypeLiteralNode(queryType)
? queryType.members
: undefined;
if (members?.map((m) => m.name?.getText()).includes(pageParam)) {
paginatableMethods.push(methodDataNames[key]);
}
}
}
}
}
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({
functionDescription: m,
client,
pageParam,
nextPageParam,
initialPageParam,
paginatableMethods,
modelNames,
}),
);
const allPrefetchQueries = allGet.map((m) =>
createPrefetchOrEnsure({ ...m, functionType: "prefetch", modelNames }),
);
const allEnsureQueries = allGet.map((m) =>
createPrefetchOrEnsure({ ...m, functionType: "ensure", modelNames }),
);
const allPostMutations = allPost.map((m) =>
createUseMutation({ functionDescription: m, modelNames, client }),
);
const allPutMutations = allPut.map((m) =>
createUseMutation({ functionDescription: m, modelNames, client }),
);
const allPatchMutations = allPatch.map((m) =>
createUseMutation({ functionDescription: m, modelNames, client }),
);
const allDeleteMutations = allDelete.map((m) =>
createUseMutation({ functionDescription: m, modelNames, client }),
);
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, key, mutationKeyFn }) => [
mutationResult,
key,
mutationKeyFn,
],
);
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,
};
};