react-query-lightbase-codegen 0.0.10 → 0.0.11
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.
|
@@ -4,18 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.importOpenApi = exports.generateRestfulComponent = exports.formatDescription = exports.generateResponsesDefinition = exports.generateRequestBodiesDefinition = exports.generateSchemasDefinition = exports.resolveDiscriminator = exports.generateInterface = exports.getParamsInPath = exports.getResReqTypes = exports.resolveValue = exports.getObject = exports.getArray = exports.getRef = exports.getScalar = exports.isReference = void 0;
|
|
7
|
-
const case_1 =
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
7
|
+
const case_1 = require("case");
|
|
8
|
+
const get_1 = __importDefault(require("lodash/get"));
|
|
9
|
+
const groupBy_1 = __importDefault(require("lodash/groupBy"));
|
|
10
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
|
11
|
+
const set_1 = __importDefault(require("lodash/set"));
|
|
12
|
+
const uniq_1 = __importDefault(require("lodash/uniq"));
|
|
11
13
|
const swagger2openapi_1 = __importDefault(require("swagger2openapi"));
|
|
12
|
-
const
|
|
14
|
+
const yaml = require('js-yaml');
|
|
13
15
|
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
14
16
|
/**
|
|
15
17
|
* Import and parse the openapi spec from a yaml/json
|
|
16
18
|
*/
|
|
17
19
|
const importSpecs = (data, extension) => {
|
|
18
|
-
const schema = extension === 'yaml' ?
|
|
20
|
+
const schema = extension === 'yaml' ? yaml.load(data) : JSON.parse(data);
|
|
19
21
|
return new Promise((resolve, reject) => {
|
|
20
22
|
if (!schema.openapi || !schema.openapi.startsWith('3.')) {
|
|
21
23
|
swagger2openapi_1.default.convertObj(schema, {}, (err, convertedObj) => {
|
|
@@ -65,16 +67,16 @@ exports.getScalar = getScalar;
|
|
|
65
67
|
*/
|
|
66
68
|
const getRef = ($ref) => {
|
|
67
69
|
if ($ref.startsWith('#/components/schemas')) {
|
|
68
|
-
return pascal($ref.replace('#/components/schemas/', ''));
|
|
70
|
+
return (0, case_1.pascal)($ref.replace('#/components/schemas/', ''));
|
|
69
71
|
}
|
|
70
72
|
else if ($ref.startsWith('#/components/responses')) {
|
|
71
|
-
return pascal($ref.replace('#/components/responses/', '')) + 'Response';
|
|
73
|
+
return (0, case_1.pascal)($ref.replace('#/components/responses/', '')) + 'Response';
|
|
72
74
|
}
|
|
73
75
|
else if ($ref.startsWith('#/components/parameters')) {
|
|
74
|
-
return pascal($ref.replace('#/components/parameters/', '')) + 'Parameter';
|
|
76
|
+
return (0, case_1.pascal)($ref.replace('#/components/parameters/', '')) + 'Parameter';
|
|
75
77
|
}
|
|
76
78
|
else if ($ref.startsWith('#/components/requestBodies')) {
|
|
77
|
-
return pascal($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
|
|
79
|
+
return (0, case_1.pascal)($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
|
|
78
80
|
}
|
|
79
81
|
else {
|
|
80
82
|
throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
|
|
@@ -117,7 +119,7 @@ const getObject = (item) => {
|
|
|
117
119
|
// Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
|
|
118
120
|
if (item.type === 'object' &&
|
|
119
121
|
!item.properties &&
|
|
120
|
-
(!item.additionalProperties || item.additionalProperties === true ||
|
|
122
|
+
(!item.additionalProperties || item.additionalProperties === true || (0, isEmpty_1.default)(item.additionalProperties))) {
|
|
121
123
|
return '{[key: string]: any}';
|
|
122
124
|
}
|
|
123
125
|
// Consolidation of item.properties & item.additionalProperties
|
|
@@ -155,7 +157,7 @@ exports.resolveValue = resolveValue;
|
|
|
155
157
|
/**
|
|
156
158
|
* Extract responses / request types from open-api specs
|
|
157
159
|
*/
|
|
158
|
-
const getResReqTypes = (responsesOrRequests) =>
|
|
160
|
+
const getResReqTypes = (responsesOrRequests) => (0, uniq_1.default)(responsesOrRequests.map(([_, res]) => {
|
|
159
161
|
if (!res) {
|
|
160
162
|
return 'void';
|
|
161
163
|
}
|
|
@@ -199,7 +201,7 @@ exports.getParamsInPath = getParamsInPath;
|
|
|
199
201
|
*/
|
|
200
202
|
const generateInterface = (name, schema) => {
|
|
201
203
|
const scalar = (0, exports.getScalar)(schema);
|
|
202
|
-
return `${(0, exports.formatDescription)(schema.description)}export interface ${pascal(name)} ${scalar}`;
|
|
204
|
+
return `${(0, exports.formatDescription)(schema.description)}export interface ${(0, case_1.pascal)(name)} ${scalar}`;
|
|
203
205
|
};
|
|
204
206
|
exports.generateInterface = generateInterface;
|
|
205
207
|
/**
|
|
@@ -218,7 +220,7 @@ const resolveDiscriminator = (specs) => {
|
|
|
218
220
|
if (!ref.startsWith('#/components/schemas/')) {
|
|
219
221
|
throw new Error('Discriminator mapping outside of `#/components/schemas` is not supported');
|
|
220
222
|
}
|
|
221
|
-
|
|
223
|
+
(0, set_1.default)(specs, `components.schemas.${ref.slice('#/components/schemas/'.length)}.properties.${propertyName}.enum`, [name]);
|
|
222
224
|
});
|
|
223
225
|
});
|
|
224
226
|
}
|
|
@@ -228,7 +230,7 @@ exports.resolveDiscriminator = resolveDiscriminator;
|
|
|
228
230
|
* Extract all types from #/components/schemas
|
|
229
231
|
*/
|
|
230
232
|
const generateSchemasDefinition = (schemas = {}) => {
|
|
231
|
-
if (
|
|
233
|
+
if ((0, isEmpty_1.default)(schemas)) {
|
|
232
234
|
return '';
|
|
233
235
|
}
|
|
234
236
|
return (Object.entries(schemas)
|
|
@@ -239,7 +241,7 @@ const generateSchemasDefinition = (schemas = {}) => {
|
|
|
239
241
|
!(0, exports.isReference)(schema) &&
|
|
240
242
|
!schema.nullable
|
|
241
243
|
? (0, exports.generateInterface)(name, schema)
|
|
242
|
-
: `${(0, exports.formatDescription)((0, exports.isReference)(schema) ? undefined : schema.description)}export type ${pascal(name)} = ${(0, exports.resolveValue)(schema)};`)
|
|
244
|
+
: `${(0, exports.formatDescription)((0, exports.isReference)(schema) ? undefined : schema.description)}export type ${(0, case_1.pascal)(name)} = ${(0, exports.resolveValue)(schema)};`)
|
|
243
245
|
.join('\n\n') + '\n');
|
|
244
246
|
};
|
|
245
247
|
exports.generateSchemasDefinition = generateSchemasDefinition;
|
|
@@ -247,7 +249,7 @@ exports.generateSchemasDefinition = generateSchemasDefinition;
|
|
|
247
249
|
* Extract all types from #/components/requestBodies
|
|
248
250
|
*/
|
|
249
251
|
const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
250
|
-
if (
|
|
252
|
+
if ((0, isEmpty_1.default)(requestBodies)) {
|
|
251
253
|
return '';
|
|
252
254
|
}
|
|
253
255
|
return ('\n' +
|
|
@@ -257,13 +259,13 @@ const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
|
257
259
|
const type = (0, exports.getResReqTypes)([['', requestBody]]);
|
|
258
260
|
const isEmptyInterface = type === '{}';
|
|
259
261
|
if (isEmptyInterface) {
|
|
260
|
-
return `export interface ${pascal(name)}RequestBody ${type}`;
|
|
262
|
+
return `export interface ${(0, case_1.pascal)(name)}RequestBody ${type}`;
|
|
261
263
|
}
|
|
262
264
|
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
263
|
-
return `${doc}export interface ${pascal(name)}RequestBody ${type}`;
|
|
265
|
+
return `${doc}export interface ${(0, case_1.pascal)(name)}RequestBody ${type}`;
|
|
264
266
|
}
|
|
265
267
|
else {
|
|
266
|
-
return `${doc}export type ${pascal(name)}RequestBody = ${type};`;
|
|
268
|
+
return `${doc}export type ${(0, case_1.pascal)(name)}RequestBody = ${type};`;
|
|
267
269
|
}
|
|
268
270
|
})
|
|
269
271
|
.join('\n\n') +
|
|
@@ -274,7 +276,7 @@ exports.generateRequestBodiesDefinition = generateRequestBodiesDefinition;
|
|
|
274
276
|
* Extract all types from #/components/responses
|
|
275
277
|
*/
|
|
276
278
|
const generateResponsesDefinition = (responses = {}) => {
|
|
277
|
-
if (
|
|
279
|
+
if ((0, isEmpty_1.default)(responses)) {
|
|
278
280
|
return '';
|
|
279
281
|
}
|
|
280
282
|
return ('\n' +
|
|
@@ -284,13 +286,13 @@ const generateResponsesDefinition = (responses = {}) => {
|
|
|
284
286
|
const type = (0, exports.getResReqTypes)([['', response]]);
|
|
285
287
|
const isEmptyInterface = type === '{}';
|
|
286
288
|
if (isEmptyInterface) {
|
|
287
|
-
return `export interface RQ${pascal(name)}Response ${type}`;
|
|
289
|
+
return `export interface RQ${(0, case_1.pascal)(name)}Response ${type}`;
|
|
288
290
|
}
|
|
289
291
|
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
290
|
-
return `${doc}export interface RQ${pascal(name)}Response ${type}`;
|
|
292
|
+
return `${doc}export interface RQ${(0, case_1.pascal)(name)}Response ${type}`;
|
|
291
293
|
}
|
|
292
294
|
else {
|
|
293
|
-
return `${doc}export type RQ${pascal(name)}Response = ${type};`;
|
|
295
|
+
return `${doc}export type RQ${(0, case_1.pascal)(name)}Response = ${type};`;
|
|
294
296
|
}
|
|
295
297
|
})
|
|
296
298
|
.join('\n\n') +
|
|
@@ -322,15 +324,15 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
322
324
|
route = route.replace(/\{/g, '${'); // `/pet/{id}` => `/pet/${id}`
|
|
323
325
|
// Remove the last param of the route if we are in the DELETE case
|
|
324
326
|
let lastParamInTheRoute = null;
|
|
325
|
-
const componentName = pascal(operationId);
|
|
327
|
+
const componentName = (0, case_1.pascal)(operationId);
|
|
326
328
|
const isOk = ([statusCode]) => statusCode.toString().startsWith('2');
|
|
327
329
|
const responseTypes = (0, exports.getResReqTypes)(Object.entries(operation.responses).filter(isOk)) || 'void';
|
|
328
330
|
const requestBodyTypes = (0, exports.getResReqTypes)([['body', operation.requestBody]]);
|
|
329
331
|
const needAResponseComponent = true;
|
|
330
332
|
const paramsInPath = (0, exports.getParamsInPath)(route).filter((param) => !(verb === 'delete' && param === lastParamInTheRoute));
|
|
331
|
-
const { query: queryParams = [], path: pathParams = [], header: headerParams = [] } =
|
|
333
|
+
const { query: queryParams = [], path: pathParams = [], header: headerParams = [] } = (0, groupBy_1.default)([...parameters, ...(operation.parameters || [])].map((p) => {
|
|
332
334
|
if ((0, exports.isReference)(p)) {
|
|
333
|
-
return
|
|
335
|
+
return (0, get_1.default)(schemasComponents, p.$ref.replace('#/components/', '').replace('/', '.'));
|
|
334
336
|
}
|
|
335
337
|
else {
|
|
336
338
|
return p;
|
|
@@ -7,7 +7,7 @@ exports.importSpecs = void 0;
|
|
|
7
7
|
const chalk_1 = __importDefault(require("chalk"));
|
|
8
8
|
const fs_1 = require("fs");
|
|
9
9
|
const path_1 = require("path");
|
|
10
|
-
const
|
|
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
13
|
function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, }) {
|
|
@@ -21,7 +21,7 @@ function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, }) {
|
|
|
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,
|
|
24
|
+
const fileExports = await (0, import_open_api_1.importOpenApi)({ data, format, apiDir: apiDirectory });
|
|
25
25
|
(0, fs_1.writeFileSync)((0, path_1.join)(process.cwd(), `${exportDirectory}/${name}`), fileExports);
|
|
26
26
|
log(createSuccessMessage(filename));
|
|
27
27
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { pascal } from 'case';
|
|
2
|
+
import get from 'lodash/get';
|
|
3
|
+
import groupBy from 'lodash/groupBy';
|
|
4
|
+
import isEmpty from 'lodash/isEmpty';
|
|
5
|
+
import set from 'lodash/set';
|
|
6
|
+
import uniq from 'lodash/uniq';
|
|
5
7
|
import swagger2openapi from 'swagger2openapi';
|
|
6
|
-
|
|
8
|
+
const yaml = require('js-yaml');
|
|
7
9
|
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
8
10
|
/**
|
|
9
11
|
* Import and parse the openapi spec from a yaml/json
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import { readFileSync, writeFileSync, readdir } from 'fs';
|
|
3
3
|
import { join, parse } from 'path';
|
|
4
|
-
import { importOpenApi } from './import-open-api
|
|
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
7
|
export function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, }) {
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-query-lightbase-codegen",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./lib/cjs/index.js",
|
|
6
6
|
"module": "./lib/esm/index.js",
|
|
7
7
|
"files": [
|
|
8
8
|
"lib/"
|
|
9
9
|
],
|
|
10
|
-
"type": "module",
|
|
11
10
|
"scripts": {
|
|
12
11
|
"tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
|
|
13
12
|
"test": "yarn tsc && node test/script.mjs"
|
|
@@ -15,7 +14,7 @@
|
|
|
15
14
|
"dependencies": {
|
|
16
15
|
"axios": "0.27.2",
|
|
17
16
|
"case": "1.6.3",
|
|
18
|
-
"chalk": "
|
|
17
|
+
"chalk": "4.1.0",
|
|
19
18
|
"js-yaml": "4.1.0",
|
|
20
19
|
"lodash": "4.17.21",
|
|
21
20
|
"openapi3-ts": "2.0.2",
|
|
@@ -27,19 +26,15 @@
|
|
|
27
26
|
},
|
|
28
27
|
"devDependencies": {
|
|
29
28
|
"@babel/eslint-parser": "7.17.0",
|
|
30
|
-
"@react-native-community/eslint-config": "3.0.
|
|
31
|
-
"@types/js-yaml": "^4.0.5",
|
|
29
|
+
"@react-native-community/eslint-config": "3.0.2",
|
|
32
30
|
"@types/lodash": "4.14.182",
|
|
33
31
|
"@types/node": "17.0.32",
|
|
34
32
|
"@types/qs": "6.9.7",
|
|
33
|
+
"@types/query-string": "6.3.0",
|
|
35
34
|
"@types/yamljs": "0.2.31",
|
|
35
|
+
"eslint": "8.15.0",
|
|
36
|
+
"eslint-plugin-prettier": "4.0.0",
|
|
36
37
|
"prettier": "2.6.2",
|
|
37
38
|
"typescript": "4.6.4"
|
|
38
|
-
},
|
|
39
|
-
"resolutions": {
|
|
40
|
-
"@typescript-eslint/eslint-plugin": "5.15.0",
|
|
41
|
-
"@typescript-eslint/parser": "5.15.0",
|
|
42
|
-
"eslint-plugin-flowtype": "8.0.0",
|
|
43
|
-
"@types/react": "17.0.44"
|
|
44
39
|
}
|
|
45
40
|
}
|