react-query-lightbase-codegen 0.3.0 → 0.5.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/lib/cjs/convertSwaggerFile.js +32 -0
- package/lib/cjs/generateHooks.js +401 -0
- package/lib/cjs/generateImports.js +31 -0
- package/lib/cjs/generateRequestBodiesDefinition.js +36 -0
- package/lib/cjs/generateResponsesDefinition.js +36 -0
- package/lib/cjs/generateSchemas.js +103 -0
- package/lib/cjs/generateSchemasDefinition.js +40 -0
- package/lib/cjs/getResReqTypes.js +162 -0
- package/lib/cjs/import-open-api.js +10 -1005
- package/lib/cjs/importSpecs.js +87 -0
- package/lib/cjs/index.js +5 -3
- package/lib/cjs/react-query-codegen-import.js +20 -12
- package/lib/cjs/resolveDiscriminator.js +31 -0
- package/lib/cjs/utils.js +168 -0
- package/lib/esm/convertSwaggerFile.js +25 -0
- package/lib/esm/generateHooks.js +394 -0
- package/lib/esm/generateImports.js +27 -0
- package/lib/esm/generateRequestBodiesDefinition.js +29 -0
- package/lib/esm/generateResponsesDefinition.js +29 -0
- package/lib/esm/generateSchemas.js +96 -0
- package/lib/esm/generateSchemasDefinition.js +33 -0
- package/lib/esm/getResReqTypes.js +150 -0
- package/lib/esm/import-open-api.js +8 -988
- package/lib/esm/importSpecs.js +80 -0
- package/lib/esm/index.js +3 -2
- package/lib/esm/react-query-codegen-import.js +21 -13
- package/lib/esm/resolveDiscriminator.js +24 -0
- package/lib/esm/utils.js +156 -0
- package/package.json +17 -15
|
@@ -0,0 +1,150 @@
|
|
|
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(' | ');
|