Skip to content

Commit

Permalink
feat: 发出请求前进行动态地址插值
Browse files Browse the repository at this point in the history
  • Loading branch information
zjxxxxxxxxx committed May 9, 2023
1 parent 6f1093f commit b6698ca
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 40 deletions.
2 changes: 2 additions & 0 deletions src/core/createInstance.ts
@@ -1,3 +1,4 @@
import { dynamicURL } from '../helpers/dynamicURL';
import { combineURL } from '../helpers/combineURL';
import { transformURL } from '../helpers/transformURL';
import Axios, {
Expand Down Expand Up @@ -58,6 +59,7 @@ export function createInstance(defaults: AxiosRequestConfig, parent?: Axios) {
const instance = context.request as AxiosInstance;

instance.getUri = function getUri(config) {
config.url = dynamicURL(config.url!, config.params, config.data);
return transformURL(mergeConfig(defaults, config));
};
instance.create = function create(config) {
Expand Down
8 changes: 6 additions & 2 deletions src/helpers/dynamicURL.ts
@@ -1,11 +1,15 @@
import { AxiosRequestData } from '../core/Axios';
import { ensureObject } from './ensureObject';

const dynamicRE = /\/:([^/]+)/g;
export function dynamicURL(
url: string,
params: AnyObject = {},
data: AnyObject = {},
data: AxiosRequestData = {},
) {
const dataObject = ensureObject(data);
return url.replace(dynamicRE, (_, $2) => {
const value = params[$2] ?? data[$2];
const value = params[$2] ?? dataObject[$2];
if ($2 in params) {
delete params[$2];
}
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/ensureObject.ts
@@ -0,0 +1,5 @@
import { isPlainObject } from './types';

export function ensureObject(value?: any) {
return isPlainObject(value) ? value : {};
}
8 changes: 1 addition & 7 deletions src/helpers/transformURL.ts
@@ -1,14 +1,8 @@
import { AxiosRequestConfig } from '../core/Axios';
import { isPlainObject } from './types';
import { buildURL } from './buildURL';
import { combineURL } from './combineURL';
import { dynamicURL } from './dynamicURL';

export function transformURL(config: AxiosRequestConfig) {
const fullPath = dynamicURL(
combineURL(config.baseURL, config.url),
config.params,
isPlainObject(config.data) ? config.data : {},
);
const fullPath = combineURL(config.baseURL, config.url);
return buildURL(fullPath, config.params, config.paramsSerializer);
}
3 changes: 0 additions & 3 deletions src/request/createError.ts
Expand Up @@ -19,12 +19,9 @@ class AxiosError extends Error {
request: AxiosAdapterPlatformTask,
) {
super(message);

this.config = config;
this.request = request;
this.response = response;

Object.setPrototypeOf(this, AxiosError.prototype);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/request/dispatchRequest.ts
@@ -1,6 +1,7 @@
import { WITH_DATA_RE } from '../constants/methods';
import { isFunction, isString } from '../helpers/types';
import { assert } from '../helpers/error';
import { dynamicURL } from '../helpers/dynamicURL';
import { AxiosRequestConfig, AxiosResponse } from '../core/Axios';
import { Cancel, isCancel, isCancelToken } from './cancel';
import { flattenHeaders } from './flattenHeaders';
Expand All @@ -22,6 +23,7 @@ export function dispatchRequest(config: AxiosRequestConfig) {
assert(isString(config.url), 'url 不是一个 string');
assert(isString(config.method), 'method 不是一个 string');

config.url = dynamicURL(config.url!, config.params, config.data);
config.headers = flattenHeaders(config);

// 可以携带 data 的请求方法,转换 data
Expand Down
16 changes: 16 additions & 0 deletions test/helpers/ensureObject.test.ts
@@ -0,0 +1,16 @@
import { describe, test, expect } from 'vitest';
import { ensureObject } from '@/helpers/ensureObject';

describe('src/helpers/ensureObject.ts', () => {
test('应该始终返回对象', () => {
expect(ensureObject()).toEqual({});
expect(ensureObject(1)).toEqual({});
expect(ensureObject('')).toEqual({});
expect(ensureObject([])).toEqual({});
expect(ensureObject(new Date())).toEqual({});
});

test('应该返回对象参数', () => {
expect(ensureObject({ a: 1 })).toEqual({ a: 1 });
});
});
23 changes: 0 additions & 23 deletions test/helpers/transformURL.test.ts
Expand Up @@ -32,29 +32,6 @@ describe('src/helpers/transformURL.ts', () => {
).toBe('http://api2.com');
});

test('应该支持动态 URL', () => {
expect(
transformURL({
baseURL: 'http://api.com',
url: 'test/:name/:type',
params: {
name: 'axios',
type: 0,
},
}),
).toBe('http://api.com/test/axios/0');
expect(
transformURL({
baseURL: 'http://api.com',
url: 'test/:name/:type',
data: {
name: 'axios',
type: 0,
},
}),
).toBe('http://api.com/test/axios/0');
});

test('应该支持自定义参数系列化器', () => {
expect(
transformURL({
Expand Down
17 changes: 16 additions & 1 deletion test/request/dispatchRequest.test.ts
@@ -1,5 +1,5 @@
import { describe, test, expect, vi } from 'vitest';
import { asyncNext, mockAdapter } from 'scripts/test.utils';
import { asyncNext, mockAdapter, testEachMethods } from 'scripts/test.utils';
import {
PLAIN_METHODS,
WITH_DATA_METHODS,
Expand Down Expand Up @@ -74,6 +74,21 @@ describe('src/request/dispatchRequest.ts', () => {
`);
});

testEachMethods('应该支持动态地址', (k) => {
const c = {
...defaults,
url: '/test/:id',
method: k,
params: {
id: 1,
},
};

dispatchRequest(c);

expect(c.url).toBe('/test/1');
});

test('应该支持拉平请求头', () => {
const c = {
...defaults,
Expand Down
8 changes: 4 additions & 4 deletions test/request/request.test.ts
Expand Up @@ -22,21 +22,21 @@ describe('src/request/request.ts', () => {
};
const c2 = {
adapter(config: AnyObject) {
expect(config.url).toBe('http://api.com/test/1');
expect(config.url).toBe('http://api.com/test?id=1');
},
baseURL: 'http://api.com',
url: 'test/:id',
url: 'test',
method: 'get' as const,
params: {
id: 1,
},
};
const c3 = {
adapter(config: AnyObject) {
expect(config.url).toBe('http://api.com/test/1');
expect(config.url).toBe('http://api.com/test');
},
baseURL: 'http://api.com',
url: 'test/:id',
url: 'test',
method: 'post' as const,
data: {
id: 1,
Expand Down

0 comments on commit b6698ca

Please sign in to comment.