Skip to content

Commit

Permalink
Merge pull request #3 from philipodev/headers-factory
Browse files Browse the repository at this point in the history
Add option to pass `headers` as a function in config
  • Loading branch information
vladkens authored Oct 12, 2024
2 parents 05897f0 + 55dab80 commit 9c2151b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
12 changes: 10 additions & 2 deletions examples/petstore-v2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Auto-generated by https://github.com/vladkens/apigen-ts
// Source: https://petstore.swagger.io/v2/swagger.json

type Headers = Record<string, string>
export type ApigenHeaders = Headers | ((method: string, path: string) => Headers | Promise<Headers>)

export interface ApigenConfig {
baseUrl: string
headers: Record<string, string>
headers: ApigenHeaders
}

export interface ApigenRequest extends Omit<RequestInit, "body"> {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions examples/petstore-v3.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>
export type ApigenHeaders = Headers | ((method: string, path: string) => Headers | Promise<Headers>)

export interface ApigenConfig {
baseUrl: string
headers: Record<string, string>
headers: ApigenHeaders
}

export interface ApigenRequest extends Omit<RequestInit, "body"> {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/_template.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// Note: Use uppercase for names in ApiClient to avoid conflict with the generated code

type Headers = Record<string, string>
export type ApigenHeaders = Headers | ((method: string, path: string) => Headers | Promise<Headers>)

export interface ApigenConfig {
baseUrl: string
headers: Record<string, string>
headers: ApigenHeaders
}

export interface ApigenRequest extends Omit<RequestInit, "body"> {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9c2151b

Please sign in to comment.