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