@nahkies/typescript-axios-runtime
Version:
Runtime package for code generated by @nahkies/openapi-code-generator using the typescript-axios template
139 lines (138 loc) • 4.73 kB
JavaScript
import axios, { AxiosHeaders } from "axios";
//#region ../typescript-common-runtime/dist/esm/request-bodies/url-search-params.mjs
function getEncoding(key, encoding) {
return {
style: "form",
explode: true,
allowReserved: false,
...encoding[key]
};
}
const separators = {
deepObject: ",",
form: ",",
pipeDelimited: "|",
spaceDelimited: " "
};
function addArrayValue(result, key, value, encoding) {
if (encoding.style === "deepObject" && encoding.explode) return addObjectValue(result, key, value, encoding);
if (encoding.explode) for (const it of value) result.append(key, String(it));
else result.append(key, value.join(separators[encoding.style]));
}
function addObjectValue(result, key, value, encoding) {
if (encoding.explode) if (encoding.style === "deepObject") for (const it of Object.entries(value)) {
const path = `${key}[${it[0]}]`;
const value = it[1];
if (typeof value === "object") addObjectValue(result, path, value, encoding);
else result.append(path, String(value));
}
else for (const it of Object.entries(value)) result.append(it[0], String(it[1]));
else if ([
"form",
"spaceDelimited",
"pipeDelimited"
].includes(encoding.style)) {
const sep = separators[encoding.style];
result.append(key, Object.entries(value).map((entry) => [entry[0], typeof entry[1] === "object" ? JSON.stringify(entry[1]) : entry[1]].join(sep)).join(sep));
} else result.append(key, JSON.stringify(value));
}
/**
* Serializes a request body as `application/x-www-form-urlencoded` with the exact
* semantics defined by the provided encodings, falling back to the default encoding
* specified by the OAI specification.
*/
function requestBodyToUrlSearchParams(obj, encodings = {}) {
const result = new URLSearchParams();
for (const [key, value] of Object.entries(obj)) {
const encoding = getEncoding(key, encodings);
if (value === void 0 || value === null) continue;
if (typeof value === "object") if (Array.isArray(value)) addArrayValue(result, key, value, encoding);
else addObjectValue(result, key, value, encoding);
else result.append(key, String(value));
}
return result;
}
//#endregion
//#region src/main.ts
var AbstractAxiosClient = class {
axios;
basePath;
defaultHeaders;
defaultTimeout;
constructor(config) {
this.axios = config.axios ?? axios;
this.basePath = config.basePath;
this.defaultHeaders = config.defaultHeaders ?? {};
this.defaultTimeout = config.defaultTimeout;
}
_request(opts) {
const headers = opts.headers ?? this._headers();
const timeout = opts.timeout ?? this.defaultTimeout;
return this.axios.request({
baseURL: this.basePath,
...opts,
...timeout !== void 0 ? { timeout } : {},
headers
});
}
_query(params, encodings) {
const asString = requestBodyToUrlSearchParams(params, encodings).toString();
if (!asString.length) return "";
return `?${asString}`;
}
/**
* Combines headers for a request, with precedence
* 1. default headers
* 2. route level header parameters
* 3. raw request config (escape hatch)
*
* following these rules:
* - header values of `undefined` are skipped
* - header values of `null` will remove/delete any previously set headers
*
* Eg:
* Passing `Authorization: null` as a parameter, will clear out any
* default `Authorization` header.
*
* But passing `Authorization: undefined` as parameter will fallthrough
* to the default `Authorization` header.
*
* @param paramHeaders
* @param optsHeaders
* @protected
*/
_headers(paramHeaders = {}, optsHeaders = {}) {
const headers = new AxiosHeaders();
this.setHeaders(headers, this.defaultHeaders);
this.setHeaders(headers, paramHeaders);
this.setHeaders(headers, optsHeaders);
return headers;
}
_requestBodyToUrlSearchParams(obj, encoding = {}) {
return requestBodyToUrlSearchParams(obj, encoding);
}
_parseBlobResponse(res) {
const contentType = res.headers["content-type"];
return new Blob([res.data], { type: typeof contentType === "string" ? contentType : "application/octet-stream" });
}
setHeaders(headers, headersInit) {
const headersArray = this.headersAsArray(headersInit);
for (const [headerName, headerValue] of headersArray) if (headerValue === null) headers.delete(headerName);
else if (headerValue !== void 0) headers.set(headerName.toLowerCase(), headerValue.toString());
}
headersAsArray(headers) {
if (Array.isArray(headers)) return headers;
if (headers instanceof Headers) {
const result = [];
headers.forEach((value, key) => {
result.push([key, value]);
});
return result;
}
if (headers && typeof headers === "object") return Object.entries(headers);
return [];
}
};
//#endregion
export { AbstractAxiosClient };
//# sourceMappingURL=main.mjs.map