feat: getUri 支持 baseURL/dynamicURL
parent
0b82403c4c
commit
633c920247
|
@ -1,5 +1,5 @@
|
|||
import { combineURL } from '../helpers/combineURL';
|
||||
import { buildURL } from '../helpers/buildURL';
|
||||
import { transformURL } from '../helpers/transformURL';
|
||||
import Axios, {
|
||||
AxiosRequest,
|
||||
AxiosRequestConfig,
|
||||
|
@ -58,11 +58,7 @@ export function createInstance(config: AxiosRequestConfig, parent?: Axios) {
|
|||
const instance = context.request as AxiosInstance;
|
||||
|
||||
instance.getUri = function getUri(config) {
|
||||
const { url, params, paramsSerializer } = mergeConfig(
|
||||
instance.defaults,
|
||||
config,
|
||||
);
|
||||
return buildURL(url, params, paramsSerializer).replace(/^\?/, '');
|
||||
return transformURL(mergeConfig(instance.defaults, config));
|
||||
};
|
||||
instance.create = function create(config) {
|
||||
return createInstance(mergeConfig(instance.defaults, config));
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { isPlainObject } from '../helpers/isTypes';
|
||||
import { buildURL } from '../helpers/buildURL';
|
||||
import { combineURL } from '../helpers/combineURL';
|
||||
import { dynamicURL } from '../helpers/dynamicURL';
|
||||
import { AxiosRequestConfig } from '../core/Axios';
|
||||
import { isPlainObject } from './isTypes';
|
||||
import { buildURL } from './buildURL';
|
||||
import { combineURL } from './combineURL';
|
||||
import { dynamicURL } from './dynamicURL';
|
||||
|
||||
export function transformURL(config: AxiosRequestConfig) {
|
||||
let url = combineURL(config.baseURL, config.url);
|
|
@ -1,4 +1,5 @@
|
|||
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
||||
import { transformURL } from '../helpers/transformURL';
|
||||
import {
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
|
@ -14,7 +15,6 @@ import {
|
|||
import { isCancelToken } from './cancel';
|
||||
import { AxiosErrorResponse, createError } from './createError';
|
||||
import { generateType } from './generateType';
|
||||
import { transformURL } from './transformURL';
|
||||
|
||||
/**
|
||||
* 开始请求
|
||||
|
|
|
@ -160,11 +160,15 @@ describe('src/axios.ts', () => {
|
|||
});
|
||||
|
||||
test('应该可以获取 URI', () => {
|
||||
expect(axios.getUri({ url: 'test' })).toBe('test');
|
||||
expect(axios.getUri({ url: 'test', params: { id: 1 } })).toBe('test?id=1');
|
||||
expect(axios.getUri({ url: 'test', paramsSerializer: () => 'id=1' })).toBe(
|
||||
'test?id=1',
|
||||
);
|
||||
expect(
|
||||
axios.getUri({
|
||||
baseURL: 'https://api.com',
|
||||
url: '/test/:id/',
|
||||
params: {
|
||||
id: 1,
|
||||
},
|
||||
}),
|
||||
).toBe('https://api.com/test/1/');
|
||||
});
|
||||
|
||||
test('应该支持中间件', async () => {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { describe, test, expect } from 'vitest';
|
||||
import { transformURL } from '@/request/transformURL';
|
||||
import { transformURL } from '@/helpers/transformURL';
|
||||
|
||||
describe('src/request/transformURL.ts', () => {
|
||||
describe('src/helpers/transformURL.ts', () => {
|
||||
test('应该支持空配置', () => {
|
||||
expect(transformURL({})).toBe('');
|
||||
expect(transformURL({ baseURL: 'http://api.com' })).toBe('http://api.com');
|
Loading…
Reference in New Issue