@globalbrain/sefirot 3.50.0 → 3.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/http/Http.ts +8 -0
- package/package.json +1 -1
package/lib/http/Http.ts
CHANGED
|
@@ -5,6 +5,8 @@ import { type FetchOptions, type FetchRequest, type FetchResponse, ofetch } from
|
|
|
5
5
|
import { stringify } from 'qs'
|
|
6
6
|
import { type Lang } from '../composables/Lang'
|
|
7
7
|
|
|
8
|
+
type Awaitable<T> = T | PromiseLike<T>
|
|
9
|
+
|
|
8
10
|
export interface HttpClient {
|
|
9
11
|
<T = any>(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<T>
|
|
10
12
|
raw<T = any>(request: FetchRequest, options?: Omit<FetchOptions, 'method'>): Promise<FetchResponse<T>>
|
|
@@ -16,6 +18,7 @@ export interface HttpOptions {
|
|
|
16
18
|
client?: HttpClient
|
|
17
19
|
lang?: Lang
|
|
18
20
|
payloadKey?: string
|
|
21
|
+
headers?: () => Awaitable<Record<string, string>>
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
export class Http {
|
|
@@ -24,6 +27,7 @@ export class Http {
|
|
|
24
27
|
private static client: HttpClient = ofetch
|
|
25
28
|
private static lang: Lang | undefined = undefined
|
|
26
29
|
private static payloadKey = '__payload__'
|
|
30
|
+
private static headers: () => Awaitable<Record<string, string>> = async () => ({})
|
|
27
31
|
|
|
28
32
|
static config(options: HttpOptions) {
|
|
29
33
|
if (options.baseUrl) {
|
|
@@ -41,6 +45,9 @@ export class Http {
|
|
|
41
45
|
if (options.payloadKey) {
|
|
42
46
|
Http.payloadKey = options.payloadKey
|
|
43
47
|
}
|
|
48
|
+
if (options.headers) {
|
|
49
|
+
Http.headers = options.headers
|
|
50
|
+
}
|
|
44
51
|
}
|
|
45
52
|
|
|
46
53
|
private async ensureXsrfToken(): Promise<string | undefined> {
|
|
@@ -81,6 +88,7 @@ export class Http {
|
|
|
81
88
|
...options,
|
|
82
89
|
headers: {
|
|
83
90
|
Accept: 'application/json',
|
|
91
|
+
...(await Http.headers()),
|
|
84
92
|
...(xsrfToken && { 'X-Xsrf-Token': xsrfToken }),
|
|
85
93
|
...(Http.lang && { 'Accept-Language': Http.lang }),
|
|
86
94
|
...options.headers
|