diff --git a/examples/petstore-v2.ts b/examples/petstore-v2.ts index f068b3a..944102b 100644 --- a/examples/petstore-v2.ts +++ b/examples/petstore-v2.ts @@ -1,9 +1,12 @@ // Auto-generated by https://github.com/vladkens/apigen-ts // Source: https://petstore.swagger.io/v2/swagger.json +type Headers = Record +export type ApigenHeaders = Headers | ((method: string, path: string) => Headers | Promise) + export interface ApigenConfig { baseUrl: string - headers: Record + headers: ApigenHeaders } export interface ApigenRequest extends Omit { @@ -56,7 +59,12 @@ export class ApiClient { url.searchParams.append(k, Array.isArray(v) ? v.join(",") : (v as string)) } - const headers = new Headers({ ...this.Config.headers, ...opts.headers }) + 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 | undefined = undefined diff --git a/examples/petstore-v3.ts b/examples/petstore-v3.ts index efa5317..7c61e94 100644 --- a/examples/petstore-v3.ts +++ b/examples/petstore-v3.ts @@ -1,9 +1,12 @@ // Auto-generated by https://github.com/vladkens/apigen-ts // Source: https://petstore3.swagger.io/api/v3/openapi.json +type Headers = Record +export type ApigenHeaders = Headers | ((method: string, path: string) => Headers | Promise) + export interface ApigenConfig { baseUrl: string - headers: Record + headers: ApigenHeaders } export interface ApigenRequest extends Omit { @@ -56,7 +59,12 @@ export class ApiClient { url.searchParams.append(k, Array.isArray(v) ? v.join(",") : (v as string)) } - const headers = new Headers({ ...this.Config.headers, ...opts.headers }) + 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 | undefined = undefined diff --git a/src/_template.ts b/src/_template.ts index a02b844..fe383b1 100644 --- a/src/_template.ts +++ b/src/_template.ts @@ -1,8 +1,11 @@ // 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: Record + headers: ApigenHeaders } export interface ApigenRequest extends Omit { @@ -58,7 +61,12 @@ export class ApiClient { url.searchParams.append(k, Array.isArray(v) ? v.join(",") : (v as string)) } - const headers = new Headers({ ...this.Config.headers, ...opts.headers }) + 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 | undefined = undefined