diff --git a/packages/axios/README.md b/packages/axios/README.md index 0c8c42b..8bbc1fe 100644 --- a/packages/axios/README.md +++ b/packages/axios/README.md @@ -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) diff --git a/packages/axios/package.json b/packages/axios/package.json index 99aaa00..d5715d9 100644 --- a/packages/axios/package.json +++ b/packages/axios/package.json @@ -37,6 +37,7 @@ "typecheck": "tsc --noEmit" }, "dependencies": { + "@142vip/utils": "workspace:*", "axios": "^1.7.3" }, "publishConfig": { diff --git a/packages/axios/src/constants.ts b/packages/axios/src/constants.ts deleted file mode 100644 index 886dca6..0000000 --- a/packages/axios/src/constants.ts +++ /dev/null @@ -1,68 +0,0 @@ -/** - * 状态码 - */ -export enum HttpStatus { - CONTINUE = 100, - SWITCHING_PROTOCOLS = 101, - PROCESSING = 102, - EARLYHINTS = 103, - OK = 200, - CREATED = 201, - ACCEPTED = 202, - NON_AUTHORITATIVE_INFORMATION = 203, - NO_CONTENT = 204, - RESET_CONTENT = 205, - PARTIAL_CONTENT = 206, - AMBIGUOUS = 300, - MOVED_PERMANENTLY = 301, - FOUND = 302, - SEE_OTHER = 303, - NOT_MODIFIED = 304, - TEMPORARY_REDIRECT = 307, - PERMANENT_REDIRECT = 308, - BAD_REQUEST = 400, - UNAUTHORIZED = 401, - PAYMENT_REQUIRED = 402, - FORBIDDEN = 403, - NOT_FOUND = 404, - METHOD_NOT_ALLOWED = 405, - NOT_ACCEPTABLE = 406, - PROXY_AUTHENTICATION_REQUIRED = 407, - REQUEST_TIMEOUT = 408, - CONFLICT = 409, - GONE = 410, - LENGTH_REQUIRED = 411, - PRECONDITION_FAILED = 412, - PAYLOAD_TOO_LARGE = 413, - URI_TOO_LONG = 414, - UNSUPPORTED_MEDIA_TYPE = 415, - REQUESTED_RANGE_NOT_SATISFIABLE = 416, - EXPECTATION_FAILED = 417, - I_AM_A_TEAPOT = 418, - MISDIRECTED = 421, - UNPROCESSABLE_ENTITY = 422, - FAILED_DEPENDENCY = 424, - PRECONDITION_REQUIRED = 428, - TOO_MANY_REQUESTS = 429, - INTERNAL_SERVER_ERROR = 500, - NOT_IMPLEMENTED = 501, - BAD_GATEWAY = 502, - SERVICE_UNAVAILABLE = 503, - GATEWAY_TIMEOUT = 504, - HTTP_VERSION_NOT_SUPPORTED = 505, -} - -/** - * 路由类型枚举 - * - 请求方法 - */ -export enum HttpMethod { - GET = 'GET', - POST = 'POST', - PUT = 'PUT', - DELETE = 'DELETE', - PATCH = 'PATCH', - OPTIONS = 'OPTIONS', - HEAD = 'HEAD', - ALL = 'ALL', -} diff --git a/packages/axios/src/index.ts b/packages/axios/src/index.ts index 55a83a9..da22e43 100644 --- a/packages/axios/src/index.ts +++ b/packages/axios/src/index.ts @@ -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() +} diff --git a/packages/axios/src/interceptors.ts b/packages/axios/src/interceptors.ts index fc0d92a..2975d3e 100644 --- a/packages/axios/src/interceptors.ts +++ b/packages/axios/src/interceptors.ts @@ -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(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 } diff --git a/packages/axios/src/vip-axios.ts b/packages/axios/src/vip-axios.ts index 4d09907..ac983f2 100644 --- a/packages/axios/src/vip-axios.ts +++ b/packages/axios/src/vip-axios.ts @@ -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() + } + } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9412c6d..b839be7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -104,6 +104,9 @@ importers: packages/axios: dependencies: + '@142vip/utils': + specifier: workspace:* + version: link:../utils axios: specifier: ^1.7.3 version: 1.7.9 @@ -168,6 +171,9 @@ importers: packages/egg-axios: dependencies: + '@142vip/axios': + specifier: workspace:* + version: link:../axios '@142vip/egg': specifier: workspace:* version: link:../egg @@ -2227,101 +2233,121 @@ packages: resolution: {integrity: sha512-QAg11ZIt6mcmzpNE6JZBpKfJaKkqTm1A9+y9O+frdZJEuhQxiugM05gnCWiANHj4RmbgeVJpTdmKRmH/a+0QbA==} cpu: [arm] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm-gnueabihf@4.29.1': resolution: {integrity: sha512-Py5vFd5HWYN9zxBv3WMrLAXY3yYJ6Q/aVERoeUFwiDGiMOWsMs7FokXihSOaT/PMWUty/Pj60XDQndK3eAfE6A==} cpu: [arm] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm-musleabihf@4.28.1': resolution: {integrity: sha512-dRP9PEBfolq1dmMcFqbEPSd9VlRuVWEGSmbxVEfiq2cs2jlZAl0YNxFzAQS2OrQmsLBLAATDMb3Z6MFv5vOcXg==} cpu: [arm] os: [linux] + libc: [musl] '@rollup/rollup-linux-arm-musleabihf@4.29.1': resolution: {integrity: sha512-RiWpGgbayf7LUcuSNIbahr0ys2YnEERD4gYdISA06wa0i8RALrnzflh9Wxii7zQJEB2/Eh74dX4y/sHKLWp5uQ==} cpu: [arm] os: [linux] + libc: [musl] '@rollup/rollup-linux-arm64-gnu@4.28.1': resolution: {integrity: sha512-uGr8khxO+CKT4XU8ZUH1TTEUtlktK6Kgtv0+6bIFSeiSlnGJHG1tSFSjm41uQ9sAO/5ULx9mWOz70jYLyv1QkA==} cpu: [arm64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm64-gnu@4.29.1': resolution: {integrity: sha512-Z80O+taYxTQITWMjm/YqNoe9d10OX6kDh8X5/rFCMuPqsKsSyDilvfg+vd3iXIqtfmp+cnfL1UrYirkaF8SBZA==} cpu: [arm64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-arm64-musl@4.28.1': resolution: {integrity: sha512-QF54q8MYGAqMLrX2t7tNpi01nvq5RI59UBNx+3+37zoKX5KViPo/gk2QLhsuqok05sSCRluj0D00LzCwBikb0A==} cpu: [arm64] os: [linux] + libc: [musl] '@rollup/rollup-linux-arm64-musl@4.29.1': resolution: {integrity: sha512-fOHRtF9gahwJk3QVp01a/GqS4hBEZCV1oKglVVq13kcK3NeVlS4BwIFzOHDbmKzt3i0OuHG4zfRP0YoG5OF/rA==} cpu: [arm64] os: [linux] + libc: [musl] '@rollup/rollup-linux-loongarch64-gnu@4.28.1': resolution: {integrity: sha512-vPul4uodvWvLhRco2w0GcyZcdyBfpfDRgNKU+p35AWEbJ/HPs1tOUrkSueVbBS0RQHAf/A+nNtDpvw95PeVKOA==} cpu: [loong64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-loongarch64-gnu@4.29.1': resolution: {integrity: sha512-5a7q3tnlbcg0OodyxcAdrrCxFi0DgXJSoOuidFUzHZ2GixZXQs6Tc3CHmlvqKAmOs5eRde+JJxeIf9DonkmYkw==} cpu: [loong64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-powerpc64le-gnu@4.28.1': resolution: {integrity: sha512-pTnTdBuC2+pt1Rmm2SV7JWRqzhYpEILML4PKODqLz+C7Ou2apEV52h19CR7es+u04KlqplggmN9sqZlekg3R1A==} cpu: [ppc64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-powerpc64le-gnu@4.29.1': resolution: {integrity: sha512-9b4Mg5Yfz6mRnlSPIdROcfw1BU22FQxmfjlp/CShWwO3LilKQuMISMTtAu/bxmmrE6A902W2cZJuzx8+gJ8e9w==} cpu: [ppc64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.28.1': resolution: {integrity: sha512-vWXy1Nfg7TPBSuAncfInmAI/WZDd5vOklyLJDdIRKABcZWojNDY0NJwruY2AcnCLnRJKSaBgf/GiJfauu8cQZA==} cpu: [riscv64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-riscv64-gnu@4.29.1': resolution: {integrity: sha512-G5pn0NChlbRM8OJWpJFMX4/i8OEU538uiSv0P6roZcbpe/WfhEO+AT8SHVKfp8qhDQzaz7Q+1/ixMy7hBRidnQ==} cpu: [riscv64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-s390x-gnu@4.28.1': resolution: {integrity: sha512-/yqC2Y53oZjb0yz8PVuGOQQNOTwxcizudunl/tFs1aLvObTclTwZ0JhXF2XcPT/zuaymemCDSuuUPXJJyqeDOg==} cpu: [s390x] os: [linux] + libc: [glibc] '@rollup/rollup-linux-s390x-gnu@4.29.1': resolution: {integrity: sha512-WM9lIkNdkhVwiArmLxFXpWndFGuOka4oJOZh8EP3Vb8q5lzdSCBuhjavJsw68Q9AKDGeOOIHYzYm4ZFvmWez5g==} cpu: [s390x] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.28.1': resolution: {integrity: sha512-fzgeABz7rrAlKYB0y2kSEiURrI0691CSL0+KXwKwhxvj92VULEDQLpBYLHpF49MSiPG4sq5CK3qHMnb9tlCjBw==} cpu: [x64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-gnu@4.29.1': resolution: {integrity: sha512-87xYCwb0cPGZFoGiErT1eDcssByaLX4fc0z2nRM6eMtV9njAfEE6OW3UniAoDhX4Iq5xQVpE6qO9aJbCFumKYQ==} cpu: [x64] os: [linux] + libc: [glibc] '@rollup/rollup-linux-x64-musl@4.28.1': resolution: {integrity: sha512-xQTDVzSGiMlSshpJCtudbWyRfLaNiVPXt1WgdWTwWz9n0U12cI2ZVtWe/Jgwyv/6wjL7b66uu61Vg0POWVfz4g==} cpu: [x64] os: [linux] + libc: [musl] '@rollup/rollup-linux-x64-musl@4.29.1': resolution: {integrity: sha512-xufkSNppNOdVRCEC4WKvlR1FBDyqCSCpQeMMgv9ZyXqqtKBfkw1yfGMTUTs9Qsl6WQbJnsGboWCp7pJGkeMhKA==} cpu: [x64] os: [linux] + libc: [musl] '@rollup/rollup-win32-arm64-msvc@4.28.1': resolution: {integrity: sha512-wSXmDRVupJstFP7elGMgv+2HqXelQhuNf+IS4V+nUpNVi/GUiBgDmfwD0UGN3pcAnWsgKG3I52wMOBnk1VHr/A==}