swaggie 0.6.7 → 0.6.9
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 +7 -3
- package/dist/gen/js/genTypes.js +25 -49
- package/dist/gen/js/support.js +2 -42
- package/package.json +11 -27
- package/templates/swr-axios/barrel.ejs +9 -0
- package/templates/swr-axios/baseClient.ejs +24 -0
- package/templates/swr-axios/client.ejs +19 -0
- package/templates/swr-axios/operation.ejs +58 -0
- package/templates/swr-axios/swrOperation.ejs +60 -0
- package/dist/codegen/common.js +0 -53
- package/dist/codegen/index.js +0 -31
- package/dist/codegen/models.js +0 -281
- package/dist/codegen/renderer.js +0 -42
- package/dist/schema.js +0 -41
- package/dist/swagger2openapi.d.js +0 -11
- package/dist/utils/index.js +0 -3
- package/dist/utils/queryModels.js +0 -57
- package/dist/utils/specLoader.js +0 -87
- package/dist/utils/types.js +0 -29
- package/dist/utils/utils.js +0 -59
package/README.md
CHANGED
|
@@ -4,15 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
# Swaggie
|
|
6
6
|
|
|
7
|
+

|
|
7
8
|

|
|
8
9
|
[](https://circleci.com/gh/yhnavein/swaggie)
|
|
9
|
-

|
|
10
10
|

|
|
11
11
|

|
|
12
|
-

|
|
13
12
|

|
|
14
13
|

|
|
15
14
|
|
|
15
|
+
<!--  -->
|
|
16
|
+
|
|
16
17
|
Generate ES6 or Typescript code from an OpenAPI 2.0 spec, so that accessing REST API resources from the client code is less error-prone, static-typed and just easier to use long-term.
|
|
17
18
|
|
|
18
19
|
You can take a look at the [Examples section](#example) down below.
|
|
@@ -88,6 +89,7 @@ The following templates are bundled with Swaggie:
|
|
|
88
89
|
|
|
89
90
|
```
|
|
90
91
|
axios Default template. Recommended for React / Vue / similar frameworks. Uses axios
|
|
92
|
+
swr-axios Template that embraces SRW for GET requests and as a fallback uses axios.
|
|
91
93
|
fetch Template similar to axios, but with fetch API instead. Recommended for React / Vue / similar frameworks
|
|
92
94
|
ng1 Template for Angular 1 (this is for the old one)
|
|
93
95
|
ng2 Template for Angular 2+ (uses HttpClient, InjectionTokens, etc)
|
|
@@ -183,7 +185,9 @@ You might wonder how to set up server to fully utilize Swaggie's features. For t
|
|
|
183
185
|
|
|
184
186
|
[ASP.NET Core + Nswag](./samples/dotnetcore/nswag/README.md)
|
|
185
187
|
|
|
186
|
-
|
|
188
|
+
[ASP.NET Core + Swashbuckle](./samples/dotnetcore/swashbuckle/README.md)
|
|
189
|
+
|
|
190
|
+
Server is not necessary to use Swaggie. Swaggie cares only about the JSON/yaml file with the Open API spec, but for your development purpose you might want to have a server that can serve this file automatically from the actual endpoints.
|
|
187
191
|
|
|
188
192
|
## Notes
|
|
189
193
|
|
package/dist/gen/js/genTypes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _lodash = require('lodash');
|
|
2
|
+
var _util = require('../util');
|
|
2
3
|
var _support = require('./support');
|
|
3
|
-
var _lodash = require('lodash');
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
function genTypes(
|
|
@@ -56,10 +56,9 @@ function renderTsType(name, def, options, typeToBeGeneric) {
|
|
|
56
56
|
|
|
57
57
|
const lines = [];
|
|
58
58
|
if (def.description) {
|
|
59
|
-
lines.push(
|
|
60
|
-
lines.push(_support.DOC + def.description.trim().replace(/\n/g, `\n${_support.DOC}${_support.SP}`));
|
|
61
|
-
lines.push(` */`);
|
|
59
|
+
lines.push(renderComment(def.description));
|
|
62
60
|
}
|
|
61
|
+
|
|
63
62
|
if (!!def['x-enumNames']) {
|
|
64
63
|
lines.push(renderXEnumType(name, def));
|
|
65
64
|
return lines;
|
|
@@ -90,8 +89,7 @@ function renderTsType(name, def, options, typeToBeGeneric) {
|
|
|
90
89
|
_util.join.call(void 0, lines, requiredPropLines);
|
|
91
90
|
_util.join.call(void 0, lines, optionalPropLines);
|
|
92
91
|
}
|
|
93
|
-
lines.push('}');
|
|
94
|
-
lines.push('');
|
|
92
|
+
lines.push('}\n');
|
|
95
93
|
return lines;
|
|
96
94
|
}
|
|
97
95
|
|
|
@@ -178,10 +176,11 @@ function renderTsInheritance(name, allOf, options) {
|
|
|
178
176
|
const ref = allOf[0];
|
|
179
177
|
const parentName = ref.$ref.split('/').pop();
|
|
180
178
|
const lines = renderTsType(name, allOf[1], options);
|
|
181
|
-
|
|
182
|
-
|
|
179
|
+
const interfaceLineIndex = lines.findIndex((l) => l.indexOf('export interface') === 0);
|
|
180
|
+
if (interfaceLineIndex > -1) {
|
|
181
|
+
// Let's replace generic interface definition with more specific one with inheritance info
|
|
182
|
+
lines[interfaceLineIndex] = `export interface ${name} extends ${parentName} {`;
|
|
183
183
|
}
|
|
184
|
-
lines.unshift(`export interface ${name} extends ${parentName} {`);
|
|
185
184
|
return lines;
|
|
186
185
|
}
|
|
187
186
|
|
|
@@ -198,48 +197,11 @@ function renderTsTypeProp(
|
|
|
198
197
|
type = type.replace(typeToBeGeneric, 'T');
|
|
199
198
|
}
|
|
200
199
|
if (info.description) {
|
|
201
|
-
lines.push(
|
|
202
|
-
lines.push(
|
|
203
|
-
`${_support.SP}${_support.DOC}` + (info.description || '').trim().replace(/\n/g, `\n${_support.SP}${_support.DOC}${_support.SP}`)
|
|
204
|
-
);
|
|
205
|
-
lines.push(`${_support.SP} */`);
|
|
200
|
+
lines.push(renderComment(info.desciption));
|
|
206
201
|
}
|
|
207
202
|
const req = required ? '' : '?';
|
|
208
|
-
lines.push(
|
|
209
|
-
return lines;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
function renderTypeDoc(name, def) {
|
|
213
|
-
if (def.allOf) {
|
|
214
|
-
return renderDocInheritance(name, def.allOf);
|
|
215
|
-
}
|
|
216
|
-
if (def.type !== 'object') {
|
|
217
|
-
console.warn(`Unable to render ${name} ${def.type}, skipping.`);
|
|
218
|
-
return [];
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
const lines = ['/**', `${_support.DOC}@typedef ${name}`];
|
|
222
|
-
const req = def.required || [];
|
|
223
|
-
const propLines = Object.keys(def.properties).map((prop) => {
|
|
224
|
-
const info = def.properties[prop];
|
|
225
|
-
const description = (info.description || '').trim().replace(/\n/g, `\n${_support.DOC}${_support.SP}`);
|
|
226
|
-
return `${_support.DOC}@property {${_support.getDocType.call(void 0, info)}} ${prop} ${description}`;
|
|
227
|
-
});
|
|
228
|
-
if (propLines.length) {
|
|
229
|
-
lines.push(`${_support.DOC}`);
|
|
230
|
-
}
|
|
231
|
-
_util.join.call(void 0, lines, propLines);
|
|
232
|
-
lines.push(' */');
|
|
233
|
-
lines.push('');
|
|
234
|
-
return lines;
|
|
235
|
-
}
|
|
203
|
+
lines.push(` ${prop}${req}: ${type};`);
|
|
236
204
|
|
|
237
|
-
function renderDocInheritance(name, allOf) {
|
|
238
|
-
verifyAllOf(name, allOf);
|
|
239
|
-
const ref = allOf[0];
|
|
240
|
-
const parentName = ref.$ref.split('/').pop();
|
|
241
|
-
const lines = renderTypeDoc(name, allOf[1]);
|
|
242
|
-
lines.splice(3, 0, `${_support.DOC}@extends ${parentName}`);
|
|
243
205
|
return lines;
|
|
244
206
|
}
|
|
245
207
|
|
|
@@ -277,3 +239,17 @@ function unwrapDefinitions(definitions) {
|
|
|
277
239
|
|
|
278
240
|
return result;
|
|
279
241
|
}
|
|
242
|
+
|
|
243
|
+
function renderComment(comment) {
|
|
244
|
+
if (!comment) {
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const commentLines = comment.split('\n');
|
|
249
|
+
|
|
250
|
+
if (commentLines.length === 1) {
|
|
251
|
+
return '// ' + comment.trim();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return ' /**\n' + commentLines.map((line) => ' * ' + line.trim()).join('\n') + '\n */';
|
|
255
|
+
} exports.renderComment = renderComment;
|
package/dist/gen/js/support.js
CHANGED
|
@@ -1,44 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
-
const DEFAULT_SP = ' '; exports.DEFAULT_SP = DEFAULT_SP;
|
|
3
|
-
let SP = exports.DEFAULT_SP; exports.SP = SP;
|
|
4
|
-
|
|
5
|
-
function formatDocDescription(description) {
|
|
6
|
-
return (description || '').trim().replace(/\n/g, `\n${exports.DOC}${exports.SP}`);
|
|
7
|
-
} exports.formatDocDescription = formatDocDescription;
|
|
8
|
-
|
|
9
|
-
function getDocType(param, options) {
|
|
10
|
-
if (!param) {
|
|
11
|
-
return 'object';
|
|
12
|
-
} else if (param.$ref) {
|
|
13
|
-
const type = param.$ref.split('/').pop();
|
|
14
|
-
return `module:${type}`;
|
|
15
|
-
} else if (param.schema) {
|
|
16
|
-
return getDocType(param.schema, options);
|
|
17
|
-
} else if (param['x-schema']) {
|
|
18
|
-
return getDocType(param['x-schema'], options);
|
|
19
|
-
} else if (param.type === 'array') {
|
|
20
|
-
if (param.items.type) {
|
|
21
|
-
return `${getDocType(param.items, options)}[]`;
|
|
22
|
-
} else if (param.items.$ref) {
|
|
23
|
-
const type = param.items.$ref.split('/').pop();
|
|
24
|
-
return `module:${type}[]`;
|
|
25
|
-
} else {
|
|
26
|
-
return 'object[]';
|
|
27
|
-
}
|
|
28
|
-
} else if (param.type === 'integer' || param.type === 'number') {
|
|
29
|
-
return 'number';
|
|
30
|
-
} else if (param.type === 'string' && (param.format === 'date-time' || param.format === 'date')) {
|
|
31
|
-
return options && options.dateFormat === 'string' ? 'string' : 'Date';
|
|
32
|
-
} else if (param.type === 'string') {
|
|
33
|
-
return 'string';
|
|
34
|
-
} else if (param.type === 'boolean') {
|
|
35
|
-
return 'boolean';
|
|
36
|
-
} else {
|
|
37
|
-
return 'object';
|
|
38
|
-
}
|
|
39
|
-
} exports.getDocType = getDocType;
|
|
40
|
-
|
|
41
|
-
function getTSParamType(param, options) {
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function getTSParamType(param, options) {
|
|
42
2
|
const unknownType = options.preferAny ? 'any' : 'unknown';
|
|
43
3
|
|
|
44
4
|
if (!param) {
|
|
@@ -63,7 +23,7 @@
|
|
|
63
23
|
if (param.items.enum) {
|
|
64
24
|
return `(${getTSParamType(param.items, options)})[]`;
|
|
65
25
|
} else {
|
|
66
|
-
return
|
|
26
|
+
return getTSParamType(param.items, options) + '[]';
|
|
67
27
|
}
|
|
68
28
|
} else if (param.items.$ref) {
|
|
69
29
|
const type = param.items.$ref.split('/').pop();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swaggie",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.9",
|
|
4
4
|
"description": "Generate ES6 or TypeScript service integration code from an OpenAPI spec",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Piotr Dabrowski",
|
|
@@ -25,8 +25,7 @@
|
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "sucrase ./src -d ./dist --transforms typescript,imports && npm run rm-tests",
|
|
27
27
|
"rm-tests": "find dist/ -name '*.spec.js' -type f -delete",
|
|
28
|
-
"test": "
|
|
29
|
-
"test:watch": "jest --watch"
|
|
28
|
+
"test": "mocha 'src/**/*.spec.ts'"
|
|
30
29
|
},
|
|
31
30
|
"files": [
|
|
32
31
|
"dist",
|
|
@@ -42,7 +41,7 @@
|
|
|
42
41
|
"codegen"
|
|
43
42
|
],
|
|
44
43
|
"dependencies": {
|
|
45
|
-
"commander": "^8.
|
|
44
|
+
"commander": "^8.3.0",
|
|
46
45
|
"ejs": "^3.1.6",
|
|
47
46
|
"js-yaml": "^4.1.0",
|
|
48
47
|
"lodash": "^4.17.21",
|
|
@@ -51,31 +50,16 @@
|
|
|
51
50
|
"node-fetch": "^2.6.3"
|
|
52
51
|
},
|
|
53
52
|
"devDependencies": {
|
|
54
|
-
"@
|
|
53
|
+
"@types/chai": "4.2.22",
|
|
55
54
|
"@types/ejs": "3.1.0",
|
|
56
|
-
"@types/
|
|
57
|
-
"@types/
|
|
58
|
-
"@types/lodash": "4.14.175",
|
|
55
|
+
"@types/js-yaml": "4.0.5",
|
|
56
|
+
"@types/lodash": "4.14.177",
|
|
59
57
|
"@types/mkdirp": "^1.0.2",
|
|
60
|
-
"@types/
|
|
58
|
+
"@types/mocha": "9.0.0",
|
|
61
59
|
"@types/node-fetch": "2.5.3",
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
|
|
66
|
-
"jest": {
|
|
67
|
-
"testRegex": "\\.(test|spec)\\.tsx?$",
|
|
68
|
-
"transform": {
|
|
69
|
-
"^.+\\.tsx?$": "@sucrase/jest-plugin"
|
|
70
|
-
},
|
|
71
|
-
"testPathIgnorePatterns": [
|
|
72
|
-
"<rootDir>/node_modules/",
|
|
73
|
-
"<rootDir>/dist/"
|
|
74
|
-
],
|
|
75
|
-
"testEnvironment": "node",
|
|
76
|
-
"watchPathIgnorePatterns": [
|
|
77
|
-
"<rootDir>/node_modules/",
|
|
78
|
-
"<rootDir>/dist/"
|
|
79
|
-
]
|
|
60
|
+
"chai": "4.3.4",
|
|
61
|
+
"mocha": "9.1.3",
|
|
62
|
+
"sucrase": "3.20.3",
|
|
63
|
+
"typescript": "4.5.2"
|
|
80
64
|
}
|
|
81
65
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
function serializeQueryParam(obj: any) {
|
|
3
|
+
if (obj === null || obj === undefined) return '';
|
|
4
|
+
if (obj instanceof Date) return obj.toJSON();
|
|
5
|
+
if (typeof obj !== 'object' || Array.isArray(obj)) return obj;
|
|
6
|
+
return Object.keys(obj)
|
|
7
|
+
.reduce((a: any, b) => a.push(b + '=' + obj[b]) && a, [])
|
|
8
|
+
.join('&');
|
|
9
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
//----------------------
|
|
4
|
+
// <auto-generated>
|
|
5
|
+
// Generated using Swaggie (https://github.com/yhnavein/swaggie)
|
|
6
|
+
// Please avoid doing any manual changes in this file
|
|
7
|
+
// </auto-generated>
|
|
8
|
+
//----------------------
|
|
9
|
+
// ReSharper disable InconsistentNaming
|
|
10
|
+
|
|
11
|
+
import Axios, { AxiosPromise, AxiosRequestConfig } from "axios";
|
|
12
|
+
import useSWR, { SWRConfiguration, Key } from 'swr';
|
|
13
|
+
|
|
14
|
+
export const axios = Axios.create({
|
|
15
|
+
baseURL: '<%- baseUrl || '' -%>',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
interface SwrConfig extends SWRConfiguration {
|
|
19
|
+
/* Custom key for SWR. You don't have to worry about this as by default it's the URL. You can use standard SWR Key here if you need more flexibility. */
|
|
20
|
+
key?: Key;
|
|
21
|
+
|
|
22
|
+
/* Configuration for axios fetcher */
|
|
23
|
+
axios?: AxiosRequestConfig;
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const <%- varName -%>Client = {
|
|
2
|
+
<% operations.forEach(function(operation) { %>
|
|
3
|
+
<%- include('operation', operation); %>
|
|
4
|
+
<% }); %>
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
<% var getOperations = operations.filter(function(o) { return o.method == 'GET'; });
|
|
8
|
+
if(getOperations.length > 0) { %>
|
|
9
|
+
<% getOperations.forEach(function(operation) {
|
|
10
|
+
var opName = operation.name;
|
|
11
|
+
if(opName.toLowerCase().startsWith("get")) {
|
|
12
|
+
opName = opName.substring(3);
|
|
13
|
+
}
|
|
14
|
+
opName[0] = opName[0].toUpperCase();
|
|
15
|
+
var customName = "use" + clientName + opName;
|
|
16
|
+
var swrOperation = Object.assign({ swrOpName: customName }, operation); %>
|
|
17
|
+
<%- include('swrOperation', swrOperation); %>
|
|
18
|
+
<% }); %>
|
|
19
|
+
<% } %>
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**<% parameters.forEach(function(parameter) { %>
|
|
2
|
+
* @param <%- parameter.name -%> <%- parameter.optional ? '(optional)' : '' -%> <%- parameter.name != parameter.originalName ? `(API name: ${parameter.originalName})` : '' -%>
|
|
3
|
+
<% }); %>
|
|
4
|
+
*/
|
|
5
|
+
<%- name -%>(<% parameters.forEach(function(parameter) { -%>
|
|
6
|
+
<%- parameter.name -%>: <%- parameter.type -%> <%- parameter.optional ? ' | null | undefined' : '' -%>,
|
|
7
|
+
<% }); -%>
|
|
8
|
+
$config?: AxiosRequestConfig
|
|
9
|
+
): AxiosPromise<<%- returnType -%>> {
|
|
10
|
+
let url = '<%- url -%>';
|
|
11
|
+
|
|
12
|
+
<%_ if(pathParams && pathParams.length > 0) {
|
|
13
|
+
pathParams.forEach(function(parameter) { -%>
|
|
14
|
+
url = url.replace('{<%- parameter.name -%>}', encodeURIComponent("" + <%- parameter.name -%>));
|
|
15
|
+
<% });
|
|
16
|
+
} _%>
|
|
17
|
+
<%_ if(formData && formData.length > 0) { _%>
|
|
18
|
+
const formDataBody = new FormData();
|
|
19
|
+
<%_ formData.forEach(function(parameter) { -%>
|
|
20
|
+
if (!!<%- parameter.name -%>) {
|
|
21
|
+
<%_ if(parameter.original && parameter.original.type === 'array') { -%>
|
|
22
|
+
<%- parameter.name -%>.forEach((f: any) => formDataBody.append("<%- parameter.originalName -%>", f));
|
|
23
|
+
<%_ } else { %>
|
|
24
|
+
formDataBody.append("<%- parameter.originalName -%>", <%- parameter.name -%><%- parameter.type !== 'string' && parameter.type !== 'File' && parameter.type !== 'Blob' ? '.toString()' : '' -%>);
|
|
25
|
+
<%_ } %>
|
|
26
|
+
}
|
|
27
|
+
<% });
|
|
28
|
+
} _%>
|
|
29
|
+
|
|
30
|
+
return axios.request<<%- returnType -%>>({
|
|
31
|
+
url: url,
|
|
32
|
+
method: '<%- method -%>',
|
|
33
|
+
<%_ if(formData && formData.length > 0) { _%>
|
|
34
|
+
data: formDataBody,
|
|
35
|
+
<%_ } else if(body) { _%>
|
|
36
|
+
data: <%- body.name -%>,
|
|
37
|
+
<%_ } _%>
|
|
38
|
+
<%_ if(query && query.length > 0) { _%>
|
|
39
|
+
params: {
|
|
40
|
+
<% query.forEach(function(parameter) { -%>
|
|
41
|
+
'<%- parameter.originalName -%>': serializeQueryParam(<%- parameter.name -%>),
|
|
42
|
+
<% }); -%>
|
|
43
|
+
},
|
|
44
|
+
<%_ } _%>
|
|
45
|
+
<%_ if(headers && headers.length > 0) { _%>
|
|
46
|
+
headers: {
|
|
47
|
+
<% headers.forEach(function(parameter) { -%>
|
|
48
|
+
<%_ if (parameter.value) { _%>
|
|
49
|
+
'<%- parameter.originalName -%>': '<%- parameter.value -%>',
|
|
50
|
+
<%_ } else { _%>
|
|
51
|
+
'<%- parameter.originalName -%>': <%- parameter.name -%>,
|
|
52
|
+
<%_ } _%>
|
|
53
|
+
<% }); -%>
|
|
54
|
+
},
|
|
55
|
+
<%_ } _%>
|
|
56
|
+
...$config,
|
|
57
|
+
});
|
|
58
|
+
},
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**<% parameters.forEach(function(parameter) { %>
|
|
2
|
+
* @param <%- parameter.name -%> <%- parameter.optional ? '(optional)' : '' -%> <%- parameter.name != parameter.originalName ? `(API name: ${parameter.originalName})` : '' -%>
|
|
3
|
+
<% }); %>
|
|
4
|
+
*/
|
|
5
|
+
export function <%- swrOpName -%>(<% parameters.forEach(function(parameter) { -%>
|
|
6
|
+
<%- parameter.name -%>: <%- parameter.type -%> <%- parameter.optional ? ' | null | undefined' : '' -%>,
|
|
7
|
+
<% }); -%>
|
|
8
|
+
$config?: SwrConfig
|
|
9
|
+
) {
|
|
10
|
+
let url = '<%- url -%>';
|
|
11
|
+
const { axios: $axiosConf, key, ...config } = $config || {};
|
|
12
|
+
|
|
13
|
+
<%_ if(pathParams && pathParams.length > 0) {
|
|
14
|
+
pathParams.forEach(function(parameter) { -%>
|
|
15
|
+
url = url.replace('{<%- parameter.name -%>}', encodeURIComponent("" + <%- parameter.name -%>));
|
|
16
|
+
<% });
|
|
17
|
+
} _%>
|
|
18
|
+
|
|
19
|
+
let cacheUrl = url + '?';
|
|
20
|
+
<%_ if(query && query.length > 0) { _%>
|
|
21
|
+
<% query.forEach(function(parameter) { -%>
|
|
22
|
+
if (!!<%- parameter.name -%>) {
|
|
23
|
+
cacheUrl += `<%- parameter.originalName -%>=${<%- parameter.name -%>}&`;
|
|
24
|
+
}
|
|
25
|
+
<% }); -%>
|
|
26
|
+
<%_ } _%>
|
|
27
|
+
const { data, error, mutate } = useSWR<<%- returnType -%>>(
|
|
28
|
+
key ?? cacheUrl,
|
|
29
|
+
() => axios.request({
|
|
30
|
+
url: url,
|
|
31
|
+
method: '<%- method -%>',
|
|
32
|
+
<%_ if(query && query.length > 0) { _%>
|
|
33
|
+
params: {
|
|
34
|
+
<% query.forEach(function(parameter) { -%>
|
|
35
|
+
'<%- parameter.originalName -%>': serializeQueryParam(<%- parameter.name -%>),
|
|
36
|
+
<% }); -%>
|
|
37
|
+
},
|
|
38
|
+
<%_ } _%>
|
|
39
|
+
<%_ if(headers && headers.length > 0) { _%>
|
|
40
|
+
headers: {
|
|
41
|
+
<% headers.forEach(function(parameter) { -%>
|
|
42
|
+
<%_ if (parameter.value) { _%>
|
|
43
|
+
'<%- parameter.originalName -%>': '<%- parameter.value -%>',
|
|
44
|
+
<%_ } else { _%>
|
|
45
|
+
'<%- parameter.originalName -%>': <%- parameter.name -%>,
|
|
46
|
+
<%_ } _%>
|
|
47
|
+
<% }); -%>
|
|
48
|
+
},
|
|
49
|
+
<%_ } _%>
|
|
50
|
+
...$axiosConf})
|
|
51
|
+
.then((resp) => resp.data),
|
|
52
|
+
config);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
data,
|
|
56
|
+
isLoading: !error && !data,
|
|
57
|
+
error: error,
|
|
58
|
+
mutate,
|
|
59
|
+
};
|
|
60
|
+
}
|
package/dist/codegen/common.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _typescript = require('typescript'); var _typescript2 = _interopRequireDefault(_typescript);
|
|
2
|
-
var _lodash = require('lodash');
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const _questionToken = _typescript2.default.createToken(_typescript2.default.SyntaxKind.QuestionToken);
|
|
6
|
-
|
|
7
|
-
function questionToken(condition) {
|
|
8
|
-
return condition ? _questionToken : undefined;
|
|
9
|
-
} exports.questionToken = questionToken;
|
|
10
|
-
|
|
11
|
-
const modifiers = {
|
|
12
|
-
export: _typescript2.default.createModifier(_typescript2.default.SyntaxKind.ExportKeyword),
|
|
13
|
-
}; exports.modifiers = modifiers;
|
|
14
|
-
const keywordType = {
|
|
15
|
-
any: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.AnyKeyword),
|
|
16
|
-
number: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.NumberKeyword),
|
|
17
|
-
object: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.ObjectKeyword),
|
|
18
|
-
string: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.StringKeyword),
|
|
19
|
-
boolean: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.BooleanKeyword),
|
|
20
|
-
undefined: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.UndefinedKeyword),
|
|
21
|
-
null: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.NullKeyword ),
|
|
22
|
-
void: _typescript2.default.createKeywordTypeNode(_typescript2.default.SyntaxKind.VoidKeyword),
|
|
23
|
-
}; exports.keywordType = keywordType;
|
|
24
|
-
|
|
25
|
-
function isNullable(schema) {
|
|
26
|
-
return !!(schema && schema.nullable);
|
|
27
|
-
} exports.isNullable = isNullable;
|
|
28
|
-
|
|
29
|
-
function isReference(obj) {
|
|
30
|
-
return obj && '$ref' in obj;
|
|
31
|
-
} exports.isReference = isReference;
|
|
32
|
-
|
|
33
|
-
function getReference(spec, ref) {
|
|
34
|
-
const path = ref
|
|
35
|
-
.slice(2)
|
|
36
|
-
.split('/')
|
|
37
|
-
.map((s) => unescape(s.replace(/~1/g, '/').replace(/~0/g, '~')));
|
|
38
|
-
|
|
39
|
-
const ret = _lodash.get.call(void 0, spec, path);
|
|
40
|
-
if (typeof ret === 'undefined') {
|
|
41
|
-
throw new Error(`Can't find ${path}`);
|
|
42
|
-
}
|
|
43
|
-
return ret;
|
|
44
|
-
} exports.getReference = getReference;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* If the given object is a ReferenceObject, return the last part of its path.
|
|
48
|
-
*/
|
|
49
|
-
function getReferenceName(obj) {
|
|
50
|
-
if (isReference(obj)) {
|
|
51
|
-
return _lodash.camelCase.call(void 0, obj.$ref.split('/').slice(-1)[0]);
|
|
52
|
-
}
|
|
53
|
-
} exports.getReferenceName = getReferenceName;
|
package/dist/codegen/index.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var _common = require('./common');
|
|
13
|
-
|
|
14
|
-
function generateApi(spec) {
|
|
15
|
-
const aliases = [];
|
|
16
|
-
|
|
17
|
-
function resolve(obj) {
|
|
18
|
-
if (!_common.isReference.call(void 0, obj)) return obj;
|
|
19
|
-
const ref = obj.$ref;
|
|
20
|
-
if (!ref.startsWith('#/')) {
|
|
21
|
-
throw new Error(
|
|
22
|
-
`External refs are not supported (${ref}). Make sure to call SwaggerParser.bundle() first.`
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
return _common.getReference.call(void 0, spec, ref) ;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function resolveArray(array) {
|
|
29
|
-
return array ? array.map(resolve) : [];
|
|
30
|
-
}
|
|
31
|
-
} exports.default = generateApi;
|
package/dist/codegen/models.js
DELETED
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _typescript = require('typescript'); var _typescript2 = _interopRequireDefault(_typescript);
|
|
2
|
-
|
|
3
|
-
var _lodash = require('lodash'); var _lodash2 = _interopRequireDefault(_lodash);
|
|
4
|
-
|
|
5
|
-
var _common = require('./common');
|
|
6
|
-
|
|
7
|
-
function genType(name, component) {
|
|
8
|
-
const keys = Object.keys(component.properties || {});
|
|
9
|
-
|
|
10
|
-
const fields = keys.map((p) => {
|
|
11
|
-
const property = component.properties[p];
|
|
12
|
-
const type = getTypeFromSchema(property);
|
|
13
|
-
return _typescript2.default.createPropertySignature(
|
|
14
|
-
undefined,
|
|
15
|
-
_typescript2.default.createIdentifier(p),
|
|
16
|
-
_common.questionToken.call(void 0, false),
|
|
17
|
-
type,
|
|
18
|
-
undefined
|
|
19
|
-
);
|
|
20
|
-
});
|
|
21
|
-
return _typescript2.default.createInterfaceDeclaration(
|
|
22
|
-
undefined,
|
|
23
|
-
[_common.modifiers.export],
|
|
24
|
-
_typescript2.default.createIdentifier(name),
|
|
25
|
-
undefined,
|
|
26
|
-
undefined,
|
|
27
|
-
fields
|
|
28
|
-
/*
|
|
29
|
-
[
|
|
30
|
-
ts.createPropertySignature(
|
|
31
|
-
undefined,
|
|
32
|
-
ts.createIdentifier('id'),
|
|
33
|
-
undefined,
|
|
34
|
-
ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
|
|
35
|
-
undefined
|
|
36
|
-
),
|
|
37
|
-
ts.createPropertySignature(
|
|
38
|
-
undefined,
|
|
39
|
-
ts.createIdentifier('marketCode'),
|
|
40
|
-
questionToken(true),
|
|
41
|
-
ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
|
|
42
|
-
undefined
|
|
43
|
-
),
|
|
44
|
-
ts.createPropertySignature(
|
|
45
|
-
undefined,
|
|
46
|
-
ts.createIdentifier('entries'),
|
|
47
|
-
undefined,
|
|
48
|
-
ts.createArrayTypeNode(
|
|
49
|
-
ts.createTypeReferenceNode(ts.createIdentifier('BookEntry'), undefined)
|
|
50
|
-
),
|
|
51
|
-
undefined
|
|
52
|
-
),
|
|
53
|
-
]
|
|
54
|
-
*/
|
|
55
|
-
);
|
|
56
|
-
} exports.genType = genType;
|
|
57
|
-
|
|
58
|
-
function generateTypes() {
|
|
59
|
-
return [];
|
|
60
|
-
} exports.generateTypes = generateTypes;
|
|
61
|
-
|
|
62
|
-
const aliases = [];
|
|
63
|
-
const refs = {};
|
|
64
|
-
|
|
65
|
-
function getRefAlias(obj) {
|
|
66
|
-
const { $ref } = obj;
|
|
67
|
-
let ref = refs[$ref];
|
|
68
|
-
if (!ref) {
|
|
69
|
-
const schema = resolve(obj);
|
|
70
|
-
const name = getUniqueAlias(_lodash2.default.upperFirst(schema.title || getRefBasename($ref)));
|
|
71
|
-
|
|
72
|
-
ref = refs[$ref] = _typescript2.default.createTypeReferenceNode(name, undefined);
|
|
73
|
-
|
|
74
|
-
const type = getTypeFromSchema(schema);
|
|
75
|
-
aliases.push(
|
|
76
|
-
_typescript2.default.createTypeAliasDeclaration(undefined, [_common.modifiers.export], name, undefined, type)
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
return ref;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function getTypeFromSchema(schema) {
|
|
83
|
-
const type = getBaseTypeFromSchema(schema);
|
|
84
|
-
return _common.isNullable.call(void 0, schema) ? _typescript2.default.createUnionTypeNode([type, _common.keywordType.null]) : type;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function getRefBasename(ref) {
|
|
88
|
-
return ref.replace(/.+\//, '');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function getBaseTypeFromSchema(
|
|
92
|
-
schema
|
|
93
|
-
) {
|
|
94
|
-
if (!schema) return _common.keywordType.any;
|
|
95
|
-
if (_common.isReference.call(void 0, schema)) {
|
|
96
|
-
return _common.keywordType.any;
|
|
97
|
-
// return getRefAlias(schema);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (schema.oneOf) {
|
|
101
|
-
// oneOf -> union
|
|
102
|
-
return getUnionType(schema.oneOf, schema.discriminator);
|
|
103
|
-
}
|
|
104
|
-
if (schema.anyOf) {
|
|
105
|
-
// anyOf -> union
|
|
106
|
-
return _typescript2.default.createUnionTypeNode(schema.anyOf.map(getTypeFromSchema));
|
|
107
|
-
}
|
|
108
|
-
if (schema.allOf) {
|
|
109
|
-
// allOf -> intersection
|
|
110
|
-
return _typescript2.default.createIntersectionTypeNode(schema.allOf.map(getTypeFromSchema));
|
|
111
|
-
}
|
|
112
|
-
if ('items' in schema) {
|
|
113
|
-
// items -> array
|
|
114
|
-
return _typescript2.default.createArrayTypeNode(getTypeFromSchema(schema.items));
|
|
115
|
-
}
|
|
116
|
-
if (schema.properties || schema.additionalProperties) {
|
|
117
|
-
// properties -> literal type
|
|
118
|
-
return getTypeFromProperties(
|
|
119
|
-
schema.properties || {},
|
|
120
|
-
schema.required,
|
|
121
|
-
schema.additionalProperties
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
if (schema.enum) {
|
|
125
|
-
// enum -> union of literal types
|
|
126
|
-
const types = schema.enum.map((s) =>
|
|
127
|
-
s === null ? _common.keywordType.null : _typescript2.default.createLiteralTypeNode(_typescript2.default.createStringLiteral(s))
|
|
128
|
-
);
|
|
129
|
-
return types.length > 1 ? _typescript2.default.createUnionTypeNode(types) : types[0];
|
|
130
|
-
}
|
|
131
|
-
if (schema.format === 'binary') {
|
|
132
|
-
return _typescript2.default.createTypeReferenceNode('Blob', []);
|
|
133
|
-
}
|
|
134
|
-
if (schema.type) {
|
|
135
|
-
// string, boolean, null, number
|
|
136
|
-
if (schema.type in _common.keywordType) return _common.keywordType[schema.type];
|
|
137
|
-
if (schema.type === 'integer') return _common.keywordType.number;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return _common.keywordType.any;
|
|
141
|
-
} exports.getBaseTypeFromSchema = getBaseTypeFromSchema;
|
|
142
|
-
|
|
143
|
-
function supportDeepObjects(params) {
|
|
144
|
-
const res = [];
|
|
145
|
-
const merged = {};
|
|
146
|
-
params.forEach((p) => {
|
|
147
|
-
const m = /^(.+?)\[(.*?)\]/.exec(p.name);
|
|
148
|
-
if (!m) {
|
|
149
|
-
res.push(p);
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
152
|
-
const [, name, prop] = m;
|
|
153
|
-
let obj = merged[name];
|
|
154
|
-
if (!obj) {
|
|
155
|
-
obj = merged[name] = {
|
|
156
|
-
name,
|
|
157
|
-
in: p.in,
|
|
158
|
-
style: 'deepObject',
|
|
159
|
-
schema: {
|
|
160
|
-
type: 'object',
|
|
161
|
-
properties: {},
|
|
162
|
-
},
|
|
163
|
-
};
|
|
164
|
-
res.push(obj);
|
|
165
|
-
}
|
|
166
|
-
obj.schema.properties[prop] = p.schema;
|
|
167
|
-
});
|
|
168
|
-
return res;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
const typeAliases = {};
|
|
172
|
-
function getUniqueAlias(name) {
|
|
173
|
-
let used = typeAliases[name] || 0;
|
|
174
|
-
if (used) {
|
|
175
|
-
typeAliases[name] = ++used;
|
|
176
|
-
name += used;
|
|
177
|
-
}
|
|
178
|
-
typeAliases[name] = 1;
|
|
179
|
-
return name;
|
|
180
|
-
}
|
|
181
|
-
function getTypeFromProperties(
|
|
182
|
-
props
|
|
183
|
-
|
|
184
|
-
,
|
|
185
|
-
required,
|
|
186
|
-
additionalProperties
|
|
187
|
-
) {
|
|
188
|
-
const members = Object.keys(props).map((name) => {
|
|
189
|
-
const schema = props[name];
|
|
190
|
-
const isRequired = required && required.includes(name);
|
|
191
|
-
return _typescript2.default.createPropertySignature(
|
|
192
|
-
undefined,
|
|
193
|
-
name,
|
|
194
|
-
_common.questionToken.call(void 0, !isRequired),
|
|
195
|
-
getTypeFromSchema(schema)
|
|
196
|
-
);
|
|
197
|
-
});
|
|
198
|
-
if (additionalProperties) {
|
|
199
|
-
const type =
|
|
200
|
-
additionalProperties === true ? _common.keywordType.any : getTypeFromSchema(additionalProperties);
|
|
201
|
-
|
|
202
|
-
members.push(_typescript2.default.createIndexSignature(undefined, undefined, undefined, type));
|
|
203
|
-
}
|
|
204
|
-
return _typescript2.default.createTypeLiteralNode(members);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function getUnionType(
|
|
208
|
-
variants,
|
|
209
|
-
discriminator
|
|
210
|
-
) {
|
|
211
|
-
if (discriminator) {
|
|
212
|
-
// oneOf + discriminator -> tagged union (polymorphism)
|
|
213
|
-
if (discriminator.propertyName === undefined) {
|
|
214
|
-
throw new Error('Discriminators require a propertyName');
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// By default, the last component of the ref name (i.e., after the last trailing slash) is
|
|
218
|
-
// used as the discriminator value for each variant. This can be overridden using the
|
|
219
|
-
// discriminator.mapping property.
|
|
220
|
-
const mappedValues = new Set(
|
|
221
|
-
Object.values(discriminator.mapping || {}).map((ref) => getRefBasename(ref))
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
return _typescript2.default.createUnionTypeNode(
|
|
225
|
-
([
|
|
226
|
-
...Object.entries(discriminator.mapping || {}).map(([discriminatorValue, variantRef]) => [
|
|
227
|
-
discriminatorValue,
|
|
228
|
-
{ $ref: variantRef },
|
|
229
|
-
]),
|
|
230
|
-
...variants
|
|
231
|
-
.filter((variant) => {
|
|
232
|
-
if (!_common.isReference.call(void 0, variant)) {
|
|
233
|
-
// From the Swagger spec: "When using the discriminator, inline schemas will not be
|
|
234
|
-
// considered."
|
|
235
|
-
throw new Error('Discriminators require references, not inline schemas');
|
|
236
|
-
}
|
|
237
|
-
return !mappedValues.has(getRefBasename(variant.$ref));
|
|
238
|
-
})
|
|
239
|
-
.map((schema) => [getRefBasename((schema ).$ref), schema]),
|
|
240
|
-
] ).map(([discriminatorValue, variant]) =>
|
|
241
|
-
// Yields: { [discriminator.propertyName]: discriminatorValue } & variant
|
|
242
|
-
_typescript2.default.createIntersectionTypeNode([
|
|
243
|
-
_typescript2.default.createTypeLiteralNode([
|
|
244
|
-
_typescript2.default.createPropertySignature(
|
|
245
|
-
undefined,
|
|
246
|
-
discriminator.propertyName,
|
|
247
|
-
undefined,
|
|
248
|
-
_typescript2.default.createLiteralTypeNode(_typescript2.default.createStringLiteral(discriminatorValue))
|
|
249
|
-
),
|
|
250
|
-
]),
|
|
251
|
-
getTypeFromSchema(variant),
|
|
252
|
-
])
|
|
253
|
-
)
|
|
254
|
-
);
|
|
255
|
-
} else {
|
|
256
|
-
// oneOf -> untagged union
|
|
257
|
-
return _typescript2.default.createUnionTypeNode(variants.map(getTypeFromSchema));
|
|
258
|
-
}
|
|
259
|
-
} exports.getUnionType = getUnionType;
|
|
260
|
-
|
|
261
|
-
function resolve(obj) {
|
|
262
|
-
if (!_common.isReference.call(void 0, obj)) return obj;
|
|
263
|
-
const ref = obj.$ref;
|
|
264
|
-
if (!ref.startsWith('#/')) {
|
|
265
|
-
throw new Error(
|
|
266
|
-
`External refs are not supported (${ref}). Make sure to call SwaggerParser.bundle() first.`
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
// return getReference(spec, ref) as T;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function resolveArray(array) {
|
|
273
|
-
return array ? array.map(resolve) : [];
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
package/dist/codegen/renderer.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
|
|
2
|
-
var _typescript = require('typescript'); var _typescript2 = _interopRequireDefault(_typescript);
|
|
3
|
-
|
|
4
|
-
function parseFile(file) {
|
|
5
|
-
return _typescript2.default.createSourceFile(
|
|
6
|
-
file,
|
|
7
|
-
_fs2.default.readFileSync(file, 'utf8'),
|
|
8
|
-
_typescript2.default.ScriptTarget.Latest,
|
|
9
|
-
false,
|
|
10
|
-
_typescript2.default.ScriptKind.TS
|
|
11
|
-
);
|
|
12
|
-
} exports.parseFile = parseFile;
|
|
13
|
-
|
|
14
|
-
const printer = _typescript2.default.createPrinter({
|
|
15
|
-
newLine: _typescript2.default.NewLineKind.LineFeed,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
function printNode(node) {
|
|
19
|
-
const file = _typescript2.default.createSourceFile(
|
|
20
|
-
'someFileName.ts',
|
|
21
|
-
'',
|
|
22
|
-
_typescript2.default.ScriptTarget.Latest,
|
|
23
|
-
false,
|
|
24
|
-
_typescript2.default.ScriptKind.TS
|
|
25
|
-
);
|
|
26
|
-
return printer.printNode(_typescript2.default.EmitHint.Unspecified, node, file);
|
|
27
|
-
} exports.printNode = printNode;
|
|
28
|
-
|
|
29
|
-
function printNodes(nodes) {
|
|
30
|
-
const file = _typescript2.default.createSourceFile(
|
|
31
|
-
'someFileName.ts',
|
|
32
|
-
'',
|
|
33
|
-
_typescript2.default.ScriptTarget.Latest,
|
|
34
|
-
false,
|
|
35
|
-
_typescript2.default.ScriptKind.TS
|
|
36
|
-
);
|
|
37
|
-
return nodes.map((node) => printer.printNode(_typescript2.default.EmitHint.Unspecified, node, file)).join('\n');
|
|
38
|
-
} exports.printNodes = printNodes;
|
|
39
|
-
|
|
40
|
-
function printFile(sourceFile) {
|
|
41
|
-
return printer.printFile(sourceFile);
|
|
42
|
-
} exports.printFile = printFile;
|
package/dist/schema.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
-
var _lodash = require('lodash');
|
|
3
|
-
var _utils = require('./utils');
|
|
4
|
-
|
|
5
|
-
const models = [];
|
|
6
|
-
|
|
7
|
-
exports. default = function (spec) {
|
|
8
|
-
function getAllModels() {
|
|
9
|
-
return spec.components.schemas;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function handleGenericTypes() {
|
|
13
|
-
return [];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getParamName(name) {
|
|
18
|
-
return _utils.escapeReservedWords.call(void 0,
|
|
19
|
-
name
|
|
20
|
-
.split('.')
|
|
21
|
-
.map((x) => _lodash.camelCase.call(void 0, x))
|
|
22
|
-
.join('_')
|
|
23
|
-
);
|
|
24
|
-
} exports.getParamName = getParamName;
|
|
25
|
-
|
|
26
|
-
function getOperationName(opId, group) {
|
|
27
|
-
if (!opId) {
|
|
28
|
-
return '';
|
|
29
|
-
}
|
|
30
|
-
if (!group) {
|
|
31
|
-
return opId;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return _lodash.camelCase.call(void 0, opId.replace(group + '_', ''));
|
|
35
|
-
} exports.getOperationName = getOperationName;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
package/dist/utils/index.js
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _createStarExport(obj) { Object.keys(obj) .filter((key) => key !== "default" && key !== "__esModule") .forEach((key) => { if (exports.hasOwnProperty(key)) { return; } Object.defineProperty(exports, key, {enumerable: true, get: () => obj[key]}); }); }var _utils = require('./utils'); _createStarExport(_utils);
|
|
2
|
-
var _specLoader = require('./specLoader'); _createStarExport(_specLoader);
|
|
3
|
-
var _types = require('./types'); _createStarExport(_types);
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
-
var _lodash = require('lodash');
|
|
3
|
-
var _ = require('./');
|
|
4
|
-
|
|
5
|
-
const verbs = ['GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'HEAD', 'PATCH', 'TRACE'];
|
|
6
|
-
|
|
7
|
-
// TODO: Merge with path-level parameters
|
|
8
|
-
function getQueryModels(spec) {
|
|
9
|
-
const res = Object.keys(spec.paths).map((key) => {
|
|
10
|
-
const el = spec.paths[key];
|
|
11
|
-
return Object.keys(el)
|
|
12
|
-
.filter((verb) => verbs.indexOf(verb.toUpperCase()) > -1)
|
|
13
|
-
.map((verb) => {
|
|
14
|
-
const operation = el[verb] ;
|
|
15
|
-
const queryParams = operation.parameters.filter(
|
|
16
|
-
(p) => p.in === 'query'
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
if (!queryParams || queryParams.length <= 1) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const params = queryParams.map((q) => ({ name: q.name, ...q.schema }));
|
|
24
|
-
return {
|
|
25
|
-
name: `I${_lodash.upperFirst.call(void 0, getOperationName(operation.operationId))}ServiceQuery`,
|
|
26
|
-
type: 'object',
|
|
27
|
-
additionalProperties: false,
|
|
28
|
-
properties: params.reduce((obj, item) => Object.assign(obj, { [item.name]: item }), {}),
|
|
29
|
-
} ;
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
return _lodash.flatten.call(void 0, res).filter((e) => e);
|
|
33
|
-
} exports.getQueryModels = getQueryModels;
|
|
34
|
-
|
|
35
|
-
function getParamName(name) {
|
|
36
|
-
return _.escapeReservedWords.call(void 0,
|
|
37
|
-
name
|
|
38
|
-
.split('.')
|
|
39
|
-
.map((x) => _lodash.camelCase.call(void 0, x))
|
|
40
|
-
.join('_')
|
|
41
|
-
);
|
|
42
|
-
} exports.getParamName = getParamName;
|
|
43
|
-
|
|
44
|
-
function getOperationName(opId, group) {
|
|
45
|
-
if (!opId) {
|
|
46
|
-
return '';
|
|
47
|
-
}
|
|
48
|
-
if (!group) {
|
|
49
|
-
return opId;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return _lodash.camelCase.call(void 0, opId.replace(group + '_', ''));
|
|
53
|
-
} exports.getOperationName = getOperationName;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
package/dist/utils/specLoader.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _jsyaml = require('js-yaml'); var _jsyaml2 = _interopRequireDefault(_jsyaml);
|
|
2
|
-
var _nodefetch = require('node-fetch'); var _nodefetch2 = _interopRequireDefault(_nodefetch);
|
|
3
|
-
|
|
4
|
-
/** Either tries to load spec from given location or returns it further if it was already given */
|
|
5
|
-
function loadSpecification(src) {
|
|
6
|
-
if (typeof src === 'string') {
|
|
7
|
-
return loadFile(src);
|
|
8
|
-
} else {
|
|
9
|
-
return Promise.resolve(src);
|
|
10
|
-
}
|
|
11
|
-
} exports.loadSpecification = loadSpecification;
|
|
12
|
-
|
|
13
|
-
function loadFile(src) {
|
|
14
|
-
if (/^https?:\/\//im.test(src)) {
|
|
15
|
-
return loadFromUrl(src);
|
|
16
|
-
} else if (String(process) === '[object process]') {
|
|
17
|
-
return readLocalFile(src).then((contents) => parseFileContents(contents, src));
|
|
18
|
-
} else {
|
|
19
|
-
throw new Error(`Unable to load api at '${src}'`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function loadFromUrl(url) {
|
|
24
|
-
return _nodefetch2.default.call(void 0, url)
|
|
25
|
-
.then((resp) => resp.text())
|
|
26
|
-
.then((contents) => parseFileContents(contents, url));
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function readLocalFile(filePath) {
|
|
30
|
-
return new Promise((res, rej) =>
|
|
31
|
-
require('fs').readFile(filePath, 'utf8', (err, contents) => (err ? rej(err) : res(contents)))
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function parseFileContents(contents, path) {
|
|
36
|
-
return /.ya?ml$/i.test(path) ? _jsyaml2.default.load(contents) : JSON.parse(contents);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Recursively expand internal references in the form `#/path/to/object`.
|
|
41
|
-
*
|
|
42
|
-
* @param {object} data the object to search for and update refs
|
|
43
|
-
* @param {object} lookup the object to clone refs from
|
|
44
|
-
* @param {regexp=} refMatch an optional regex to match specific refs to resolve
|
|
45
|
-
* @returns {object} the resolved data object
|
|
46
|
-
*/
|
|
47
|
-
function expandRefs(data, lookup) {
|
|
48
|
-
if (!data) {
|
|
49
|
-
return data;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (Array.isArray(data)) {
|
|
53
|
-
return data.map((item) => expandRefs(item, lookup));
|
|
54
|
-
} else if (typeof data === 'object') {
|
|
55
|
-
if (dataCache.has(data)) {
|
|
56
|
-
return data;
|
|
57
|
-
}
|
|
58
|
-
if (data.$ref) {
|
|
59
|
-
const resolved = expandRef(data.$ref, lookup);
|
|
60
|
-
delete data.$ref;
|
|
61
|
-
data = Object.assign({}, resolved, data);
|
|
62
|
-
}
|
|
63
|
-
dataCache.add(data);
|
|
64
|
-
|
|
65
|
-
for (let name in data) {
|
|
66
|
-
data[name] = expandRefs(data[name], lookup);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return data;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function expandRef(ref, lookup) {
|
|
73
|
-
const parts = ref.split('/');
|
|
74
|
-
if (parts.shift() !== '#' || !parts[0]) {
|
|
75
|
-
throw new Error(`Only support JSON Schema $refs in format '#/path/to/ref'`);
|
|
76
|
-
}
|
|
77
|
-
let value = lookup;
|
|
78
|
-
while (parts.length) {
|
|
79
|
-
value = value[parts.shift()];
|
|
80
|
-
if (!value) {
|
|
81
|
-
throw new Error(`Invalid schema reference: ${ref}`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return value;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const dataCache = new Set();
|
package/dist/utils/types.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// 'luxon', 'momentjs', etc
|
|
18
|
-
|
|
19
|
-
// interface ApiOperationParam extends ApiOperationParamBase {
|
|
20
|
-
// name: string;
|
|
21
|
-
// in: 'header' | 'path' | 'query' | 'body' | 'formData';
|
|
22
|
-
// description: string;
|
|
23
|
-
// required: boolean;
|
|
24
|
-
// readonly?: boolean;
|
|
25
|
-
// allowEmptyValue: boolean;
|
|
26
|
-
// schema: object;
|
|
27
|
-
// 'x-nullable'?: boolean;
|
|
28
|
-
// 'x-schema'?: object;
|
|
29
|
-
// }
|
package/dist/utils/utils.js
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});/** This method tries to fix potentially wrong out parameter given from commandline */
|
|
2
|
-
function prepareOutputFilename(out) {
|
|
3
|
-
if (!out) {
|
|
4
|
-
return null;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
if (/\.[jt]sx?$/i.test(out)) {
|
|
8
|
-
return out.replace(/[\\]/i, '/');
|
|
9
|
-
}
|
|
10
|
-
if (/[\/\\]$/i.test(out)) {
|
|
11
|
-
return out.replace(/[\/\\]$/i, '') + '/index.ts';
|
|
12
|
-
}
|
|
13
|
-
return out.replace(/[\\]/i, '/') + '.ts';
|
|
14
|
-
} exports.prepareOutputFilename = prepareOutputFilename;
|
|
15
|
-
|
|
16
|
-
const reservedWords = [
|
|
17
|
-
'break',
|
|
18
|
-
'case',
|
|
19
|
-
'catch',
|
|
20
|
-
'class',
|
|
21
|
-
'const',
|
|
22
|
-
'continue',
|
|
23
|
-
'debugger',
|
|
24
|
-
'default',
|
|
25
|
-
'delete',
|
|
26
|
-
'do',
|
|
27
|
-
'else',
|
|
28
|
-
'export',
|
|
29
|
-
'extends',
|
|
30
|
-
'finally',
|
|
31
|
-
'for',
|
|
32
|
-
'function',
|
|
33
|
-
'if',
|
|
34
|
-
'import',
|
|
35
|
-
'in',
|
|
36
|
-
'instanceof',
|
|
37
|
-
'new',
|
|
38
|
-
'return',
|
|
39
|
-
'super',
|
|
40
|
-
'switch',
|
|
41
|
-
'this',
|
|
42
|
-
'throw',
|
|
43
|
-
'try',
|
|
44
|
-
'typeof',
|
|
45
|
-
'var',
|
|
46
|
-
'void',
|
|
47
|
-
'while',
|
|
48
|
-
'with',
|
|
49
|
-
'yield',
|
|
50
|
-
];
|
|
51
|
-
|
|
52
|
-
function escapeReservedWords(name) {
|
|
53
|
-
let escapedName = name;
|
|
54
|
-
|
|
55
|
-
if (reservedWords.indexOf(name) >= 0) {
|
|
56
|
-
escapedName = '_' + name;
|
|
57
|
-
}
|
|
58
|
-
return escapedName;
|
|
59
|
-
} exports.escapeReservedWords = escapeReservedWords;
|