Skip to content

Commit 79c4581

Browse files
committed
feat: add allowAbsoluteUrls option
1 parent 6492e21 commit 79c4581

File tree

9 files changed

+63
-13
lines changed

9 files changed

+63
-13
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,15 @@ const instance = un.create({
288288
// `url` 是用于请求的服务器 URL
289289
url: '/user',
290290

291-
// `baseUrl` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL
291+
// `baseUrl` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL 且选项 `allowAbsoluteUrls` 为 true
292292
// 设置一个 `baseUrl` 便于为实例方法传递相对 URL
293293
baseUrl: 'https://some-domain.com/api/',
294294

295+
// 决定是否允许绝对 URL 覆盖配置的 `baseUrl`
296+
// 当设置为 true(默认)时,绝对值的 `url` 会覆盖 `baseUrl`
297+
// 当设置为 false 时,绝对值的 `url` 会始终被 `baseUrl` 前置
298+
allowAbsoluteUrls?: boolean;
299+
295300
// 自定义请求头
296301
// 不能设置 Referer
297302
headers: {

docs/api/request-config.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
// `url` 是用于请求的服务器 URL
88
url: "/user",
99

10-
// `baseUrl` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL
10+
// `baseUrl` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL 且选项 `allowAbsoluteUrls` 为 true
1111
// 设置一个 `baseUrl` 便于为实例方法传递相对 URL
1212
baseUrl: "https://some-domain.com/api/",
1313

14+
// 决定是否允许绝对 URL 覆盖配置的 `baseUrl`
15+
// 当设置为 true(默认)时,绝对值的 `url` 会覆盖 `baseUrl`
16+
// 当设置为 false 时,绝对值的 `url` 会始终被 `baseUrl` 前置
17+
allowAbsoluteUrls?: boolean;
18+
1419
// 自定义请求头
1520
// 不能设置 Referer
1621
headers: {

packages/core/src/core/Un.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export class Un<T = UnData, D = UnData> {
286286
const fullPath = buildFullPath(
287287
mergedConfig?.baseUrl ?? "",
288288
mergedConfig?.url ?? "",
289+
mergedConfig?.allowAbsoluteUrls ?? true,
289290
);
290291
return buildUrl(
291292
fullPath,

packages/core/src/types/config.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,16 @@ export interface UnConfig<T = UnData, D = UnData> {
2424
* 默认为 'GET'
2525
*/
2626
method?: UnMethod;
27-
/** 自动加在 `url` 前面,除非 `url` 是一个绝对 URL */
27+
/** 自动加在 `url` 前面,除非 `url` 是一个绝对 URL 且选项 `allowAbsoluteUrls` 为 true */
2828
baseUrl?: string;
29+
/**
30+
* 决定是否允许绝对 URL 覆盖配置的 `baseUrl`
31+
*
32+
* 当设置为 true(默认)时,绝对值的 `url` 会覆盖 `baseUrl`
33+
*
34+
* 当设置为 false 时,绝对值的 `url` 会始终被 `baseUrl` 前置
35+
*/
36+
allowAbsoluteUrls?: boolean;
2937
/** 自定义请求头,不能设置 Referer */
3038
headers?: UnHeaders;
3139
/** 与请求一起发送的 URL 参数 */

packages/core/src/utils/buildDownloadConfig.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export const buildDownloadConfig = <T = UnData, D = UnData>(
77
) =>
88
({
99
url: buildUrl(
10-
buildFullPath(config.baseUrl ?? "", config.url ?? ""),
10+
buildFullPath(
11+
config.baseUrl ?? "",
12+
config.url ?? "",
13+
config.allowAbsoluteUrls ?? true,
14+
),
1115
config.params,
1216
config.paramsSerializer,
1317
),

packages/core/src/utils/buildFullPath.test.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,36 @@ import { buildFullPath } from "./buildFullPath";
33

44
describe("utils::buildFullPath", () => {
55
it("should combine URLs when the requestedURL is relative", () => {
6-
expect(buildFullPath("https://api.github.com", "/users")).toBe(
6+
expect(buildFullPath("https://api.github.com", "/users", true)).toBe(
77
"https://github.com/api/users",
88
);
99
});
1010

11-
it("should return the requestedURL when it is absolute", () => {
11+
it("should not combine the URLs when the requestedURL is absolute", () => {
1212
expect(
13-
buildFullPath("https://api.github.com", "https://api.example.com/users"),
13+
buildFullPath(
14+
"https://api.github.com",
15+
"https://api.example.com/users",
16+
true,
17+
),
1418
).toBe("https://api.example.com/users");
1519
});
1620

21+
it("should combine the URLs when the requestedURL is absolute and allowAbsoluteUrls is false", () => {
22+
expect(
23+
buildFullPath(
24+
"https://api.github.com",
25+
"https://api.example.com/users",
26+
false,
27+
),
28+
).toBe("https://github.com/api/https://api.example.com/users");
29+
});
30+
1731
it("should not combine URLs when the baseURL is not configured", () => {
18-
expect(buildFullPath("", "/users")).toBe("/users");
32+
expect(buildFullPath("", "/users", true)).toBe("/users");
1933
});
2034

2135
it("should combine URLs when the baseURL and requestedURL are relative", () => {
22-
expect(buildFullPath("/api", "/users")).toBe("/api/users");
36+
expect(buildFullPath("/api", "/users", true)).toBe("/api/users");
2337
});
2438
});

packages/core/src/utils/buildFullPath.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { combineUrls } from "./combineUrls";
22
import { isAbsoluteUrl } from "./isAbsoluteUrl";
33

4-
export const buildFullPath = (baseUrl: string, requestedUrl: string) => {
5-
if (baseUrl && !isAbsoluteUrl(requestedUrl)) {
4+
export const buildFullPath = (
5+
baseUrl: string,
6+
requestedUrl: string,
7+
allowAbsoluteUrls: boolean,
8+
) => {
9+
const isRelativeUrl = !isAbsoluteUrl(requestedUrl);
10+
if ((baseUrl && isRelativeUrl) || !allowAbsoluteUrls) {
611
return combineUrls(baseUrl, requestedUrl);
712
}
813
return requestedUrl;

packages/core/src/utils/buildRequestConfig.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export const buildRequestConfig = <T = UnData, D = UnData>(
77
) =>
88
({
99
url: buildUrl(
10-
buildFullPath(config.baseUrl ?? "", config.url ?? ""),
10+
buildFullPath(
11+
config.baseUrl ?? "",
12+
config.url ?? "",
13+
config.allowAbsoluteUrls ?? true,
14+
),
1115
config.params,
1216
config.paramsSerializer,
1317
),

packages/core/src/utils/buildUploadConfig.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export const buildUploadConfig = <T = UnData, D = UnData>(
77
) =>
88
({
99
url: buildUrl(
10-
buildFullPath(config.baseUrl ?? "", config.url ?? ""),
10+
buildFullPath(
11+
config.baseUrl ?? "",
12+
config.url ?? "",
13+
config.allowAbsoluteUrls ?? true,
14+
),
1115
config.params,
1216
config.paramsSerializer,
1317
),

0 commit comments

Comments
 (0)