swaggie 1.8.3 → 1.8.4-beta.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/dist/browser.js +101 -0
- package/dist/cli.js +0 -0
- package/dist/gen/createBarrel.js +2 -2
- package/dist/gen/genOperations.js +4 -4
- package/dist/gen/genTypes.js +1 -1
- package/dist/generated/bundledTemplates.js +51 -0
- package/dist/index.d.ts +1 -2
- package/dist/swagger/typesExtractor.js +1 -1
- package/dist/utils/documentLoader.browser.js +42 -0
- package/dist/utils/fileUtils.js +15 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/templateEngine.js +32 -0
- package/dist/utils/templateManager.js +23 -10
- package/dist/utils/utils.js +1 -16
- package/package.json +22 -4
package/dist/browser.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
var _genOperations = require('./gen/genOperations'); var _genOperations2 = _interopRequireDefault(_genOperations);
|
|
4
|
+
var _genTypes = require('./gen/genTypes'); var _genTypes2 = _interopRequireDefault(_genTypes);
|
|
5
|
+
|
|
6
|
+
var _swagger = require('./swagger');
|
|
7
|
+
var _bundledTemplates = require('./generated/bundledTemplates');
|
|
8
|
+
var _utils = require('./utils/utils');
|
|
9
|
+
var _documentLoaderbrowser = require('./utils/documentLoader.browser');
|
|
10
|
+
var _templateEngine = require('./utils/templateEngine');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Browser-friendly code generation entrypoint.
|
|
14
|
+
* Unlike the Node entrypoint, this does not support config files or writing to disk.
|
|
15
|
+
*/
|
|
16
|
+
async function runCodeGenerator(options) {
|
|
17
|
+
try {
|
|
18
|
+
verifyOptions(options);
|
|
19
|
+
const opts = prepareAppOptions(options );
|
|
20
|
+
|
|
21
|
+
if (opts.out) {
|
|
22
|
+
throw new Error('"out" option is not supported in browser mode');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const spec = await _documentLoaderbrowser.loadSpecDocument.call(void 0, opts.src);
|
|
26
|
+
const verifiedSpec = _utils.verifyDocumentSpec.call(void 0, spec);
|
|
27
|
+
const code = await generateCode(verifiedSpec, opts);
|
|
28
|
+
|
|
29
|
+
return [code, opts];
|
|
30
|
+
} catch (e) {
|
|
31
|
+
return Promise.reject(e);
|
|
32
|
+
}
|
|
33
|
+
} exports.runCodeGenerator = runCodeGenerator;
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
function verifyOptions(options) {
|
|
38
|
+
if (!options) {
|
|
39
|
+
throw new Error('Options were not provided');
|
|
40
|
+
}
|
|
41
|
+
if (options.config) {
|
|
42
|
+
throw new Error('"config" option is not supported in browser mode');
|
|
43
|
+
}
|
|
44
|
+
if (!options.src) {
|
|
45
|
+
throw new Error('You need to provide --src parameter');
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function generateCode(spec, options) {
|
|
50
|
+
if (options.generationMode === 'schemas') {
|
|
51
|
+
return _genTypes2.default.call(void 0, spec, options, false);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const templateFiles = _bundledTemplates.BUNDLED_TEMPLATES[options.template];
|
|
55
|
+
if (!templateFiles) {
|
|
56
|
+
throw new Error(`Bundled templates for '${options.template}' are not available`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_templateEngine.initTemplateEngineFromBundled.call(void 0, templateFiles);
|
|
60
|
+
const operationsCode = await _genOperations2.default.call(void 0, spec, options);
|
|
61
|
+
|
|
62
|
+
return operationsCode + _genTypes2.default.call(void 0, spec, options);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Browser options preparation mirrors Node behavior for consistency.
|
|
67
|
+
*/
|
|
68
|
+
function prepareAppOptions(cliOpts) {
|
|
69
|
+
const {
|
|
70
|
+
allowDots,
|
|
71
|
+
arrayFormat,
|
|
72
|
+
mode,
|
|
73
|
+
schemaStyle,
|
|
74
|
+
enumStyle,
|
|
75
|
+
nullables,
|
|
76
|
+
template,
|
|
77
|
+
queryParamsSerialization = {},
|
|
78
|
+
...rest
|
|
79
|
+
} = cliOpts;
|
|
80
|
+
const mergedQueryParamsSerialization = {
|
|
81
|
+
..._swagger.APP_DEFAULTS.queryParamsSerialization,
|
|
82
|
+
...Object.fromEntries(
|
|
83
|
+
Object.entries(queryParamsSerialization).filter(([_, v]) => v !== undefined)
|
|
84
|
+
),
|
|
85
|
+
...(allowDots !== undefined ? { allowDots } : {}),
|
|
86
|
+
...(arrayFormat !== undefined ? { arrayFormat } : {}),
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return {
|
|
90
|
+
...rest,
|
|
91
|
+
template: _nullishCoalesce(template, () => ( _swagger.APP_DEFAULTS.template)),
|
|
92
|
+
servicePrefix: _nullishCoalesce(rest.servicePrefix, () => ( _swagger.APP_DEFAULTS.servicePrefix)),
|
|
93
|
+
nullableStrategy: _nullishCoalesce(_nullishCoalesce(nullables, () => ( rest.nullableStrategy)), () => ( _swagger.APP_DEFAULTS.nullableStrategy)),
|
|
94
|
+
generationMode: _nullishCoalesce(_nullishCoalesce(mode, () => ( rest.generationMode)), () => ( _swagger.APP_DEFAULTS.generationMode)),
|
|
95
|
+
schemaDeclarationStyle:
|
|
96
|
+
_nullishCoalesce(_nullishCoalesce(schemaStyle, () => ( rest.schemaDeclarationStyle)), () => ( _swagger.APP_DEFAULTS.schemaDeclarationStyle)),
|
|
97
|
+
enumDeclarationStyle:
|
|
98
|
+
_nullishCoalesce(_nullishCoalesce(enumStyle, () => ( rest.enumDeclarationStyle)), () => ( _swagger.APP_DEFAULTS.enumDeclarationStyle)),
|
|
99
|
+
queryParamsSerialization: mergedQueryParamsSerialization,
|
|
100
|
+
};
|
|
101
|
+
} exports.prepareAppOptions = prepareAppOptions;
|
package/dist/cli.js
CHANGED
|
File without changes
|
package/dist/gen/createBarrel.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _case = require('case');
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var _templateEngine = require('../utils/templateEngine');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
@@ -25,5 +25,5 @@ var _utils = require('../utils');
|
|
|
25
25
|
})),
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
return
|
|
28
|
+
return _templateEngine.renderFile.call(void 0, 'barrel.ejs', viewData);
|
|
29
29
|
} exports.generateBarrelFile = generateBarrelFile;
|
|
@@ -8,8 +8,8 @@ var _swagger = require('../swagger');
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
var
|
|
11
|
+
var _utils = require('../utils/utils');
|
|
12
|
+
var _templateEngine = require('../utils/templateEngine');
|
|
13
13
|
var _createBarrel = require('./createBarrel');
|
|
14
14
|
|
|
15
15
|
|
|
@@ -25,7 +25,7 @@ var _jsDocs = require('./jsDocs');
|
|
|
25
25
|
const operations = _swagger.getOperations.call(void 0, spec);
|
|
26
26
|
const groups = _utils.groupOperationsByGroupName.call(void 0, operations);
|
|
27
27
|
const servicePrefix = options.servicePrefix;
|
|
28
|
-
let result =
|
|
28
|
+
let result = _templateEngine.renderFile.call(void 0, 'baseClient.ejs', {
|
|
29
29
|
servicePrefix,
|
|
30
30
|
baseUrl: options.baseUrl,
|
|
31
31
|
...options.queryParamsSerialization,
|
|
@@ -39,7 +39,7 @@ var _jsDocs = require('./jsDocs');
|
|
|
39
39
|
continue;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
const renderedFile =
|
|
42
|
+
const renderedFile = _templateEngine.renderFile.call(void 0, 'client.ejs', {
|
|
43
43
|
...clientData,
|
|
44
44
|
servicePrefix,
|
|
45
45
|
});
|
package/dist/gen/genTypes.js
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// This file is auto-generated by scripts/bundle-templates.ts
|
|
2
|
+
// Do not edit this file manually.
|
|
3
|
+
|
|
4
|
+
const BUNDLED_TEMPLATES = {
|
|
5
|
+
"axios": {
|
|
6
|
+
"barrel.ejs": "\n/**\n * Serializes a params object into a query string that is compatible with different REST APIs.\n * Implementation from: https://github.com/suhaotian/xior/blob/main/src/utils.ts\n * Kudos to @suhaotian for the original implementation\n */\nfunction encodeParams<T = any>(\n params: T,\n parentKey: string | null = null,\n options?: {\n allowDots?: boolean;\n serializeDate?: (value: Date) => string;\n arrayFormat?: 'indices' | 'repeat' | 'brackets';\n }\n): string {\n if (params === undefined || params === null) return '';\n const encodedParams: string[] = [];\n const paramsIsArray = Array.isArray(params);\n const { arrayFormat, allowDots, serializeDate } = options || {};\n\n const getKey = (key: string) => {\n if (allowDots && !paramsIsArray) return `.${key}`;\n if (paramsIsArray) {\n if (arrayFormat === 'brackets') {\n return '[]';\n }\n if (arrayFormat === 'repeat') {\n return '';\n }\n }\n return `[${key}]`;\n };\n\n for (const key in params) {\n if (Object.prototype.hasOwnProperty.call(params, key)) {\n let value = (params as any)[key];\n if (value !== undefined) {\n const encodedKey = parentKey ? `${parentKey}${getKey(key)}` : (key as string);\n\n // biome-ignore lint/suspicious/noGlobalIsNan: <explanation>\n if (!isNaN(value) && value instanceof Date) {\n value = serializeDate ? serializeDate(value) : value.toISOString();\n }\n if (typeof value === 'object') {\n // If the value is an object or array, recursively encode its contents\n const result = encodeParams(value, encodedKey, options);\n if (result !== '') encodedParams.push(result);\n } else {\n // Otherwise, encode the key-value pair\n encodedParams.push(`${encodeURIComponent(encodedKey)}=${encodeURIComponent(value)}`);\n }\n }\n }\n }\n\n return encodedParams.join('&');\n}\n\n",
|
|
7
|
+
"baseClient.ejs": "/* tslint:disable */\n/* eslint-disable */\n//----------------------\n// <auto-generated>\n// Generated using Swaggie (https://github.com/yhnavein/swaggie)\n// Please avoid doing any manual changes in this file\n// </auto-generated>\n//----------------------\n// ReSharper disable InconsistentNaming\n// deno-lint-ignore-file\n\nimport Axios, { type AxiosPromise, type AxiosRequestConfig } from \"axios\";\n\nexport const axios = Axios.create({\n baseURL: '<%= it.baseUrl || '' %>',\n paramsSerializer: (params: any) =>\n encodeParams(params, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\n\n",
|
|
8
|
+
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n",
|
|
9
|
+
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n$config?: AxiosRequestConfig\n ): AxiosPromise<<%~ it.returnType %>> {\n const url = `<%= it.url %>`;\n\n return axios.request<<%~ it.returnType %>>({\n url: url,\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'urlencoded') { %>\n data: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n data: <%= it.body.name %>,\n<% } %>\n<% } %>\n<% if(it.query && it.query.length > 0) { %>\n params: {\n <% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>\n },\n<% } %>\n<% if(it.headers && it.headers.length > 0) { %>\n headers: {\n <% it.headers.forEach((parameter) => { %>\n <% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n <% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% } %>\n <% }); %>\n},\n<% } %>\n ...$config,\n });\n },\n"
|
|
10
|
+
},
|
|
11
|
+
"fetch": {
|
|
12
|
+
"barrel.ejs": "\n/**\n * Serializes a params object into a query string that is compatible with different REST APIs.\n * Implementation from: https://github.com/suhaotian/xior/blob/main/src/utils.ts\n * Kudos to @suhaotian for the original implementation\n */\nfunction encodeParams<T = any>(\n params: T,\n parentKey: string | null = null,\n options?: {\n allowDots?: boolean;\n serializeDate?: (value: Date) => string;\n arrayFormat?: 'indices' | 'repeat' | 'brackets';\n }\n): string {\n if (params === undefined || params === null) return '';\n const encodedParams: string[] = [];\n const paramsIsArray = Array.isArray(params);\n const { arrayFormat, allowDots, serializeDate } = options || {};\n\n const getKey = (key: string) => {\n if (allowDots && !paramsIsArray) return `.${key}`;\n if (paramsIsArray) {\n if (arrayFormat === 'brackets') {\n return '[]';\n }\n if (arrayFormat === 'repeat') {\n return '';\n }\n }\n return `[${key}]`;\n };\n\n for (const key in params) {\n if (Object.prototype.hasOwnProperty.call(params, key)) {\n let value = (params as any)[key];\n if (value !== undefined) {\n const encodedKey = parentKey ? `${parentKey}${getKey(key)}` : (key as string);\n\n // biome-ignore lint/suspicious/noGlobalIsNan: <explanation>\n if (!isNaN(value) && value instanceof Date) {\n value = serializeDate ? serializeDate(value) : value.toISOString();\n }\n if (typeof value === 'object') {\n // If the value is an object or array, recursively encode its contents\n const result = encodeParams(value, encodedKey, options);\n if (result !== '') encodedParams.push(result);\n } else {\n // Otherwise, encode the key-value pair\n encodedParams.push(`${encodeURIComponent(encodedKey)}=${encodeURIComponent(value)}`);\n }\n }\n }\n }\n\n return encodedParams.join('&');\n}\n\n",
|
|
13
|
+
"baseClient.ejs": "/* tslint:disable */\n/* eslint-disable */\n//----------------------\n// <auto-generated>\n// Generated using Swaggie (https://github.com/yhnavein/swaggie)\n// Please avoid doing any manual changes in this file\n// </auto-generated>\n//----------------------\n// ReSharper disable InconsistentNaming\n// deno-lint-ignore-file\n\nexport const defaults = {\n baseUrl: '<%= it.baseUrl || '' %>',\n paramsSerializer: (params: any) =>\n encodeParams(params, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n};\n\n",
|
|
14
|
+
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n",
|
|
15
|
+
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n$config?: RequestInit\n ): Promise<<%~ it.returnType %>> {\n const url = `${defaults.baseUrl}<%= it.url %>?<%\n if(it.query && it.query.length > 0) { %>${defaults.paramsSerializer({<%\n it.query.forEach((parameter) => { %>\n'<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>})}<% } %>`;\n\n<% if(it.headers && it.headers.length > 0) { %>\n const { headers: $configHeaders, ...$configRest } = $config ?? {};\n const headers = new Headers({\n<% it.headers.forEach((parameter) => { %>\n<% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n<% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %> ?? '',\n<% } %>\n<% }); %>\n });\n if ($configHeaders) {\n new Headers($configHeaders).forEach((value, key) => headers.set(key, value));\n }\n\n return fetch(url, {\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'json') { %>\n body: JSON.stringify(<%= it.body.name %>),\n<% } else if(it.body.contentType === 'urlencoded') { %>\n body: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n body: <%= it.body.name %>,\n<% } %>\n<% } %>\n headers,\n ...$configRest,\n })\n<% } else { %>\n return fetch(url, {\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'json') { %>\n body: JSON.stringify(<%= it.body.name %>),\n<% } else if(it.body.contentType === 'urlencoded') { %>\n body: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n body: <%= it.body.name %>,\n<% } %>\n<% } %>\n ...$config,\n })\n<% } %>\n<% if(it.responseContentType === 'binary') { %>\n .then((response) => response.blob() as Promise<<%~ it.returnType %>>);\n<% } else if(it.responseContentType === 'text') { %>\n .then((response) => response.text() as Promise<<%~ it.returnType %>>);\n<% } else { %>\n .then((response) => response.json() as Promise<<%~ it.returnType %>>);\n<% } %>\n },\n"
|
|
16
|
+
},
|
|
17
|
+
"ng1": {
|
|
18
|
+
"barrel.ejs": "export class ApiServices {\n public static bootstrap(moduleName: string, baseUrl: string) {\n angular\n .module(moduleName)\n .constant('Api<%= it.servicePrefix -%>BaseUrl', baseUrl)\n<% it.clients.forEach((client) => { %>\n .service('<%= client.fileName %>Service', <%= client.fileName %>Service)\n<% }); %>;\n }\n}\n\nfunction serializeQueryParam(obj: any, property: string): string {\n if (obj === null || obj === undefined || obj === '') {\n return '';\n } else if (obj instanceof Date) {\n return property + '=' + encodeURIComponent(obj.toJSON());\n } else if (Array.isArray(obj)) {\n return Object.values(obj)\n .map(value => `${property}[]=${value}`)\n .join('&');\n } else if (typeof obj !== 'object') {\n return property + '=' + encodeURIComponent(obj);\n } else if (typeof obj === 'object') {\n return Object.keys(obj)\n .filter(key => !!serializeQueryParam(obj[key], property + '.' + key))\n .reduce(\n (a: any, b) =>\n a.push(serializeQueryParam(obj[b], property + '.' + b)) && a,\n []\n )\n .join('&');\n } else {\n return '';\n }\n}\n",
|
|
19
|
+
"baseClient.ejs": "/* tslint:disable */\n/* eslint-disable */\n//----------------------\n// <auto-generated>\n// Generated using Swaggie (https://github.com/yhnavein/swaggie)\n// Please avoid doing any manual changes in this file\n// </auto-generated>\n//----------------------\n// ReSharper disable InconsistentNaming\n// deno-lint-ignore-file\n\nimport type { IHttpService, IRequestShortcutConfig, IPromise } from 'angular';\n\nabstract class BaseService {\n constructor(protected readonly $http: IHttpService, public baseUrl: string) { }\n\n protected $get<T>(\n url: string,\n config?: IRequestShortcutConfig\n ): IPromise<T> {\n return this.$http.get(this.baseUrl + url, config).then((response: any) => {\n return this.processSingle<T>(response);\n });\n }\n\n protected $getAll<T>(\n url: string,\n config?: IRequestShortcutConfig\n ): IPromise<T[]> {\n return this.$http.get(this.baseUrl + url, config).then((response: any) => {\n return this.processMany<T>(response);\n });\n }\n\n protected $delete<T>(\n url: string,\n config?: IRequestShortcutConfig\n ): IPromise<any> {\n return this.$http\n .delete(this.baseUrl + url, config)\n .then((response: any) => {\n return this.processSingle<T>(response);\n });\n }\n\n protected $head<T>(\n url: string,\n config?: IRequestShortcutConfig\n ): IPromise<any> {\n return this.$http.head(this.baseUrl + url, config).then((response: any) => {\n return this.processSingle<T>(response);\n });\n }\n\n protected $jsonp<T>(\n url: string,\n config?: IRequestShortcutConfig\n ): IPromise<any> {\n return this.$http\n .jsonp(this.baseUrl + url, config)\n .then((response: any) => {\n return this.processSingle<T>(response);\n });\n }\n\n protected $post<T>(\n url: string,\n data: any,\n config?: IRequestShortcutConfig\n ): IPromise<any> {\n return this.$http\n .post(this.baseUrl + url, data, config)\n .then((response: any) => {\n return this.processSingle<T>(response);\n });\n }\n\n protected $put<T>(\n url: string,\n data: any,\n config?: IRequestShortcutConfig\n ): IPromise<any> {\n return this.$http\n .put(this.baseUrl + url, data, config)\n .then((response: any) => {\n return this.processSingle<T>(response);\n });\n }\n\n protected $patch<T>(\n url: string,\n data: any,\n config?: IRequestShortcutConfig\n ): IPromise<any> {\n return this.$http\n .patch(this.baseUrl + url, data, config)\n .then((response: any) => {\n return this.processSingle<T>(response);\n });\n }\n\n protected processSingle<T>(response: any): T {\n var data = response.data;\n var status = response.status;\n\n if (status >= 200 && status <= 299) {\n return data;\n } else {\n throw 'error_no_callback_for_the_received_http_status';\n }\n }\n\n protected processMany<T>(response: any): T[] {\n var data = response.data;\n var status = response.status;\n\n if (status >= 200 && status <= 299) {\n return data;\n } else {\n throw 'error_no_callback_for_the_received_http_status';\n }\n }\n}\n\n",
|
|
20
|
+
"client.ejs": "export class <%= it.clientName -%>Service extends BaseService {\n /* @ngInject */\n constructor($http: IHttpService, Api<%= it.servicePrefix -%>BaseUrl: string) {\n super($http, Api<%= it.servicePrefix -%>BaseUrl);\n }\n\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n}\n\n",
|
|
21
|
+
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n config?: IRequestShortcutConfig\n ): IPromise<<%~ it.returnType %>> {\n let url = `<%= it.url %>?`;\n<% if(it.query && it.query.length > 0) { %>\n <% it.query.forEach((parameter) => { %>\n if (<%= parameter.name %> !== undefined) {\n <% if(!!parameter.original && parameter.original.type === 'array') { %>\n <%= parameter.name %>.forEach(item => { url += serializeQueryParam(item, '<%= parameter.originalName %>') + \"&\"; });\n <% } else {%>\n url += serializeQueryParam(<%= parameter.name %>, '<%= parameter.originalName %>') + \"&\";\n <% } %>\n }\n <% }); %>\n<% } %>\n\n return this.$<%= it.method.toLowerCase() %>(\n url,\n<% if(['POST', 'PUT', 'PATCH'].includes(it.method)) { %>\n <% if(it.body) { %>\n <%= it.body.contentType === 'urlencoded' ? 'new URLSearchParams(' + it.body.name + ' as any)' : it.body.name %>,\n <% } else { %>\n null,\n <% } %>\n<% } %>\n config\n );\n }\n"
|
|
22
|
+
},
|
|
23
|
+
"ng2": {
|
|
24
|
+
"barrel.ejs": "\n/**\n * Serializes a params object into a query string that is compatible with different REST APIs.\n * Implementation from: https://github.com/suhaotian/xior/blob/main/src/utils.ts\n * Kudos to @suhaotian for the original implementation\n */\nfunction encodeParams<T = any>(\n params: T,\n parentKey: string | null = null,\n options?: {\n allowDots?: boolean;\n serializeDate?: (value: Date) => string;\n arrayFormat?: 'indices' | 'repeat' | 'brackets';\n }\n): string {\n if (params === undefined || params === null) return '';\n const encodedParams: string[] = [];\n const paramsIsArray = Array.isArray(params);\n const { arrayFormat, allowDots, serializeDate } = options || {};\n\n const getKey = (key: string) => {\n if (allowDots && !paramsIsArray) return `.${key}`;\n if (paramsIsArray) {\n if (arrayFormat === 'brackets') {\n return '[]';\n }\n if (arrayFormat === 'repeat') {\n return '';\n }\n }\n return `[${key}]`;\n };\n\n for (const key in params) {\n if (Object.prototype.hasOwnProperty.call(params, key)) {\n let value = (params as any)[key];\n if (value !== undefined) {\n const encodedKey = parentKey ? `${parentKey}${getKey(key)}` : (key as string);\n\n // biome-ignore lint/suspicious/noGlobalIsNan: <explanation>\n if (!isNaN(value) && value instanceof Date) {\n value = serializeDate ? serializeDate(value) : value.toISOString();\n }\n if (typeof value === 'object') {\n // If the value is an object or array, recursively encode its contents\n const result = encodeParams(value, encodedKey, options);\n if (result !== '') encodedParams.push(result);\n } else {\n // Otherwise, encode the key-value pair\n encodedParams.push(`${encodeURIComponent(encodedKey)}=${encodeURIComponent(value)}`);\n }\n }\n }\n }\n\n return encodedParams.join('&');\n}\n\n",
|
|
25
|
+
"baseClient.ejs": "/* tslint:disable */\n/* eslint-disable */\n//----------------------\n// <auto-generated>\n// Generated using Swaggie (https://github.com/yhnavein/swaggie)\n// Please avoid doing any manual changes in this file\n// </auto-generated>\n//----------------------\n// ReSharper disable InconsistentNaming\n// deno-lint-ignore-file\n\nimport type { Observable } from \"rxjs\";\nimport { Injectable, Inject, Optional, InjectionToken } from \"@angular/core\";\nimport { HttpClient } from \"@angular/common/http\";\n\nexport const <%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL = new InjectionToken<string>(\"<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL\");\n\nabstract class BaseService {\n private httpClient: HttpClient;\n private baseUrl: string;\n\n constructor(\n @Inject(HttpClient) httpClient: HttpClient,\n @Optional() @Inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL) baseUrl?: string\n ) {\n this.httpClient = httpClient;\n this.baseUrl = baseUrl ? baseUrl : '';\n }\n\n protected $get<T>(url: string, options?: any): Observable<T> {\n return this.httpClient\n .get<T>(this.baseUrl + url, options)\n .pipe((response: any) => response);\n }\n\n protected $getAll<T>(url: string, options?: any): Observable<T[]> {\n return this.httpClient\n .get<T[]>(this.baseUrl + url, options)\n .pipe((response: any) => response);\n }\n\n protected $delete<T>(url: string, options?: any): Observable<T> {\n return this.httpClient\n .delete(this.baseUrl + url, options)\n .pipe((response: any) => response);\n }\n\n protected $post(url: string, data: any, options?: any): Observable<any> {\n return this.httpClient\n .post(this.baseUrl + url, data, options)\n .pipe((response: any) => response);\n }\n\n protected $patch<T>(url: string, data: any, options?: any): Observable<T> {\n return this.httpClient\n .patch(this.baseUrl + url, data, options)\n .pipe((response: any) => response);\n }\n\n protected $put(url: string, data: any, options?: any): Observable<any> {\n return this.httpClient\n .put(this.baseUrl + url, data, options)\n .pipe((response: any) => response);\n }\n}\n\nfunction paramsSerializer(params: any) {\n return encodeParams(params, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n });\n}\n\n",
|
|
26
|
+
"client.ejs": "@Injectable({\n providedIn: 'root'\n})\nexport class <%= it.clientName -%>Service extends BaseService {\n constructor(\n @Inject(HttpClient) httpClient: HttpClient,\n @Optional() @Inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL) baseUrl?: string\n ) {\n super(httpClient, baseUrl);\n }\n\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n}\n\n",
|
|
27
|
+
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(\n <% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\nconfig?: any\n ): Observable<<%~ it.returnType %>> {\n const url = `<%= it.url %>?<%\n if(it.query && it.query.length > 0) { %>${paramsSerializer({<%\n it.query.forEach((parameter) => { %>\n'<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>})}<% } %>`;\n\n return this.$<%= it.method.toLowerCase() %>(\n url,\n<% if(['POST', 'PUT', 'PATCH'].includes(it.method)) { %>\n<% if(it.body) { %>\n <%= it.body.contentType === 'urlencoded' ? 'new URLSearchParams(' + it.body.name + ' as any)' : it.body.name %>,\n<% } else { %>\n null,\n<% } %>\n<% } %>\n config\n );\n }\n"
|
|
28
|
+
},
|
|
29
|
+
"swr-axios": {
|
|
30
|
+
"barrel.ejs": "\n/**\n * Serializes a params object into a query string that is compatible with different REST APIs.\n * Implementation from: https://github.com/suhaotian/xior/blob/main/src/utils.ts\n * Kudos to @suhaotian for the original implementation\n */\nfunction encodeParams<T = any>(\n params: T,\n parentKey: string | null = null,\n options?: {\n allowDots?: boolean;\n serializeDate?: (value: Date) => string;\n arrayFormat?: 'indices' | 'repeat' | 'brackets';\n }\n): string {\n if (params === undefined || params === null) return '';\n const encodedParams: string[] = [];\n const paramsIsArray = Array.isArray(params);\n const { arrayFormat, allowDots, serializeDate } = options || {};\n\n const getKey = (key: string) => {\n if (allowDots && !paramsIsArray) return `.${key}`;\n if (paramsIsArray) {\n if (arrayFormat === 'brackets') {\n return '[]';\n }\n if (arrayFormat === 'repeat') {\n return '';\n }\n }\n return `[${key}]`;\n };\n\n for (const key in params) {\n if (Object.prototype.hasOwnProperty.call(params, key)) {\n let value = (params as any)[key];\n if (value !== undefined) {\n const encodedKey = parentKey ? `${parentKey}${getKey(key)}` : (key as string);\n\n // biome-ignore lint/suspicious/noGlobalIsNan: <explanation>\n if (!isNaN(value) && value instanceof Date) {\n value = serializeDate ? serializeDate(value) : value.toISOString();\n }\n if (typeof value === 'object') {\n // If the value is an object or array, recursively encode its contents\n const result = encodeParams(value, encodedKey, options);\n if (result !== '') encodedParams.push(result);\n } else {\n // Otherwise, encode the key-value pair\n encodedParams.push(`${encodeURIComponent(encodedKey)}=${encodeURIComponent(value)}`);\n }\n }\n }\n }\n\n return encodedParams.join('&');\n}\n\n",
|
|
31
|
+
"baseClient.ejs": "/* tslint:disable */\n/* eslint-disable */\n//----------------------\n// <auto-generated>\n// Generated using Swaggie (https://github.com/yhnavein/swaggie)\n// Please avoid doing any manual changes in this file\n// </auto-generated>\n//----------------------\n// ReSharper disable InconsistentNaming\n// deno-lint-ignore-file\n\nimport Axios, { type AxiosPromise, type AxiosRequestConfig } from \"axios\";\nimport useSWR, { type SWRConfiguration, type Key } from 'swr';\n\nexport const axios = Axios.create({\n baseURL: '<%= it.baseUrl || '' -%>',\n paramsSerializer: (params: any) =>\n encodeParams(params, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\n\ninterface SwrConfig extends SWRConfiguration {\n /* 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. */\n key?: Key;\n\n /* Configuration for axios fetcher */\n axios?: AxiosRequestConfig;\n}\n",
|
|
32
|
+
"client.ejs": "export const <%= it.camelCaseName -%>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n <% }); %>\n};\n\n<% var getOperations = it.operations.filter((o) => o.method === 'GET');\nif(getOperations.length > 0) { %>\n <% getOperations.forEach((operation) => {\n var opName = operation.name;\n if(opName.toLowerCase().startsWith(\"get\")) {\n opName = opName.substring(3);\n }\n opName[0] = opName[0].toUpperCase();\n var customName = \"use\" + it.clientName + opName;\n var swrOperation = Object.assign({ swrOpName: customName }, operation); %>\n<%~ include('swrOperation.ejs', swrOperation); %>\n\n <% }); %>\n<% } %>\n",
|
|
33
|
+
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n $config?: AxiosRequestConfig\n ): AxiosPromise<<%~ it.returnType %>> {\n const url = `<%= it.url %>`;\n\n return axios.request<<%~ it.returnType %>>({\n url: url,\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'urlencoded') { %>\n data: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n data: <%= it.body.name %>,\n<% } %>\n<% } %>\n<% if(it.query && it.query.length > 0) { %>\n params: {\n <% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>\n },\n<% } %>\n<% if(it.headers && it.headers.length > 0) { %>\n headers: {\n <% it.headers.forEach((parameter) => { %>\n <% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n <% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% } %>\n <% }); %>\n},\n<% } %>\n ...$config,\n });\n },\n",
|
|
34
|
+
"swrOperation.ejs": "<%~ it.jsDocs %>\n\nexport function <%= it.swrOpName %>(<% it.parameters.forEach((parameter) => { %>\n <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n $config?: SwrConfig\n ) {\n const url = `<%= it.url %>`;\n const { axios: $axiosConf, key, ...config } = $config || {};\n\n const cacheUrl = `${url}?<%\n if(it.query && it.query.length > 0) { %>${encodeParams({<%\n it.query.forEach((parameter) => { %>\n'<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>})}<% } %>`;\n\nconst { data, error, mutate } = useSWR<<%~ it.returnType %>>(\n key ?? cacheUrl,\n () => axios.request({\n url: url,\n method: '<%= it.method %>',\n<% if(it.query && it.query.length > 0) { %>\n params: {\n <% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>\n},\n<% } %>\n<% if(it.headers && it.headers.length > 0) { %>\n headers: {\n <% it.headers.forEach((parameter) => { %>\n <% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n <% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% } %>\n <% }); %>\n },\n<% } %>\n ...$axiosConf})\n .then((resp) => resp.data),\n config);\n\n return {\n data,\n isLoading: !error && !data,\n error: error,\n mutate,\n };\n}\n"
|
|
35
|
+
},
|
|
36
|
+
"tsq-xior": {
|
|
37
|
+
"barrel.ejs": "",
|
|
38
|
+
"baseClient.ejs": "/* tslint:disable */\n/* eslint-disable */\n//----------------------\n// <auto-generated>\n// Generated using Swaggie (https://github.com/yhnavein/swaggie)\n// Please avoid doing any manual changes in this file\n// </auto-generated>\n//----------------------\n// ReSharper disable InconsistentNaming\n// deno-lint-ignore-file\n\nimport xior, { type XiorResponse, type XiorRequestConfig, encodeParams } from \"xior\";\nimport { QueryClient, type UseQueryOptions, useQuery } from '@tanstack/react-query';\n\nexport const queryClient = new QueryClient();\n\nexport const http = xior.create({\n baseURL: '<%= it.baseUrl || '' %>',\n paramsSerializer: (params) =>\n encodeParams(params, true, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\n\n",
|
|
39
|
+
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n\n<% var getOperations = it.operations.filter((o) => o.method === 'GET');\nif(getOperations.length > 0) { %>\n <% getOperations.forEach((operation) => {\n var opName = operation.name;\n if(opName.toLowerCase().startsWith(\"get\")) {\n opName = opName.substring(3);\n }\n opName[0] = opName[0].toUpperCase();\n var customName = \"use\" + it.clientName + opName;\n var queryOperation = Object.assign({ rqOpName: customName, opKey: it.clientName + opName, clientName: it.camelCaseName }, operation); %>\n<%~ include('queryOperation.ejs', queryOperation); %>\n\n <% }); %>\n<% } %>\n",
|
|
40
|
+
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n$config?: XiorRequestConfig\n ): Promise<XiorResponse<<%~ it.returnType %>>> {\n const url = `<%= it.url %>`;\n\n return http.request<<%~ it.returnType %>>({\n url: url,\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'urlencoded') { %>\n data: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n data: <%= it.body.name %>,\n<% } %>\n<% } %>\n<% if(it.query && it.query.length > 0) { %>\n params: {\n <% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>\n },\n<% } %>\n<% if(it.headers && it.headers.length > 0) { %>\n headers: {\n <% it.headers.forEach((parameter) => { %>\n <% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n <% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% } %>\n <% }); %>\n},\n<% } %>\n ...$config,\n });\n },\n",
|
|
41
|
+
"queryOperation.ejs": "<%\nif (it.jsDocs) {\n const additionalParams = ` * @param $config (optional) Additional configuration for TanStack Query\n * @param $httpConfig (optional) Additional configuration for xior request (actually executes the request)`;\n\n // Replace the closing */ with newline + additional params + closing */\n const modifiedDocs = it.jsDocs.replace(/(\\s*)\\*\\/\\s*$/, `\\n${additionalParams}\\n */`);\n-%>\n<%~ modifiedDocs %>\n<% } else { -%>\n<%~ it.jsDocs %>\n<% } %>\n\nexport function <%= it.rqOpName %><TData = <%~ it.returnType %>, TError = Error>(<% it.parameters.forEach((parameter) => { %>\n <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n$config?: Omit<\n UseQueryOptions<<%~ it.returnType %>, TError, TData>,\n 'queryKey' | 'queryFn'\n>,\n $httpConfig?: XiorRequestConfig\n ) {\n return useQuery<<%~ it.returnType %>, TError, TData>({\n queryKey: ['<%= it.clientName %>', '<%= it.opKey %>', <% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>],\n queryFn: () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %>, <% }); %>$httpConfig).then(res => res.data),\n ...$config\n });\n}\n<%= it.rqOpName %>.queryKeys = ['<%= it.clientName %>', '<%= it.opKey %>'];\n"
|
|
42
|
+
},
|
|
43
|
+
"xior": {
|
|
44
|
+
"barrel.ejs": "",
|
|
45
|
+
"baseClient.ejs": "/* tslint:disable */\n/* eslint-disable */\n//----------------------\n// <auto-generated>\n// Generated using Swaggie (https://github.com/yhnavein/swaggie)\n// Please avoid doing any manual changes in this file\n// </auto-generated>\n//----------------------\n// ReSharper disable InconsistentNaming\n// deno-lint-ignore-file\n\nimport xior, { type XiorResponse, type XiorRequestConfig, encodeParams } from \"xior\";\n\nexport const http = xior.create({\n baseURL: '<%= it.baseUrl || '' %>',\n paramsSerializer: (params) =>\n encodeParams(params, true, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\n\n",
|
|
46
|
+
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n",
|
|
47
|
+
"operation.ejs": "<%~ it.jsDocs %>\n\n <%= it.name %>(<% it.parameters.forEach((parameter) => { %>\n<%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,\n <% }); %>\n$config?: XiorRequestConfig\n ): Promise<XiorResponse<<%~ it.returnType %>>> {\n const url = `<%= it.url %>`;\n\n return http.request<<%~ it.returnType %>>({\n url: url,\n method: '<%= it.method %>',\n<% if(it.body) { %>\n<% if(it.body.contentType === 'urlencoded') { %>\n data: new URLSearchParams(<%= it.body.name %> as any),\n<% } else { %>\n data: <%= it.body.name %>,\n<% } %>\n<% } %>\n<% if(it.query && it.query.length > 0) { %>\n params: {\n <% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>\n },\n<% } %>\n<% if(it.headers && it.headers.length > 0) { %>\n headers: {\n <% it.headers.forEach((parameter) => { %>\n <% if (parameter.value) { %>\n '<%= parameter.originalName %>': '<%= parameter.value %>',\n <% } else { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% } %>\n <% }); %>\n},\n<% } %>\n ...$config,\n });\n },\n"
|
|
48
|
+
}
|
|
49
|
+
} ; exports.BUNDLED_TEMPLATES = BUNDLED_TEMPLATES;
|
|
50
|
+
|
|
51
|
+
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
2
|
|
|
3
|
-
var _utils = require('../utils');
|
|
3
|
+
var _utils = require('../utils/utils');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Converts a parameter object to a TypeScript type.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _yaml = require('yaml');
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Browser-safe OpenAPI loader.
|
|
6
|
+
* Supports URL and already parsed objects.
|
|
7
|
+
*/
|
|
8
|
+
async function loadSpecDocument(src) {
|
|
9
|
+
if (typeof src !== 'string') {
|
|
10
|
+
return src ;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!/^https?:\/\//im.test(src)) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
`Unable to load api at '${src}'. In browser mode pass URL or parsed object as "src".`
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const response = await fetch(src);
|
|
20
|
+
if (!response.ok) {
|
|
21
|
+
throw new Error(`Failed to load API from '${src}': ${response.statusText}`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const contents = await response.text();
|
|
25
|
+
return parseFileContents(contents, src) ;
|
|
26
|
+
} exports.loadSpecDocument = loadSpecDocument;
|
|
27
|
+
|
|
28
|
+
function parseFileContents(contents, path) {
|
|
29
|
+
if (/.ya?ml$/i.test(path)) {
|
|
30
|
+
return _yaml.parse.call(void 0, contents);
|
|
31
|
+
}
|
|
32
|
+
if (/.json$/i.test(path)) {
|
|
33
|
+
return JSON.parse(contents);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const firstChar = contents.trimStart()[0];
|
|
37
|
+
if (firstChar === '{') {
|
|
38
|
+
return JSON.parse(contents);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return _yaml.parse.call(void 0, contents);
|
|
42
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});var _nodefs = require('node:fs');
|
|
2
|
+
var _nodepath = require('node:path');
|
|
3
|
+
|
|
4
|
+
function saveFile(filePath, contents) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
_nodefs.mkdir.call(void 0, _nodepath.dirname.call(void 0, filePath), { recursive: true }, (err) => {
|
|
7
|
+
if (err) {
|
|
8
|
+
reject(err);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
_nodefs.writeFileSync.call(void 0, filePath, contents);
|
|
12
|
+
resolve(true);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
} exports.saveFile = saveFile;
|
package/dist/utils/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
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, configurable: true, get: () => obj[key]}); }); }var _utils = require('./utils'); _createStarExport(_utils);
|
|
2
|
+
var _fileUtils = require('./fileUtils'); _createStarExport(_fileUtils);
|
|
2
3
|
var _documentLoader = require('./documentLoader'); _createStarExport(_documentLoader);
|
|
3
4
|
var _refResolver = require('./refResolver'); _createStarExport(_refResolver);
|
|
4
5
|
var _templateManager = require('./templateManager'); _createStarExport(_templateManager);
|
|
6
|
+
var _templateEngine = require('./templateEngine'); _createStarExport(_templateEngine);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _eta = require('eta');
|
|
2
|
+
|
|
3
|
+
let engine;
|
|
4
|
+
|
|
5
|
+
function initTemplateEngineFromDirectory(templatesDir) {
|
|
6
|
+
engine = new (0, _eta.Eta)({ views: templatesDir });
|
|
7
|
+
} exports.initTemplateEngineFromDirectory = initTemplateEngineFromDirectory;
|
|
8
|
+
|
|
9
|
+
function initTemplateEngineFromBundled(templateFiles) {
|
|
10
|
+
engine = new (0, _eta.Eta)({ views: '.' });
|
|
11
|
+
engine.resolvePath = (template) => template;
|
|
12
|
+
engine.readFile = (templatePath) => {
|
|
13
|
+
const directMatch = templateFiles[templatePath];
|
|
14
|
+
if (directMatch !== undefined) {
|
|
15
|
+
return directMatch;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const baseName = _nullishCoalesce(_optionalChain([templatePath, 'access', _ => _.split, 'call', _2 => _2('/'), 'access', _3 => _3.pop, 'call', _4 => _4(), 'optionalAccess', _5 => _5.split, 'call', _6 => _6('\\'), 'access', _7 => _7.pop, 'call', _8 => _8()]), () => ( templatePath));
|
|
19
|
+
return templateFiles[baseName];
|
|
20
|
+
};
|
|
21
|
+
} exports.initTemplateEngineFromBundled = initTemplateEngineFromBundled;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Get's a template file and renders it with the provided data.
|
|
25
|
+
*/
|
|
26
|
+
function renderFile(templateFile, data = {}) {
|
|
27
|
+
if (!engine) {
|
|
28
|
+
throw new Error('Template engine has not been initialized');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return engine.render(templateFile, data);
|
|
32
|
+
} exports.renderFile = renderFile;
|
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _nodefs = require('node:fs'); var _nodefs2 = _interopRequireDefault(_nodefs);
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _nodefs = require('node:fs'); var _nodefs2 = _interopRequireDefault(_nodefs);
|
|
2
2
|
var _nodepath = require('node:path'); var _nodepath2 = _interopRequireDefault(_nodepath);
|
|
3
|
-
var
|
|
3
|
+
var _templateEngine = require('./templateEngine');
|
|
4
|
+
let bundledTemplates = null;
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
function setBundledTemplates(templates) {
|
|
9
|
+
bundledTemplates = templates;
|
|
10
|
+
} exports.setBundledTemplates = setBundledTemplates;
|
|
6
11
|
|
|
7
12
|
function loadAllTemplateFiles(templateName) {
|
|
8
13
|
if (!templateName) {
|
|
9
14
|
throw new Error('No template name was provided');
|
|
10
15
|
}
|
|
11
16
|
|
|
17
|
+
if (loadFromBundledTemplates(templateName)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
12
21
|
const templatesDir = _nodefs2.default.existsSync(templateName)
|
|
13
22
|
? templateName
|
|
14
23
|
: _nodepath2.default.join(__dirname, '..', '..', 'templates', templateName);
|
|
@@ -18,12 +27,16 @@ let engine;
|
|
|
18
27
|
`Could not find directory with the template (we tried ${templatesDir}). Is the template name correct?`
|
|
19
28
|
);
|
|
20
29
|
}
|
|
21
|
-
|
|
30
|
+
_templateEngine.initTemplateEngineFromDirectory.call(void 0, templatesDir);
|
|
22
31
|
} exports.loadAllTemplateFiles = loadAllTemplateFiles;
|
|
23
32
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
33
|
+
function loadFromBundledTemplates(templateName) {
|
|
34
|
+
const templateFiles = _optionalChain([bundledTemplates, 'optionalAccess', _ => _[templateName]]);
|
|
35
|
+
if (!templateFiles) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_templateEngine.initTemplateEngineFromBundled.call(void 0, templateFiles);
|
|
40
|
+
|
|
41
|
+
return true;
|
|
42
|
+
}
|
package/dist/utils/utils.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
-
var _nodepath = require('node:path');
|
|
3
|
-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
4
2
|
|
|
5
3
|
|
|
6
4
|
|
|
@@ -130,19 +128,6 @@ const reservedKeywords = new Set([
|
|
|
130
128
|
|
|
131
129
|
|
|
132
130
|
|
|
133
|
-
function saveFile(filePath, contents) {
|
|
134
|
-
return new Promise((resolve, reject) => {
|
|
135
|
-
_nodefs.mkdir.call(void 0, _nodepath.dirname.call(void 0, filePath), { recursive: true }, (err) => {
|
|
136
|
-
if (err) {
|
|
137
|
-
reject(err);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
_nodefs.writeFileSync.call(void 0, filePath, contents);
|
|
141
|
-
resolve(true);
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
} exports.saveFile = saveFile;
|
|
145
|
-
|
|
146
131
|
/**
|
|
147
132
|
* Operations list contains tags, which can be used to group them.
|
|
148
133
|
* The grouping allows us to generate multiple client classes dedicated
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swaggie",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.4-beta.1",
|
|
4
4
|
"description": "Generate a fully typed TypeScript API client from your OpenAPI 3 spec",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Piotr Dabrowski",
|
|
@@ -20,13 +20,30 @@
|
|
|
20
20
|
},
|
|
21
21
|
"main": "dist/index.js",
|
|
22
22
|
"types": "dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./browser": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/browser.js"
|
|
31
|
+
},
|
|
32
|
+
"./dist/*": "./dist/*",
|
|
33
|
+
"./templates/*": "./templates/*",
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
23
36
|
"bin": {
|
|
24
37
|
"swaggie": "dist/cli.js"
|
|
25
38
|
},
|
|
26
39
|
"scripts": {
|
|
27
|
-
"build": "sucrase ./src -d ./dist --transforms typescript,imports && bun run rm-tests && bun run types",
|
|
40
|
+
"build": "bun run bundle-templates && sucrase ./src -d ./dist --transforms typescript,imports && bun run rm-tests && bun run types",
|
|
41
|
+
"bundle-templates": "bun scripts/bundle-templates.ts",
|
|
28
42
|
"rm-tests": "find dist/ \\( -name '*.spec.js' -o -name 'types.js' \\) -type f -delete",
|
|
29
|
-
"types": "tsc src/types.ts --outDir dist/ --declaration --emitDeclarationOnly && cp test/index.d.ts ./dist/"
|
|
43
|
+
"types": "tsc src/types.ts --outDir dist/ --declaration --emitDeclarationOnly && cp test/index.d.ts ./dist/",
|
|
44
|
+
"docs:build": "vitepress build docs",
|
|
45
|
+
"docs:dev": "vitepress dev docs",
|
|
46
|
+
"docs:preview": "vitepress preview docs"
|
|
30
47
|
},
|
|
31
48
|
"files": [
|
|
32
49
|
"dist",
|
|
@@ -62,6 +79,7 @@
|
|
|
62
79
|
"bun-types": "1.3.10",
|
|
63
80
|
"openapi-types": "^12.1.3",
|
|
64
81
|
"sucrase": "3.35.1",
|
|
65
|
-
"typescript": "5.9.3"
|
|
82
|
+
"typescript": "5.9.3",
|
|
83
|
+
"vitepress": "^2.0.0-alpha.16"
|
|
66
84
|
}
|
|
67
85
|
}
|