From 60dd08a1b19fa26ac942d6409596f2f15c3d615b Mon Sep 17 00:00:00 2001 From: Philip Andersson Date: Wed, 9 Oct 2024 14:17:56 +0200 Subject: [PATCH 1/2] feat: add optional headers factory in config. --- src/_template.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 From 55dab80b832b091f8bb5b0304ab97d22b59ddd66 Mon Sep 17 00:00:00 2001 From: Philip Andersson Date: Wed, 9 Oct 2024 14:18:02 +0200 Subject: [PATCH 2/2] update examples --- examples/petstore-v2.ts | 12 ++++++++++-- examples/petstore-v3.ts | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) 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