swaggie 1.8.8 → 1.9.0-alpha.2
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 +1 -1
- package/dist/gen/genOperations.js +2 -1
- package/dist/gen/header.js +2 -1
- package/dist/generated/bundledTemplates.js +14 -13
- package/package.json +1 -1
- package/templates/axios/baseClient.ejs +0 -11
- package/templates/fetch/baseClient.ejs +0 -11
- package/templates/ng1/baseClient.ejs +0 -11
- package/templates/ng2/baseClient.ejs +1 -61
- package/templates/ng2/client.ejs +3 -7
- package/templates/ng2/operation.ejs +18 -15
- package/templates/swr-axios/baseClient.ejs +1 -11
- package/templates/swr-axios/client.ejs +30 -12
- package/templates/swr-axios/swrOperation.ejs +41 -43
- package/templates/tsq-xior/baseClient.ejs +2 -15
- package/templates/tsq-xior/client.ejs +45 -12
- package/templates/tsq-xior/mutationOperation.ejs +31 -0
- package/templates/tsq-xior/queryOperation.ejs +18 -29
- package/templates/xior/baseClient.ejs +1 -12
package/README.md
CHANGED
|
@@ -179,7 +179,7 @@ import Axios, { AxiosPromise } from 'axios';
|
|
|
179
179
|
|
|
180
180
|
const axios = Axios.create({
|
|
181
181
|
baseURL: '/api',
|
|
182
|
-
paramsSerializer: (params) =>
|
|
182
|
+
paramsSerializer: (params: any) =>
|
|
183
183
|
encodeParams(params, null, {
|
|
184
184
|
allowDots: true,
|
|
185
185
|
arrayFormat: 'repeat',
|
|
@@ -11,6 +11,7 @@ var _swagger = require('../swagger');
|
|
|
11
11
|
var _utils = require('../utils/utils');
|
|
12
12
|
var _templateEngine = require('../utils/templateEngine');
|
|
13
13
|
var _createBarrel = require('./createBarrel');
|
|
14
|
+
var _header = require('./header');
|
|
14
15
|
|
|
15
16
|
|
|
16
17
|
var _jsDocs = require('./jsDocs');
|
|
@@ -25,7 +26,7 @@ var _jsDocs = require('./jsDocs');
|
|
|
25
26
|
const operations = _swagger.getOperations.call(void 0, spec);
|
|
26
27
|
const groups = _utils.groupOperationsByGroupName.call(void 0, operations);
|
|
27
28
|
const servicePrefix = options.servicePrefix;
|
|
28
|
-
let result = _templateEngine.renderFile.call(void 0, 'baseClient.ejs', {
|
|
29
|
+
let result = _header.FILE_HEADER + _templateEngine.renderFile.call(void 0, 'baseClient.ejs', {
|
|
29
30
|
servicePrefix,
|
|
30
31
|
baseUrl: options.baseUrl,
|
|
31
32
|
...options.queryParamsSerialization,
|
package/dist/gen/header.js
CHANGED
|
@@ -12,4 +12,5 @@
|
|
|
12
12
|
'// </auto-generated>\n' +
|
|
13
13
|
'//----------------------\n' +
|
|
14
14
|
'// ReSharper disable InconsistentNaming\n' +
|
|
15
|
-
'// deno-lint-ignore-file\n
|
|
15
|
+
'// deno-lint-ignore-file\n' +
|
|
16
|
+
'// biome-ignore-all lint: auto-generated code\n\n'; exports.FILE_HEADER = FILE_HEADER;
|
|
@@ -4,45 +4,46 @@
|
|
|
4
4
|
const BUNDLED_TEMPLATES = {
|
|
5
5
|
"axios": {
|
|
6
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": "
|
|
7
|
+
"baseClient.ejs": "import 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
8
|
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n",
|
|
9
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
10
|
},
|
|
11
11
|
"fetch": {
|
|
12
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": "
|
|
13
|
+
"baseClient.ejs": "export 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
14
|
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n",
|
|
15
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
16
|
},
|
|
17
17
|
"ng1": {
|
|
18
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": "
|
|
19
|
+
"baseClient.ejs": "import 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
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
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
22
|
},
|
|
23
23
|
"ng2": {
|
|
24
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": "
|
|
26
|
-
"client.ejs": "@Injectable({\n providedIn: 'root'\n})\nexport class <%= it.clientName -%>Service
|
|
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
|
|
25
|
+
"baseClient.ejs": "import type { Observable } from \"rxjs\";\nimport { Injectable, InjectionToken, inject } 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\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.charAt(0).toUpperCase() + it.clientName.slice(1) -%>Service {\n private http = inject(HttpClient);\n private baseUrl = inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL, { optional: true }) ?? '';\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<% if(it.query && it.query.length > 0) { -%>\n const params = paramsSerializer({<%\n it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n <% }); %>});\n const url = `<%= it.url %>${params ? '?' + params : ''}`;\n<% } else { -%>\n const url = `<%= it.url %>`;\n<% } -%>\n<% if(it.method === 'GET') { %>\n return this.http.get<<%~ it.returnType %>>(this.baseUrl + url, config);\n<% } else if(it.method === 'DELETE') { %>\n return this.http.delete<<%~ it.returnType %>>(this.baseUrl + url, config);\n<% } else if(['POST', 'PUT', 'PATCH'].includes(it.method)) {\n const bodyArg = it.body\n ? (it.body.contentType === 'urlencoded' ? 'new URLSearchParams(' + it.body.name + ' as any)' : it.body.name)\n : 'null';\n%>\n return this.http.<%= it.method.toLowerCase() %><%~ '<' + it.returnType + '>' %>(this.baseUrl + url, <%~ bodyArg %>, config);\n<% } %>\n }\n"
|
|
28
28
|
},
|
|
29
29
|
"swr-axios": {
|
|
30
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": "
|
|
32
|
-
"client.ejs": "export const <%= it.camelCaseName -%>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n <% }); %>\n};\n\n
|
|
31
|
+
"baseClient.ejs": "import 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\n",
|
|
32
|
+
"client.ejs": "export const <%= it.camelCaseName -%>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n <% }); %>\n};\n\n<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\n\nfunction toOpName(name) {\n var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;\n return n.charAt(0).toUpperCase() + n.slice(1);\n}\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var swrOperation = Object.assign({\n swrOpName: 'use' + opName,\n clientName: it.camelCaseName,\n }, operation);\n%>\n<%~ include('swrOperation.ejs', swrOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n%>\n <%= keyName %>: (<% operation.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => `<%= operation.url %><% if(operation.query && operation.query.length > 0) { %>?${encodeParams({<% operation.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= parameter.name %>, <% }); %>})}<% } %>`,\n<% }); %>\n },\n};\n",
|
|
33
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": "
|
|
34
|
+
"swrOperation.ejs": "<% var docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\\n' : ''; %>\n<%~ docs %>\n <%= it.swrOpName %>(\n<% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,\n<% }); %> $config?: SwrConfig\n ) {\n const url = `<%= it.url %>`;\n const { axios: $axiosConf, key, ...config } = $config || {};\n\n<% if(it.query && it.query.length > 0) { %>\n const cacheKey = encodeParams({<% it.query.forEach((parameter) => { %>\n '<%= parameter.originalName %>': <%= parameter.name %>,<% }); %>\n });\n const cacheUrl = cacheKey ? `${url}?${cacheKey}` : url;\n<% } else { %>\n const cacheUrl = url;\n<% } %>\n\n const { data, error, isLoading, 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\n return { data, isLoading, error, mutate };\n },\n"
|
|
35
35
|
},
|
|
36
36
|
"tsq-xior": {
|
|
37
37
|
"barrel.ejs": "",
|
|
38
|
-
"baseClient.ejs": "
|
|
39
|
-
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n
|
|
38
|
+
"baseClient.ejs": "import xior, { type XiorResponse, type XiorRequestConfig, encodeParams } from \"xior\";\nimport { type UseQueryOptions, type UseMutationOptions, useQuery, useMutation } from '@tanstack/react-query';\n\nexport const http = xior.create({\n baseURL: '<%= it.baseUrl || '' %>',\n paramsSerializer: (params: any) =>\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<%\nvar getOperations = it.operations.filter((o) => o.method === 'GET');\nvar mutationOperations = it.operations.filter((o) => o.method !== 'GET');\n\n// Helper: strip leading \"get\" and capitalise\nfunction toOpName(name) {\n var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;\n return n.charAt(0).toUpperCase() + n.slice(1);\n}\n%>\n\nexport const <%= it.camelCaseName %> = {\n queries: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var queryOperation = Object.assign({\n rqOpName: 'use' + opName,\n opKey: it.camelCaseName + opName,\n clientName: it.camelCaseName,\n }, operation);\n%>\n<%~ include('queryOperation.ejs', queryOperation); %>\n\n<% }); %>\n },\n\n mutations: {\n<% mutationOperations.forEach((operation) => {\n var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);\n var mutationOperation = Object.assign({\n mutOpName: 'use' + opName,\n clientName: it.camelCaseName,\n }, operation);\n%>\n<%~ include('mutationOperation.ejs', mutationOperation); %>\n\n<% }); %>\n },\n\n queryKeys: {\n<% getOperations.forEach((operation) => {\n var opName = toOpName(operation.name);\n var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);\n%>\n <%= keyName %>: (<% operation.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% operation.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,\n<% }); %>\n },\n};\n",
|
|
40
|
+
"mutationOperation.ejs": "<%\nvar hasParams = it.parameters.length > 0;\n\nvar variablesType;\nif (!hasParams) {\n variablesType = 'void';\n} else {\n var parts = it.parameters.map(function(p) {\n return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');\n });\n variablesType = '{ ' + parts.join('; ') + ' }';\n}\n\nvar callArgs = it.parameters.map(function(p) { return 'vars.' + p.name; });\n\nvar baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\\n * @param $httpConfig (optional) Additional configuration for xior request (actually executes the request)';\nvar rawDocs = it.jsDocs\n ? it.jsDocs.replace(/(\\s*)\\*\\/\\s*$/, '\\n' + baseAdditionalParams + '\\n */')\n : '';\nvar docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\\n' : '';\n%>\n<%~ docs %>\n <%= it.mutOpName %><TData = <%~ it.returnType %>, TError = Error>(\n $config?: UseMutationOptions<<%~ it.returnType %>, TError, <%~ variablesType %>>,\n $httpConfig?: XiorRequestConfig\n ) {\n return useMutation<<%~ it.returnType %>, TError, <%~ variablesType %>>({\n mutationFn: (<%= hasParams ? 'vars' : '' %>) => <%= it.clientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig).then(res => res.data),\n ...$config\n });\n },\n",
|
|
40
41
|
"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": "<%\
|
|
42
|
+
"queryOperation.ejs": "<%\nvar baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\\n * @param $httpConfig (optional) Additional configuration for xior request (actually executes the request)';\nvar rawDocs = it.jsDocs\n ? it.jsDocs.replace(/(\\s*)\\*\\/\\s*$/, '\\n' + baseAdditionalParams + '\\n */')\n : '';\nvar docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\\n' : '';\n%>\n<%~ docs %>\n <%= it.rqOpName %><TData = <%~ it.returnType %>, TError = Error>(\n<% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,\n<% }); %> $config?: Omit<UseQueryOptions<<%~ it.returnType %>, TError, TData>, 'queryKey' | 'queryFn'>,\n $httpConfig?: XiorRequestConfig\n ) {\n return useQuery<<%~ it.returnType %>, TError, TData>({\n queryKey: <%= it.clientName %>.queryKeys.<%= it.rqOpName.charAt(3).toLowerCase() + it.rqOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>),\n queryFn: () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig).then(res => res.data),\n ...$config\n });\n },\n"
|
|
42
43
|
},
|
|
43
44
|
"xior": {
|
|
44
45
|
"barrel.ejs": "",
|
|
45
|
-
"baseClient.ejs": "
|
|
46
|
+
"baseClient.ejs": "import xior, { type XiorResponse, type XiorRequestConfig, encodeParams } from \"xior\";\n\nexport const http = xior.create({\n baseURL: '<%= it.baseUrl || '' %>',\n paramsSerializer: (params: any) =>\n encodeParams(params, true, null, {\n allowDots: <%= it.allowDots %>,\n arrayFormat: '<%= it.arrayFormat %>',\n }),\n});\n\n",
|
|
46
47
|
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', operation); %>\n\n<% }); %>\n};\n\n",
|
|
47
48
|
"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
|
}
|
package/package.json
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
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
|
-
// deno-lint-ignore-file
|
|
11
|
-
|
|
12
1
|
import Axios, { type AxiosPromise, type AxiosRequestConfig } from "axios";
|
|
13
2
|
|
|
14
3
|
export const axios = Axios.create({
|
|
@@ -1,14 +1,3 @@
|
|
|
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
|
-
// deno-lint-ignore-file
|
|
11
|
-
|
|
12
1
|
export const defaults = {
|
|
13
2
|
baseUrl: '<%= it.baseUrl || '' %>',
|
|
14
3
|
paramsSerializer: (params: any) =>
|
|
@@ -1,14 +1,3 @@
|
|
|
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
|
-
// deno-lint-ignore-file
|
|
11
|
-
|
|
12
1
|
import type { IHttpService, IRequestShortcutConfig, IPromise } from 'angular';
|
|
13
2
|
|
|
14
3
|
abstract class BaseService {
|
|
@@ -1,69 +1,9 @@
|
|
|
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
|
-
// deno-lint-ignore-file
|
|
11
|
-
|
|
12
1
|
import type { Observable } from "rxjs";
|
|
13
|
-
import { Injectable,
|
|
2
|
+
import { Injectable, InjectionToken, inject } from "@angular/core";
|
|
14
3
|
import { HttpClient } from "@angular/common/http";
|
|
15
4
|
|
|
16
5
|
export const <%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL = new InjectionToken<string>("<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL");
|
|
17
6
|
|
|
18
|
-
abstract class BaseService {
|
|
19
|
-
private httpClient: HttpClient;
|
|
20
|
-
private baseUrl: string;
|
|
21
|
-
|
|
22
|
-
constructor(
|
|
23
|
-
@Inject(HttpClient) httpClient: HttpClient,
|
|
24
|
-
@Optional() @Inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL) baseUrl?: string
|
|
25
|
-
) {
|
|
26
|
-
this.httpClient = httpClient;
|
|
27
|
-
this.baseUrl = baseUrl ? baseUrl : '';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
protected $get<T>(url: string, options?: any): Observable<T> {
|
|
31
|
-
return this.httpClient
|
|
32
|
-
.get<T>(this.baseUrl + url, options)
|
|
33
|
-
.pipe((response: any) => response);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
protected $getAll<T>(url: string, options?: any): Observable<T[]> {
|
|
37
|
-
return this.httpClient
|
|
38
|
-
.get<T[]>(this.baseUrl + url, options)
|
|
39
|
-
.pipe((response: any) => response);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
protected $delete<T>(url: string, options?: any): Observable<T> {
|
|
43
|
-
return this.httpClient
|
|
44
|
-
.delete(this.baseUrl + url, options)
|
|
45
|
-
.pipe((response: any) => response);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
protected $post(url: string, data: any, options?: any): Observable<any> {
|
|
49
|
-
return this.httpClient
|
|
50
|
-
.post(this.baseUrl + url, data, options)
|
|
51
|
-
.pipe((response: any) => response);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
protected $patch<T>(url: string, data: any, options?: any): Observable<T> {
|
|
55
|
-
return this.httpClient
|
|
56
|
-
.patch(this.baseUrl + url, data, options)
|
|
57
|
-
.pipe((response: any) => response);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
protected $put(url: string, data: any, options?: any): Observable<any> {
|
|
61
|
-
return this.httpClient
|
|
62
|
-
.put(this.baseUrl + url, data, options)
|
|
63
|
-
.pipe((response: any) => response);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
7
|
function paramsSerializer(params: any) {
|
|
68
8
|
return encodeParams(params, null, {
|
|
69
9
|
allowDots: <%= it.allowDots %>,
|
package/templates/ng2/client.ejs
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
@Injectable({
|
|
2
2
|
providedIn: 'root'
|
|
3
3
|
})
|
|
4
|
-
export class <%= it.clientName -%>Service
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
@Optional() @Inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL) baseUrl?: string
|
|
8
|
-
) {
|
|
9
|
-
super(httpClient, baseUrl);
|
|
10
|
-
}
|
|
4
|
+
export class <%= it.clientName.charAt(0).toUpperCase() + it.clientName.slice(1) -%>Service {
|
|
5
|
+
private http = inject(HttpClient);
|
|
6
|
+
private baseUrl = inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL, { optional: true }) ?? '';
|
|
11
7
|
|
|
12
8
|
<% it.operations.forEach((operation) => { %>
|
|
13
9
|
<%~ include('operation.ejs', operation); %>
|
|
@@ -6,21 +6,24 @@
|
|
|
6
6
|
<% }); %>
|
|
7
7
|
config?: any
|
|
8
8
|
): Observable<<%~ it.returnType %>> {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
<% if(it.query && it.query.length > 0) { -%>
|
|
10
|
+
const params = paramsSerializer({<%
|
|
11
11
|
it.query.forEach((parameter) => { %>
|
|
12
|
-
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
13
|
-
<% }); %>})
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
<%
|
|
18
|
-
<% if(it.
|
|
19
|
-
|
|
20
|
-
<% } else { %>
|
|
21
|
-
|
|
22
|
-
<% }
|
|
12
|
+
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
13
|
+
<% }); %>});
|
|
14
|
+
const url = `<%= it.url %>${params ? '?' + params : ''}`;
|
|
15
|
+
<% } else { -%>
|
|
16
|
+
const url = `<%= it.url %>`;
|
|
17
|
+
<% } -%>
|
|
18
|
+
<% if(it.method === 'GET') { %>
|
|
19
|
+
return this.http.get<<%~ it.returnType %>>(this.baseUrl + url, config);
|
|
20
|
+
<% } else if(it.method === 'DELETE') { %>
|
|
21
|
+
return this.http.delete<<%~ it.returnType %>>(this.baseUrl + url, config);
|
|
22
|
+
<% } else if(['POST', 'PUT', 'PATCH'].includes(it.method)) {
|
|
23
|
+
const bodyArg = it.body
|
|
24
|
+
? (it.body.contentType === 'urlencoded' ? 'new URLSearchParams(' + it.body.name + ' as any)' : it.body.name)
|
|
25
|
+
: 'null';
|
|
26
|
+
%>
|
|
27
|
+
return this.http.<%= it.method.toLowerCase() %><%~ '<' + it.returnType + '>' %>(this.baseUrl + url, <%~ bodyArg %>, config);
|
|
23
28
|
<% } %>
|
|
24
|
-
config
|
|
25
|
-
);
|
|
26
29
|
}
|
|
@@ -1,14 +1,3 @@
|
|
|
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
|
-
// deno-lint-ignore-file
|
|
11
|
-
|
|
12
1
|
import Axios, { type AxiosPromise, type AxiosRequestConfig } from "axios";
|
|
13
2
|
import useSWR, { type SWRConfiguration, type Key } from 'swr';
|
|
14
3
|
|
|
@@ -28,3 +17,4 @@ interface SwrConfig extends SWRConfiguration {
|
|
|
28
17
|
/* Configuration for axios fetcher */
|
|
29
18
|
axios?: AxiosRequestConfig;
|
|
30
19
|
}
|
|
20
|
+
|
|
@@ -5,17 +5,35 @@ export const <%= it.camelCaseName -%>Client = {
|
|
|
5
5
|
<% }); %>
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
-
<%
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
8
|
+
<%
|
|
9
|
+
var getOperations = it.operations.filter((o) => o.method === 'GET');
|
|
10
|
+
|
|
11
|
+
function toOpName(name) {
|
|
12
|
+
var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;
|
|
13
|
+
return n.charAt(0).toUpperCase() + n.slice(1);
|
|
14
|
+
}
|
|
15
|
+
%>
|
|
16
|
+
|
|
17
|
+
export const <%= it.camelCaseName %> = {
|
|
18
|
+
queries: {
|
|
19
|
+
<% getOperations.forEach((operation) => {
|
|
20
|
+
var opName = toOpName(operation.name);
|
|
21
|
+
var swrOperation = Object.assign({
|
|
22
|
+
swrOpName: 'use' + opName,
|
|
23
|
+
clientName: it.camelCaseName,
|
|
24
|
+
}, operation);
|
|
25
|
+
%>
|
|
18
26
|
<%~ include('swrOperation.ejs', swrOperation); %>
|
|
19
27
|
|
|
20
|
-
|
|
21
|
-
|
|
28
|
+
<% }); %>
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
queryKeys: {
|
|
32
|
+
<% getOperations.forEach((operation) => {
|
|
33
|
+
var opName = toOpName(operation.name);
|
|
34
|
+
var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);
|
|
35
|
+
%>
|
|
36
|
+
<%= keyName %>: (<% operation.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => `<%= operation.url %><% if(operation.query && operation.query.length > 0) { %>?${encodeParams({<% operation.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= parameter.name %>, <% }); %>})}<% } %>`,
|
|
37
|
+
<% }); %>
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -1,50 +1,48 @@
|
|
|
1
|
-
|
|
1
|
+
<% var docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\n' : ''; %>
|
|
2
|
+
<%~ docs %>
|
|
3
|
+
<%= it.swrOpName %>(
|
|
4
|
+
<% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,
|
|
5
|
+
<% }); %> $config?: SwrConfig
|
|
6
|
+
) {
|
|
7
|
+
const url = `<%= it.url %>`;
|
|
8
|
+
const { axios: $axiosConf, key, ...config } = $config || {};
|
|
2
9
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const cacheUrl = `${url}?<%
|
|
12
|
-
if(it.query && it.query.length > 0) { %>${encodeParams({<%
|
|
13
|
-
it.query.forEach((parameter) => { %>
|
|
14
|
-
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
15
|
-
<% }); %>})}<% } %>`;
|
|
10
|
+
<% if(it.query && it.query.length > 0) { %>
|
|
11
|
+
const cacheKey = encodeParams({<% it.query.forEach((parameter) => { %>
|
|
12
|
+
'<%= parameter.originalName %>': <%= parameter.name %>,<% }); %>
|
|
13
|
+
});
|
|
14
|
+
const cacheUrl = cacheKey ? `${url}?${cacheKey}` : url;
|
|
15
|
+
<% } else { %>
|
|
16
|
+
const cacheUrl = url;
|
|
17
|
+
<% } %>
|
|
16
18
|
|
|
17
|
-
const { data, error, mutate } = useSWR<<%~ it.returnType %>>(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const { data, error, isLoading, mutate } = useSWR<<%~ it.returnType %>>(
|
|
20
|
+
key ?? cacheUrl,
|
|
21
|
+
() => axios.request({
|
|
22
|
+
url: url,
|
|
23
|
+
method: '<%= it.method %>',
|
|
22
24
|
<% if(it.query && it.query.length > 0) { %>
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
},
|
|
25
|
+
params: {
|
|
26
|
+
<% it.query.forEach((parameter) => { %>
|
|
27
|
+
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
28
|
+
<% }); %>
|
|
29
|
+
},
|
|
28
30
|
<% } %>
|
|
29
31
|
<% if(it.headers && it.headers.length > 0) { %>
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
headers: {
|
|
33
|
+
<% it.headers.forEach((parameter) => { %>
|
|
34
|
+
<% if (parameter.value) { %>
|
|
35
|
+
'<%= parameter.originalName %>': '<%= parameter.value %>',
|
|
36
|
+
<% } else { %>
|
|
37
|
+
'<%= parameter.originalName %>': <%= parameter.name %>,
|
|
38
|
+
<% } %>
|
|
39
|
+
<% }); %>
|
|
40
|
+
},
|
|
39
41
|
<% } %>
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
...$axiosConf,
|
|
43
|
+
}).then((resp) => resp.data),
|
|
44
|
+
config
|
|
45
|
+
);
|
|
43
46
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
isLoading: !error && !data,
|
|
47
|
-
error: error,
|
|
48
|
-
mutate,
|
|
49
|
-
};
|
|
50
|
-
}
|
|
47
|
+
return { data, isLoading, error, mutate };
|
|
48
|
+
},
|
|
@@ -1,22 +1,9 @@
|
|
|
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
|
-
// deno-lint-ignore-file
|
|
11
|
-
|
|
12
1
|
import xior, { type XiorResponse, type XiorRequestConfig, encodeParams } from "xior";
|
|
13
|
-
import {
|
|
14
|
-
|
|
15
|
-
export const queryClient = new QueryClient();
|
|
2
|
+
import { type UseQueryOptions, type UseMutationOptions, useQuery, useMutation } from '@tanstack/react-query';
|
|
16
3
|
|
|
17
4
|
export const http = xior.create({
|
|
18
5
|
baseURL: '<%= it.baseUrl || '' %>',
|
|
19
|
-
paramsSerializer: (params) =>
|
|
6
|
+
paramsSerializer: (params: any) =>
|
|
20
7
|
encodeParams(params, true, null, {
|
|
21
8
|
allowDots: <%= it.allowDots %>,
|
|
22
9
|
arrayFormat: '<%= it.arrayFormat %>',
|
|
@@ -5,18 +5,51 @@ export const <%= it.camelCaseName %>Client = {
|
|
|
5
5
|
<% }); %>
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
+
<%
|
|
9
|
+
var getOperations = it.operations.filter((o) => o.method === 'GET');
|
|
10
|
+
var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
// Helper: strip leading "get" and capitalise
|
|
13
|
+
function toOpName(name) {
|
|
14
|
+
var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;
|
|
15
|
+
return n.charAt(0).toUpperCase() + n.slice(1);
|
|
16
|
+
}
|
|
17
|
+
%>
|
|
18
|
+
|
|
19
|
+
export const <%= it.camelCaseName %> = {
|
|
20
|
+
queries: {
|
|
21
|
+
<% getOperations.forEach((operation) => {
|
|
22
|
+
var opName = toOpName(operation.name);
|
|
23
|
+
var queryOperation = Object.assign({
|
|
24
|
+
rqOpName: 'use' + opName,
|
|
25
|
+
opKey: it.camelCaseName + opName,
|
|
26
|
+
clientName: it.camelCaseName,
|
|
27
|
+
}, operation);
|
|
28
|
+
%>
|
|
19
29
|
<%~ include('queryOperation.ejs', queryOperation); %>
|
|
20
30
|
|
|
21
|
-
|
|
22
|
-
|
|
31
|
+
<% }); %>
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
mutations: {
|
|
35
|
+
<% mutationOperations.forEach((operation) => {
|
|
36
|
+
var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);
|
|
37
|
+
var mutationOperation = Object.assign({
|
|
38
|
+
mutOpName: 'use' + opName,
|
|
39
|
+
clientName: it.camelCaseName,
|
|
40
|
+
}, operation);
|
|
41
|
+
%>
|
|
42
|
+
<%~ include('mutationOperation.ejs', mutationOperation); %>
|
|
43
|
+
|
|
44
|
+
<% }); %>
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
queryKeys: {
|
|
48
|
+
<% getOperations.forEach((operation) => {
|
|
49
|
+
var opName = toOpName(operation.name);
|
|
50
|
+
var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);
|
|
51
|
+
%>
|
|
52
|
+
<%= keyName %>: (<% operation.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? ' | null' : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% operation.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,
|
|
53
|
+
<% }); %>
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<%
|
|
2
|
+
var hasParams = it.parameters.length > 0;
|
|
3
|
+
|
|
4
|
+
var variablesType;
|
|
5
|
+
if (!hasParams) {
|
|
6
|
+
variablesType = 'void';
|
|
7
|
+
} else {
|
|
8
|
+
var parts = it.parameters.map(function(p) {
|
|
9
|
+
return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');
|
|
10
|
+
});
|
|
11
|
+
variablesType = '{ ' + parts.join('; ') + ' }';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
var callArgs = it.parameters.map(function(p) { return 'vars.' + p.name; });
|
|
15
|
+
|
|
16
|
+
var baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\n * @param $httpConfig (optional) Additional configuration for xior request (actually executes the request)';
|
|
17
|
+
var rawDocs = it.jsDocs
|
|
18
|
+
? it.jsDocs.replace(/(\s*)\*\/\s*$/, '\n' + baseAdditionalParams + '\n */')
|
|
19
|
+
: '';
|
|
20
|
+
var docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\n' : '';
|
|
21
|
+
%>
|
|
22
|
+
<%~ docs %>
|
|
23
|
+
<%= it.mutOpName %><TData = <%~ it.returnType %>, TError = Error>(
|
|
24
|
+
$config?: UseMutationOptions<<%~ it.returnType %>, TError, <%~ variablesType %>>,
|
|
25
|
+
$httpConfig?: XiorRequestConfig
|
|
26
|
+
) {
|
|
27
|
+
return useMutation<<%~ it.returnType %>, TError, <%~ variablesType %>>({
|
|
28
|
+
mutationFn: (<%= hasParams ? 'vars' : '' %>) => <%= it.clientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig).then(res => res.data),
|
|
29
|
+
...$config
|
|
30
|
+
});
|
|
31
|
+
},
|
|
@@ -1,30 +1,19 @@
|
|
|
1
1
|
<%
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
<%~
|
|
10
|
-
<%
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
>,
|
|
21
|
-
$httpConfig?: XiorRequestConfig
|
|
22
|
-
) {
|
|
23
|
-
return useQuery<<%~ it.returnType %>, TError, TData>({
|
|
24
|
-
queryKey: ['<%= it.clientName %>', '<%= it.opKey %>', <% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>],
|
|
25
|
-
queryFn: () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %>
|
|
26
|
-
<%= parameter.name %>, <% }); %>$httpConfig).then(res => res.data),
|
|
27
|
-
...$config
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
<%= it.rqOpName %>.queryKeys = ['<%= it.clientName %>', '<%= it.opKey %>'];
|
|
2
|
+
var baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\n * @param $httpConfig (optional) Additional configuration for xior request (actually executes the request)';
|
|
3
|
+
var rawDocs = it.jsDocs
|
|
4
|
+
? it.jsDocs.replace(/(\s*)\*\/\s*$/, '\n' + baseAdditionalParams + '\n */')
|
|
5
|
+
: '';
|
|
6
|
+
var docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\n' : '';
|
|
7
|
+
%>
|
|
8
|
+
<%~ docs %>
|
|
9
|
+
<%= it.rqOpName %><TData = <%~ it.returnType %>, TError = Error>(
|
|
10
|
+
<% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,
|
|
11
|
+
<% }); %> $config?: Omit<UseQueryOptions<<%~ it.returnType %>, TError, TData>, 'queryKey' | 'queryFn'>,
|
|
12
|
+
$httpConfig?: XiorRequestConfig
|
|
13
|
+
) {
|
|
14
|
+
return useQuery<<%~ it.returnType %>, TError, TData>({
|
|
15
|
+
queryKey: <%= it.clientName %>.queryKeys.<%= it.rqOpName.charAt(3).toLowerCase() + it.rqOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>),
|
|
16
|
+
queryFn: () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig).then(res => res.data),
|
|
17
|
+
...$config
|
|
18
|
+
});
|
|
19
|
+
},
|
|
@@ -1,19 +1,8 @@
|
|
|
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
|
-
// deno-lint-ignore-file
|
|
11
|
-
|
|
12
1
|
import xior, { type XiorResponse, type XiorRequestConfig, encodeParams } from "xior";
|
|
13
2
|
|
|
14
3
|
export const http = xior.create({
|
|
15
4
|
baseURL: '<%= it.baseUrl || '' %>',
|
|
16
|
-
paramsSerializer: (params) =>
|
|
5
|
+
paramsSerializer: (params: any) =>
|
|
17
6
|
encodeParams(params, true, null, {
|
|
18
7
|
allowDots: <%= it.allowDots %>,
|
|
19
8
|
arrayFormat: '<%= it.arrayFormat %>',
|