react-query-lightbase-codegen 1.0.3 → 1.0.4
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/convertSwaggerFile.d.ts +5 -0
- package/dist/convertSwaggerFile.js +26 -0
- package/dist/convertSwaggerFile.js.map +1 -0
- package/dist/generateHooks.d.ts +25 -0
- package/dist/generateHooks.js +395 -0
- package/dist/generateHooks.js.map +1 -0
- package/dist/generateImports.d.ts +7 -0
- package/dist/generateImports.js +28 -0
- package/dist/generateImports.js.map +1 -0
- package/dist/generateSchemas.d.ts +7 -0
- package/dist/generateSchemas.js +97 -0
- package/dist/generateSchemas.js.map +1 -0
- package/dist/importSpecs.d.ts +15 -0
- package/dist/importSpecs.js +81 -0
- package/dist/importSpecs.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +24 -0
- package/dist/utils.js +157 -0
- package/dist/utils.js.map +1 -0
- package/package.json +2 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import swagger2openapi from 'swagger2openapi';
|
|
2
|
+
import yaml from 'js-yaml';
|
|
3
|
+
/**
|
|
4
|
+
* Import and parse the openapi spec from a yaml/json
|
|
5
|
+
*/
|
|
6
|
+
export const convertSwaggerFile = (data, extension) => {
|
|
7
|
+
const schema = extension === 'yaml' ? yaml.load(data) : JSON.parse(data);
|
|
8
|
+
return new Promise((resolve, reject) => {
|
|
9
|
+
if (!schema.openapi || !schema.openapi.startsWith('3.')) {
|
|
10
|
+
swagger2openapi.convertObj(schema, {}, (err, convertedObj) => {
|
|
11
|
+
if (err) {
|
|
12
|
+
reject(err);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
convertedObj.openapi.basePath = convertedObj.original.basePath;
|
|
17
|
+
resolve(convertedObj.openapi);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
resolve(schema);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=convertSwaggerFile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"convertSwaggerFile.js","sourceRoot":"","sources":["../src/convertSwaggerFile.ts"],"names":[],"mappings":"AACA,OAAO,eAAe,MAAM,iBAAiB,CAAC;AAC9C,OAAO,IAAI,MAAM,SAAS,CAAC;AAE3B;;GAEG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,IAAY,EAAE,SAA0B,EAA0B,EAAE;IACrG,MAAM,MAAM,GAAG,SAAS,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACzE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE;YACvD,eAAe,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,EAAE,CAAC,GAAG,EAAE,YAAY,EAAE,EAAE;gBAC3D,IAAI,GAAG,EAAE;oBACP,MAAM,CAAC,GAAG,CAAC,CAAC;iBACb;qBAAM;oBACL,aAAa;oBACb,YAAY,CAAC,OAAO,CAAC,QAAQ,GAAG,YAAY,CAAC,QAAQ,CAAC,QAAQ,CAAC;oBAC/D,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC;iBAC/B;YACH,CAAC,CAAC,CAAC;SACJ;aAAM;YACL,OAAO,CAAC,MAAM,CAAC,CAAC;SACjB;IACH,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ComponentsObject, OperationObject, ParameterObject, ReferenceObject } from 'openapi3-ts';
|
|
2
|
+
/**
|
|
3
|
+
* Generate a react-query component from openapi operation specs
|
|
4
|
+
*/
|
|
5
|
+
export declare const createHook: ({ operation, verb, route, operationIds, parameters, schemasComponents, headerFilters, overrides, }: {
|
|
6
|
+
operation: OperationObject;
|
|
7
|
+
verb: string;
|
|
8
|
+
route: string;
|
|
9
|
+
operationIds: string[];
|
|
10
|
+
parameters: (ReferenceObject | ParameterObject)[] | undefined;
|
|
11
|
+
schemasComponents?: ComponentsObject | undefined;
|
|
12
|
+
headerFilters?: string[] | undefined;
|
|
13
|
+
overrides?: Record<string, {
|
|
14
|
+
type: 'query';
|
|
15
|
+
} | {
|
|
16
|
+
type: 'mutation';
|
|
17
|
+
} | {
|
|
18
|
+
type: 'infiniteQuery';
|
|
19
|
+
infiniteQueryParm: string;
|
|
20
|
+
}> | undefined;
|
|
21
|
+
}) => {
|
|
22
|
+
implementation: string;
|
|
23
|
+
imports: string[];
|
|
24
|
+
queryImports: ("query" | "mutation" | "infiniteQuery")[];
|
|
25
|
+
};
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import lodash from 'lodash';
|
|
2
|
+
const { get, groupBy } = lodash;
|
|
3
|
+
import { formatDescription, getResReqTypes, isReference, resolveValue } from './utils.js';
|
|
4
|
+
import pasCase from 'case';
|
|
5
|
+
import chalk from 'chalk';
|
|
6
|
+
const { pascal, camel } = pasCase;
|
|
7
|
+
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
8
|
+
/**
|
|
9
|
+
* Return every params in a path
|
|
10
|
+
*/
|
|
11
|
+
const getParamsInPath = (path) => {
|
|
12
|
+
let n;
|
|
13
|
+
const output = [];
|
|
14
|
+
const templatePathRegex = /\{(\w+)}/g;
|
|
15
|
+
while ((n = templatePathRegex.exec(path)) !== null) {
|
|
16
|
+
output.push(n[1]);
|
|
17
|
+
}
|
|
18
|
+
return output;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Generate a react-query component from openapi operation specs
|
|
22
|
+
*/
|
|
23
|
+
export const createHook = ({ operation, verb, route, operationIds, parameters, schemasComponents, headerFilters, overrides, }) => {
|
|
24
|
+
const { operationId = route.replace('/', '') } = operation;
|
|
25
|
+
if (operationId === '*') {
|
|
26
|
+
throw new Error(`Invalid operationId/Route set for ${verb} ${route}`);
|
|
27
|
+
}
|
|
28
|
+
if (operationIds.includes(operationId)) {
|
|
29
|
+
return { implementation: '', imports: [], queryImports: [] };
|
|
30
|
+
}
|
|
31
|
+
operationIds.push(operationId);
|
|
32
|
+
route = route.replace(/\{/g, '${').replace('//', '/'); // `/pet/{id}` => `/pet/${id}`
|
|
33
|
+
// Remove the last param of the route if we are in the DELETE case
|
|
34
|
+
let lastParamInTheRoute = null;
|
|
35
|
+
const componentName = pascal(operationId);
|
|
36
|
+
const isOk = ([statusCode]) => statusCode.toString().startsWith('2');
|
|
37
|
+
const responseTypes = getResReqTypes(Object.entries(operation.responses).filter(isOk)) || 'void';
|
|
38
|
+
const requestBodyTypes = getResReqTypes([['body', operation.requestBody]]);
|
|
39
|
+
let imports = [responseTypes];
|
|
40
|
+
let queryImports = [];
|
|
41
|
+
const paramsInPath = getParamsInPath(route).filter((param) => !(verb === 'delete' && param === lastParamInTheRoute));
|
|
42
|
+
const { query: queryParams = [], path: pathParams = [], header = [], } = groupBy([...(parameters || []), ...(operation.parameters || [])].map((p) => {
|
|
43
|
+
if (isReference(p)) {
|
|
44
|
+
return get(schemasComponents, p.$ref.replace('#/components/', '').replace('/', '.'));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return p;
|
|
48
|
+
}
|
|
49
|
+
}), 'in');
|
|
50
|
+
const headerParams = header.filter((p) => !headerFilters?.includes(p.name));
|
|
51
|
+
let enabled = [];
|
|
52
|
+
// TODO: extract all requestBody or remove useQuery variants
|
|
53
|
+
let enabledParam = '!!params';
|
|
54
|
+
[...queryParams, ...pathParams, ...headerParams].forEach((item) => {
|
|
55
|
+
if (item.required) {
|
|
56
|
+
enabled.push(`["${item.name}"]`);
|
|
57
|
+
if (enabledParam && enabledParam !== '!!params') {
|
|
58
|
+
enabledParam += `&& params['${item.name}'] != null`;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
enabledParam = `params['${item.name}'] != null`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
// `!props${enabled.join('== null && !props')}`;
|
|
66
|
+
const paramsTypes = paramsInPath
|
|
67
|
+
.map((p) => {
|
|
68
|
+
try {
|
|
69
|
+
const { name, required, schema } = pathParams.find((i) => i.name === p);
|
|
70
|
+
return `${name}${required ? '' : '?'}: ${resolveValue(schema)}`;
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
throw new Error(`The path params ${p} can't be found in parameters (${operationId})`);
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
.join('; ');
|
|
77
|
+
const queryParamsType = queryParams
|
|
78
|
+
.map((p) => {
|
|
79
|
+
const processedName = IdentifierRegexp.test(p.name) ? p.name : `"${p.name}"`;
|
|
80
|
+
return `${formatDescription(p.description)}
|
|
81
|
+
${processedName}${p.required ? '' : '?'}: ${resolveValue(p.schema)}`;
|
|
82
|
+
})
|
|
83
|
+
.join(';\n ');
|
|
84
|
+
const headerType = headerParams
|
|
85
|
+
.map((p) => {
|
|
86
|
+
try {
|
|
87
|
+
const { name, required, schema } = headerParams.find((i) => i.name === p.name);
|
|
88
|
+
return `"${name}"${required ? '' : '?'}: ${resolveValue(schema)}`;
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
throw new Error(`The path params ${p} can't be found in parameters (${operationId})`);
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
.join('; ');
|
|
95
|
+
// Retrieve the type of the param for delete verb
|
|
96
|
+
const lastParamInTheRouteDefinition = operation.parameters && lastParamInTheRoute
|
|
97
|
+
? operation.parameters.find((p) => {
|
|
98
|
+
if (isReference(p)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return p.name === lastParamInTheRoute;
|
|
102
|
+
}) // Reference is not possible
|
|
103
|
+
: { schema: { type: 'string' } };
|
|
104
|
+
if (!lastParamInTheRouteDefinition) {
|
|
105
|
+
throw new Error(`The path params ${lastParamInTheRoute} can't be found in parameters (${operationId})`);
|
|
106
|
+
}
|
|
107
|
+
const defaultDescription = `type: ${verb}\noperationId: ${operationId}\nurl: ${route}`;
|
|
108
|
+
const description = formatDescription(operation.summary && operation.description
|
|
109
|
+
? `${defaultDescription}\n\n${operation.summary}\n\n${operation.description}`
|
|
110
|
+
: `${defaultDescription}`);
|
|
111
|
+
let output = `\n\n${description}`;
|
|
112
|
+
const headerParam = headerType && headerType !== 'void' ? `${headerType};` : '';
|
|
113
|
+
const queryParam = queryParamsType && queryParamsType !== 'void' ? `${queryParamsType}` : '';
|
|
114
|
+
const requestBodyComponent = requestBodyTypes && requestBodyTypes !== 'void' ? `${requestBodyTypes}` : '';
|
|
115
|
+
if (requestBodyComponent) {
|
|
116
|
+
imports.push(requestBodyComponent);
|
|
117
|
+
}
|
|
118
|
+
const isUpdateRequest = ['post', 'patch', 'put'].includes(verb);
|
|
119
|
+
const generateProps = (props) => {
|
|
120
|
+
return props.map((item) => `["${item.name}"]: props["${item.name}"]`).join(',');
|
|
121
|
+
};
|
|
122
|
+
const generateBodyProps = () => {
|
|
123
|
+
const definitionKey = Object.keys(schemasComponents?.schemas || {}).find((key) => pascal(key) === requestBodyComponent);
|
|
124
|
+
if (definitionKey) {
|
|
125
|
+
const scheme = schemasComponents?.schemas?.[definitionKey];
|
|
126
|
+
return Object.keys(scheme.properties)
|
|
127
|
+
.map((item) => `["${item}"]: props["${item}"]`)
|
|
128
|
+
.join(',');
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const fetchName = camel(componentName);
|
|
132
|
+
const createQueryHooks = (emptyParams) => {
|
|
133
|
+
const params = emptyParams ? '' : `params: ${componentName}Params,`;
|
|
134
|
+
const key = emptyParams ? '' : `params`;
|
|
135
|
+
const mutationParams = emptyParams ? 'void' : `${componentName}Params`;
|
|
136
|
+
const queryParamType = emptyParams ? '' : `${componentName}Params &`;
|
|
137
|
+
const filterParams = emptyParams
|
|
138
|
+
? '{ filters }: { filters?: QueryFilters }'
|
|
139
|
+
: `{ params, filters }: { params: ${componentName}Params, filters?: QueryFilters }`;
|
|
140
|
+
const cacheParams = emptyParams
|
|
141
|
+
? `{updater, options}: {updater: Updater<${responseTypes} | undefined, ${responseTypes} | undefined>, options?: SetDataOptions | undefined}`
|
|
142
|
+
: `{params, updater, options}: {params: ${componentName}Params, updater: Updater<${responseTypes} | undefined, ${responseTypes} | undefined>, options?: SetDataOptions | undefined}`;
|
|
143
|
+
const queryKey = emptyParams
|
|
144
|
+
? `use${componentName}Query.baseKey()`
|
|
145
|
+
: `[...use${componentName}Query.baseKey(), params]`;
|
|
146
|
+
const createQuery = () => `
|
|
147
|
+
type ${componentName}QueryProps<T = ${responseTypes}> = ${queryParamType} {
|
|
148
|
+
options?: UseQueryOptions<${responseTypes}, AxiosError, T, any>
|
|
149
|
+
}
|
|
150
|
+
export function use${componentName}Query<T = ${responseTypes}>({ options = {}, ...params }: ${componentName}QueryProps<T>) {
|
|
151
|
+
return useQuery(use${componentName}Query.queryKey(${key}), async () => ${fetchName}(${key}), { enabled: ${enabledParam}, ...options });
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
155
|
+
|
|
156
|
+
use${componentName}Query.queryKey = (${params}): QueryKey => ${queryKey};
|
|
157
|
+
|
|
158
|
+
use${componentName}Query.updateCache = (${cacheParams}) => queryClient.setQueryData<${responseTypes}>(use${componentName}Query.queryKey(${key}), updater, options);
|
|
159
|
+
|
|
160
|
+
use${componentName}Query.getQueryState = (${filterParams})=> queryClient.getQueryState<${responseTypes}>(use${componentName}Query.queryKey(${key}), filters);
|
|
161
|
+
|
|
162
|
+
use${componentName}Query.getQueryData = (${filterParams})=> queryClient.getQueryData<${responseTypes}>(use${componentName}Query.queryKey(${key}), filters);
|
|
163
|
+
|
|
164
|
+
use${componentName}Query.prefetch = (${params}) => queryClient.prefetchQuery<${responseTypes}>(use${componentName}Query.queryKey(${key}), ()=> ${fetchName}(${key}));
|
|
165
|
+
|
|
166
|
+
use${componentName}Query.cancelQueries = (${params}) => queryClient.cancelQueries(use${componentName}Query.queryKey(${key}))
|
|
167
|
+
|
|
168
|
+
use${componentName}Query.invalidate = (${params}) => queryClient.invalidateQueries<${responseTypes}>(use${componentName}Query.queryKey(${key}));
|
|
169
|
+
|
|
170
|
+
use${componentName}Query.refetchStale = (${params}) => queryClient.refetchQueries<${responseTypes}>(use${componentName}Query.queryKey(${key}), { stale: true });
|
|
171
|
+
`;
|
|
172
|
+
const getInfiniteQuery = ({ pageParam }) => `
|
|
173
|
+
type ${componentName}QueryProps<T = ${responseTypes}> = ${queryParamType} {
|
|
174
|
+
options: UseInfiniteQueryOptions<${responseTypes}, AxiosError, T, any>
|
|
175
|
+
}
|
|
176
|
+
export function use${componentName}Query<T = ${responseTypes}>({ options = {}, ...params }: ${componentName}QueryProps<T>) {
|
|
177
|
+
return useInfiniteQuery(use${componentName}Query.queryKey(${key}), async ({ pageParam = 0 }) => ${fetchName}({${pageParam}: pageParam, ...params}), { enabled: ${enabledParam}, ...options });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
use${componentName}Query.baseKey = (): QueryKey => ["${componentName.toLowerCase()}"];
|
|
181
|
+
|
|
182
|
+
use${componentName}Query.queryKey = (${params}): QueryKey => ${queryKey};
|
|
183
|
+
|
|
184
|
+
`;
|
|
185
|
+
const createMutation = () => `
|
|
186
|
+
type ${componentName}MutationProps<T> = {
|
|
187
|
+
options?: UseMutationOptions<${responseTypes}, AxiosError, ${mutationParams}, T>
|
|
188
|
+
}
|
|
189
|
+
export function use${componentName}Mutation<T = ${responseTypes}>(props?: ${componentName}MutationProps<T>) {
|
|
190
|
+
return useMutation(async (${key}) => ${fetchName}(${key}), props?.options)
|
|
191
|
+
};
|
|
192
|
+
`;
|
|
193
|
+
const override = overrides?.[operationId];
|
|
194
|
+
if (override?.type === 'query') {
|
|
195
|
+
console.log(chalk.blue(`⚙️ [${operationId}] has been changed to a useQuery hook`));
|
|
196
|
+
queryImports.push('query');
|
|
197
|
+
return createQuery();
|
|
198
|
+
}
|
|
199
|
+
if (override?.type === 'mutation') {
|
|
200
|
+
queryImports.push('mutation');
|
|
201
|
+
console.log(chalk.blue(`⚙️ [${operationId}] has been changed to a useMutation hook`));
|
|
202
|
+
return createMutation();
|
|
203
|
+
}
|
|
204
|
+
if (override?.type === 'infiniteQuery') {
|
|
205
|
+
console.log(chalk.blue(`⚙️ [${operationId}] has been changed to a useInfiniteQuery hook`));
|
|
206
|
+
queryImports.push('infiniteQuery');
|
|
207
|
+
return getInfiniteQuery({ pageParam: override.infiniteQueryParm });
|
|
208
|
+
}
|
|
209
|
+
if (verb === 'get') {
|
|
210
|
+
queryImports.push('query');
|
|
211
|
+
return createQuery();
|
|
212
|
+
}
|
|
213
|
+
queryImports.push('mutation');
|
|
214
|
+
return createMutation();
|
|
215
|
+
};
|
|
216
|
+
output += createQueryHooks(!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam);
|
|
217
|
+
if (!requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
218
|
+
output += `
|
|
219
|
+
const ${fetchName} = async () => {
|
|
220
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`);
|
|
221
|
+
return result.data;
|
|
222
|
+
}
|
|
223
|
+
`;
|
|
224
|
+
}
|
|
225
|
+
if (!requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
226
|
+
const config = isUpdateRequest ? 'body,{params}' : '{params}';
|
|
227
|
+
output += `
|
|
228
|
+
type ${componentName}Params = {
|
|
229
|
+
${paramsTypes}
|
|
230
|
+
${queryParamsType};
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const ${fetchName} = async (props:${componentName}Params) => {
|
|
234
|
+
const {${paramsInPath.join(', ')}, ...params} = props
|
|
235
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
|
|
236
|
+
return result.data;
|
|
237
|
+
}`;
|
|
238
|
+
}
|
|
239
|
+
if (!requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
240
|
+
output += `
|
|
241
|
+
type ${componentName}Params = {
|
|
242
|
+
${paramsTypes}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const ${fetchName} = async (props: ${componentName}Params ) => {
|
|
246
|
+
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`);
|
|
247
|
+
return result.data;
|
|
248
|
+
}
|
|
249
|
+
`;
|
|
250
|
+
}
|
|
251
|
+
if (!requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
252
|
+
const config = isUpdateRequest ? 'null,{params}' : '{params}';
|
|
253
|
+
output += `
|
|
254
|
+
type ${componentName}Params = {
|
|
255
|
+
${queryParamsType}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const ${fetchName} = async (params: ${componentName}Params) => {
|
|
259
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
|
|
260
|
+
return result.data;
|
|
261
|
+
}
|
|
262
|
+
`;
|
|
263
|
+
}
|
|
264
|
+
if (!requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
265
|
+
const config = isUpdateRequest ? 'null,{headers, params: queryParams}' : '{headers, params: queryParams}';
|
|
266
|
+
output += `
|
|
267
|
+
type ${componentName}Params = {
|
|
268
|
+
${headerParam}
|
|
269
|
+
${queryParamsType}
|
|
270
|
+
}
|
|
271
|
+
const ${fetchName} = async (props: ${componentName}Params) => {
|
|
272
|
+
const headers = {${generateProps(header)}}
|
|
273
|
+
const queryParams = {${generateProps(queryParams)}}
|
|
274
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
|
|
275
|
+
return result.data
|
|
276
|
+
}`;
|
|
277
|
+
}
|
|
278
|
+
if (!requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
279
|
+
const config = isUpdateRequest ? 'null,{headers}' : '{headers}';
|
|
280
|
+
output += `
|
|
281
|
+
type ${componentName}Params = {
|
|
282
|
+
${headerParam}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
const ${fetchName} = async (headers: ${componentName}Params) => {
|
|
286
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config});
|
|
287
|
+
return result.data;
|
|
288
|
+
}
|
|
289
|
+
`;
|
|
290
|
+
}
|
|
291
|
+
if (requestBodyComponent && !paramsInPath.length && !queryParam && !headerParam) {
|
|
292
|
+
output += `
|
|
293
|
+
type ${componentName}Params = ${requestBodyComponent}
|
|
294
|
+
|
|
295
|
+
const ${fetchName} = async (body: ${componentName}Params) => {
|
|
296
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, body)
|
|
297
|
+
return result.data
|
|
298
|
+
}
|
|
299
|
+
`;
|
|
300
|
+
}
|
|
301
|
+
if (requestBodyComponent && !paramsInPath.length && queryParam && !headerParam) {
|
|
302
|
+
const config = isUpdateRequest ? 'null,{headers, params: queryParams}' : '{headers, params: queryParams}';
|
|
303
|
+
const queryParamsProps = queryParams.map((item) => `["${item.name}"]: props["${item.name}"]`);
|
|
304
|
+
output += `
|
|
305
|
+
type ${componentName}Params = ${requestBodyComponent} & {
|
|
306
|
+
${queryParamsType}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
type ${componentName}QueryProps<T = ${responseTypes}> = ${componentName}Params & {
|
|
310
|
+
const body = {${generateBodyProps()}}
|
|
311
|
+
const params = {${generateProps(queryParams)}}
|
|
312
|
+
options?: UseQueryOptions<${responseTypes}, AxiosError, T, any>
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const ${fetchName} = async ({body, params}: ${componentName}Params) => {
|
|
316
|
+
const queryParams = {${queryParamsProps.join(',')}}
|
|
317
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, ${config})
|
|
318
|
+
return result.data
|
|
319
|
+
}
|
|
320
|
+
`;
|
|
321
|
+
}
|
|
322
|
+
if (requestBodyComponent && !paramsInPath.length && !queryParam && headerParam) {
|
|
323
|
+
output += `
|
|
324
|
+
type ${componentName}Params = ${requestBodyComponent} & {
|
|
325
|
+
${headerParam}
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
const ${fetchName} = async (props: ${componentName}Params) => {
|
|
329
|
+
const headers = {${generateProps(header)}}
|
|
330
|
+
const body = {${generateBodyProps()}}
|
|
331
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, body, {headers})
|
|
332
|
+
return result.data
|
|
333
|
+
}
|
|
334
|
+
`;
|
|
335
|
+
}
|
|
336
|
+
if (requestBodyComponent && !paramsInPath.length && queryParam && headerParam) {
|
|
337
|
+
output += `
|
|
338
|
+
type ${componentName}Params = ${requestBodyComponent} & {
|
|
339
|
+
${headerParam}
|
|
340
|
+
${paramsTypes}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const ${fetchName} = async (props: ${componentName}Params) => {
|
|
344
|
+
const body = {${generateBodyProps()}}
|
|
345
|
+
const headers = {${generateProps(header)}}
|
|
346
|
+
const params = {${generateProps(queryParams)}}
|
|
347
|
+
const result = await api.${verb}<${responseTypes}>(\`${route}\`, body, {headers, params})
|
|
348
|
+
return result.data
|
|
349
|
+
}
|
|
350
|
+
`;
|
|
351
|
+
}
|
|
352
|
+
if (requestBodyComponent && paramsInPath.length && !queryParam && !headerParam) {
|
|
353
|
+
output += `
|
|
354
|
+
type ${componentName}Params = ${requestBodyComponent} & {
|
|
355
|
+
${headerParam}
|
|
356
|
+
${paramsTypes}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
const ${fetchName} = async (props: ${componentName}Params) => {
|
|
360
|
+
const body = {${generateBodyProps()}}
|
|
361
|
+
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`, body)
|
|
362
|
+
return result.data
|
|
363
|
+
}
|
|
364
|
+
`;
|
|
365
|
+
}
|
|
366
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && !headerParam) {
|
|
367
|
+
output += `// TODO: NOT SUPPORTED requestBodyComponent && paramsInPath && queryParam)`;
|
|
368
|
+
}
|
|
369
|
+
if (requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
370
|
+
output += `// TODO: NOT SUPPORTED requestBodyComponent && paramsInPath && queryParam && headerParam)`;
|
|
371
|
+
}
|
|
372
|
+
if (!requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
373
|
+
const config = isUpdateRequest ? 'null,{headers}' : '{headers}';
|
|
374
|
+
output += `
|
|
375
|
+
type ${componentName}Params = {
|
|
376
|
+
${headerParam}
|
|
377
|
+
${paramsTypes}
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const ${fetchName} = async (props: ${componentName}Params) => {
|
|
381
|
+
const headers = {${generateProps(header)}}
|
|
382
|
+
const result = await api.${verb}<${responseTypes}>(\`${route.replace(/\{/g, '{props.')}\`, ${config})
|
|
383
|
+
return result.data
|
|
384
|
+
}
|
|
385
|
+
`;
|
|
386
|
+
}
|
|
387
|
+
if (!requestBodyComponent && paramsInPath.length && queryParam && headerParam) {
|
|
388
|
+
output += `// TODO: NOT SUPPORTED (paramsInPath && queryParam && headerParam)`;
|
|
389
|
+
}
|
|
390
|
+
if (requestBodyComponent && paramsInPath.length && !queryParam && headerParam) {
|
|
391
|
+
output += `// TODO: NOT SUPPORTED (requestBodyComponent && paramsInPath && headerParam)`;
|
|
392
|
+
}
|
|
393
|
+
return { implementation: output, imports, queryImports };
|
|
394
|
+
};
|
|
395
|
+
//# sourceMappingURL=generateHooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateHooks.js","sourceRoot":"","sources":["../src/generateHooks.ts"],"names":[],"mappings":"AAAA,OAA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,EAAE;QAC7E,MAAM,IAAI,oEAAoE,CAAC;KAChF;IAED,IAAI,oBAAoB,IAAI,YAAY,CAAC,MAAM,IAAI,CAAC,UAAU,IAAI,WAAW,EAAE;QAC7E,MAAM,IAAI,8EAA8E,CAAC;KAC1F;IAED,OAAO,EAAE,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,CAAC;AAC3D,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const generateImports: ({ schemaName, apiDirectory, queryClientDir, schemaImports, queryImports, }: {
|
|
2
|
+
apiDirectory: string;
|
|
3
|
+
queryClientDir: string;
|
|
4
|
+
schemaName: string;
|
|
5
|
+
schemaImports: string[];
|
|
6
|
+
queryImports: ('query' | 'mutation' | 'infiniteQuery')[];
|
|
7
|
+
}) => string;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export const generateImports = ({ schemaName, apiDirectory, queryClientDir, schemaImports, queryImports, }) => {
|
|
2
|
+
const importTypes = schemaImports.join(',');
|
|
3
|
+
let imports = [];
|
|
4
|
+
if (queryImports.includes('query')) {
|
|
5
|
+
imports = [...imports, 'useQuery', 'UseQueryOptions', 'QueryKey', 'SetDataOptions', 'QueryFilters'];
|
|
6
|
+
}
|
|
7
|
+
if (queryImports.includes('infiniteQuery')) {
|
|
8
|
+
imports = [...imports, 'useInfiniteQuery', 'UseInfiniteQueryOptions', 'QueryKey'];
|
|
9
|
+
}
|
|
10
|
+
if (queryImports.includes('mutation')) {
|
|
11
|
+
imports = [...imports, 'UseMutationOptions', 'useMutation'];
|
|
12
|
+
}
|
|
13
|
+
const importString = [...new Set(imports)].join(',');
|
|
14
|
+
return `
|
|
15
|
+
import {
|
|
16
|
+
${importString}
|
|
17
|
+
} from '@tanstack/react-query';
|
|
18
|
+
|
|
19
|
+
import { AxiosError } from 'axios';
|
|
20
|
+
import { api } from '${apiDirectory}';
|
|
21
|
+
import { queryClient } from '${queryClientDir}';
|
|
22
|
+
|
|
23
|
+
import {${importTypes}} from './${schemaName}'
|
|
24
|
+
|
|
25
|
+
type Updater<TInput, TOutput> = TOutput | ((input: TInput) => TOutput);
|
|
26
|
+
`;
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=generateImports.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateImports.js","sourceRoot":"","sources":["../src/generateImports.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,EAC9B,UAAU,EACV,YAAY,EACZ,cAAc,EACd,aAAa,EACb,YAAY,GAOb,EAAE,EAAE;IACH,MAAM,WAAW,GAAG,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC5C,IAAI,OAAO,GAAG,EAAc,CAAC;IAC7B,IAAI,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;QAClC,OAAO,GAAG,CAAC,GAAG,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,UAAU,EAAE,gBAAgB,EAAE,cAAc,CAAC,CAAC;KACrG;IACD,IAAI,YAAY,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE;QAC1C,OAAO,GAAG,CAAC,GAAG,OAAO,EAAE,kBAAkB,EAAE,yBAAyB,EAAE,UAAU,CAAC,CAAC;KACnF;IACD,IAAI,YAAY,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE;QACrC,OAAO,GAAG,CAAC,GAAG,OAAO,EAAE,oBAAoB,EAAE,aAAa,CAAC,CAAC;KAC7D;IAED,MAAM,YAAY,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAErD,OAAO;;MAEH,YAAY;;;;yBAIO,YAAY;iCACJ,cAAc;;YAEnC,WAAW,aAAa,UAAU;;;GAG3C,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import pasCase from 'case';
|
|
2
|
+
import lodash from 'lodash';
|
|
3
|
+
const { isEmpty } = lodash;
|
|
4
|
+
const { pascal } = pasCase;
|
|
5
|
+
import { formatDescription, getScalar, isReference, resolveValue } from './utils.js';
|
|
6
|
+
import { getDocs, getResReqTypes } from './utils.js';
|
|
7
|
+
/**
|
|
8
|
+
* Generate the interface string
|
|
9
|
+
*/
|
|
10
|
+
const generateInterface = (name, schema) => {
|
|
11
|
+
const scalar = getScalar(schema);
|
|
12
|
+
return `${formatDescription(schema.description)}
|
|
13
|
+
export type ${pascal(name)} = ${scalar}
|
|
14
|
+
`;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Extract all types from #/components/schemas
|
|
18
|
+
*/
|
|
19
|
+
const generateSchemasDefinition = (schemas = {}) => {
|
|
20
|
+
if (isEmpty(schemas)) {
|
|
21
|
+
return '';
|
|
22
|
+
}
|
|
23
|
+
return ('\n // SCEHMAS \n' +
|
|
24
|
+
Object.entries(schemas)
|
|
25
|
+
.map(([name, schema]) => !isReference(schema) &&
|
|
26
|
+
(!schema.type || schema.type === 'object') &&
|
|
27
|
+
!schema.allOf &&
|
|
28
|
+
!schema.oneOf &&
|
|
29
|
+
!isReference(schema) &&
|
|
30
|
+
!schema.nullable
|
|
31
|
+
? generateInterface(name, schema)
|
|
32
|
+
: `${formatDescription(isReference(schema) ? undefined : schema.description)} export type ${pascal(name)} = ${resolveValue(schema)};`)
|
|
33
|
+
.join('\n\n') +
|
|
34
|
+
'\n');
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Extract all types from #/components/requestBodies
|
|
38
|
+
*/
|
|
39
|
+
const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
40
|
+
if (isEmpty(requestBodies)) {
|
|
41
|
+
return '';
|
|
42
|
+
}
|
|
43
|
+
return ('\n // REQUEST BODIES \n' +
|
|
44
|
+
Object.entries(requestBodies)
|
|
45
|
+
.map(([name, requestBody]) => {
|
|
46
|
+
const doc = getDocs(requestBody);
|
|
47
|
+
const type = getResReqTypes([['', requestBody]]);
|
|
48
|
+
const isEmptyInterface = type === '{}';
|
|
49
|
+
if (isEmptyInterface) {
|
|
50
|
+
return `${doc}\nexport type ${pascal(name)}RequestBody = ${type}`;
|
|
51
|
+
}
|
|
52
|
+
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
53
|
+
return `${doc}\nexport type ${pascal(name)}RequestBody = ${type}`;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return `${doc}\nexport type ${pascal(name)}RequestBody = ${type};`;
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
.join('\n\n') +
|
|
60
|
+
'\n');
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Extract all types from #/components/responses
|
|
64
|
+
*/
|
|
65
|
+
const generateResponsesDefinition = (responses = {}) => {
|
|
66
|
+
if (isEmpty(responses)) {
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
return ('\n // RESPONSES \n' +
|
|
70
|
+
Object.entries(responses)
|
|
71
|
+
.map(([name, response]) => {
|
|
72
|
+
const doc = getDocs(response);
|
|
73
|
+
const type = getResReqTypes([['', response]]);
|
|
74
|
+
const isEmptyInterface = type === '{}';
|
|
75
|
+
if (isEmptyInterface) {
|
|
76
|
+
return `export type RQ${pascal(name)}Response = ${type}`;
|
|
77
|
+
}
|
|
78
|
+
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
79
|
+
return `${doc}export type RQ${pascal(name)}Response = ${type}`;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return `${doc}export type RQ${pascal(name)}Response = ${type};`;
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
.join('\n\n') +
|
|
86
|
+
'\n');
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Extract all types from #/components/schemas
|
|
90
|
+
*/
|
|
91
|
+
export const generateSchemas = ({ spec }) => {
|
|
92
|
+
const schemaOutput = generateRequestBodiesDefinition(spec.components?.requestBodies) +
|
|
93
|
+
generateResponsesDefinition(spec.components?.responses) +
|
|
94
|
+
generateSchemasDefinition(spec.components?.schemas);
|
|
95
|
+
return schemaOutput;
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=generateSchemas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generateSchemas.js","sourceRoot":"","sources":["../src/generateSchemas.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,MAAM,CAAC;AAC3B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;AAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;AAE3B,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAErF,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAErD;;GAEG;AACH,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAE,MAAoB,EAAE,EAAE;IAC/D,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IACjC,OAAO,GAAG,iBAAiB,CAAC,MAAM,CAAC,WAAW,CAAC;qBAC5B,MAAM,CAAC,IAAI,CAAC,MAAM,MAAM;MACvC,CAAC;AACP,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,yBAAyB,GAAG,CAAC,UAAuC,EAAE,EAAE,EAAE;IAC9E,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;QACpB,OAAO,EAAE,CAAC;KACX;IAED,OAAO,CACL,kBAAkB;QAClB,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC;aACpB,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,EAAE,CACtB,CAAC,WAAW,CAAC,MAAM,CAAC;YACpB,CAAC,CAAC,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,K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L,CAAC;AACJ,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,EAAE,IAAI,EAA2B,EAAE,EAAE;IACnE,MAAM,YAAY,GAChB,+BAA+B,CAAC,IAAI,CAAC,UAAU,EAAE,aAAa,CAAC;QAC/D,2BAA2B,CAAC,IAAI,CAAC,UAAU,EAAE,SAAS,CAAC;QACvD,yBAAyB,CAAC,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IAEtD,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, queryClientDir, headerFilters, overrides, }: {
|
|
2
|
+
sourceDirectory: string;
|
|
3
|
+
exportDirectory: string;
|
|
4
|
+
apiDirectory: string;
|
|
5
|
+
queryClientDir: string;
|
|
6
|
+
headerFilters?: string[];
|
|
7
|
+
overrides?: Record<string, {
|
|
8
|
+
type: 'query';
|
|
9
|
+
} | {
|
|
10
|
+
type: 'mutation';
|
|
11
|
+
} | {
|
|
12
|
+
type: 'infiniteQuery';
|
|
13
|
+
infiniteQueryParm: string;
|
|
14
|
+
}>;
|
|
15
|
+
}): void;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { readFileSync, writeFileSync, readdir, mkdirSync } from 'fs';
|
|
3
|
+
import { join, parse } from 'path';
|
|
4
|
+
import { convertSwaggerFile } from './convertSwaggerFile.js';
|
|
5
|
+
import { createHook } from './generateHooks.js';
|
|
6
|
+
import { generateImports } from './generateImports.js';
|
|
7
|
+
import { generateSchemas } from './generateSchemas.js';
|
|
8
|
+
export function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, queryClientDir, headerFilters, overrides, }) {
|
|
9
|
+
readdir(sourceDirectory, function (err, filenames) {
|
|
10
|
+
if (err) {
|
|
11
|
+
console.log(err);
|
|
12
|
+
throw err;
|
|
13
|
+
}
|
|
14
|
+
filenames.map(async (filename) => {
|
|
15
|
+
try {
|
|
16
|
+
const data = readFileSync(join(process.cwd(), sourceDirectory + '/' + filename), 'utf-8');
|
|
17
|
+
const { ext } = parse(sourceDirectory + '/' + filename);
|
|
18
|
+
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
19
|
+
let spec = await convertSwaggerFile(data, format);
|
|
20
|
+
const schemaName = `useQueries${filename.split('.')[0]}.schema`;
|
|
21
|
+
const hooksName = `useQueries${filename.split('.')[0]}`;
|
|
22
|
+
const name = filename.split('.')[0];
|
|
23
|
+
mkdirSync(join(process.cwd(), `${exportDirectory}/${name}`), { recursive: true });
|
|
24
|
+
const operationIds = [];
|
|
25
|
+
let hooks = '';
|
|
26
|
+
let schemaImportsArray = [];
|
|
27
|
+
let collectedQueryImports = [];
|
|
28
|
+
Object.entries(spec.paths).forEach(([route, verbs]) => {
|
|
29
|
+
Object.entries(verbs).forEach(([verb, operation]) => {
|
|
30
|
+
if (['get', 'post', 'patch', 'put', 'delete'].includes(verb) && !operation.deprecated) {
|
|
31
|
+
const { implementation, imports, queryImports } = createHook({
|
|
32
|
+
operation,
|
|
33
|
+
verb,
|
|
34
|
+
route: (spec.basePath || '') + route,
|
|
35
|
+
operationIds,
|
|
36
|
+
parameters: verbs.parameters,
|
|
37
|
+
schemasComponents: spec.components,
|
|
38
|
+
headerFilters,
|
|
39
|
+
overrides,
|
|
40
|
+
});
|
|
41
|
+
hooks += implementation;
|
|
42
|
+
imports.forEach((element) => {
|
|
43
|
+
const formattedImport = element.replace('[]', '');
|
|
44
|
+
if (!schemaImportsArray.includes(formattedImport) &&
|
|
45
|
+
element !== 'void' &&
|
|
46
|
+
element !== 'string' &&
|
|
47
|
+
!element.includes('{')) {
|
|
48
|
+
schemaImportsArray.push(formattedImport);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
queryImports.forEach((element) => {
|
|
52
|
+
if (!schemaImportsArray.includes(element)) {
|
|
53
|
+
collectedQueryImports.push(element);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
const imports = generateImports({
|
|
60
|
+
apiDirectory,
|
|
61
|
+
queryClientDir,
|
|
62
|
+
schemaName,
|
|
63
|
+
schemaImports: schemaImportsArray,
|
|
64
|
+
queryImports: collectedQueryImports,
|
|
65
|
+
});
|
|
66
|
+
writeFileSync(join(process.cwd(), `${exportDirectory}/${name}/${hooksName}.tsx`), imports + hooks);
|
|
67
|
+
const schemas = generateSchemas({ spec });
|
|
68
|
+
writeFileSync(join(process.cwd(), `${exportDirectory}/${name}/${schemaName}.tsx`), schemas);
|
|
69
|
+
console.log(chalk.green(`🎉 [${filename}] Your OpenAPI spec has been converted into react query hooks`));
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (error.code === 'EISDIR') {
|
|
73
|
+
console.log(chalk.red('nested folder structure not supported'));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
console.log(chalk.red(`[${filename}]:`), chalk.red(error));
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=importSpecs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"importSpecs.js","sourceRoot":"","sources":["../src/importSpecs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAErE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AACnC,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,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,CAAC;aAC5D;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject } from 'openapi3-ts';
|
|
2
|
+
export declare const getDocs: (data: ReferenceObject | {
|
|
3
|
+
description?: string;
|
|
4
|
+
}) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Discriminator helper for `ReferenceObject`
|
|
7
|
+
*/
|
|
8
|
+
export declare const isReference: (property: any) => property is ReferenceObject;
|
|
9
|
+
/**
|
|
10
|
+
* Return the typescript equivalent of open-api data type
|
|
11
|
+
*/
|
|
12
|
+
export declare const getScalar: (item: SchemaObject) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the value of a schema object to a proper type definition.
|
|
15
|
+
*/
|
|
16
|
+
export declare const resolveValue: (schema: SchemaObject) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Format a description to code documentation.
|
|
19
|
+
*/
|
|
20
|
+
export declare const formatDescription: (description?: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Extract responses / request types from open-api specs
|
|
23
|
+
*/
|
|
24
|
+
export declare const getResReqTypes: (responsesOrRequests: Array<[string, ResponseObject | ReferenceObject | RequestBodyObject]>) => string;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import lodash from 'lodash';
|
|
2
|
+
const { uniq, isEmpty } = lodash;
|
|
3
|
+
import pasCase from 'case';
|
|
4
|
+
const { pascal } = pasCase;
|
|
5
|
+
export const getDocs = (data) => isReference(data) ? '' : formatDescription(data.description);
|
|
6
|
+
/**
|
|
7
|
+
* Discriminator helper for `ReferenceObject`
|
|
8
|
+
*/
|
|
9
|
+
export const isReference = (property) => {
|
|
10
|
+
return Boolean(property.$ref);
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Return the typescript equivalent of open-api data type
|
|
14
|
+
*/
|
|
15
|
+
export const getScalar = (item) => {
|
|
16
|
+
const nullable = item.nullable ? ' | null' : '';
|
|
17
|
+
switch (item.type) {
|
|
18
|
+
case 'number':
|
|
19
|
+
case 'integer':
|
|
20
|
+
return 'number' + nullable;
|
|
21
|
+
case 'boolean':
|
|
22
|
+
return 'boolean' + nullable;
|
|
23
|
+
case 'array':
|
|
24
|
+
return getArray(item) + nullable;
|
|
25
|
+
case 'string':
|
|
26
|
+
return (item.enum ? `"${item.enum.join(`" | "`)}"` : 'string') + nullable;
|
|
27
|
+
case 'object':
|
|
28
|
+
default:
|
|
29
|
+
return getObject(item) + nullable;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Return the output type from the $ref
|
|
34
|
+
*/
|
|
35
|
+
const getRef = ($ref) => {
|
|
36
|
+
if ($ref.startsWith('#/components/schemas')) {
|
|
37
|
+
return pascal($ref.replace('#/components/schemas/', ''));
|
|
38
|
+
}
|
|
39
|
+
else if ($ref.startsWith('#/components/responses')) {
|
|
40
|
+
return pascal($ref.replace('#/components/responses/', '')) + 'Response';
|
|
41
|
+
}
|
|
42
|
+
else if ($ref.startsWith('#/components/parameters')) {
|
|
43
|
+
return pascal($ref.replace('#/components/parameters/', '')) + 'Parameter';
|
|
44
|
+
}
|
|
45
|
+
else if ($ref.startsWith('#/components/requestBodies')) {
|
|
46
|
+
return pascal($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Return the output type from an array
|
|
54
|
+
*/
|
|
55
|
+
const getArray = (item) => {
|
|
56
|
+
if (item.items) {
|
|
57
|
+
if (!isReference(item.items) && (item.items.oneOf || item.items.allOf || item.items.enum)) {
|
|
58
|
+
return `(${resolveValue(item.items)})[]`;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
return `${resolveValue(item.items)}[]`;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
throw new Error('All arrays must have an `items` key define');
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Return the output type from an object
|
|
70
|
+
*/
|
|
71
|
+
const getObject = (item) => {
|
|
72
|
+
if (isReference(item)) {
|
|
73
|
+
return getRef(item.$ref);
|
|
74
|
+
}
|
|
75
|
+
if (item.allOf) {
|
|
76
|
+
return item.allOf.map(resolveValue).join(' & ');
|
|
77
|
+
}
|
|
78
|
+
if (item.oneOf) {
|
|
79
|
+
return item.oneOf.map(resolveValue).join(' | ');
|
|
80
|
+
}
|
|
81
|
+
if (!item.type && !item.properties && !item.additionalProperties) {
|
|
82
|
+
return '{}';
|
|
83
|
+
}
|
|
84
|
+
// Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
|
|
85
|
+
if (item.type === 'object' &&
|
|
86
|
+
!item.properties &&
|
|
87
|
+
(!item.additionalProperties || item.additionalProperties === true || isEmpty(item.additionalProperties))) {
|
|
88
|
+
return '{[key: string]: any}';
|
|
89
|
+
}
|
|
90
|
+
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
91
|
+
// Consolidation of item.properties & item.additionalProperties
|
|
92
|
+
let output = '{\n';
|
|
93
|
+
if (item.properties) {
|
|
94
|
+
output += Object.entries(item.properties)
|
|
95
|
+
.map(([key, prop]) => {
|
|
96
|
+
const doc = getDocs(prop);
|
|
97
|
+
const isRequired = (item.required || []).includes(key);
|
|
98
|
+
const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
|
|
99
|
+
return `${doc}\n${processedKey}${isRequired ? '' : '?'}: ${resolveValue(prop)};`;
|
|
100
|
+
})
|
|
101
|
+
.join('');
|
|
102
|
+
}
|
|
103
|
+
if (item.additionalProperties) {
|
|
104
|
+
if (item.properties) {
|
|
105
|
+
output += '\n';
|
|
106
|
+
}
|
|
107
|
+
output += `} & { [key: string]: ${item.additionalProperties === true ? 'any' : resolveValue(item.additionalProperties)}`;
|
|
108
|
+
}
|
|
109
|
+
if (item.properties || item.additionalProperties) {
|
|
110
|
+
if (output === '{\n') {
|
|
111
|
+
return '{}';
|
|
112
|
+
}
|
|
113
|
+
return output + '\n}';
|
|
114
|
+
}
|
|
115
|
+
return item.type === 'object' ? '{[key: string]: any}' : 'any';
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Resolve the value of a schema object to a proper type definition.
|
|
119
|
+
*/
|
|
120
|
+
export const resolveValue = (schema) => isReference(schema) ? getRef(schema.$ref) : getScalar(schema);
|
|
121
|
+
/**
|
|
122
|
+
* Format a description to code documentation.
|
|
123
|
+
*/
|
|
124
|
+
export const formatDescription = (description) => {
|
|
125
|
+
if (!description) {
|
|
126
|
+
return '';
|
|
127
|
+
}
|
|
128
|
+
return `\n/**\n${description
|
|
129
|
+
.split('\n')
|
|
130
|
+
.map((i) => `* ${i}`)
|
|
131
|
+
.join('\n')}\n */`;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Extract responses / request types from open-api specs
|
|
135
|
+
*/
|
|
136
|
+
export const getResReqTypes = (responsesOrRequests) => {
|
|
137
|
+
return uniq(responsesOrRequests.map(([_, res]) => {
|
|
138
|
+
if (!res) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (isReference(res)) {
|
|
142
|
+
return getRef(res.$ref);
|
|
143
|
+
}
|
|
144
|
+
if (res.content) {
|
|
145
|
+
for (let contentType of Object.keys(res.content)) {
|
|
146
|
+
if (contentType.startsWith('application/json') ||
|
|
147
|
+
contentType.startsWith('application/octet-stream')) {
|
|
148
|
+
const schema = res.content[contentType].schema;
|
|
149
|
+
return resolveValue(schema);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
})).join(' | ');
|
|
156
|
+
};
|
|
157
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;AACjC,OAAO,OAAO,MAAM,MAAM,CAAC;AAE3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;AAI3B,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,IAAgD,EAAE,EAAE,CAC1E,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,iBAAiB,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;AAC/D;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,QAAa,EAA+B,EAAE;IACxE,OAAO,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AAChC,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,IAAkB,EAAE,EAAE;IAC9C,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;IAEhD,QAAQ,IAAI,CAAC,IAAI,EAAE;QACjB,KAAK,QAAQ,CAAC;QACd,KAAK,SAAS;YACZ,OAAO,QAAQ,GAAG,QAAQ,CAAC;QAE7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AC,OAAO,CAAC,WAAW,CAAC,CAAC,MAAO,CAAC;oBAEhD,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC;iBAC7B;aACF;YACD,OAAO;SACR;QAED,OAAO;IACT,CAAC,CAAC,CACH,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAChB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-query-lightbase-codegen",
|
|
3
3
|
"description": "Fully typed react query code generation tool based on openApi specifications",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": "./dist/index.js",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
|
39
39
|
"tsc": "tsc -p tsconfig.json",
|
|
40
40
|
"test": "yarn tsc && node test/script.mjs && prettier --check ./test/generated/**/*.tsx --write && eslint ./test/generated/**/*.tsx --fix",
|
|
41
|
+
"deploy": "tsc && semantic-release --no-ci",
|
|
41
42
|
"prepare": "husky install"
|
|
42
43
|
},
|
|
43
44
|
"dependencies": {
|