react-query-lightbase-codegen 0.5.0 → 1.0.0
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 +34 -2
- package/lib/cjs/generateRequestBodiesDefinition.js +0 -36
- package/lib/cjs/generateResponsesDefinition.js +0 -36
- 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/react-query-codegen-import.js +0 -49
- package/lib/cjs/resolveDiscriminator.js +0 -31
- package/lib/cjs/types.js +0 -2
- package/lib/esm/generateRequestBodiesDefinition.js +0 -29
- package/lib/esm/generateResponsesDefinition.js +0 -29
- 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/react-query-codegen-import.js +0 -42
- package/lib/esm/resolveDiscriminator.js +0 -24
- package/lib/esm/types.js +0 -1
package/README.md
CHANGED
|
@@ -1 +1,77 @@
|
|
|
1
|
+
# React Query Code Generation
|
|
1
2
|
|
|
3
|
+
Generate fully typed react query hooks from OpenAPI specifications.
|
|
4
|
+
|
|
5
|
+
- GET requests will automatically generate `useQuery` hooks together with helper functions
|
|
6
|
+
- getQueryState
|
|
7
|
+
- getQueryData
|
|
8
|
+
- prefetch
|
|
9
|
+
- cancelQueries
|
|
10
|
+
- invalidate
|
|
11
|
+
- refetchStale
|
|
12
|
+
|
|
13
|
+
- Override default generation forcing query, mutation or infiniteQuery output
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
You can install react-query-lightbase-codegen with NPM or Yarn.
|
|
18
|
+
|
|
19
|
+
Using NPM:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
$ npm i -D react-query-lightbase-codegen
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Using Yarn:
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
$ yarn add -D react-query-lightbase-codegen
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Configuration
|
|
32
|
+
|
|
33
|
+
create a generateQueries.mjs file in project root folder
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
// generateQueries.mjs
|
|
37
|
+
import { importSpecs } from 'react-query-lightbase-codegen';
|
|
38
|
+
|
|
39
|
+
importSpecs({
|
|
40
|
+
// folder location of the openapi/swagger documents (yaml or JSON)
|
|
41
|
+
sourceDirectory: './specs',
|
|
42
|
+
// export folder for hooks and schema code generated files
|
|
43
|
+
exportDirectory: './src/generated',
|
|
44
|
+
// api client - as named export labelled 'api'
|
|
45
|
+
apiDirectory: './src/api',
|
|
46
|
+
// React query client directory - names export 'queryClient'
|
|
47
|
+
queryClientDir: './src/api',
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Code generation
|
|
52
|
+
|
|
53
|
+
To generate the code generated scheme and react query hooks run the above script
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
node scripts/generateQueries.mjs && prettier --check ./src/generated/*.tsx --write
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Configuration Options
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
// generateQueries.mjs
|
|
63
|
+
import { importSpecs } from 'react-query-lightbase-codegen';
|
|
64
|
+
|
|
65
|
+
importSpecs({
|
|
66
|
+
...
|
|
67
|
+
// Filter out any headers from individual queries (these might be applied globally in the axios instance)
|
|
68
|
+
headerFilters: ['X-Session-Token'],
|
|
69
|
+
overrides: {
|
|
70
|
+
// operationId listed in the openApi spec
|
|
71
|
+
findPetsByStatus: {
|
|
72
|
+
// Override the default query code generation type ('query' | 'mutation' | 'infiniteQuery')
|
|
73
|
+
type: 'query',
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
```
|
package/package.json
CHANGED
|
@@ -1,19 +1,43 @@
|
|
|
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.0",
|
|
4
5
|
"license": "MIT",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"exports": "./src/index.js",
|
|
7
8
|
"files": [
|
|
8
9
|
"lib/"
|
|
9
10
|
],
|
|
11
|
+
"keywords": [
|
|
12
|
+
"rest",
|
|
13
|
+
"client",
|
|
14
|
+
"swagger",
|
|
15
|
+
"open-api",
|
|
16
|
+
"fetch",
|
|
17
|
+
"data fetching",
|
|
18
|
+
"code-generation",
|
|
19
|
+
"react",
|
|
20
|
+
"react-query",
|
|
21
|
+
"axios",
|
|
22
|
+
"vue-query",
|
|
23
|
+
"tanstack"
|
|
24
|
+
],
|
|
25
|
+
"author": {
|
|
26
|
+
"name": "Oliver Winter",
|
|
27
|
+
"email": "owinter86@gmail.com"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/owinter86/react-query-codegen"
|
|
32
|
+
},
|
|
10
33
|
"engines": {
|
|
11
34
|
"node": ">=14.16"
|
|
12
35
|
},
|
|
13
36
|
"scripts": {
|
|
14
37
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
|
15
38
|
"tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
|
|
16
|
-
"test": "yarn tsc && node test/script.mjs && prettier --check ./test/generated/**/*.tsx --write && eslint ./test/generated/**/*.tsx --fix"
|
|
39
|
+
"test": "yarn tsc && node test/script.mjs && prettier --check ./test/generated/**/*.tsx --write && eslint ./test/generated/**/*.tsx --fix",
|
|
40
|
+
"prepare": "husky install"
|
|
17
41
|
},
|
|
18
42
|
"dependencies": {
|
|
19
43
|
"axios": "0.27.2",
|
|
@@ -28,7 +52,13 @@
|
|
|
28
52
|
"devDependencies": {
|
|
29
53
|
"@babel/core": "7.18.10",
|
|
30
54
|
"@babel/eslint-parser": "7.17.0",
|
|
55
|
+
"@commitlint/cli": "^17.0.3",
|
|
56
|
+
"@commitlint/config-conventional": "^17.0.3",
|
|
31
57
|
"@react-native-community/eslint-config": "3.1.0",
|
|
58
|
+
"@semantic-release/changelog": "^6.0.1",
|
|
59
|
+
"@semantic-release/git": "^10.0.1",
|
|
60
|
+
"@semantic-release/github": "^8.0.5",
|
|
61
|
+
"@semantic-release/npm": "^9.0.1",
|
|
32
62
|
"@tanstack/react-query": "4.0.10",
|
|
33
63
|
"@types/js-yaml": "4.0.5",
|
|
34
64
|
"@types/lodash": "4.14.182",
|
|
@@ -38,8 +68,10 @@
|
|
|
38
68
|
"@types/yamljs": "0.2.31",
|
|
39
69
|
"eslint": "8.21.0",
|
|
40
70
|
"eslint-plugin-prettier": "4.2.1",
|
|
71
|
+
"husky": "^8.0.1",
|
|
41
72
|
"prettier": "2.7.1",
|
|
42
73
|
"react": "17.0.2",
|
|
74
|
+
"semantic-release": "^19.0.3",
|
|
43
75
|
"typescript": "4.7.4"
|
|
44
76
|
}
|
|
45
77
|
}
|
|
@@ -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.generateRequestBodiesDefinition = 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/requestBodies
|
|
12
|
-
*/
|
|
13
|
-
const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
14
|
-
if ((0, isEmpty_1.default)(requestBodies)) {
|
|
15
|
-
return '';
|
|
16
|
-
}
|
|
17
|
-
return ('\n // REQUEST BODIES \n' +
|
|
18
|
-
Object.entries(requestBodies)
|
|
19
|
-
.map(([name, requestBody]) => {
|
|
20
|
-
const doc = (0, getResReqTypes_1.getDocs)(requestBody);
|
|
21
|
-
const type = (0, getResReqTypes_1.getResReqTypes)([['', requestBody]]);
|
|
22
|
-
const isEmptyInterface = type === '{}';
|
|
23
|
-
if (isEmptyInterface) {
|
|
24
|
-
return `export type ${(0, case_1.pascal)(name)}RequestBody = ${type}`;
|
|
25
|
-
}
|
|
26
|
-
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
27
|
-
return `${doc}export type ${(0, case_1.pascal)(name)}RequestBody = ${type}`;
|
|
28
|
-
}
|
|
29
|
-
else {
|
|
30
|
-
return `${doc}export type ${(0, case_1.pascal)(name)}RequestBody = ${type};`;
|
|
31
|
-
}
|
|
32
|
-
})
|
|
33
|
-
.join('\n\n') +
|
|
34
|
-
'\n');
|
|
35
|
-
};
|
|
36
|
-
exports.generateRequestBodiesDefinition = generateRequestBodiesDefinition;
|
|
@@ -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,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;
|
|
@@ -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
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { pascal } from 'case';
|
|
2
|
-
import isEmpty from 'lodash/isEmpty';
|
|
3
|
-
import { getDocs, getResReqTypes } from './getResReqTypes';
|
|
4
|
-
/**
|
|
5
|
-
* Extract all types from #/components/requestBodies
|
|
6
|
-
*/
|
|
7
|
-
export const generateRequestBodiesDefinition = (requestBodies = {}) => {
|
|
8
|
-
if (isEmpty(requestBodies)) {
|
|
9
|
-
return '';
|
|
10
|
-
}
|
|
11
|
-
return ('\n // REQUEST BODIES \n' +
|
|
12
|
-
Object.entries(requestBodies)
|
|
13
|
-
.map(([name, requestBody]) => {
|
|
14
|
-
const doc = getDocs(requestBody);
|
|
15
|
-
const type = getResReqTypes([['', requestBody]]);
|
|
16
|
-
const isEmptyInterface = type === '{}';
|
|
17
|
-
if (isEmptyInterface) {
|
|
18
|
-
return `export type ${pascal(name)}RequestBody = ${type}`;
|
|
19
|
-
}
|
|
20
|
-
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
21
|
-
return `${doc}export type ${pascal(name)}RequestBody = ${type}`;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
return `${doc}export type ${pascal(name)}RequestBody = ${type};`;
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
.join('\n\n') +
|
|
28
|
-
'\n');
|
|
29
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { pascal } from 'case';
|
|
2
|
-
import isEmpty from 'lodash/isEmpty';
|
|
3
|
-
import { getDocs, getResReqTypes } from './getResReqTypes';
|
|
4
|
-
/**
|
|
5
|
-
* Extract all types from #/components/responses
|
|
6
|
-
*/
|
|
7
|
-
const generateResponsesDefinition = (responses = {}) => {
|
|
8
|
-
if (isEmpty(responses)) {
|
|
9
|
-
return '';
|
|
10
|
-
}
|
|
11
|
-
return ('\n // RESPONSES \n' +
|
|
12
|
-
Object.entries(responses)
|
|
13
|
-
.map(([name, response]) => {
|
|
14
|
-
const doc = getDocs(response);
|
|
15
|
-
const type = getResReqTypes([['', response]]);
|
|
16
|
-
const isEmptyInterface = type === '{}';
|
|
17
|
-
if (isEmptyInterface) {
|
|
18
|
-
return `export type RQ${pascal(name)}Response = ${type}`;
|
|
19
|
-
}
|
|
20
|
-
else if (type.includes('{') && !type.includes('|') && !type.includes('&')) {
|
|
21
|
-
return `${doc}export type RQ${pascal(name)}Response = ${type}`;
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
return `${doc}export type RQ${pascal(name)}Response = ${type};`;
|
|
25
|
-
}
|
|
26
|
-
})
|
|
27
|
-
.join('\n\n') +
|
|
28
|
-
'\n');
|
|
29
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { pascal } from 'case';
|
|
2
|
-
import isEmpty from 'lodash/isEmpty';
|
|
3
|
-
import { formatDescription, getScalar, isReference, resolveValue } from './getResReqTypes';
|
|
4
|
-
/**
|
|
5
|
-
* Generate the interface string
|
|
6
|
-
*/
|
|
7
|
-
const generateInterface = (name, schema) => {
|
|
8
|
-
const scalar = getScalar(schema);
|
|
9
|
-
return `
|
|
10
|
-
${formatDescription(schema.description)}
|
|
11
|
-
export type ${pascal(name)} = ${scalar}
|
|
12
|
-
`;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Extract all types from #/components/schemas
|
|
16
|
-
*/
|
|
17
|
-
export const generateSchemasDefinition = (schemas = {}) => {
|
|
18
|
-
if (isEmpty(schemas)) {
|
|
19
|
-
return '';
|
|
20
|
-
}
|
|
21
|
-
return ('\n // SCEHMAS' +
|
|
22
|
-
Object.entries(schemas)
|
|
23
|
-
.map(([name, schema]) => !isReference(schema) &&
|
|
24
|
-
(!schema.type || schema.type === 'object') &&
|
|
25
|
-
!schema.allOf &&
|
|
26
|
-
!schema.oneOf &&
|
|
27
|
-
!isReference(schema) &&
|
|
28
|
-
!schema.nullable
|
|
29
|
-
? generateInterface(name, schema)
|
|
30
|
-
: `${formatDescription(isReference(schema) ? undefined : schema.description)} type ${pascal(name)} = ${resolveValue(schema)};`)
|
|
31
|
-
.join('\n\n') +
|
|
32
|
-
'\n');
|
|
33
|
-
};
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { pascal } from 'case';
|
|
2
|
-
import { uniq } from 'lodash';
|
|
3
|
-
import isEmpty from 'lodash/isEmpty';
|
|
4
|
-
export const getDocs = (data) => isReference(data) ? '' : formatDescription(data.description);
|
|
5
|
-
/**
|
|
6
|
-
* Discriminator helper for `ReferenceObject`
|
|
7
|
-
*/
|
|
8
|
-
export const isReference = (property) => {
|
|
9
|
-
return Boolean(property.$ref);
|
|
10
|
-
};
|
|
11
|
-
/**
|
|
12
|
-
* Return the typescript equivalent of open-api data type
|
|
13
|
-
*/
|
|
14
|
-
export const getScalar = (item) => {
|
|
15
|
-
const nullable = item.nullable ? ' | null' : '';
|
|
16
|
-
switch (item.type) {
|
|
17
|
-
case 'number':
|
|
18
|
-
case 'integer':
|
|
19
|
-
return 'number' + nullable;
|
|
20
|
-
case 'boolean':
|
|
21
|
-
return 'boolean' + nullable;
|
|
22
|
-
case 'array':
|
|
23
|
-
return getArray(item) + nullable;
|
|
24
|
-
case 'string':
|
|
25
|
-
return (item.enum ? `"${item.enum.join(`" | "`)}"` : 'string') + nullable;
|
|
26
|
-
case 'object':
|
|
27
|
-
default:
|
|
28
|
-
return getObject(item) + nullable;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
/**
|
|
32
|
-
* Return the output type from the $ref
|
|
33
|
-
*/
|
|
34
|
-
const getRef = ($ref) => {
|
|
35
|
-
if ($ref.startsWith('#/components/schemas')) {
|
|
36
|
-
return pascal($ref.replace('#/components/schemas/', ''));
|
|
37
|
-
}
|
|
38
|
-
else if ($ref.startsWith('#/components/responses')) {
|
|
39
|
-
return pascal($ref.replace('#/components/responses/', '')) + 'Response';
|
|
40
|
-
}
|
|
41
|
-
else if ($ref.startsWith('#/components/parameters')) {
|
|
42
|
-
return pascal($ref.replace('#/components/parameters/', '')) + 'Parameter';
|
|
43
|
-
}
|
|
44
|
-
else if ($ref.startsWith('#/components/requestBodies')) {
|
|
45
|
-
return pascal($ref.replace('#/components/requestBodies/', '')) + 'RequestBody';
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
throw new Error('This library only resolve $ref that are include into `#/components/*` for now');
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Return the output type from an array
|
|
53
|
-
*/
|
|
54
|
-
const getArray = (item) => {
|
|
55
|
-
if (item.items) {
|
|
56
|
-
if (!isReference(item.items) && (item.items.oneOf || item.items.allOf || item.items.enum)) {
|
|
57
|
-
return `(${resolveValue(item.items)})[]`;
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
return `${resolveValue(item.items)}[]`;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
throw new Error('All arrays must have an `items` key define');
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
/**
|
|
68
|
-
* Return the output type from an object
|
|
69
|
-
*/
|
|
70
|
-
const getObject = (item) => {
|
|
71
|
-
if (isReference(item)) {
|
|
72
|
-
return getRef(item.$ref);
|
|
73
|
-
}
|
|
74
|
-
if (item.allOf) {
|
|
75
|
-
return item.allOf.map(resolveValue).join(' & ');
|
|
76
|
-
}
|
|
77
|
-
if (item.oneOf) {
|
|
78
|
-
return item.oneOf.map(resolveValue).join(' | ');
|
|
79
|
-
}
|
|
80
|
-
if (!item.type && !item.properties && !item.additionalProperties) {
|
|
81
|
-
return '{}';
|
|
82
|
-
}
|
|
83
|
-
// Free form object (https://swagger.io/docs/specification/data-models/data-types/#free-form)
|
|
84
|
-
if (item.type === 'object' &&
|
|
85
|
-
!item.properties &&
|
|
86
|
-
(!item.additionalProperties || item.additionalProperties === true || isEmpty(item.additionalProperties))) {
|
|
87
|
-
return '{[key: string]: any}';
|
|
88
|
-
}
|
|
89
|
-
const IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
90
|
-
// Consolidation of item.properties & item.additionalProperties
|
|
91
|
-
let output = '{\n';
|
|
92
|
-
if (item.properties) {
|
|
93
|
-
output += Object.entries(item.properties)
|
|
94
|
-
.map(([key, prop]) => {
|
|
95
|
-
const doc = getDocs(prop);
|
|
96
|
-
const isRequired = (item.required || []).includes(key);
|
|
97
|
-
const processedKey = IdentifierRegexp.test(key) ? key : `"${key}"`;
|
|
98
|
-
return `${doc}\n${processedKey}${isRequired ? '' : '?'}: ${resolveValue(prop)};`;
|
|
99
|
-
})
|
|
100
|
-
.join('\n');
|
|
101
|
-
}
|
|
102
|
-
if (item.additionalProperties) {
|
|
103
|
-
if (item.properties) {
|
|
104
|
-
output += '\n';
|
|
105
|
-
}
|
|
106
|
-
output += `} & { [key: string]: ${item.additionalProperties === true ? 'any' : resolveValue(item.additionalProperties)}`;
|
|
107
|
-
}
|
|
108
|
-
if (item.properties || item.additionalProperties) {
|
|
109
|
-
if (output === '{\n') {
|
|
110
|
-
return '{}';
|
|
111
|
-
}
|
|
112
|
-
return output + '\n}';
|
|
113
|
-
}
|
|
114
|
-
return item.type === 'object' ? '{[key: string]: any}' : 'any';
|
|
115
|
-
};
|
|
116
|
-
/**
|
|
117
|
-
* Resolve the value of a schema object to a proper type definition.
|
|
118
|
-
*/
|
|
119
|
-
export const resolveValue = (schema) => isReference(schema) ? getRef(schema.$ref) : getScalar(schema);
|
|
120
|
-
/**
|
|
121
|
-
* Format a description to code documentation.
|
|
122
|
-
*/
|
|
123
|
-
export const formatDescription = (description, tabSize = 0) => description
|
|
124
|
-
? `/**\n${description
|
|
125
|
-
.split('\n')
|
|
126
|
-
.map((i) => `${' '.repeat(tabSize)} * ${i}`)
|
|
127
|
-
.join('\n')}\n${' '.repeat(tabSize)} */${' '.repeat(tabSize)}`
|
|
128
|
-
: '';
|
|
129
|
-
/**
|
|
130
|
-
* Extract responses / request types from open-api specs
|
|
131
|
-
*/
|
|
132
|
-
export const getResReqTypes = (responsesOrRequests) => uniq(responsesOrRequests.map(([_, res]) => {
|
|
133
|
-
if (!res) {
|
|
134
|
-
return 'void';
|
|
135
|
-
}
|
|
136
|
-
if (isReference(res)) {
|
|
137
|
-
return getRef(res.$ref);
|
|
138
|
-
}
|
|
139
|
-
if (res.content) {
|
|
140
|
-
for (let contentType of Object.keys(res.content)) {
|
|
141
|
-
if (contentType.startsWith('application/json') ||
|
|
142
|
-
contentType.startsWith('application/octet-stream')) {
|
|
143
|
-
const schema = res.content[contentType].schema;
|
|
144
|
-
return resolveValue(schema);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return 'void';
|
|
148
|
-
}
|
|
149
|
-
return 'void';
|
|
150
|
-
})).join(' | ');
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import set from 'lodash/set';
|
|
2
|
-
import { generateQueryHooks } from './generateHooks';
|
|
3
|
-
import { generateImports } from './generateImports';
|
|
4
|
-
import { isReference } from './getResReqTypes';
|
|
5
|
-
/**
|
|
6
|
-
* Propagate every `discriminator.propertyName` mapping to the original ref
|
|
7
|
-
*
|
|
8
|
-
* Note: This method directly mutate the `specs` object.
|
|
9
|
-
*/
|
|
10
|
-
const resolveDiscriminator = (specs) => {
|
|
11
|
-
if (specs.components && specs.components.schemas) {
|
|
12
|
-
Object.values(specs.components.schemas).forEach((schema) => {
|
|
13
|
-
if (isReference(schema) || !schema.discriminator || !schema.discriminator.mapping) {
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const { mapping, propertyName } = schema.discriminator;
|
|
17
|
-
Object.entries(mapping).forEach(([name, ref]) => {
|
|
18
|
-
if (!ref.startsWith('#/components/schemas/')) {
|
|
19
|
-
throw new Error('Discriminator mapping outside of `#/components/schemas` is not supported');
|
|
20
|
-
}
|
|
21
|
-
set(specs, `components.schemas.${ref.slice('#/components/schemas/'.length)}.properties.${propertyName}.enum`, [name]);
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
export const importOpenApi = async ({ specs, apiDirectory, queryClientDir, headerFilters = [], }) => {
|
|
27
|
-
const operationIds = [];
|
|
28
|
-
resolveDiscriminator(specs);
|
|
29
|
-
const imports = generateImports({ apiDirectory, queryClientDir });
|
|
30
|
-
const hooks = generateQueryHooks(specs, operationIds, headerFilters);
|
|
31
|
-
return imports + hooks;
|
|
32
|
-
};
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import { readFileSync, writeFileSync, readdir, mkdirSync } from 'fs';
|
|
3
|
-
import { join, parse } from 'path';
|
|
4
|
-
import { convertSwaggerFile } from './convertSwaggerFile';
|
|
5
|
-
import { generateQueryHooks } from './generateHooks';
|
|
6
|
-
import { generateSchemas } from './generateSchemas';
|
|
7
|
-
export function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, queryClientDir, headerFilters, }) {
|
|
8
|
-
readdir(sourceDirectory, function (err, filenames) {
|
|
9
|
-
if (err) {
|
|
10
|
-
console.log(err);
|
|
11
|
-
throw err;
|
|
12
|
-
}
|
|
13
|
-
filenames.map(async (filename) => {
|
|
14
|
-
const data = readFileSync(join(process.cwd(), sourceDirectory + '/' + filename), 'utf-8');
|
|
15
|
-
const { ext } = parse(sourceDirectory + '/' + filename);
|
|
16
|
-
const format = ['.yaml', '.yml'].includes(ext.toLowerCase()) ? 'yaml' : 'json';
|
|
17
|
-
try {
|
|
18
|
-
const name = filename.split('.')[0];
|
|
19
|
-
const hooksName = `useQueries${filename.split('.')[0]}`;
|
|
20
|
-
const schemaName = `useQueries${filename.split('.')[0]}.schema`;
|
|
21
|
-
let spec = await convertSwaggerFile(data, format);
|
|
22
|
-
const operationIds = [];
|
|
23
|
-
mkdirSync(join(process.cwd(), `${exportDirectory}/${name}`), { recursive: true });
|
|
24
|
-
const hooks = generateQueryHooks({
|
|
25
|
-
spec,
|
|
26
|
-
schemaName,
|
|
27
|
-
operationIds,
|
|
28
|
-
headerFilters,
|
|
29
|
-
apiDirectory,
|
|
30
|
-
queryClientDir,
|
|
31
|
-
});
|
|
32
|
-
writeFileSync(join(process.cwd(), `${exportDirectory}/${name}/${hooksName}.tsx`), hooks);
|
|
33
|
-
const schemas = generateSchemas(spec);
|
|
34
|
-
writeFileSync(join(process.cwd(), `${exportDirectory}/${name}/${schemaName}.tsx`), schemas);
|
|
35
|
-
console.log(chalk.green(`🎉 [${filename}] Your OpenAPI spec has been converted into react query hooks`));
|
|
36
|
-
}
|
|
37
|
-
catch (error) {
|
|
38
|
-
console.log(chalk.red(`[${filename}]:`), chalk.red(error));
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import set from 'lodash/set';
|
|
2
|
-
import { isReference } from './getResReqTypes';
|
|
3
|
-
/**
|
|
4
|
-
* Propagate every `discriminator.propertyName` mapping to the original ref
|
|
5
|
-
*
|
|
6
|
-
* Note: This method directly mutate the `specs` object.
|
|
7
|
-
*/
|
|
8
|
-
export const resolveDiscriminator = (specs) => {
|
|
9
|
-
if (specs.components && specs.components.schemas) {
|
|
10
|
-
Object.values(specs.components.schemas).forEach((schema) => {
|
|
11
|
-
if (isReference(schema) || !schema.discriminator || !schema.discriminator.mapping) {
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
const { mapping, propertyName } = schema.discriminator;
|
|
15
|
-
Object.entries(mapping).forEach(([name, ref]) => {
|
|
16
|
-
if (!ref.startsWith('#/components/schemas/')) {
|
|
17
|
-
throw new Error('Discriminator mapping outside of `#/components/schemas` is not supported');
|
|
18
|
-
}
|
|
19
|
-
console.log('HELLO');
|
|
20
|
-
set(specs, `components.schemas.${ref.slice('#/components/schemas/'.length)}.properties.${propertyName}.enum`, [name]);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
};
|
package/lib/esm/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|