react-query-lightbase-codegen 0.0.3 → 0.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/lib/cjs/{scripts/import-open-api.js → import-open-api.js} +11 -10
- package/lib/cjs/index.js +3 -3
- package/lib/cjs/react-query-codegen-import.js +25 -34
- package/lib/esm/{scripts/import-open-api.js → import-open-api.js} +10 -10
- package/lib/esm/index.js +2 -2
- package/lib/esm/react-query-codegen-import.js +24 -35
- package/package.json +2 -6
- package/lib/cjs/react-query-codegen.js +0 -17
- package/lib/esm/react-query-codegen.js +0 -10
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
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;
|
|
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
7
|
const case_1 = require("case");
|
|
8
8
|
const get_1 = __importDefault(require("lodash/get"));
|
|
9
9
|
const groupBy_1 = __importDefault(require("lodash/groupBy"));
|
|
@@ -313,17 +313,18 @@ exports.formatDescription = formatDescription;
|
|
|
313
313
|
* Generate a react-query component from openapi operation specs
|
|
314
314
|
*/
|
|
315
315
|
const generateRestfulComponent = (operation, verb, route, operationIds, parameters = [], schemasComponents) => {
|
|
316
|
-
|
|
317
|
-
|
|
316
|
+
const { operationId = route.replace('/', '') } = operation;
|
|
317
|
+
if (operationId === '*') {
|
|
318
|
+
throw new Error(`Invalid operationId/Route set for ${verb} ${route}`);
|
|
318
319
|
}
|
|
319
|
-
if (operationIds.includes(
|
|
320
|
-
throw new Error(`"${
|
|
320
|
+
if (operationIds.includes(operationId)) {
|
|
321
|
+
throw new Error(`"${operationId}" is duplicated in your schema definition!`);
|
|
321
322
|
}
|
|
322
|
-
operationIds.push(
|
|
323
|
+
operationIds.push(operationId);
|
|
323
324
|
route = route.replace(/\{/g, '${'); // `/pet/{id}` => `/pet/${id}`
|
|
324
325
|
// Remove the last param of the route if we are in the DELETE case
|
|
325
326
|
let lastParamInTheRoute = null;
|
|
326
|
-
const componentName = case_1.pascal(
|
|
327
|
+
const componentName = case_1.pascal(operationId);
|
|
327
328
|
const isOk = ([statusCode]) => statusCode.toString().startsWith('2');
|
|
328
329
|
const responseTypes = exports.getResReqTypes(Object.entries(operation.responses).filter(isOk)) || 'void';
|
|
329
330
|
const requestBodyTypes = exports.getResReqTypes([['body', operation.requestBody]]);
|
|
@@ -344,7 +345,7 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
344
345
|
return `${name}${required ? '' : '?'}: ${exports.resolveValue(schema)}`;
|
|
345
346
|
}
|
|
346
347
|
catch (err) {
|
|
347
|
-
throw new Error(`The path params ${p} can't be found in parameters (${
|
|
348
|
+
throw new Error(`The path params ${p} can't be found in parameters (${operationId})`);
|
|
348
349
|
}
|
|
349
350
|
})
|
|
350
351
|
.join('; ');
|
|
@@ -364,7 +365,7 @@ const generateRestfulComponent = (operation, verb, route, operationIds, paramete
|
|
|
364
365
|
}) // Reference is not possible
|
|
365
366
|
: { schema: { type: 'string' } };
|
|
366
367
|
if (!lastParamInTheRouteDefinition) {
|
|
367
|
-
throw new Error(`The path params ${lastParamInTheRoute} can't be found in parameters (${
|
|
368
|
+
throw new Error(`The path params ${lastParamInTheRoute} can't be found in parameters (${operationId})`);
|
|
368
369
|
}
|
|
369
370
|
let genericsTypes = `${needAResponseComponent ? componentName + 'Res' : responseTypes}`;
|
|
370
371
|
if (verb !== 'get') {
|
|
@@ -623,4 +624,4 @@ const importOpenApi = async ({ data, format }) => {
|
|
|
623
624
|
});
|
|
624
625
|
return output;
|
|
625
626
|
};
|
|
626
|
-
exports.
|
|
627
|
+
exports.importOpenApi = importOpenApi;
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
Object.defineProperty(exports, "
|
|
3
|
+
exports.importSpecs = void 0;
|
|
4
|
+
const react_query_codegen_import_1 = require("./react-query-codegen-import");
|
|
5
|
+
Object.defineProperty(exports, "importSpecs", { enumerable: true, get: function () { return react_query_codegen_import_1.importSpecs; } });
|
|
@@ -3,42 +3,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.importSpecs = void 0;
|
|
6
7
|
const chalk_1 = __importDefault(require("chalk"));
|
|
7
|
-
const commander_1 = __importDefault(require("commander"));
|
|
8
8
|
const fs_1 = require("fs");
|
|
9
9
|
const path_1 = require("path");
|
|
10
|
-
const import_open_api_1 =
|
|
10
|
+
const import_open_api_1 = require("./import-open-api");
|
|
11
11
|
const log = console.log; // tslint:disable-line:no-console
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
12
|
+
const createSuccessMessage = (backend) => chalk_1.default.green(`🎉 ${backend ? `[${backend}] ` : ''} Your OpenAPI spec has been converted into react query hooks`);
|
|
13
|
+
function importSpecs(dirname, exportDirName) {
|
|
14
|
+
fs_1.readdir(dirname, function (err, filenames) {
|
|
15
|
+
if (err) {
|
|
16
|
+
throw err;
|
|
17
|
+
}
|
|
18
|
+
filenames.map(async (filename) => {
|
|
19
|
+
const data = fs_1.readFileSync(path_1.join(process.cwd(), dirname + '/' + filename), 'utf-8');
|
|
20
|
+
const { ext } = path_1.parse(dirname + '/' + filename);
|
|
21
|
+
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
22
|
+
try {
|
|
23
|
+
const name = `useQueries${filename.split('.')[0]}.tsx`;
|
|
24
|
+
const fileExports = await import_open_api_1.importOpenApi({ data, format });
|
|
25
|
+
fs_1.writeFileSync(path_1.join(process.cwd(), `${exportDirName}/${name}`), fileExports);
|
|
26
|
+
log(createSuccessMessage(filename));
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
log(chalk_1.default.red(`[${filename}]:`), chalk_1.default.red(error));
|
|
30
|
+
// process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
27
33
|
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
importSpecs(commander_1.default)
|
|
31
|
-
.then((data) => {
|
|
32
|
-
if (commander_1.default.output) {
|
|
33
|
-
fs_1.writeFileSync(path_1.join(process.cwd(), commander_1.default.output), data);
|
|
34
|
-
log(createSuccessMessage());
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
log(data);
|
|
38
|
-
log(successWithoutOutputMessage);
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
.catch((err) => {
|
|
42
|
-
log(chalk_1.default.red(err));
|
|
43
|
-
process.exit(1);
|
|
44
|
-
});
|
|
34
|
+
}
|
|
35
|
+
exports.importSpecs = importSpecs;
|
|
@@ -293,17 +293,18 @@ export const formatDescription = (description, tabSize = 0) => description
|
|
|
293
293
|
* Generate a react-query component from openapi operation specs
|
|
294
294
|
*/
|
|
295
295
|
export const generateRestfulComponent = (operation, verb, route, operationIds, parameters = [], schemasComponents) => {
|
|
296
|
-
|
|
297
|
-
|
|
296
|
+
const { operationId = route.replace('/', '') } = operation;
|
|
297
|
+
if (operationId === '*') {
|
|
298
|
+
throw new Error(`Invalid operationId/Route set for ${verb} ${route}`);
|
|
298
299
|
}
|
|
299
|
-
if (operationIds.includes(
|
|
300
|
-
throw new Error(`"${
|
|
300
|
+
if (operationIds.includes(operationId)) {
|
|
301
|
+
throw new Error(`"${operationId}" is duplicated in your schema definition!`);
|
|
301
302
|
}
|
|
302
|
-
operationIds.push(
|
|
303
|
+
operationIds.push(operationId);
|
|
303
304
|
route = route.replace(/\{/g, '${'); // `/pet/{id}` => `/pet/${id}`
|
|
304
305
|
// Remove the last param of the route if we are in the DELETE case
|
|
305
306
|
let lastParamInTheRoute = null;
|
|
306
|
-
const componentName = pascal(
|
|
307
|
+
const componentName = pascal(operationId);
|
|
307
308
|
const isOk = ([statusCode]) => statusCode.toString().startsWith('2');
|
|
308
309
|
const responseTypes = getResReqTypes(Object.entries(operation.responses).filter(isOk)) || 'void';
|
|
309
310
|
const requestBodyTypes = getResReqTypes([['body', operation.requestBody]]);
|
|
@@ -324,7 +325,7 @@ export const generateRestfulComponent = (operation, verb, route, operationIds, p
|
|
|
324
325
|
return `${name}${required ? '' : '?'}: ${resolveValue(schema)}`;
|
|
325
326
|
}
|
|
326
327
|
catch (err) {
|
|
327
|
-
throw new Error(`The path params ${p} can't be found in parameters (${
|
|
328
|
+
throw new Error(`The path params ${p} can't be found in parameters (${operationId})`);
|
|
328
329
|
}
|
|
329
330
|
})
|
|
330
331
|
.join('; ');
|
|
@@ -344,7 +345,7 @@ export const generateRestfulComponent = (operation, verb, route, operationIds, p
|
|
|
344
345
|
}) // Reference is not possible
|
|
345
346
|
: { schema: { type: 'string' } };
|
|
346
347
|
if (!lastParamInTheRouteDefinition) {
|
|
347
|
-
throw new Error(`The path params ${lastParamInTheRoute} can't be found in parameters (${
|
|
348
|
+
throw new Error(`The path params ${lastParamInTheRoute} can't be found in parameters (${operationId})`);
|
|
348
349
|
}
|
|
349
350
|
let genericsTypes = `${needAResponseComponent ? componentName + 'Res' : responseTypes}`;
|
|
350
351
|
if (verb !== 'get') {
|
|
@@ -576,7 +577,7 @@ export const generateRestfulComponent = (operation, verb, route, operationIds, p
|
|
|
576
577
|
/**
|
|
577
578
|
* Main entry of the generator. Generate react-query component from openAPI.
|
|
578
579
|
*/
|
|
579
|
-
const importOpenApi = async ({ data, format }) => {
|
|
580
|
+
export const importOpenApi = async ({ data, format }) => {
|
|
580
581
|
const operationIds = [];
|
|
581
582
|
let specs = await importSpecs(data, format);
|
|
582
583
|
resolveDiscriminator(specs);
|
|
@@ -602,4 +603,3 @@ const importOpenApi = async ({ data, format }) => {
|
|
|
602
603
|
});
|
|
603
604
|
return output;
|
|
604
605
|
};
|
|
605
|
-
export default importOpenApi;
|
package/lib/esm/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { importSpecs } from './react-query-codegen-import';
|
|
2
|
+
export { importSpecs };
|
|
@@ -1,39 +1,28 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
-
import
|
|
3
|
-
import { readFileSync, writeFileSync } from 'fs';
|
|
2
|
+
import { readFileSync, writeFileSync, readdir } from 'fs';
|
|
4
3
|
import { join, parse } from 'path';
|
|
5
|
-
import importOpenApi from './
|
|
4
|
+
import { importOpenApi } from './import-open-api';
|
|
6
5
|
const log = console.log; // tslint:disable-line:no-console
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
6
|
+
const createSuccessMessage = (backend) => chalk.green(`🎉 ${backend ? `[${backend}] ` : ''} Your OpenAPI spec has been converted into react query hooks`);
|
|
7
|
+
export function importSpecs(dirname, exportDirName) {
|
|
8
|
+
readdir(dirname, function (err, filenames) {
|
|
9
|
+
if (err) {
|
|
10
|
+
throw err;
|
|
11
|
+
}
|
|
12
|
+
filenames.map(async (filename) => {
|
|
13
|
+
const data = readFileSync(join(process.cwd(), dirname + '/' + filename), 'utf-8');
|
|
14
|
+
const { ext } = parse(dirname + '/' + filename);
|
|
15
|
+
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
16
|
+
try {
|
|
17
|
+
const name = `useQueries${filename.split('.')[0]}.tsx`;
|
|
18
|
+
const fileExports = await importOpenApi({ data, format });
|
|
19
|
+
writeFileSync(join(process.cwd(), `${exportDirName}/${name}`), fileExports);
|
|
20
|
+
log(createSuccessMessage(filename));
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
log(chalk.red(`[${filename}]:`), chalk.red(error));
|
|
24
|
+
// process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
22
27
|
});
|
|
23
|
-
}
|
|
24
|
-
// Use flags as configuration
|
|
25
|
-
importSpecs(program)
|
|
26
|
-
.then((data) => {
|
|
27
|
-
if (program.output) {
|
|
28
|
-
writeFileSync(join(process.cwd(), program.output), data);
|
|
29
|
-
log(createSuccessMessage());
|
|
30
|
-
}
|
|
31
|
-
else {
|
|
32
|
-
log(data);
|
|
33
|
-
log(successWithoutOutputMessage);
|
|
34
|
-
}
|
|
35
|
-
})
|
|
36
|
-
.catch((err) => {
|
|
37
|
-
log(chalk.red(err));
|
|
38
|
-
process.exit(1);
|
|
39
|
-
});
|
|
28
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-query-lightbase-codegen",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./lib/cjs/index.js",
|
|
6
6
|
"module": "./lib/esm/index.js",
|
|
@@ -9,11 +9,7 @@
|
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
11
|
"tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
|
|
12
|
-
"
|
|
13
|
-
"build:project": "tsdx build",
|
|
14
|
-
"build:bin": "tsc && rollup -c rollup.config.js",
|
|
15
|
-
"test": "yarn build && node dist/react-query-codegen.js import --file src/swagger.json --output dist/useQueries.tsx && prettier --check 'dist/**/*.{ts,tsx}' --write",
|
|
16
|
-
"codegen": "yarn build && node dist/react-query-codegen.js import --file src/mobile-api.yaml --output dist/useQueries.tsx && prettier --check 'dist/**/*.{ts,tsx}' --write"
|
|
12
|
+
"test": "yarn tsc && node test/script.js"
|
|
17
13
|
},
|
|
18
14
|
"dependencies": {
|
|
19
15
|
"axios": "^0.26.0",
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.codegen = void 0;
|
|
7
|
-
const commander_1 = __importDefault(require("commander"));
|
|
8
|
-
const fs_1 = require("fs");
|
|
9
|
-
const path_1 = require("path");
|
|
10
|
-
const { version } = JSON.parse(fs_1.readFileSync(path_1.join(__dirname, '../../package.json'), 'utf-8'));
|
|
11
|
-
const codegen = () => {
|
|
12
|
-
commander_1.default
|
|
13
|
-
.version(version)
|
|
14
|
-
.command('import [open-api-file]', 'generate react-query hooks from OpenAPI specs')
|
|
15
|
-
.parse(process.argv);
|
|
16
|
-
};
|
|
17
|
-
exports.codegen = codegen;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import program from 'commander';
|
|
2
|
-
import { readFileSync } from 'fs';
|
|
3
|
-
import { join } from 'path';
|
|
4
|
-
const { version } = JSON.parse(readFileSync(join(__dirname, '../../package.json'), 'utf-8'));
|
|
5
|
-
export const codegen = () => {
|
|
6
|
-
program
|
|
7
|
-
.version(version)
|
|
8
|
-
.command('import [open-api-file]', 'generate react-query hooks from OpenAPI specs')
|
|
9
|
-
.parse(process.argv);
|
|
10
|
-
};
|