Skip to content

Commit

Permalink
feat(@142vip/axios): 移除HttpStatus枚举,支持VipAxios父类封装
Browse files Browse the repository at this point in the history
  • Loading branch information
mmdapl committed Jan 7, 2025
1 parent 34c92b0 commit 4d7172c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 84 deletions.
20 changes: 20 additions & 0 deletions packages/axios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

[![NPM version](https://img.shields.io/npm/v/@142vip/axios?labelColor=0b3d52&color=1da469&label=version)](https://www.npmjs.com/package/@142vip/axios)

## 安装

```shell
# 安装
pnpm i @142vip/axios
```

## 功能

- [x] 封装`VipAxios`父类
- [x] `createAxiosInstance`函数
- [x] 拦截器

```ts
const axiosConfig = {}

// 创建对象
const vipAxios = new VipAxios(axiosConfig)
```

## 参考

- [axios](https://github.com/axios/axios)
Expand Down
1 change: 1 addition & 0 deletions packages/axios/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@142vip/utils": "workspace:*",
"axios": "^1.7.3"
},
"publishConfig": {
Expand Down
68 changes: 0 additions & 68 deletions packages/axios/src/constants.ts

This file was deleted.

14 changes: 13 additions & 1 deletion packages/axios/src/index.ts
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()
}
44 changes: 34 additions & 10 deletions packages/axios/src/interceptors.ts
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
}
53 changes: 48 additions & 5 deletions packages/axios/src/vip-axios.ts
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()
}
}
}
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4d7172c

Please sign in to comment.