Skip to content

Commit 24f7619

Browse files
committed
feat: support more request options, omit request/upload/download config nullish value
1 parent 765dd97 commit 24f7619

File tree

6 files changed

+162
-49
lines changed

6 files changed

+162
-49
lines changed

README.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,21 @@ const instance = un.create({
377377
// 默认值是 'text'
378378
responseType: 'text',
379379

380+
// request 使用
381+
// 是否验证 ssl 证书
382+
// 默认值是 true
383+
sslVerify: true,
384+
385+
// request 使用
386+
// 跨域请求时是否需要使用凭证
387+
// 默认值是 false
388+
withCredentials: false,
389+
390+
// request 使用
391+
// 是否在 DNS 解析时优先使用 ipv4
392+
// 默认值是 false
393+
firstIpv4: false,
394+
380395
// request 使用
381396
// 是否开启 http2
382397
// 默认值是 false
@@ -411,26 +426,33 @@ const instance = un.create({
411426
// 默认值是 false
412427
forceCellularNetwork: false,
413428

414-
// request 使用
415-
// 是否验证 ssl 证书
416-
// 默认值是 true
417-
sslVerify: true,
418-
419-
// request 使用
420-
// 跨域请求时是否需要使用凭证
421-
// 默认值是 false
422-
withCredentials: false,
423-
424-
// request 使用
425-
// 是否在 DNS 解析时优先使用 ipv4
426-
// 默认值是 false
427-
firstIpv4: false,
428-
429429
// request 使用
430430
// 监听 Transfer-Encoding Chunk Received 事件
431431
// 当接收到新的 chunk 时触发
432432
onChunkReceived?: (response) => { /* ... */ },
433433

434+
// Request 使用
435+
// 是否可在 headers 中编辑 cookie
436+
// 默认为 false
437+
enableCookie?: boolean;
438+
439+
// Request 使用
440+
// 是否开启云加速
441+
// 默认为 false
442+
cloudCache?: boolean | {
443+
// 用于指定 query 中哪些字段不作为缓存依据
444+
excludeURLQueries?: string[];
445+
// 用于表达云加速缓存的最快刷新时间
446+
// 单位 s
447+
minRefresh?: number;
448+
[key: string]: any;
449+
};
450+
451+
// Request 使用
452+
// 控制当前请求是否延时至首屏内容渲染后发送
453+
// 默认为 false
454+
defer?: boolean;
455+
434456
// upload 使用
435457
// 需要上传的文件列表,files 和 filePath 必填一个
436458
// 使用该参数时,filePath 和 name 无效

docs/api/request-config.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@
104104
// 默认值是 'text'
105105
responseType: "text",
106106

107+
// request 使用
108+
// 是否验证 ssl 证书
109+
// 默认值是 true
110+
sslVerify: true,
111+
112+
// request 使用
113+
// 跨域请求时是否需要使用凭证
114+
// 默认值是 false
115+
withCredentials: false,
116+
117+
// request 使用
118+
// 是否在 DNS 解析时优先使用 ipv4
119+
// 默认值是 false
120+
firstIpv4: false,
121+
107122
// request 使用
108123
// 是否开启 http2
109124
// 默认值是 false
@@ -138,28 +153,35 @@
138153
// 默认值是 false
139154
forceCellularNetwork: false,
140155

141-
// request 使用
142-
// 是否验证 ssl 证书
143-
// 默认值是 true
144-
sslVerify: true,
145-
146-
// request 使用
147-
// 跨域请求时是否需要使用凭证
148-
// 默认值是 false
149-
withCredentials: false,
150-
151-
// request 使用
152-
// 是否在 DNS 解析时优先使用 ipv4
153-
// 默认值是 false
154-
firstIpv4: false,
155-
156156
// request 使用
157157
// 监听 Transfer-Encoding Chunk Received 事件
158158
// 当接收到新的 chunk 时触发
159159
onChunkReceived: (response) => {
160160
/* ... */
161161
},
162162

163+
// Request 使用
164+
// 是否可在 headers 中编辑 cookie
165+
// 默认为 false
166+
enableCookie?: boolean;
167+
168+
// Request 使用
169+
// 是否开启云加速
170+
// 默认为 false
171+
cloudCache?: boolean | {
172+
// 用于指定 query 中哪些字段不作为缓存依据
173+
excludeURLQueries?: string[];
174+
// 用于表达云加速缓存的最快刷新时间
175+
// 单位 s
176+
minRefresh?: number;
177+
[key: string]: any;
178+
};
179+
180+
// Request 使用
181+
// 控制当前请求是否延时至首屏内容渲染后发送
182+
// 默认为 false
183+
defer?: boolean;
184+
163185
// upload 使用
164186
// 需要上传的文件列表,files 和 filePath 必填一个
165187
// 使用该参数时,filePath 和 name 无效

packages/core/src/types/config.ts

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ export interface UnConfig<T = UnData, D = UnData> {
126126
* 默认为 text
127127
*/
128128
responseType?: UnResponseType;
129+
/**
130+
* Request 使用
131+
*
132+
* 是否验证 ssl 证书
133+
*
134+
* 默认为 true
135+
*/
136+
sslVerify?: boolean;
137+
/**
138+
* Request 使用
139+
*
140+
* 跨域请求时是否需要使用凭证
141+
*
142+
* 默认为 false
143+
*/
144+
withCredentials?: boolean;
145+
/**
146+
* Request 使用
147+
*
148+
* 是否在 DNS 解析时优先使用 ipv4
149+
*
150+
* 默认为 false
151+
*/
152+
firstIpv4?: boolean;
129153
/**
130154
* Request 使用
131155
*
@@ -183,27 +207,42 @@ export interface UnConfig<T = UnData, D = UnData> {
183207
/**
184208
* Request 使用
185209
*
186-
* 是否验证 ssl 证书
210+
* 是否可在 headers 中编辑 cookie
187211
*
188-
* 默认为 true
212+
* 默认为 false
189213
*/
190-
sslVerify?: boolean;
214+
enableCookie?: boolean;
191215
/**
192216
* Request 使用
193217
*
194-
* 跨域请求时是否需要使用凭证
218+
* 是否开启云加速
195219
*
196220
* 默认为 false
197221
*/
198-
withCredentials?: boolean;
222+
cloudCache?:
223+
| boolean
224+
| {
225+
/**
226+
* 用于指定 query 中哪些字段不作为缓存依据
227+
*/
228+
excludeURLQueries?: string[];
229+
/**
230+
* 用于表达云加速缓存的最快刷新时间
231+
*
232+
* 单位 s
233+
*/
234+
minRefresh?: number;
235+
[key: string]: any;
236+
};
199237
/**
200238
* Request 使用
201239
*
202-
* 是否在 DNS 解析时优先使用 ipv4
240+
* 控制当前请求是否延时至首屏内容渲染后发送
203241
*
204242
* 默认为 false
205243
*/
206-
firstIpv4?: boolean;
244+
defer?: boolean;
245+
207246
/**
208247
* Request 使用
209248
*

packages/core/src/utils/buildDownloadConfig.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { buildUrl } from "./buildUrl";
44

55
export const buildDownloadConfig = <T = UnData, D = UnData>(
66
config: UnConfig<T, D>,
7-
) =>
8-
({
7+
): UniApp.DownloadFileOption => {
8+
const result = {
99
url: buildUrl(
1010
buildFullPath(
1111
config.baseUrl ?? "",
@@ -18,4 +18,13 @@ export const buildDownloadConfig = <T = UnData, D = UnData>(
1818
header: config.headers,
1919
timeout: config.timeout,
2020
filePath: config.filePath,
21-
}) as UniApp.DownloadFileOption;
21+
};
22+
23+
const entries = Object.entries(result) as [
24+
keyof typeof result,
25+
(typeof result)[keyof typeof result],
26+
][];
27+
return Object.fromEntries(
28+
entries.filter(([k]) => result[k] != null),
29+
) as unknown as UniApp.DownloadFileOption;
30+
};

packages/core/src/utils/buildRequestConfig.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { buildUrl } from "./buildUrl";
44

55
export const buildRequestConfig = <T = UnData, D = UnData>(
66
config: UnConfig<T, D>,
7-
) =>
8-
({
7+
): UniApp.RequestOptions => {
8+
const result = {
99
url: buildUrl(
1010
buildFullPath(
1111
config.baseUrl ?? "",
@@ -21,14 +21,26 @@ export const buildRequestConfig = <T = UnData, D = UnData>(
2121
timeout: config.timeout,
2222
dataType: config.dataType,
2323
responseType: config.responseType,
24+
sslVerify: config.sslVerify,
25+
withCredentials: config.withCredentials,
26+
firstIpv4: config.firstIpv4,
2427
enableHttp2: config.enableHttp2,
2528
enableQuic: config.enableQuic,
2629
enableCache: config.enableCache,
2730
enableHttpDNS: config.enableHttpDNS,
2831
httpDNSServiceId: config.httpDNSServiceId,
2932
enableChunked: config.enableChunked,
3033
forceCellularNetwork: config.forceCellularNetwork,
31-
sslVerify: config.sslVerify,
32-
withCredentials: config.withCredentials,
33-
firstIpv4: config.firstIpv4,
34-
}) as UniApp.RequestOptions;
34+
enableCookie: config.enableCookie,
35+
cloudCache: config.cloudCache,
36+
defer: config.defer,
37+
};
38+
39+
const entries = Object.entries(result) as [
40+
keyof typeof result,
41+
(typeof result)[keyof typeof result],
42+
][];
43+
return Object.fromEntries(
44+
entries.filter(([k]) => result[k] != null),
45+
) as unknown as UniApp.RequestOptions;
46+
};

packages/core/src/utils/buildUploadConfig.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { buildUrl } from "./buildUrl";
44

55
export const buildUploadConfig = <T = UnData, D = UnData>(
66
config: UnConfig<T, D>,
7-
) =>
8-
({
7+
): UniApp.UploadFileOption => {
8+
const result = {
99
url: buildUrl(
1010
buildFullPath(
1111
config.baseUrl ?? "",
@@ -23,4 +23,13 @@ export const buildUploadConfig = <T = UnData, D = UnData>(
2323
header: config.headers,
2424
timeout: config.timeout,
2525
formData: config.formData,
26-
}) as UniApp.UploadFileOption;
26+
};
27+
28+
const entries = Object.entries(result) as [
29+
keyof typeof result,
30+
(typeof result)[keyof typeof result],
31+
][];
32+
return Object.fromEntries(
33+
entries.filter(([k]) => result[k] != null),
34+
) as unknown as UniApp.UploadFileOption;
35+
};

0 commit comments

Comments
 (0)