Skip to content

Commit 14dae11

Browse files
committed
feat: add request hook beforeRequest
1 parent 1f35286 commit 14dae11

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/request.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { defaultEnvironment } from './constants'
44
import { generateUUID } from './utils'
55
import type { KyInstance } from 'ky/distribution/types/ky'
66
import type { BeforeRetryState } from 'ky/distribution/types/hooks'
7-
import type { Options, ResponsePromise } from 'ky'
7+
import type { BeforeRequestHook, Options, ResponsePromise } from 'ky'
88

99
/**
1010
* API 配置
@@ -28,6 +28,7 @@ export interface ApiConfigResolved {
2828
idfv: string
2929
/** `User-Agent` 请求头 */
3030
userAgent: string
31+
beforeRequest: BeforeRequestHook
3132
/** 是否重试 */
3233
beforeRetry: (state: BeforeRetryState) => boolean | Promise<boolean>
3334
}
@@ -60,6 +61,7 @@ export const resolveApiConfig = (config: ApiConfig): ApiConfigResolved => {
6061
generateUUID() ||
6162
defaultEnvironment.idfv
6263
).toUpperCase(),
64+
beforeRequest: config.beforeRequest ?? (() => undefined),
6365
beforeRetry: config.beforeRetry ?? (() => false),
6466
}
6567
}
@@ -109,6 +111,8 @@ export const resolveKyOptions = (): Options => {
109111
)
110112
req.headers.set(`App-Version`, apiConfig.appVersion)
111113
;(req as any).highWaterMark = 1024 * 1024
114+
115+
return apiConfig.beforeRequest(req, opt)
112116
},
113117
],
114118
beforeRetry: [

0 commit comments

Comments
 (0)