Skip to content

Commit

Permalink
feat(Http): add headers option (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Aug 26, 2024
1 parent e5f47f7 commit d9fe2e1
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/http/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { stringify } from 'qs'
import { type Lang } from '../composables/Lang'
import { isBlob, isError, isFormData, isRequest, isResponse, isString } from '../support/Utils'

type Awaitable<T> = T | PromiseLike<T>

export interface HttpClient {
<T = any>(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<T>
raw<T = any>(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<FetchResponse<T>>
Expand All @@ -23,6 +25,7 @@ export interface HttpOptions {
client?: HttpClient
lang?: Lang
payloadKey?: string
headers?: () => Awaitable<Record<string, string>>
}

export class Http {
Expand All @@ -31,6 +34,7 @@ export class Http {
private static client: HttpClient = ofetch
private static lang: Lang | undefined = undefined
private static payloadKey = '__payload__'
private static headers: () => Awaitable<Record<string, string>> = async () => ({})

static config(options: HttpOptions) {
if (options.baseUrl) {
Expand All @@ -48,6 +52,9 @@ export class Http {
if (options.payloadKey) {
Http.payloadKey = options.payloadKey
}
if (options.headers) {
Http.headers = options.headers
}
}

private async ensureXsrfToken(): Promise<string | undefined> {
Expand Down Expand Up @@ -88,6 +95,7 @@ export class Http {
...options,
headers: {
Accept: 'application/json',
...(await Http.headers()),
...(xsrfToken && { 'X-Xsrf-Token': xsrfToken }),
...(Http.lang && { 'Accept-Language': Http.lang }),
...options.headers
Expand Down

0 comments on commit d9fe2e1

Please sign in to comment.