// Note: Use uppercase for names in ApiClient to avoid conflict with the generated code type Headers = Record export type ApigenHeaders = Headers | ((method: string, path: string) => Headers | Promise) export interface ApigenConfig { baseUrl: string headers: ApigenHeaders } export interface ApigenRequest extends Omit { search?: Record body?: unknown } export class ApiClient { ISO_FORMAT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/ Config: ApigenConfig constructor(config?: Partial) { this.Config = { baseUrl: "/", headers: {}, ...config } } PopulateDates(d: T): T { if (d === null || d === undefined || typeof d !== "object") return d const t = d as unknown as Record for (const [k, v] of Object.entries(t)) { if (typeof v === "string" && this.ISO_FORMAT.test(v)) t[k] = new Date(v) else if (typeof v === "object") this.PopulateDates(v) } return d } async ParseError(rep: Response) { try { // try to parse domain error from response body return await rep.json() } catch (e) { // otherwise return response as is throw rep } } PrepareFetchUrl(path: string): URL { let base = this.Config.baseUrl if ("location" in globalThis && (base === "" || base.startsWith("/"))) { // make ts happy in pure nodejs environment, should never pass here const { location } = globalThis as unknown as { location: { origin: string } } base = `${location.origin}${base.endsWith("/") ? base : `/${base}`}` } return new URL(path, base) } // same as "${v}" in path template, but keeps "/", "?", "#" inside the path segment EncodePathParam(v: unknown) { return encodeURIComponent(String(v)) } // query params with explode: false, eg. Swagger 2 `collectionFormat: csv` JoinQueryParam(v: unknown, sep: string) { return Array.isArray(v) ? v.join(sep) : v } async Fetch(method: string, path: string, opts: ApigenRequest = {}): Promise { const url = this.PrepareFetchUrl(path) // skip omitted values; arrays repeat the key per item (OpenAPI default: form + explode) for (const [k, v] of Object.entries(opts?.search ?? {})) { for (const x of Array.isArray(v) ? v : [v]) { if (x !== undefined && x !== null) url.searchParams.append(k, x as string) } } const configHeaders = typeof this.Config.headers === "function" ? await this.Config.headers(method, path) : this.Config.headers const headers = new Headers({ ...configHeaders, ...opts.headers }) const ct = headers.get("content-type") ?? "application/json" let body: FormData | URLSearchParams | string | null = null if (ct === "multipart/form-data" || ct === "application/x-www-form-urlencoded") { // https://stackoverflow.com/a/61053359/3664464 headers.delete("content-type") body = ct === "multipart/form-data" ? new FormData() : new URLSearchParams() for (const [k, v] of Object.entries(opts.body as Record)) { if (v !== undefined && v !== null) body.append(k, v) } } if (ct === "application/json" && typeof opts.body !== "string") { headers.set("content-type", "application/json") body = JSON.stringify(opts.body) } const credentials = opts.credentials ?? "include" const rep = await fetch(url.toString(), { method, ...opts, headers, body, credentials }) if (!rep.ok) throw await this.ParseError(rep) const rs = await rep.text() try { return this.PopulateDates(JSON.parse(rs) as T) } catch (e) { return rs as unknown as T } } // apigen:modules } // apigen:types