-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@142vip/axios): 移除
HttpStatus
枚举,支持VipAxios
父类封装
- Loading branch information
Showing
7 changed files
with
142 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,14 @@ | ||
export * from './constants' | ||
import type { AxiosInstance, CreateAxiosDefaults } from 'axios' | ||
import { VipAxios } from './vip-axios' | ||
|
||
export * from './interceptors' | ||
export * from './vip-axios' | ||
|
||
/** | ||
* 创建axios实例 | ||
* @param config | ||
*/ | ||
export function createAxiosInstance(config: CreateAxiosDefaults): AxiosInstance { | ||
const vipAxios = VipAxios.getInstance(config) | ||
return vipAxios.getAxios() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,52 @@ | ||
// 拦截器类型 | ||
export enum Interceptor_Type { | ||
Request = 'request', | ||
Response = 'response', | ||
import type { AxiosRequestConfig, AxiosResponse } from 'axios' | ||
import { HttpStatus } from '@142vip/utils' | ||
|
||
/** | ||
* 拦截器类型 | ||
*/ | ||
export enum InterceptorType { | ||
REQUEST = 'request', | ||
RESPONSE = 'response', | ||
} | ||
|
||
/** | ||
* 请求拦截器 | ||
*/ | ||
export function requestInterceptor() { | ||
|
||
export function requestInterceptor(config: AxiosRequestConfig): AxiosRequestConfig { | ||
return config | ||
} | ||
|
||
/** | ||
* 响应拦截器 | ||
*/ | ||
export function responseInterceptor<T>(response: AxiosResponse): T { | ||
return response as T | ||
} | ||
|
||
export function responseInterceptor() { | ||
/** | ||
* 默认请求拦截器 | ||
*/ | ||
export function defaultRequestInterceptor(config: AxiosRequestConfig): AxiosRequestConfig { | ||
return config | ||
} | ||
|
||
/** | ||
* 默认响应拦截器 | ||
*/ | ||
export function defaultResponseInterceptor(response: AxiosResponse): AxiosResponse { | ||
return response | ||
} | ||
|
||
export function useInterceptor() { | ||
export function defaultVipRequestInterceptor(config: AxiosRequestConfig): AxiosRequestConfig { | ||
// todo 添加traceId | ||
|
||
return config | ||
} | ||
|
||
export function clearInterceptor() { | ||
|
||
export function defaultVipResponseInterceptor(response: AxiosResponse): AxiosResponse { | ||
// 200 响应状态码, | ||
if (response.status === HttpStatus.OK) { | ||
return response.data | ||
} | ||
return response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,53 @@ | ||
import type { CreateAxiosDefaults } from 'axios' | ||
import type { AxiosInstance, CreateAxiosDefaults } from 'axios' | ||
import axios from 'axios' | ||
import { InterceptorType } from './interceptors' | ||
|
||
/** | ||
* 创建axios实例 | ||
* @param config | ||
* axios | ||
* - 参考:https://www.npmjs.com/package/axios#features | ||
*/ | ||
export function getInstance(config?: CreateAxiosDefaults) { | ||
return axios.create(config) | ||
export class VipAxios { | ||
public static vipAxios: VipAxios | ||
private readonly config: CreateAxiosDefaults | ||
private readonly axiosInstance: AxiosInstance | ||
|
||
constructor(config: CreateAxiosDefaults) { | ||
this.config = config | ||
this.axiosInstance = axios.create(config) | ||
} | ||
|
||
/** | ||
* 创建单例 | ||
*/ | ||
public static getInstance(config: CreateAxiosDefaults): VipAxios { | ||
if (this.vipAxios == null) { | ||
this.vipAxios = new VipAxios(config) | ||
} | ||
return this.vipAxios | ||
} | ||
|
||
/** | ||
* 获取axios实例 | ||
*/ | ||
public getAxios(): AxiosInstance { | ||
return this.axiosInstance | ||
} | ||
|
||
public getAxiosConfig() { | ||
return this.config | ||
} | ||
|
||
/** | ||
* 清除拦截器 | ||
*/ | ||
public clearInterceptor(type: InterceptorType) { | ||
// 移除请求拦截器 | ||
if (type === InterceptorType.REQUEST) { | ||
this.axiosInstance.interceptors.request.clear() | ||
} | ||
// 移除响应拦截器 | ||
if (type === InterceptorType.RESPONSE) { | ||
this.axiosInstance.interceptors.response.clear() | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.