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