react-query-lightbase-codegen 1.6.4 → 2.0.1
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.
- package/dist/config/loadConfig.d.ts +2 -0
- package/dist/config/loadConfig.js +24 -0
- package/dist/generator/clientGenerator.d.ts +13 -0
- package/dist/generator/clientGenerator.js +199 -0
- package/dist/generator/instanceGenerator.d.ts +1 -0
- package/dist/generator/instanceGenerator.js +21 -0
- package/dist/generator/reactQueryGenerator.d.ts +2 -0
- package/dist/generator/reactQueryGenerator.js +82 -0
- package/dist/generator/schemaGenerator.d.ts +5 -0
- package/dist/generator/schemaGenerator.js +114 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +106 -4
- package/dist/types/config.d.ts +11 -0
- package/dist/types/config.js +2 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +2 -0
- package/dist/utils.d.ts +31 -20
- package/dist/utils.js +56 -179
- package/package.json +54 -82
- package/readme.md +94 -0
- package/src/config/loadConfig.ts +25 -0
- package/src/generator/clientGenerator.ts +236 -0
- package/src/generator/instanceGenerator.ts +20 -0
- package/src/generator/reactQueryGenerator.ts +94 -0
- package/src/generator/schemaGenerator.ts +138 -0
- package/src/index.ts +78 -3
- package/src/types/config.ts +12 -0
- package/src/types.ts +5 -0
- package/src/utils.ts +55 -213
- package/README.md +0 -77
- package/dist/convertSwaggerFile.d.ts +0 -5
- package/dist/convertSwaggerFile.js +0 -26
- package/dist/convertSwaggerFile.js.map +0 -1
- package/dist/generateHooks.d.ts +0 -18
- package/dist/generateHooks.js +0 -448
- package/dist/generateHooks.js.map +0 -1
- package/dist/generateImports.d.ts +0 -6
- package/dist/generateImports.js +0 -21
- package/dist/generateImports.js.map +0 -1
- package/dist/generateSchemas.d.ts +0 -7
- package/dist/generateSchemas.js +0 -100
- package/dist/generateSchemas.js.map +0 -1
- package/dist/importSpecs.d.ts +0 -10
- package/dist/importSpecs.js +0 -127
- package/dist/importSpecs.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/convertSwaggerFile.ts +0 -25
- package/src/generateHooks.ts +0 -540
- package/src/generateImports.ts +0 -32
- package/src/generateSchemas.ts +0 -117
- package/src/importSpecs.ts +0 -168
package/src/generateHooks.ts
DELETED
|
@@ -1,540 +0,0 @@
|
|
|
1
|
-
import lodash from 'lodash';
|
|
2
|
-
const { get, groupBy } = lodash;
|
|
3
|
-
import {
|
|
4
|
-
ComponentsObject,
|
|
5
|
-
OperationObject,
|
|
6
|
-
ParameterObject,
|
|
7
|
-
ReferenceObject,
|
|
8
|
-
ResponseObject,
|
|
9
|
-
SchemaObject,
|
|
10
|
-
} from 'openapi3-ts';
|
|
11
|
-
import { formatDescription, getResReqTypes, isReference, resolveValue } from './utils.js';
|
|
12
|
-
import pasCase from 'case';
|
|
13
|
-
|
|
14
|
-
const { pascal, camel } = pasCase;
|
|
15
|
-
|
|
16
|
-
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Return every params in a path
|
|
20
|
-
*/
|
|
21
|
-
const getParamsInPath = (path: string) => {
|
|
22
|
-
let n;
|
|
23
|
-
const output = [];
|
|
24
|
-
const templatePathRegex = /\{(\w+)}/g;
|
|
25
|
-
while ((n = templatePathRegex.exec(path)) !== null) {
|
|
26
|
-
output.push(n[1]);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return output;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export let imports: string[] = [];
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Generate a react-query component from openapi operation specs
|
|
36
|
-
*/
|
|
37
|
-
export const createHook = ({
|
|
38
|
-
operation,
|
|
39
|
-
verb,
|
|
40
|
-
route,
|
|
41
|
-
operationIds,
|
|
42
|
-
parameters,
|
|
43
|
-
schemasComponents,
|
|
44
|
-
headerFilters,
|
|
45
|
-
}: {
|
|
46
|
-
operation: OperationObject;
|
|
47
|
-
verb: string;
|
|
48
|
-
route: string;
|
|
49
|
-
operationIds: string[];
|
|
50
|
-
parameters: (ReferenceObject | ParameterObject)[] | undefined;
|
|
51
|
-
schemasComponents?: ComponentsObject;
|
|
52
|
-
headerFilters?: string[];
|
|
53
|
-
}) => {
|
|
54
|
-
const { operationId = route.replace('/', '') } = operation;
|
|
55
|
-
if (operationId === '*') {
|
|
56
|
-
throw new Error(`Invalid operationId/Route set for ${verb} ${route}`);
|
|
57
|
-
}
|
|
58
|
-
if (operationIds.includes(operationId)) {
|
|
59
|
-
return { implementation: '', imports: [], queryImports: [] };
|
|
60
|
-
}
|
|
61
|
-
operationIds.push(operationId);
|
|
62
|
-
|
|
63
|
-
imports = [];
|
|
64
|
-
|
|
65
|
-
route = route.replace(/\{/g, '${').replace('//', '/'); // `/pet/{id}` => `/pet/${id}`
|
|
66
|
-
|
|
67
|
-
// Remove the last param of the route if we are in the DELETE case
|
|
68
|
-
let lastParamInTheRoute: string | null = null;
|
|
69
|
-
|
|
70
|
-
const componentName = pascal(operationId!);
|
|
71
|
-
|
|
72
|
-
const isOk = ([statusCode]: [string, ResponseObject | ReferenceObject]) =>
|
|
73
|
-
statusCode.toString().startsWith('2');
|
|
74
|
-
|
|
75
|
-
const responseTypes = getResReqTypes(Object.entries(operation.responses).filter(isOk)) || 'unknown';
|
|
76
|
-
const requestBodyTypes = operation.requestBody ? getResReqTypes([['body', operation.requestBody]]) : null;
|
|
77
|
-
|
|
78
|
-
let queryImports = [] as Array<'mutation' | 'query' | 'infiniteQuery'>;
|
|
79
|
-
|
|
80
|
-
const paramsInPath = getParamsInPath(route).filter(
|
|
81
|
-
(param) => !(verb === 'delete' && param === lastParamInTheRoute)
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
const {
|
|
85
|
-
query: queryParams = [],
|
|
86
|
-
path: pathParams = [],
|
|
87
|
-
header = [],
|
|
88
|
-
} = groupBy(
|
|
89
|
-
[...(parameters || []), ...(operation.parameters || [])].map<ParameterObject>((p) => {
|
|
90
|
-
if (isReference(p)) {
|
|
91
|
-
return get(schemasComponents, p.$ref.replace('#/components/', '').replace('/', '.'));
|
|
92
|
-
} else {
|
|
93
|
-
return p;
|
|
94
|
-
}
|
|
95
|
-
}),
|
|
96
|
-
'in'
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
const headerParams = header.filter((p) => !headerFilters?.includes(p.name));
|
|
100
|
-
const requiredParams = [...queryParams, ...pathParams, ...headerParams].filter((p) => p.required);
|
|
101
|
-
// TODO: extract all requestBody or remove useQuery variants
|
|
102
|
-
let enabledParam: boolean | string = true;
|
|
103
|
-
if (requiredParams.length) {
|
|
104
|
-
enabledParam = 'hasDefinedProps(params,';
|
|
105
|
-
enabledParam += requiredParams.map((p) => `"${p.name}"`).join(', ');
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (operation.requestBody && 'content' in operation.requestBody) {
|
|
109
|
-
// get all required requestBody schemas and output the required properties
|
|
110
|
-
const generatedBodyProps = Object.keys(operation.requestBody.content).reduce((acc, contentType) => {
|
|
111
|
-
if (
|
|
112
|
-
contentType.startsWith('application/json') ||
|
|
113
|
-
contentType.startsWith('application/ld+json') ||
|
|
114
|
-
contentType.startsWith('application/octet-stream')
|
|
115
|
-
) {
|
|
116
|
-
// @ts-ignore
|
|
117
|
-
const schema = operation.requestBody.content[contentType].schema!;
|
|
118
|
-
|
|
119
|
-
if ('required' in schema) {
|
|
120
|
-
// @ts-ignore
|
|
121
|
-
acc.push(...schema.required);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return acc;
|
|
125
|
-
}, []);
|
|
126
|
-
|
|
127
|
-
if (generatedBodyProps.length) {
|
|
128
|
-
if (enabledParam === true) {
|
|
129
|
-
enabledParam = 'hasDefinedProps(params,';
|
|
130
|
-
enabledParam += generatedBodyProps.map((p) => `"${p}"`).join(', ');
|
|
131
|
-
} else {
|
|
132
|
-
enabledParam += ',';
|
|
133
|
-
enabledParam += generatedBodyProps.map((p) => `"${p}"`).join(', ');
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const paramsTypes = paramsInPath
|
|
139
|
-
.map((p) => {
|
|
140
|
-
try {
|
|
141
|
-
const { name, required, schema } = pathParams.find((i) => i.name === p)!;
|
|
142
|
-
return `${name}${required ? '' : '?'}: ${resolveValue(schema!)}`;
|
|
143
|
-
} catch (err) {
|
|
144
|
-
throw new Error(`The path params ${p} can't be found in parameters (${operationId})`);
|
|
145
|
-
}
|
|
146
|
-
})
|
|
147
|
-
.join('; ');
|
|
148
|
-
|
|
149
|
-
const queryParamsType = queryParams
|
|
150
|
-
.map((p) => {
|
|
151
|
-
const processedName = IdentifierRegexp.test(p.name) ? p.name : `"${p.name}"`;
|
|
152
|
-
return `${formatDescription(p.description)}
|
|
153
|
-
${processedName}${p.required ? '' : '?'}: ${resolveValue(p.schema!)}`;
|
|
154
|
-
})
|
|
155
|
-
.join(';\n ');
|
|
156
|
-
|
|
157
|
-
const headerType = headerParams
|
|
158
|
-
.map((p) => {
|
|
159
|
-
try {
|
|
160
|
-
const { name, required, schema } = headerParams.find((i) => i.name === p.name)!;
|
|
161
|
-
return `"${name}"${required ? '' : '?'}: ${resolveValue(schema!)}`;
|
|
162
|
-
} catch (err) {
|
|
163
|
-
throw new Error(`The path params ${p} can't be found in parameters (${operationId})`);
|
|
164
|
-
}
|
|
165
|
-
})
|
|
166
|
-
.join('; ');
|
|
167
|
-
|
|
168
|
-
// Retrieve the type of the param for delete verb
|
|
169
|
-
const lastParamInTheRouteDefinition =
|
|
170
|
-
operation.parameters && lastParamInTheRoute
|
|
171
|
-
? (operation.parameters.find((p) => {
|
|
172
|
-
if (isReference(p)) {
|
|
173
|
-
return false;
|
|
174
|
-
}
|
|
175
|
-
return p.name === lastParamInTheRoute;
|
|
176
|
-
}) as ParameterObject | undefined) // Reference is not possible
|
|
177
|
-
: { schema: { type: 'string' } };
|
|
178
|
-
|
|
179
|
-
if (!lastParamInTheRouteDefinition) {
|
|
180
|
-
throw new Error(`The path params ${lastParamInTheRoute} can't be found in parameters (${operationId})`);
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
let defaultDescription = `type: ${verb}\noperationId: ${operationId}\nurl: ${route}`;
|
|
184
|
-
if (operation.summary) {
|
|
185
|
-
defaultDescription += `\n\n${operation.summary}`;
|
|
186
|
-
}
|
|
187
|
-
if (operation.description) {
|
|
188
|
-
defaultDescription += `\n\n${operation.description}`;
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
const description = formatDescription(defaultDescription);
|
|
192
|
-
|
|
193
|
-
let output = `\n\n${description}`;
|
|
194
|
-
|
|
195
|
-
const headerParam = headerType && headerType !== 'void' ? `${headerType};` : '';
|
|
196
|
-
const queryParam = queryParamsType && queryParamsType !== 'void' ? `${queryParamsType}` : '';
|
|
197
|
-
const requestBodyComponent = requestBodyTypes && requestBodyTypes !== 'void' ? `${requestBodyTypes}` : '';
|
|
198
|
-
|
|
199
|
-
const isUpdateRequest = ['post', 'patch', 'put'].includes(verb);
|
|
200
|
-
|
|
201
|
-
const fetchName = camel(componentName);
|
|
202
|
-
|
|
203
|
-
const hasRequestBodyArrray = requestBodyComponent && requestBodyComponent.includes('[]');
|
|
204
|
-
const body = hasRequestBodyArrray ? `{body: ${requestBodyComponent}}` : `${requestBodyComponent}`;
|
|
205
|
-
const bodyProps = hasRequestBodyArrray ? `{body, ...props}` : 'props';
|
|
206
|
-
|
|
207
|
-
// const generateEnabledBodyProps = () => {
|
|
208
|
-
const definitionKey = Object.keys(schemasComponents?.schemas || {}).find(
|
|
209
|
-
(key) => pascal(key) === requestBodyComponent
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
if (definitionKey && !hasRequestBodyArrray) {
|
|
213
|
-
const scheme = schemasComponents?.schemas?.[definitionKey] as SchemaObject;
|
|
214
|
-
const generatedBodyProps = Object.keys(scheme.properties as SchemaObject);
|
|
215
|
-
const requiredBodyProps = generatedBodyProps.filter((p) => scheme.required?.includes(p));
|
|
216
|
-
|
|
217
|
-
if (requiredBodyProps.length) {
|
|
218
|
-
if (enabledParam === true) {
|
|
219
|
-
enabledParam = 'hasDefinedProps(params,';
|
|
220
|
-
enabledParam += requiredBodyProps.map((p) => `"${p}"`).join(', ');
|
|
221
|
-
} else {
|
|
222
|
-
enabledParam += ',';
|
|
223
|
-
enabledParam += requiredBodyProps.map((p) => `"${p}"`).join(', ');
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (operation.requestBody && 'content' in operation.requestBody) {
|
|
229
|
-
let generatedBodyProps = [];
|
|
230
|
-
for (let contentType of Object.keys(operation.requestBody.content)) {
|
|
231
|
-
if (
|
|
232
|
-
contentType.startsWith('application/json') ||
|
|
233
|
-
contentType.startsWith('application/ld+json') ||
|
|
234
|
-
contentType.startsWith('application/octet-stream')
|
|
235
|
-
) {
|
|
236
|
-
const schema = operation.requestBody.content[contentType].schema!;
|
|
237
|
-
|
|
238
|
-
if ('properties' in schema) {
|
|
239
|
-
// @ts-ignore
|
|
240
|
-
generatedBodyProps.push(...Object.keys(schema.properties));
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (enabledParam !== true) {
|
|
247
|
-
enabledParam += ')';
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const createQueryHooks = (emptyParams?: boolean) => {
|
|
251
|
-
const key = emptyParams ? '' : `params`;
|
|
252
|
-
const queryParamType = emptyParams ? '' : `Partial<${componentName}Params>`;
|
|
253
|
-
const queryKey = emptyParams
|
|
254
|
-
? `["${componentName.toLowerCase()}"]`
|
|
255
|
-
: `["${componentName.toLowerCase()}", params]`;
|
|
256
|
-
const props = emptyParams ? `props?` : `params`;
|
|
257
|
-
const propTest = emptyParams ? '' : `${props}: ${queryParamType}`;
|
|
258
|
-
const createQuery = () => `
|
|
259
|
-
export function get${componentName}QueryOptions(${propTest}) {
|
|
260
|
-
const enabled = ${enabledParam}
|
|
261
|
-
|
|
262
|
-
return queryOptions({
|
|
263
|
-
queryKey: ${queryKey},
|
|
264
|
-
queryFn: enabled ? () => ${fetchName}(${key}) : skipToken,
|
|
265
|
-
});
|
|
266
|
-
}`;
|
|
267
|
-
|
|
268
|
-
queryImports.push('query');
|
|
269
|
-
return createQuery();
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
const generateProps = (props: ParameterObject[]) => {
|
|
273
|
-
return props.map((item) => `["${item.name}"]: props["${item.name}"]`).join(',');
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const generateBodyProps = () => {
|
|
277
|
-
if (definitionKey && !hasRequestBodyArrray) {
|
|
278
|
-
const scheme = schemasComponents?.schemas?.[definitionKey] as SchemaObject;
|
|
279
|
-
const schemProperties = Object.keys(scheme.properties as SchemaObject);
|
|
280
|
-
if (
|
|
281
|
-
operation.requestBody &&
|
|
282
|
-
'content' in operation.requestBody &&
|
|
283
|
-
Object.keys(operation.requestBody.content)[0] === 'multipart/form-data'
|
|
284
|
-
) {
|
|
285
|
-
let formData = `const body = new FormData()`;
|
|
286
|
-
schemProperties.forEach((item) => {
|
|
287
|
-
if (scheme.required?.includes(item)) {
|
|
288
|
-
formData += `\nbody.append("${item}", props["${item}"]);`;
|
|
289
|
-
} else {
|
|
290
|
-
formData += `\n if (props["${item}"] != null) {\n body.append("${item}", props["${item}"]);\n}`;
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
return formData;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const generatedBodyProps = schemProperties
|
|
297
|
-
.map((item: string) => `["${item}"]: props["${item}"]`)
|
|
298
|
-
.join(',');
|
|
299
|
-
return `const body = {${generatedBodyProps}}`;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
if (operation.requestBody && 'content' in operation.requestBody) {
|
|
303
|
-
let generatedBodyProps = [];
|
|
304
|
-
let bodyData = '';
|
|
305
|
-
const contentTypes = Object.keys(operation.requestBody.content);
|
|
306
|
-
if (contentTypes.includes('multipart/form-data')) {
|
|
307
|
-
console.log('UPDATE2');
|
|
308
|
-
|
|
309
|
-
bodyData += `const body = new FormData()`;
|
|
310
|
-
|
|
311
|
-
for (let contentType of contentTypes) {
|
|
312
|
-
const schema = operation.requestBody.content[contentType].schema!;
|
|
313
|
-
if ('properties' in schema) {
|
|
314
|
-
const schemaProperties = Object.keys(schema.properties || []);
|
|
315
|
-
for (let item of schemaProperties) {
|
|
316
|
-
if (schema.required?.includes(item)) {
|
|
317
|
-
bodyData += `\nbody.append("${item}", props["${item}"].toString());`;
|
|
318
|
-
} else {
|
|
319
|
-
bodyData += `\n if (props["${item}"] != null) {\n body.append("${item}", props["${item}"].toString());\n}`;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return bodyData;
|
|
325
|
-
}
|
|
326
|
-
for (let contentType of contentTypes) {
|
|
327
|
-
if (
|
|
328
|
-
contentType.startsWith('application/json') ||
|
|
329
|
-
contentType.startsWith('application/ld+json') ||
|
|
330
|
-
contentType.startsWith('application/octet-stream')
|
|
331
|
-
) {
|
|
332
|
-
const schema = operation.requestBody.content[contentType].schema!;
|
|
333
|
-
|
|
334
|
-
if ('properties' in schema) {
|
|
335
|
-
// @ts-ignore
|
|
336
|
-
generatedBodyProps.push(...Object.keys(schema.properties));
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
return `const body = {${generatedBodyProps.map((item) => `${item}: props["${item}"]`).join(',')}}`;
|
|
341
|
-
}
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
const hasFormData = generateBodyProps()?.includes('FormData');
|
|
345
|
-
const defaultHeaders = JSON.stringify({
|
|
346
|
-
ContentType: hasFormData ? 'multipart/form-data' : 'application/json',
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
350
|
-
output += `
|
|
351
|
-
export const ${fetchName} = async () => {
|
|
352
|
-
const result = await api.${verb}<${responseTypes}>("${route}");
|
|
353
|
-
return result.data;
|
|
354
|
-
}
|
|
355
|
-
`;
|
|
356
|
-
}
|
|
357
|
-
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
358
|
-
const config = isUpdateRequest ? 'undefined,{params}' : '{params}';
|
|
359
|
-
output += `
|
|
360
|
-
export type ${componentName}Params = {
|
|
361
|
-
${paramsTypes}
|
|
362
|
-
${queryParamsType};
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
export const ${fetchName} = async (props:${componentName}Params) => {
|
|
366
|
-
const {${paramsInPath.join(', ')}, ...params} = props
|
|
367
|
-
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
|
|
368
|
-
return result.data;
|
|
369
|
-
}`;
|
|
370
|
-
}
|
|
371
|
-
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
372
|
-
output += `
|
|
373
|
-
export type ${componentName}Params = {
|
|
374
|
-
${paramsTypes}
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
export const ${fetchName} = async (props: ${componentName}Params ) => {
|
|
378
|
-
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
379
|
-
return result.data;
|
|
380
|
-
}
|
|
381
|
-
`;
|
|
382
|
-
}
|
|
383
|
-
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
384
|
-
const config = isUpdateRequest ? 'null,{params}' : '{params}';
|
|
385
|
-
output += `
|
|
386
|
-
export type ${componentName}Params = {
|
|
387
|
-
${queryParamsType}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
export const ${fetchName} = async (params: ${componentName}Params) => {
|
|
391
|
-
const result = await api.${verb}<${responseTypes}>("${route}", ${config})
|
|
392
|
-
return result.data;
|
|
393
|
-
}
|
|
394
|
-
`;
|
|
395
|
-
}
|
|
396
|
-
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
397
|
-
const config = isUpdateRequest ? 'null,{headers, params: queryParams}' : '{headers, params: queryParams}';
|
|
398
|
-
output += `
|
|
399
|
-
export type ${componentName}Params = {
|
|
400
|
-
${headerParam}
|
|
401
|
-
${queryParamsType}
|
|
402
|
-
}
|
|
403
|
-
export const ${fetchName} = async (props: ${componentName}Params) => {
|
|
404
|
-
const headers = {${generateProps(header)}}
|
|
405
|
-
const queryParams = {${generateProps(queryParams)}}
|
|
406
|
-
const result = await api.${verb}<${responseTypes}>("${route}", ${config})
|
|
407
|
-
return result.data
|
|
408
|
-
}`;
|
|
409
|
-
}
|
|
410
|
-
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
411
|
-
const config = isUpdateRequest ? 'null,{headers}' : '{headers}';
|
|
412
|
-
output += `
|
|
413
|
-
export type ${componentName}Params = {
|
|
414
|
-
${headerParam}
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
export const ${fetchName} = async (headers: ${componentName}Params) => {
|
|
418
|
-
const result = await api.${verb}<${responseTypes}>("${route}", ${config});
|
|
419
|
-
return result.data;
|
|
420
|
-
}
|
|
421
|
-
`;
|
|
422
|
-
}
|
|
423
|
-
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
424
|
-
output += `
|
|
425
|
-
|
|
426
|
-
export type ${componentName}Params = ${requestBodyComponent}
|
|
427
|
-
|
|
428
|
-
export const ${fetchName} = async (body: ${componentName}Params) => {
|
|
429
|
-
const result = await api.${verb}<${responseTypes}>("${route}", body, {headers: ${defaultHeaders}})
|
|
430
|
-
return result.data
|
|
431
|
-
}
|
|
432
|
-
`;
|
|
433
|
-
}
|
|
434
|
-
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
435
|
-
output += `
|
|
436
|
-
export type ${componentName}Params = ${body} & {
|
|
437
|
-
${queryParamsType}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
|
|
441
|
-
${generateBodyProps()}
|
|
442
|
-
const params = {${generateProps(queryParams)}}
|
|
443
|
-
const result = await api.${verb}<${responseTypes}>("${route}", body, {params, headers: ${defaultHeaders}})
|
|
444
|
-
return result.data
|
|
445
|
-
}
|
|
446
|
-
`;
|
|
447
|
-
}
|
|
448
|
-
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
449
|
-
output += `
|
|
450
|
-
export type ${componentName}Params = ${body} & {
|
|
451
|
-
${headerParam}
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
|
|
455
|
-
${generateBodyProps()}
|
|
456
|
-
const headers = null
|
|
457
|
-
const result = await api.${verb}<${responseTypes}>("${route}", body, {headers})
|
|
458
|
-
return result.data
|
|
459
|
-
}
|
|
460
|
-
`;
|
|
461
|
-
}
|
|
462
|
-
if (requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
463
|
-
output += `
|
|
464
|
-
export type ${componentName}Params = ${body} & {
|
|
465
|
-
${headerParam}
|
|
466
|
-
${queryParamsType}
|
|
467
|
-
};
|
|
468
|
-
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
|
|
469
|
-
${generateBodyProps()}
|
|
470
|
-
const headers = null
|
|
471
|
-
const params = {${generateProps(queryParams)}}
|
|
472
|
-
const result = await api.${verb}<${responseTypes}>("${route}", body, {headers, params})
|
|
473
|
-
return result.data
|
|
474
|
-
}
|
|
475
|
-
`;
|
|
476
|
-
}
|
|
477
|
-
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
478
|
-
output += `
|
|
479
|
-
export type ${componentName}Params = ${body} & {
|
|
480
|
-
${headerParam}
|
|
481
|
-
${paramsTypes}
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
|
|
485
|
-
${generateBodyProps()}
|
|
486
|
-
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`, body, {headers: ${defaultHeaders}})
|
|
487
|
-
return result.data
|
|
488
|
-
}
|
|
489
|
-
`;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
if (requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
493
|
-
output += `
|
|
494
|
-
export type ${componentName}Params = ${body} & {
|
|
495
|
-
${headerParam}
|
|
496
|
-
${paramsTypes}
|
|
497
|
-
${queryParamsType}
|
|
498
|
-
};
|
|
499
|
-
|
|
500
|
-
export const ${fetchName} = async (${bodyProps}: ${componentName}Params) => {
|
|
501
|
-
${generateBodyProps()}
|
|
502
|
-
const params = {${generateProps(queryParams)}}
|
|
503
|
-
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`, body, {params, headers: ${defaultHeaders}})
|
|
504
|
-
return result.data
|
|
505
|
-
}
|
|
506
|
-
`;
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
if (requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
510
|
-
output += `// TODO: NOT SUPPORTED requestBodyComponent && paramsInPath && queryParam && headerParam)`;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
if (!requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
514
|
-
const config = isUpdateRequest ? 'null,{headers}' : '{headers}';
|
|
515
|
-
output += `
|
|
516
|
-
export type ${componentName}Params = {
|
|
517
|
-
${headerParam}
|
|
518
|
-
${paramsTypes}
|
|
519
|
-
};
|
|
520
|
-
|
|
521
|
-
export const ${fetchName} = async (props: ${componentName}Params) => {
|
|
522
|
-
const headers = {${generateProps(header)}}
|
|
523
|
-
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`, ${config})
|
|
524
|
-
return result.data
|
|
525
|
-
}
|
|
526
|
-
`;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
if (!requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
530
|
-
output += `// TODO: NOT SUPPORTED (paramsInPath && queryParam && headerParam)`;
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
if (requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
534
|
-
output += `// TODO: NOT SUPPORTED (requestBodyComponent && paramsInPath && headerParam)`;
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
output += createQueryHooks(!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam);
|
|
538
|
-
|
|
539
|
-
return { implementation: output, imports, queryImports };
|
|
540
|
-
};
|
package/src/generateImports.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
export const generateImports = ({
|
|
2
|
-
schemaName,
|
|
3
|
-
apiDirectory,
|
|
4
|
-
schemaImports,
|
|
5
|
-
queryImports,
|
|
6
|
-
}: {
|
|
7
|
-
apiDirectory: string;
|
|
8
|
-
schemaName: string;
|
|
9
|
-
schemaImports: string[];
|
|
10
|
-
queryImports: ('query' | 'mutation' | 'infiniteQuery')[];
|
|
11
|
-
}) => {
|
|
12
|
-
const importTypes = schemaImports.join(',');
|
|
13
|
-
let imports = [] as string[];
|
|
14
|
-
if (queryImports.includes('query')) {
|
|
15
|
-
imports = [...imports, 'skipToken', 'queryOptions'];
|
|
16
|
-
}
|
|
17
|
-
if (queryImports.includes('infiniteQuery')) {
|
|
18
|
-
imports = [...imports, 'infiniteQueryOptions'];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const importString = [...new Set(imports)].join(',');
|
|
22
|
-
|
|
23
|
-
return `
|
|
24
|
-
import {
|
|
25
|
-
${importString}
|
|
26
|
-
} from '@tanstack/react-query';
|
|
27
|
-
|
|
28
|
-
import { api } from '${apiDirectory}';
|
|
29
|
-
import {${importTypes}} from './${schemaName}'
|
|
30
|
-
|
|
31
|
-
`;
|
|
32
|
-
};
|
package/src/generateSchemas.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import pasCase from 'case';
|
|
2
|
-
import lodash from 'lodash';
|
|
3
|
-
const { isEmpty } = lodash;
|
|
4
|
-
const { pascal } = pasCase;
|
|
5
|
-
import { ComponentsObject, OpenAPIObject, SchemaObject } from 'openapi3-ts';
|
|
6
|
-
import { formatDescription, getScalar, isReference, resolveValue } from './utils.js';
|
|
7
|
-
|
|
8
|
-
import { getDocs, getResReqTypes } from './utils.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Generate the interface string
|
|
12
|
-
*/
|
|
13
|
-
const generateInterface = (name: string, schema: SchemaObject) => {
|
|
14
|
-
const scalar = getScalar(schema);
|
|
15
|
-
return `${formatDescription(schema.description)}
|
|
16
|
-
export type ${pascal(name)} = ${scalar}
|
|
17
|
-
`;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Extract all types from #/components/schemas
|
|
22
|
-
*/
|
|
23
|
-
const generateSchemasDefinition = (schemas: ComponentsObject['schemas'] = {}) => {
|
|
24
|
-
if (isEmpty(schemas)) {
|
|
25
|
-
return '';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return (
|
|
29
|
-
'\n // SCEHMAS \n' +
|
|
30
|
-
Object.entries(schemas)
|
|
31
|
-
.map(([name, schema]) =>
|
|
32
|
-
!isReference(schema) &&
|
|
33
|
-
(!schema.type || schema.type === 'object') &&
|
|
34
|
-
!schema.allOf &&
|
|
35
|
-
!schema.oneOf &&
|
|
36
|
-
!isReference(schema) &&
|
|
37
|
-
!schema.nullable
|
|
38
|
-
? generateInterface(name, schema)
|
|
39
|
-
: `${formatDescription(isReference(schema) ? undefined : schema.description)} export type ${pascal(
|
|
40
|
-
name
|
|
41
|
-
)} = ${resolveValue(schema)};`
|
|
42
|
-
)
|
|
43
|
-
.join('\n\n') +
|
|
44
|
-
'\n'
|
|
45
|
-
);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Extract all types from #/components/requestBodies
|
|
50
|
-
*/
|
|
51
|
-
const generateRequestBodiesDefinition = (requestBodies: ComponentsObject['requestBodies'] = {}) => {
|
|
52
|
-
if (isEmpty(requestBodies)) {
|
|
53
|
-
return '';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return (
|
|
57
|
-
'\n // REQUEST BODIES \n' +
|
|
58
|
-
Object.entries(requestBodies)
|
|
59
|
-
.map(([name, requestBody]) => {
|
|
60
|
-
const doc = getDocs(requestBody);
|
|
61
|
-
const type = getResReqTypes([['', requestBody]]);
|
|
62
|
-
const isEmptyInterface = type === '{}';
|
|
63
|
-
if (isEmptyInterface) {
|
|
64
|
-
return `${doc}\nexport type ${pascal(name)}RequestBody = ${type}`;
|
|
65
|
-
} else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
66
|
-
return `${doc}\nexport type ${pascal(name)}RequestBody = ${type}`;
|
|
67
|
-
} else {
|
|
68
|
-
return `${doc}\nexport type ${pascal(name)}RequestBody = ${type};`;
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
.join('\n\n') +
|
|
72
|
-
'\n'
|
|
73
|
-
);
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Extract all types from #/components/responses
|
|
78
|
-
*/
|
|
79
|
-
const generateResponsesDefinition = (responses: ComponentsObject['responses'] = {}) => {
|
|
80
|
-
if (isEmpty(responses)) {
|
|
81
|
-
return '';
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return (
|
|
85
|
-
'\n // RESPONSES \n' +
|
|
86
|
-
Object.entries(responses)
|
|
87
|
-
.map(([name, response]) => {
|
|
88
|
-
const doc = getDocs(response);
|
|
89
|
-
const type = getResReqTypes([['', response]]);
|
|
90
|
-
const isEmptyInterface = type === '{}';
|
|
91
|
-
if (!type) {
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
if (isEmptyInterface) {
|
|
95
|
-
return `export type RQ${pascal(name)}Response = ${type}`;
|
|
96
|
-
} else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
97
|
-
return `${doc}\nexport type RQ${pascal(name)}Response = ${type}`;
|
|
98
|
-
} else {
|
|
99
|
-
return `${doc}\nexport type RQ${pascal(name)}Response = ${type};`;
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
.join('\n\n') +
|
|
103
|
-
'\n'
|
|
104
|
-
);
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Extract all types from #/components/schemas
|
|
109
|
-
*/
|
|
110
|
-
export const generateSchemas = ({ spec }: { spec: OpenAPIObject }) => {
|
|
111
|
-
const schemaOutput =
|
|
112
|
-
generateRequestBodiesDefinition(spec.components?.requestBodies) +
|
|
113
|
-
generateResponsesDefinition(spec.components?.responses) +
|
|
114
|
-
generateSchemasDefinition(spec.components?.schemas);
|
|
115
|
-
|
|
116
|
-
return schemaOutput;
|
|
117
|
-
};
|