swaggie 2.4.0 → 2.4.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.
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"baseClientSetup.ejs": "/**\n * Ky client setup — GENERATED ONCE, will NOT be overwritten on subsequent runs.\n * Re-generate intentionally with: swaggie ... --clientSetup <path> --forceSetup\n *\n * This file configures the ky HTTP client used by the generated API client.\n * Because ky requires hooks to be provided at creation time, this file exports\n * a `createKyConfig()` function that is called by `initKyHttp()` in api.ts.\n *\n * Usage in your app:\n * import { initKyHttp } from '<%= it.relativeApiImport %>';\n * initKyHttp(); // call once at startup, e.g. in a React provider or main.ts\n *\n * If you need to pass runtime values into hooks (e.g. an auth token getter),\n * store them in module-level variables and expose a configuration function:\n *\n * let _getToken: () => Promise<string> = () => Promise.resolve('');\n *\n * export function configureKyClient(opts: { getToken: () => Promise<string> }) {\n * _getToken = opts.getToken;\n * }\n *\n * Then call configureKyClient(...) before initKyHttp() at startup.\n */\nimport type { Options as KyOptions } from 'ky';\n\nexport type KySetupConfig = Pick<KyOptions, 'hooks' | 'prefix' | 'retry' | 'timeout'>;\n\n/**\n * Returns the ky configuration used to create the HTTP client instance.\n * Called once by `initKyHttp()` in the generated api.ts.\n *\n * Add your hooks here — beforeRequest for auth/headers, afterResponse for\n * error handling and monitoring, beforeError for error enrichment.\n */\nexport function createKyConfig(): KySetupConfig {\n return {\n prefix: '<%= it.baseUrl || '' %>',\n hooks: {\n beforeRequest: [\n // TODO: Add request hooks, e.g. attach an Authorization header:\n // async ({ request }) => {\n // const token = await _getToken();\n // if (token) request.headers.set('Authorization', `Bearer ${token}`);\n // },\n ],\n afterResponse: [\n // TODO: Add response hooks, e.g. handle 401 Unauthorized:\n // async ({ response }) => {\n // if (response.status === 401) {\n // await loginWithRedirect({ returnTo: window.location.toString() });\n // }\n // },\n ],\n beforeError: [\n // TODO: Add error hooks, e.g. enrich errors with request context:\n // ({ error }) => error,\n ],\n },\n };\n}\n",
|
|
23
23
|
"baseClientWithSetup.ejs": "import ky, { type KyInstance, type Options as KyOptions } from 'ky';\nimport { createKyConfig } from '<%= it.relativeSetupImport %>';\n\nlet _http: KyInstance | null = null;\n\n/**\n * Initialises the ky HTTP client using the configuration returned by\n * `createKyConfig()` from your setup file.\n *\n * Call this once at application startup — for example inside a React provider\n * or your app's entry point — before any API calls are made.\n *\n * Re-calling this function replaces the existing instance. ky instances are\n * immutable, so hook changes require re-initialisation.\n */\nexport function initKyHttp(): void {\n _http = ky.create(createKyConfig());\n}\n\n/**\n * Returns the initialised ky instance. Throws if called before `initKyHttp()`.\n *\n * This guard prevents accidental use of the client before it has been\n * configured with hooks (auth, error handling, etc.).\n */\nexport function getKyHttp(): KyInstance {\n if (!_http) {\n throw new Error(\n '[Swaggie] ky client is not initialised. ' +\n 'Call initKyHttp() at application startup before making any API requests.'\n );\n }\n return _http;\n}\n<% if (it.responseShape === 'full') { %>\nexport interface <%= it.apiResponseType %><T> {\n data: T;\n headers: Headers;\n status: number;\n}\n<% } %>\n",
|
|
24
24
|
"client.ejs": "export const <%= it.camelCaseName %>Client = {\n <% it.operations.forEach((operation) => { %>\n<%~ include('operation.ejs', Object.assign({ httpAccessor: it.httpAccessor }, operation)); %>\n\n<% }); %>\n};\n",
|
|
25
|
-
"operation.ejs": "<%\nvar parseMethod = it.responseContentType === 'binary' ? 'blob' : (it.responseContentType === 'text' ? 'text' : 'json');\nvar returnTypeWrapped = it.responseShape === 'full'\n ? 'Promise<' + it.apiResponseType + '<' + it.returnType + '>>'\n : 'Promise<' + it.returnType + '>';\n%>\n<%~ 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?: KyOptions\n ): <%~ returnTypeWrapped %> {\n const url = `<%= it.url.replace(/^\\//, '') %>`;\n\n return <%= it.httpAccessor %>.<%= it.method.toLowerCase() %>(url, {\n<% if(it.body) { -%>\n<% if(it.body.contentType === 'json') { -%>\n json: <%= 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<% if(it.query && it.query.length > 0) { -%>\n searchParams: encodeParams({\n<% it.query.forEach((parameter) => { -%>\n<% if (it.queryParamObject) { -%>\n '<%= parameter.originalName %>': <%= it.queryParamObject.name %>?.<%= parameter.name %>,\n<% } else { -%>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n<% } -%>\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
|
|
25
|
+
"operation.ejs": "<%\nvar parseMethod = it.responseContentType === 'binary' ? 'blob' : (it.responseContentType === 'text' ? 'text' : 'json');\nvar returnTypeWrapped = it.responseShape === 'full'\n ? 'Promise<' + it.apiResponseType + '<' + it.returnType + '>>'\n : 'Promise<' + it.returnType + '>';\n%>\n<%~ 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?: KyOptions\n ): <%~ returnTypeWrapped %> {\n const url = `<%= it.url.replace(/^\\//, '') %>`;\n\n return <%= it.httpAccessor %>.<%= it.method.toLowerCase() %>(url, {\n<% if(it.body) { -%>\n<% if(it.body.contentType === 'json') { -%>\n json: <%= 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<% if(it.query && it.query.length > 0) { -%>\n searchParams: encodeParams({\n<% it.query.forEach((parameter) => { -%>\n<% if (it.queryParamObject) { -%>\n '<%= parameter.originalName %>': <%= it.queryParamObject.name %>?.<%= parameter.name %>,\n<% } else { -%>\n '<%= parameter.originalName %>': <%= parameter.name %>,\n<% } -%>\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 %> ?? undefined,\n<% } -%>\n<% }); -%>\n },\n<% } -%>\n ...$config,\n<% if (it.responseShape === 'full') { -%>\n }).then(async (response) => ({\n data: (await response.<%= parseMethod %>()) as <%~ it.returnType %>,\n headers: response.headers,\n status: response.status,\n }));\n<% } else if(it.responseContentType === 'binary') { -%>\n }).blob() as Promise<<%~ it.returnType %>>;\n<% } else if(it.responseContentType === 'text') { -%>\n }).text() as Promise<<%~ it.returnType %>>;\n<% } else { -%>\n }).json<<%~ it.returnType %>>();\n<% } -%>\n },\n"
|
|
26
26
|
},
|
|
27
27
|
"ng1": {
|
|
28
28
|
"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",
|
package/package.json
CHANGED
|
@@ -40,7 +40,7 @@ $config?: KyOptions
|
|
|
40
40
|
<% if (parameter.value) { -%>
|
|
41
41
|
'<%= parameter.originalName %>': '<%= parameter.value %>',
|
|
42
42
|
<% } else { -%>
|
|
43
|
-
'<%= parameter.originalName %>': <%= parameter.name
|
|
43
|
+
'<%= parameter.originalName %>': <%= parameter.name %> ?? undefined,
|
|
44
44
|
<% } -%>
|
|
45
45
|
<% }); -%>
|
|
46
46
|
},
|