react-query-lightbase-codegen 1.0.0 → 1.0.1
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/commonjs/convertSwaggerFile.js +37 -0
- package/lib/commonjs/convertSwaggerFile.js.map +1 -0
- package/lib/commonjs/generateHooks.js +492 -0
- package/lib/commonjs/generateHooks.js.map +1 -0
- package/lib/commonjs/generateImports.js +48 -0
- package/lib/commonjs/generateImports.js.map +1 -0
- package/lib/commonjs/generateSchemas.js +119 -0
- package/lib/commonjs/generateSchemas.js.map +1 -0
- package/lib/commonjs/importSpecs.js +117 -0
- package/lib/commonjs/importSpecs.js.map +1 -0
- package/lib/commonjs/index.js +22 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/utils.js +212 -0
- package/lib/commonjs/utils.js.map +1 -0
- package/lib/{cjs → src}/convertSwaggerFile.js +7 -10
- package/lib/src/generateHooks.js +249 -0
- package/lib/src/generateImports.js +29 -0
- package/lib/src/generateSchemas.js +108 -0
- package/lib/src/importSpecs.js +134 -0
- package/lib/src/index.js +7 -0
- package/lib/{cjs → src}/utils.js +40 -36
- package/lib/typescript/convertSwaggerFile.d.ts +5 -0
- package/lib/typescript/generateHooks.d.ts +25 -0
- package/lib/typescript/generateImports.d.ts +7 -0
- package/lib/typescript/generateSchemas.d.ts +7 -0
- package/lib/typescript/importSpecs.d.ts +15 -0
- package/lib/{esm/index.js → typescript/index.d.ts} +0 -0
- package/lib/typescript/utils.d.ts +24 -0
- package/package.json +26 -6
- package/src/convertSwaggerFile.ts +25 -0
- package/src/generateHooks.ts +490 -0
- package/src/generateImports.ts +41 -0
- package/src/generateSchemas.ts +114 -0
- package/src/importSpecs.ts +110 -0
- package/src/index.ts +3 -0
- package/src/utils.ts +190 -0
- package/lib/cjs/generateHooks.js +0 -401
- package/lib/cjs/generateImports.js +0 -31
- package/lib/cjs/generateSchemas.js +0 -103
- package/lib/cjs/importSpecs.js +0 -87
- package/lib/cjs/index.js +0 -7
- package/lib/esm/convertSwaggerFile.js +0 -25
- package/lib/esm/generateHooks.js +0 -394
- package/lib/esm/generateImports.js +0 -27
- package/lib/esm/generateSchemas.js +0 -96
- package/lib/esm/importSpecs.js +0 -80
- package/lib/esm/utils.js +0 -156
package/lib/{cjs → src}/utils.js
RENAMED
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
2
|
+
exports.__esModule = true;
|
|
6
3
|
exports.getResReqTypes = exports.formatDescription = exports.resolveValue = exports.getScalar = exports.isReference = exports.getDocs = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
var lodash_1 = require("lodash");
|
|
5
|
+
var uniq = lodash_1["default"].uniq, isEmpty = lodash_1["default"].isEmpty;
|
|
6
|
+
var case_1 = require("case");
|
|
7
|
+
var pascal = case_1["default"].pascal;
|
|
8
|
+
var getDocs = function (data) {
|
|
9
|
+
return (0, exports.isReference)(data) ? '' : (0, exports.formatDescription)(data.description);
|
|
10
|
+
};
|
|
12
11
|
exports.getDocs = getDocs;
|
|
13
12
|
/**
|
|
14
13
|
* Discriminator helper for `ReferenceObject`
|
|
15
14
|
*/
|
|
16
|
-
|
|
15
|
+
var isReference = function (property) {
|
|
17
16
|
return Boolean(property.$ref);
|
|
18
17
|
};
|
|
19
18
|
exports.isReference = isReference;
|
|
20
19
|
/**
|
|
21
20
|
* Return the typescript equivalent of open-api data type
|
|
22
21
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
var getScalar = function (item) {
|
|
23
|
+
var nullable = item.nullable ? ' | null' : '';
|
|
25
24
|
switch (item.type) {
|
|
26
25
|
case 'number':
|
|
27
26
|
case 'integer':
|
|
@@ -31,7 +30,7 @@ const getScalar = (item) => {
|
|
|
31
30
|
case 'array':
|
|
32
31
|
return getArray(item) + nullable;
|
|
33
32
|
case 'string':
|
|
34
|
-
return (item
|
|
33
|
+
return (item["enum"] ? "\"".concat(item["enum"].join("\" | \""), "\"") : 'string') + nullable;
|
|
35
34
|
case 'object':
|
|
36
35
|
default:
|
|
37
36
|
return getObject(item) + nullable;
|
|
@@ -41,7 +40,7 @@ exports.getScalar = getScalar;
|
|
|
41
40
|
/**
|
|
42
41
|
* Return the output type from the $ref
|
|
43
42
|
*/
|
|
44
|
-
|
|
43
|
+
var getRef = function ($ref) {
|
|
45
44
|
if ($ref.startsWith('#/components/schemas')) {
|
|
46
45
|
return pascal($ref.replace('#/components/schemas/', ''));
|
|
47
46
|
}
|
|
@@ -61,13 +60,13 @@ const getRef = ($ref) => {
|
|
|
61
60
|
/**
|
|
62
61
|
* Return the output type from an array
|
|
63
62
|
*/
|
|
64
|
-
|
|
63
|
+
var getArray = function (item) {
|
|
65
64
|
if (item.items) {
|
|
66
|
-
if (!(0, exports.isReference)(item.items) && (item.items.oneOf || item.items.allOf || item.items
|
|
67
|
-
return
|
|
65
|
+
if (!(0, exports.isReference)(item.items) && (item.items.oneOf || item.items.allOf || item.items["enum"])) {
|
|
66
|
+
return "(".concat((0, exports.resolveValue)(item.items), ")[]");
|
|
68
67
|
}
|
|
69
68
|
else {
|
|
70
|
-
return
|
|
69
|
+
return "".concat((0, exports.resolveValue)(item.items), "[]");
|
|
71
70
|
}
|
|
72
71
|
}
|
|
73
72
|
else {
|
|
@@ -77,7 +76,7 @@ const getArray = (item) => {
|
|
|
77
76
|
/**
|
|
78
77
|
* Return the output type from an object
|
|
79
78
|
*/
|
|
80
|
-
|
|
79
|
+
var getObject = function (item) {
|
|
81
80
|
if ((0, exports.isReference)(item)) {
|
|
82
81
|
return getRef(item.$ref);
|
|
83
82
|
}
|
|
@@ -96,16 +95,17 @@ const getObject = (item) => {
|
|
|
96
95
|
(!item.additionalProperties || item.additionalProperties === true || isEmpty(item.additionalProperties))) {
|
|
97
96
|
return '{[key: string]: any}';
|
|
98
97
|
}
|
|
99
|
-
|
|
98
|
+
var IdentifierRegexp = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
|
|
100
99
|
// Consolidation of item.properties & item.additionalProperties
|
|
101
|
-
|
|
100
|
+
var output = '{\n';
|
|
102
101
|
if (item.properties) {
|
|
103
102
|
output += Object.entries(item.properties)
|
|
104
|
-
.map((
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
103
|
+
.map(function (_a) {
|
|
104
|
+
var key = _a[0], prop = _a[1];
|
|
105
|
+
var doc = (0, exports.getDocs)(prop);
|
|
106
|
+
var isRequired = (item.required || []).includes(key);
|
|
107
|
+
var processedKey = IdentifierRegexp.test(key) ? key : "\"".concat(key, "\"");
|
|
108
|
+
return "".concat(doc, "\n").concat(processedKey).concat(isRequired ? '' : '?', ": ").concat((0, exports.resolveValue)(prop), ";");
|
|
109
109
|
})
|
|
110
110
|
.join('');
|
|
111
111
|
}
|
|
@@ -113,7 +113,7 @@ const getObject = (item) => {
|
|
|
113
113
|
if (item.properties) {
|
|
114
114
|
output += '\n';
|
|
115
115
|
}
|
|
116
|
-
output +=
|
|
116
|
+
output += "} & { [key: string]: ".concat(item.additionalProperties === true ? 'any' : (0, exports.resolveValue)(item.additionalProperties));
|
|
117
117
|
}
|
|
118
118
|
if (item.properties || item.additionalProperties) {
|
|
119
119
|
if (output === '{\n') {
|
|
@@ -126,26 +126,29 @@ const getObject = (item) => {
|
|
|
126
126
|
/**
|
|
127
127
|
* Resolve the value of a schema object to a proper type definition.
|
|
128
128
|
*/
|
|
129
|
-
|
|
129
|
+
var resolveValue = function (schema) {
|
|
130
|
+
return (0, exports.isReference)(schema) ? getRef(schema.$ref) : (0, exports.getScalar)(schema);
|
|
131
|
+
};
|
|
130
132
|
exports.resolveValue = resolveValue;
|
|
131
133
|
/**
|
|
132
134
|
* Format a description to code documentation.
|
|
133
135
|
*/
|
|
134
|
-
|
|
136
|
+
var formatDescription = function (description) {
|
|
135
137
|
if (!description) {
|
|
136
138
|
return '';
|
|
137
139
|
}
|
|
138
|
-
return
|
|
140
|
+
return "\n/**\n".concat(description
|
|
139
141
|
.split('\n')
|
|
140
|
-
.map((i)
|
|
141
|
-
.join('\n')
|
|
142
|
+
.map(function (i) { return "* ".concat(i); })
|
|
143
|
+
.join('\n'), "\n */");
|
|
142
144
|
};
|
|
143
145
|
exports.formatDescription = formatDescription;
|
|
144
146
|
/**
|
|
145
147
|
* Extract responses / request types from open-api specs
|
|
146
148
|
*/
|
|
147
|
-
|
|
148
|
-
return uniq(responsesOrRequests.map((
|
|
149
|
+
var getResReqTypes = function (responsesOrRequests) {
|
|
150
|
+
return uniq(responsesOrRequests.map(function (_a) {
|
|
151
|
+
var _ = _a[0], res = _a[1];
|
|
149
152
|
if (!res) {
|
|
150
153
|
return;
|
|
151
154
|
}
|
|
@@ -153,10 +156,11 @@ const getResReqTypes = (responsesOrRequests) => {
|
|
|
153
156
|
return getRef(res.$ref);
|
|
154
157
|
}
|
|
155
158
|
if (res.content) {
|
|
156
|
-
for (
|
|
159
|
+
for (var _i = 0, _b = Object.keys(res.content); _i < _b.length; _i++) {
|
|
160
|
+
var contentType = _b[_i];
|
|
157
161
|
if (contentType.startsWith('application/json') ||
|
|
158
162
|
contentType.startsWith('application/octet-stream')) {
|
|
159
|
-
|
|
163
|
+
var schema = res.content[contentType].schema;
|
|
160
164
|
return (0, exports.resolveValue)(schema);
|
|
161
165
|
}
|
|
162
166
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ComponentsObject, OperationObject, ParameterObject, ReferenceObject } from 'openapi3-ts';
|
|
2
|
+
/**
|
|
3
|
+
* Generate a react-query component from openapi operation specs
|
|
4
|
+
*/
|
|
5
|
+
export declare const createHook: ({ operation, verb, route, operationIds, parameters, schemasComponents, headerFilters, overrides, }: {
|
|
6
|
+
operation: OperationObject;
|
|
7
|
+
verb: string;
|
|
8
|
+
route: string;
|
|
9
|
+
operationIds: string[];
|
|
10
|
+
parameters: (ReferenceObject | ParameterObject)[] | undefined;
|
|
11
|
+
schemasComponents?: ComponentsObject | undefined;
|
|
12
|
+
headerFilters?: string[] | undefined;
|
|
13
|
+
overrides?: Record<string, {
|
|
14
|
+
type: 'query';
|
|
15
|
+
} | {
|
|
16
|
+
type: 'mutation';
|
|
17
|
+
} | {
|
|
18
|
+
type: 'infiniteQuery';
|
|
19
|
+
infiniteQueryParm: string;
|
|
20
|
+
}> | undefined;
|
|
21
|
+
}) => {
|
|
22
|
+
implementation: string;
|
|
23
|
+
imports: string[];
|
|
24
|
+
queryImports: ("query" | "mutation" | "infiniteQuery")[];
|
|
25
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const generateImports: ({ schemaName, apiDirectory, queryClientDir, schemaImports, queryImports, }: {
|
|
2
|
+
apiDirectory: string;
|
|
3
|
+
queryClientDir: string;
|
|
4
|
+
schemaName: string;
|
|
5
|
+
schemaImports: string[];
|
|
6
|
+
queryImports: ('query' | 'mutation' | 'infiniteQuery')[];
|
|
7
|
+
}) => string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function importSpecs({ sourceDirectory, exportDirectory, apiDirectory, queryClientDir, headerFilters, overrides, }: {
|
|
2
|
+
sourceDirectory: string;
|
|
3
|
+
exportDirectory: string;
|
|
4
|
+
apiDirectory: string;
|
|
5
|
+
queryClientDir: string;
|
|
6
|
+
headerFilters?: string[];
|
|
7
|
+
overrides?: Record<string, {
|
|
8
|
+
type: 'query';
|
|
9
|
+
} | {
|
|
10
|
+
type: 'mutation';
|
|
11
|
+
} | {
|
|
12
|
+
type: 'infiniteQuery';
|
|
13
|
+
infiniteQueryParm: string;
|
|
14
|
+
}>;
|
|
15
|
+
}): void;
|
|
File without changes
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ReferenceObject, RequestBodyObject, ResponseObject, SchemaObject } from 'openapi3-ts';
|
|
2
|
+
export declare const getDocs: (data: ReferenceObject | {
|
|
3
|
+
description?: string;
|
|
4
|
+
}) => string;
|
|
5
|
+
/**
|
|
6
|
+
* Discriminator helper for `ReferenceObject`
|
|
7
|
+
*/
|
|
8
|
+
export declare const isReference: (property: any) => property is ReferenceObject;
|
|
9
|
+
/**
|
|
10
|
+
* Return the typescript equivalent of open-api data type
|
|
11
|
+
*/
|
|
12
|
+
export declare const getScalar: (item: SchemaObject) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Resolve the value of a schema object to a proper type definition.
|
|
15
|
+
*/
|
|
16
|
+
export declare const resolveValue: (schema: SchemaObject) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Format a description to code documentation.
|
|
19
|
+
*/
|
|
20
|
+
export declare const formatDescription: (description?: string) => string;
|
|
21
|
+
/**
|
|
22
|
+
* Extract responses / request types from open-api specs
|
|
23
|
+
*/
|
|
24
|
+
export declare const getResReqTypes: (responsesOrRequests: Array<[string, ResponseObject | ReferenceObject | RequestBodyObject]>) => string;
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
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.
|
|
4
|
+
"version": "1.0.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": "./src/index.js",
|
|
8
8
|
"files": [
|
|
9
|
-
"
|
|
9
|
+
"src",
|
|
10
|
+
"lib",
|
|
11
|
+
"!**/__tests__",
|
|
12
|
+
"!**/__fixtures__",
|
|
13
|
+
"!**/__mocks__"
|
|
10
14
|
],
|
|
11
15
|
"keywords": [
|
|
12
16
|
"rest",
|
|
@@ -36,13 +40,13 @@
|
|
|
36
40
|
"scripts": {
|
|
37
41
|
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
|
38
42
|
"tsc": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json",
|
|
39
|
-
"test": "
|
|
40
|
-
"prepare": "
|
|
43
|
+
"test": "node test/script.mjs && prettier --check ./test/generated/**/*.tsx --write && eslint ./test/generated/**/*.tsx --fix",
|
|
44
|
+
"prepare": "bob build"
|
|
41
45
|
},
|
|
42
46
|
"dependencies": {
|
|
43
47
|
"axios": "0.27.2",
|
|
44
48
|
"case": "1.6.3",
|
|
45
|
-
"chalk": "
|
|
49
|
+
"chalk": "4.1.2",
|
|
46
50
|
"js-yaml": "4.1.0",
|
|
47
51
|
"lodash": "4.17.21",
|
|
48
52
|
"openapi3-ts": "2.0.2",
|
|
@@ -71,7 +75,23 @@
|
|
|
71
75
|
"husky": "^8.0.1",
|
|
72
76
|
"prettier": "2.7.1",
|
|
73
77
|
"react": "17.0.2",
|
|
78
|
+
"react-native-builder-bob": "^0.18.3",
|
|
74
79
|
"semantic-release": "^19.0.3",
|
|
75
80
|
"typescript": "4.7.4"
|
|
76
|
-
}
|
|
81
|
+
},
|
|
82
|
+
"main": "lib/commonjs/index.js",
|
|
83
|
+
"react-native": "src/index.ts",
|
|
84
|
+
"types": "lib/typescript/src/index.d.ts",
|
|
85
|
+
"react-native-builder-bob": {
|
|
86
|
+
"source": "src",
|
|
87
|
+
"output": "lib",
|
|
88
|
+
"targets": [
|
|
89
|
+
"commonjs",
|
|
90
|
+
"typescript"
|
|
91
|
+
]
|
|
92
|
+
},
|
|
93
|
+
"eslintIgnore": [
|
|
94
|
+
"node_modules/",
|
|
95
|
+
"lib/"
|
|
96
|
+
]
|
|
77
97
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { OpenAPIObject } from 'openapi3-ts';
|
|
2
|
+
import swagger2openapi from 'swagger2openapi';
|
|
3
|
+
import yaml from 'js-yaml';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Import and parse the openapi spec from a yaml/json
|
|
7
|
+
*/
|
|
8
|
+
export const convertSwaggerFile = (data: string, extension: 'yaml' | 'json'): Promise<OpenAPIObject> => {
|
|
9
|
+
const schema = extension === 'yaml' ? yaml.load(data) : JSON.parse(data);
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
if (!schema.openapi || !schema.openapi.startsWith('3.')) {
|
|
12
|
+
swagger2openapi.convertObj(schema, {}, (err, convertedObj) => {
|
|
13
|
+
if (err) {
|
|
14
|
+
reject(err);
|
|
15
|
+
} else {
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
convertedObj.openapi.basePath = convertedObj.original.basePath;
|
|
18
|
+
resolve(convertedObj.openapi);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
} else {
|
|
22
|
+
resolve(schema);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
};
|