UNPKG

@nahkies/typescript-axios-runtime

Version:

Runtime package for code generated by @nahkies/openapi-code-generator using the typescript-axios template

1 lines 9.75 kB
{"version":3,"file":"main.mjs","names":[],"sources":["../../../typescript-common-runtime/dist/esm/request-bodies/url-search-params.mjs","../../src/main.ts"],"sourcesContent":["//#region src/request-bodies/url-search-params.ts\nfunction getEncoding(key, encoding) {\n\treturn {\n\t\tstyle: \"form\",\n\t\texplode: true,\n\t\tallowReserved: false,\n\t\t...encoding[key]\n\t};\n}\nconst separators = {\n\tdeepObject: \",\",\n\tform: \",\",\n\tpipeDelimited: \"|\",\n\tspaceDelimited: \" \"\n};\nfunction addArrayValue(result, key, value, encoding) {\n\tif (encoding.style === \"deepObject\" && encoding.explode) return addObjectValue(result, key, value, encoding);\n\tif (encoding.explode) for (const it of value) result.append(key, String(it));\n\telse result.append(key, value.join(separators[encoding.style]));\n}\nfunction addObjectValue(result, key, value, encoding) {\n\tif (encoding.explode) if (encoding.style === \"deepObject\") for (const it of Object.entries(value)) {\n\t\tconst path = `${key}[${it[0]}]`;\n\t\tconst value = it[1];\n\t\tif (typeof value === \"object\") addObjectValue(result, path, value, encoding);\n\t\telse result.append(path, String(value));\n\t}\n\telse for (const it of Object.entries(value)) result.append(it[0], String(it[1]));\n\telse if ([\n\t\t\"form\",\n\t\t\"spaceDelimited\",\n\t\t\"pipeDelimited\"\n\t].includes(encoding.style)) {\n\t\tconst sep = separators[encoding.style];\n\t\tresult.append(key, Object.entries(value).map((entry) => [entry[0], typeof entry[1] === \"object\" ? JSON.stringify(entry[1]) : entry[1]].join(sep)).join(sep));\n\t} else result.append(key, JSON.stringify(value));\n}\n/**\n* Serializes a request body as `application/x-www-form-urlencoded` with the exact\n* semantics defined by the provided encodings, falling back to the default encoding\n* specified by the OAI specification.\n*/\nfunction requestBodyToUrlSearchParams(obj, encodings = {}) {\n\tconst result = new URLSearchParams();\n\tfor (const [key, value] of Object.entries(obj)) {\n\t\tconst encoding = getEncoding(key, encodings);\n\t\tif (value === void 0 || value === null) continue;\n\t\tif (typeof value === \"object\") if (Array.isArray(value)) addArrayValue(result, key, value, encoding);\n\t\telse addObjectValue(result, key, value, encoding);\n\t\telse result.append(key, String(value));\n\t}\n\treturn result;\n}\n//#endregion\nexport { requestBodyToUrlSearchParams };\n\n//# sourceMappingURL=url-search-params.mjs.map","import {\n type Encoding,\n requestBodyToUrlSearchParams,\n} from \"@nahkies/typescript-common-runtime/request-bodies/url-search-params\"\nimport type {\n HeaderParams,\n QueryParams,\n} from \"@nahkies/typescript-common-runtime/types\"\nimport axios, {\n AxiosHeaders,\n type AxiosInstance,\n type AxiosRequestConfig,\n type AxiosResponse,\n type RawAxiosRequestHeaders,\n} from \"axios\"\n\nexport type {\n HeaderParams,\n QueryParams,\n Server,\n} from \"@nahkies/typescript-common-runtime/types\"\n\nexport interface AbstractAxiosConfig {\n axios?: AxiosInstance | undefined\n basePath: string\n defaultHeaders?: Record<string, string> | undefined\n defaultTimeout?: number | undefined\n}\n\nexport abstract class AbstractAxiosClient {\n protected readonly axios: AxiosInstance\n protected readonly basePath: string\n protected readonly defaultHeaders: Record<string, string>\n protected readonly defaultTimeout: number | undefined\n\n protected constructor(config: AbstractAxiosConfig) {\n this.axios = config.axios ?? axios\n this.basePath = config.basePath\n this.defaultHeaders = config.defaultHeaders ?? {}\n this.defaultTimeout = config.defaultTimeout\n }\n\n protected _request<R extends AxiosResponse>(\n opts: AxiosRequestConfig,\n ): Promise<R> {\n const headers = opts.headers ?? this._headers()\n const timeout = opts.timeout ?? this.defaultTimeout\n\n return this.axios.request({\n baseURL: this.basePath,\n ...opts,\n ...(timeout !== undefined ? {timeout} : {}),\n headers,\n })\n }\n\n protected _query(\n params: QueryParams,\n encodings?: Record<string, Encoding>,\n ): string {\n const urlSearchParams = requestBodyToUrlSearchParams(params, encodings)\n const asString = urlSearchParams.toString()\n\n if (!asString.length) {\n return \"\"\n }\n\n return `?${asString}`\n }\n\n /**\n * Combines headers for a request, with precedence\n * 1. default headers\n * 2. route level header parameters\n * 3. raw request config (escape hatch)\n *\n * following these rules:\n * - header values of `undefined` are skipped\n * - header values of `null` will remove/delete any previously set headers\n *\n * Eg:\n * Passing `Authorization: null` as a parameter, will clear out any\n * default `Authorization` header.\n *\n * But passing `Authorization: undefined` as parameter will fallthrough\n * to the default `Authorization` header.\n *\n * @param paramHeaders\n * @param optsHeaders\n * @protected\n */\n protected _headers(\n paramHeaders: HeaderParams = {},\n optsHeaders: AxiosRequestConfig[\"headers\"] = {},\n ): RawAxiosRequestHeaders {\n const headers = new AxiosHeaders()\n\n // axios doesn't know how to append headers, so we just apply\n // from the lowest priority to highest.\n\n this.setHeaders(headers, this.defaultHeaders)\n this.setHeaders(headers, paramHeaders)\n this.setHeaders(headers, optsHeaders)\n\n return headers\n }\n\n protected _requestBodyToUrlSearchParams(\n obj: Record<string, unknown>,\n encoding: Record<string, Encoding> = {},\n ): URLSearchParams {\n return requestBodyToUrlSearchParams(obj, encoding)\n }\n\n protected _parseBlobResponse(res: AxiosResponse) {\n const contentType = res.headers[\"content-type\"]\n\n return new Blob([res.data], {\n type:\n typeof contentType === \"string\"\n ? contentType\n : \"application/octet-stream\",\n })\n }\n\n private setHeaders(\n headers: Pick<Headers, \"set\" | \"delete\">,\n headersInit: HeaderParams | AxiosRequestConfig[\"headers\"],\n ) {\n const headersArray = this.headersAsArray(headersInit)\n\n for (const [headerName, headerValue] of headersArray) {\n if (headerValue === null) {\n headers.delete(headerName)\n } else if (headerValue !== undefined) {\n headers.set(headerName.toLowerCase(), headerValue.toString())\n }\n }\n }\n\n private headersAsArray(\n headers: HeaderParams | AxiosRequestConfig[\"headers\"],\n ): [string, string | number | boolean | undefined | null][] {\n if (Array.isArray(headers)) {\n return headers\n }\n\n if (headers instanceof Headers) {\n const result: [string, string][] = []\n headers.forEach((value, key) => {\n result.push([key, value])\n })\n return result\n }\n\n if (headers && typeof headers === \"object\") {\n return Object.entries(headers)\n }\n\n return []\n }\n}\n"],"mappings":";;AACA,SAAS,YA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