react-query-lightbase-codegen 1.1.1 → 1.1.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/{lib/esm → dist}/convertSwaggerFile.js +1 -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/{lib/esm → dist}/generateSchemas.js +7 -7
- package/dist/generateSchemas.js.map +1 -0
- package/dist/importSpecs.d.ts +15 -0
- package/dist/importSpecs.js +90 -0
- package/dist/importSpecs.js.map +1 -0
- package/{lib/esm/index.js → dist/index.d.ts} +0 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/utils.d.ts +24 -0
- package/{lib/esm → dist}/utils.js +22 -18
- package/dist/utils.js.map +1 -0
- package/package.json +3 -3
- package/src/generateHooks.ts +5 -2
- package/lib/cjs/convertSwaggerFile.js +0 -32
- package/lib/cjs/generateHooks.js +0 -368
- package/lib/cjs/generateImports.js +0 -26
- package/lib/cjs/generateSchemas.js +0 -104
- package/lib/cjs/importSpecs.js +0 -46
- package/lib/cjs/index.js +0 -7
- package/lib/cjs/utils.js +0 -165
- package/lib/esm/generateHooks.js +0 -360
- package/lib/esm/generateImports.js +0 -22
- package/lib/esm/importSpecs.js +0 -39
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.generateImports = void 0;
|
|
4
|
-
const generateImports = ({ schemaName, apiDirectory, queryClientDir, schemaImports, }) => {
|
|
5
|
-
const importTypes = schemaImports.join(',');
|
|
6
|
-
return `
|
|
7
|
-
import {
|
|
8
|
-
useQuery,
|
|
9
|
-
useMutation,
|
|
10
|
-
UseQueryOptions,
|
|
11
|
-
UseMutationOptions,
|
|
12
|
-
QueryKey,
|
|
13
|
-
SetDataOptions,
|
|
14
|
-
QueryFilters
|
|
15
|
-
} from '@tanstack/react-query';
|
|
16
|
-
|
|
17
|
-
import { AxiosError } from 'axios';
|
|
18
|
-
import { api } from '${apiDirectory}';
|
|
19
|
-
import { queryClient } from '${queryClientDir}';
|
|
20
|
-
|
|
21
|
-
import {${importTypes}} from './${schemaName}'
|
|
22
|
-
|
|
23
|
-
type Updater<TInput, TOutput> = TOutput | ((input: TInput) => TOutput);
|
|
24
|
-
`;
|
|
25
|
-
};
|
|
26
|
-
exports.generateImports = generateImports;
|
|
@@ -1,104 +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.generateSchemas = void 0;
|
|
7
|
-
const case_1 = __importDefault(require("case"));
|
|
8
|
-
const lodash_1 = __importDefault(require("lodash"));
|
|
9
|
-
const { isEmpty } = lodash_1.default;
|
|
10
|
-
const { pascal } = case_1.default;
|
|
11
|
-
const utils_js_1 = require("./utils.js");
|
|
12
|
-
const utils_js_2 = require("./utils.js");
|
|
13
|
-
/**
|
|
14
|
-
* Generate the interface string
|
|
15
|
-
*/
|
|
16
|
-
const generateInterface = (name, schema) => {
|
|
17
|
-
const scalar = (0, utils_js_1.getScalar)(schema);
|
|
18
|
-
return `
|
|
19
|
-
${(0, utils_js_1.formatDescription)(schema.description)}
|
|
20
|
-
export type ${pascal(name)} = ${scalar}
|
|
21
|
-
`;
|
|
22
|
-
};
|
|
23
|
-
/**
|
|
24
|
-
* Extract all types from #/components/schemas
|
|
25
|
-
*/
|
|
26
|
-
const generateSchemasDefinition = (schemas = {}) => {
|
|
27
|
-
if (isEmpty(schemas)) {
|
|
28
|
-
return '';
|
|
29
|
-
}
|
|
30
|
-
return ('\n // SCEHMAS \n' +
|
|
31
|
-
Object.entries(schemas)
|
|
32
|
-
.map(([name, schema]) => !(0, utils_js_1.isReference)(schema) &&
|
|
33
|
-
(!schema.type || schema.type === 'object') &&
|
|
34
|
-
!schema.allOf &&
|
|
35
|
-
!schema.oneOf &&
|
|
36
|
-
!(0, utils_js_1.isReference)(schema) &&
|
|
37
|
-
!schema.nullable
|
|
38
|
-
? generateInterface(name, schema)
|
|
39
|
-
: `${(0, utils_js_1.formatDescription)((0, utils_js_1.isReference)(schema) ? undefined : schema.description)} export type ${pascal(name)} = ${(0, utils_js_1.resolveValue)(schema)};`)
|
|
40
|
-
.join('\n\n') +
|
|
41
|
-
'\n');
|
|
42
|
-
};
|
|
43
|
-
/**
|
|
44
|
-
* Extract all types from #/components/requestBodies
|
|
45
|
-
*/
|
|
46
|
-
const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
47
|
-
if (isEmpty(requestBodies)) {
|
|
48
|
-
return '';
|
|
49
|
-
}
|
|
50
|
-
return ('\n // REQUEST BODIES \n' +
|
|
51
|
-
Object.entries(requestBodies)
|
|
52
|
-
.map(([name, requestBody]) => {
|
|
53
|
-
const doc = (0, utils_js_2.getDocs)(requestBody);
|
|
54
|
-
const type = (0, utils_js_2.getResReqTypes)([['', requestBody]]);
|
|
55
|
-
const isEmptyInterface = type === '{}';
|
|
56
|
-
if (isEmptyInterface) {
|
|
57
|
-
return `${doc}export type ${pascal(name)}RequestBody = ${type}`;
|
|
58
|
-
}
|
|
59
|
-
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
60
|
-
return `${doc}export type ${pascal(name)}RequestBody = ${type}`;
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return `${doc}export type ${pascal(name)}RequestBody = ${type};`;
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
.join('\n\n') +
|
|
67
|
-
'\n');
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Extract all types from #/components/responses
|
|
71
|
-
*/
|
|
72
|
-
const generateResponsesDefinition = (responses = {}) => {
|
|
73
|
-
if (isEmpty(responses)) {
|
|
74
|
-
return '';
|
|
75
|
-
}
|
|
76
|
-
return ('\n // RESPONSES \n' +
|
|
77
|
-
Object.entries(responses)
|
|
78
|
-
.map(([name, response]) => {
|
|
79
|
-
const doc = (0, utils_js_2.getDocs)(response);
|
|
80
|
-
const type = (0, utils_js_2.getResReqTypes)([['', response]]);
|
|
81
|
-
const isEmptyInterface = type === '{}';
|
|
82
|
-
if (isEmptyInterface) {
|
|
83
|
-
return `export type RQ${pascal(name)}Response = ${type}`;
|
|
84
|
-
}
|
|
85
|
-
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
86
|
-
return `${doc}export type RQ${pascal(name)}Response = ${type}`;
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
return `${doc}export type RQ${pascal(name)}Response = ${type};`;
|
|
90
|
-
}
|
|
91
|
-
})
|
|
92
|
-
.join('\n\n') +
|
|
93
|
-
'\n');
|
|
94
|
-
};
|
|
95
|
-
/**
|
|
96
|
-
* Extract all types from #/components/schemas
|
|
97
|
-
*/
|
|
98
|
-
const generateSchemas = ({ spec }) => {
|
|
99
|
-
const schemaOutput = generateSchemasDefinition(spec.components?.schemas) +
|
|
100
|
-
generateResponsesDefinition(spec.components?.responses) +
|
|
101
|
-
generateRequestBodiesDefinition(spec.components?.requestBodies);
|
|
102
|
-
return schemaOutput;
|
|
103
|
-
};
|
|
104
|
-
exports.generateSchemas = generateSchemas;
|
package/lib/cjs/importSpecs.js
DELETED
|
@@ -1,46 +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.importSpecs = void 0;
|
|
7
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
-
const fs_1 = require("fs");
|
|
9
|
-
const path_1 = require("path");
|
|
10
|
-
const convertSwaggerFile_js_1 = require("./convertSwaggerFile.js");
|
|
11
|
-
const generateHooks_js_1 = require("./generateHooks.js");
|
|
12
|
-
const generateSchemas_js_1 = require("./generateSchemas.js");
|
|
13
|
-
function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, queryClientDir, headerFilters, }) {
|
|
14
|
-
(0, fs_1.readdir)(sourceDirectory, function (err, filenames) {
|
|
15
|
-
if (err) {
|
|
16
|
-
console.log(err);
|
|
17
|
-
throw err;
|
|
18
|
-
}
|
|
19
|
-
filenames.map(async (filename) => {
|
|
20
|
-
try {
|
|
21
|
-
const data = (0, fs_1.readFileSync)((0, path_1.join)(process.cwd(), sourceDirectory + '/' + filename), 'utf-8');
|
|
22
|
-
const { ext } = (0, path_1.parse)(sourceDirectory + '/' + filename);
|
|
23
|
-
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
24
|
-
const name = filename.split('.')[0];
|
|
25
|
-
let spec = await (0, convertSwaggerFile_js_1.convertSwaggerFile)(data, format);
|
|
26
|
-
const operationIds = [];
|
|
27
|
-
(0, fs_1.mkdirSync)((0, path_1.join)(process.cwd(), `${exportDirectory}/${name}`), { recursive: true });
|
|
28
|
-
const { implementation, schemaImports } = (0, generateHooks_js_1.generateQueryHooks)({ spec, operationIds, headerFilters });
|
|
29
|
-
const schemaName = `useQueries${filename.split('.')[0]}.schema`;
|
|
30
|
-
const imports = (0, generateHooks_js_1.generateImports)({ apiDirectory, queryClientDir, schemaName, schemaImports });
|
|
31
|
-
const hooksName = `useQueries${filename.split('.')[0]}`;
|
|
32
|
-
(0, fs_1.writeFileSync)((0, path_1.join)(process.cwd(), `${exportDirectory}/${name}/${hooksName}.tsx`), imports + implementation);
|
|
33
|
-
const schemas = (0, generateSchemas_js_1.generateSchemas)({ spec });
|
|
34
|
-
(0, fs_1.writeFileSync)((0, path_1.join)(process.cwd(), `${exportDirectory}/${name}/${schemaName}.tsx`), schemas);
|
|
35
|
-
console.log(chalk_1.default.green(`🎉 [${filename}] Your OpenAPI spec has been converted into react query hooks`));
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
if (error.code === 'EISDIR') {
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
console.log(chalk_1.default.red(`[${filename}]:`), chalk_1.default.red(error));
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
exports.importSpecs = importSpecs;
|
package/lib/cjs/index.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertSwaggerFile = exports.importSpecs = void 0;
|
|
4
|
-
const convertSwaggerFile_js_1 = require("./convertSwaggerFile.js");
|
|
5
|
-
Object.defineProperty(exports, "convertSwaggerFile", { enumerable: true, get: function () { return convertSwaggerFile_js_1.convertSwaggerFile; } });
|
|
6
|
-
const importSpecs_js_1 = require("./importSpecs.js");
|
|
7
|
-
Object.defineProperty(exports, "importSpecs", { enumerable: true, get: function () { return importSpecs_js_1.importSpecs; } });
|
package/lib/cjs/utils.js
DELETED
|
@@ -1,165 +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.getResReqTypes = exports.formatDescription = exports.resolveValue = exports.getScalar = exports.isReference = exports.getDocs = void 0;
|
|
7
|
-
const lodash_1 = __importDefault(require("lodash"));
|
|
8
|
-
const { uniq, isEmpty } = lodash_1.default;
|
|
9
|
-
const case_1 = __importDefault(require("case"));
|
|
10
|
-
const { pascal } = case_1.default;
|
|
11
|
-
const getDocs = (data) => (0, exports.isReference)(data) ? '' : (0, exports.formatDescription)(data.description);
|
|
12
|
-
exports.getDocs = getDocs;
|
|
13
|
-
/**
|
|
14
|
-
* Discriminator helper for `ReferenceObject`
|
|
15
|
-
*/
|
|
16
|
-
const isReference = (property) => {
|
|
17
|
-
return Boolean(property.$ref);
|
|
18
|
-
};
|
|
19
|
-
exports.isReference = isReference;
|
|
20
|
-
/**
|
|
21
|
-
* Return the typescript equivalent of open-api data type
|
|
22
|
-
*/
|
|
23
|
-
const getScalar = (item) => {
|
|
24
|
-
const nullable = item.nullable ? ' | null' : '';
|
|
25
|
-
switch (item.type) {
|
|
26
|
-
case 'number':
|
|
27
|
-
case 'integer':
|
|
28
|
-
return 'number' + nullable;
|
|
29
|
-
case 'boolean':
|
|
30
|
-
return 'boolean' + nullable;
|
|
31
|
-
case 'array':
|
|
32
|
-
return getArray(item) + nullable;
|
|
33
|
-
case 'string':
|
|
34
|
-
return (item.enum ? `"${item.enum.join(`" | "`)}"` : 'string') + nullable;
|
|
35
|
-
case 'object':
|
|
36
|
-
default:
|
|
37
|
-
return getObject(item) + nullable;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
exports.getScalar = getScalar;
|
|
41
|
-
/**
|
|
42
|
-
* Return the output type from the $ref
|
|
43
|
-
*/
|
|
44
|
-
const getRef = ($ref) => {
|
|
45
|
-
if ($ref.startsWith('#/components/schemas')) {
|
|
46
|
-
return pascal($ref.replace('#/components/schemas/', ''));
|
|
47
|
-
}
|
|
48
|
-
else if ($ref.startsWith('#/components/responses')) {
|
|
49
|
-
return pascal($ref.replace('#/components/responses/', '')) + 'Response';
|
|
50
|
-
}
|
|
51
|
-
else if ($ref.startsWith('#/components/parameters')) {
|
|
52
|
-
return pascal($ref.replace('#/components/parameters/', '')) + 'Parameter';
|
|
53
|
-
}
|
|
54
|
-
else if ($ref.startsWith('#/components/requestBodies')) {
|
|
55
|
-
return pascal($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* Return the output type from an array
|
|
63
|
-
*/
|
|
64
|
-
const getArray = (item) => {
|
|
65
|
-
if (item.items) {
|
|
66
|
-
if (!(0, exports.isReference)(item.items) && (item.items.oneOf || item.items.allOf || item.items.enum)) {
|
|
67
|
-
return `(${(0, exports.resolveValue)(item.items)})[]`;
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
return `${(0, exports.resolveValue)(item.items)}[]`;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
throw new Error('All arrays must have an `items` key define');
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* Return the output type from an object
|
|
79
|
-
*/
|
|
80
|
-
const getObject = (item) => {
|
|
81
|
-
if ((0, exports.isReference)(item)) {
|
|
82
|
-
return getRef(item.$ref);
|
|
83
|
-
}
|
|
84
|
-
if (item.allOf) {
|
|
85
|
-
return item.allOf.map(exports.resolveValue).join(' & ');
|
|
86
|
-
}
|
|
87
|
-
if (item.oneOf) {
|
|
88
|
-
return item.oneOf.map(exports.resolveValue).join(' | ');
|
|
89
|
-
}
|
|
90
|
-
if (!item.type && !item.properties && !item.additionalProperties) {
|
|
91
|
-
return '{}';
|
|
92
|
-
}
|
|
93
|
-
// Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
|
|
94
|
-
if (item.type === 'object' &&
|
|
95
|
-
!item.properties &&
|
|
96
|
-
(!item.additionalProperties || item.additionalProperties === true || isEmpty(item.additionalProperties))) {
|
|
97
|
-
return '{[key: string]: any}';
|
|
98
|
-
}
|
|
99
|
-
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
100
|
-
// Consolidation of item.properties & item.additionalProperties
|
|
101
|
-
let output = '{\n';
|
|
102
|
-
if (item.properties) {
|
|
103
|
-
output += Object.entries(item.properties)
|
|
104
|
-
.map(([key, prop]) => {
|
|
105
|
-
const doc = (0, exports.getDocs)(prop);
|
|
106
|
-
const isRequired = (item.required || []).includes(key);
|
|
107
|
-
const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
|
|
108
|
-
return `${doc}\n${processedKey}${isRequired ? '' : '?'}: ${(0, exports.resolveValue)(prop)};`;
|
|
109
|
-
})
|
|
110
|
-
.join('');
|
|
111
|
-
}
|
|
112
|
-
if (item.additionalProperties) {
|
|
113
|
-
if (item.properties) {
|
|
114
|
-
output += '\n';
|
|
115
|
-
}
|
|
116
|
-
output += `} & { [key: string]: ${item.additionalProperties === true ? 'any' : (0, exports.resolveValue)(item.additionalProperties)}`;
|
|
117
|
-
}
|
|
118
|
-
if (item.properties || item.additionalProperties) {
|
|
119
|
-
if (output === '{\n') {
|
|
120
|
-
return '{}';
|
|
121
|
-
}
|
|
122
|
-
return output + '\n}';
|
|
123
|
-
}
|
|
124
|
-
return item.type === 'object' ? '{[key: string]: any}' : 'any';
|
|
125
|
-
};
|
|
126
|
-
/**
|
|
127
|
-
* Resolve the value of a schema object to a proper type definition.
|
|
128
|
-
*/
|
|
129
|
-
const resolveValue = (schema) => (0, exports.isReference)(schema) ? getRef(schema.$ref) : (0, exports.getScalar)(schema);
|
|
130
|
-
exports.resolveValue = resolveValue;
|
|
131
|
-
/**
|
|
132
|
-
* Format a description to code documentation.
|
|
133
|
-
*/
|
|
134
|
-
const formatDescription = (description) => {
|
|
135
|
-
if (!description)
|
|
136
|
-
return '';
|
|
137
|
-
return `/**\n${description
|
|
138
|
-
.split('\n')
|
|
139
|
-
.map((i) => `* ${i}`)
|
|
140
|
-
.join('\n')}\n */`;
|
|
141
|
-
};
|
|
142
|
-
exports.formatDescription = formatDescription;
|
|
143
|
-
/**
|
|
144
|
-
* Extract responses / request types from open-api specs
|
|
145
|
-
*/
|
|
146
|
-
const getResReqTypes = (responsesOrRequests) => uniq(responsesOrRequests.map(([_, res]) => {
|
|
147
|
-
if (!res) {
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
if ((0, exports.isReference)(res)) {
|
|
151
|
-
return getRef(res.$ref);
|
|
152
|
-
}
|
|
153
|
-
if (res.content) {
|
|
154
|
-
for (let contentType of Object.keys(res.content)) {
|
|
155
|
-
if (contentType.startsWith('application/json') ||
|
|
156
|
-
contentType.startsWith('application/octet-stream')) {
|
|
157
|
-
const schema = res.content[contentType].schema;
|
|
158
|
-
return (0, exports.resolveValue)(schema);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
return;
|
|
164
|
-
})).join(' | ');
|
|
165
|
-
exports.getResReqTypes = getResReqTypes;
|