From c5d6ff3c4523192aa72d0b277effd8ffeff616d0 Mon Sep 17 00:00:00 2001 From: Shigma Date: Thu, 31 Aug 2023 00:10:57 +0800 Subject: [PATCH] feat(axios): support cause for axios error, fix #1002 --- packages/axios/package.json | 2 +- packages/axios/src/index.ts | 21 ++++++++++++++------- packages/axios/tsconfig.json | 2 ++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/axios/package.json b/packages/axios/package.json index 98903bde..0f4f597b 100644 --- a/packages/axios/package.json +++ b/packages/axios/package.json @@ -1,7 +1,7 @@ { "name": "cordis-axios", "description": "Axios service for cordis", - "version": "3.2.1", + "version": "3.3.0", "main": "lib/index.js", "types": "lib/index.d.ts", "exports": { diff --git a/packages/axios/src/index.ts b/packages/axios/src/index.ts index 2956b84d..cdaad54f 100644 --- a/packages/axios/src/index.ts +++ b/packages/axios/src/index.ts @@ -4,7 +4,7 @@ import { arrayBufferToBase64, base64ToArrayBuffer, Dict, pick, trimSlash } from import { ClientRequestArgs } from 'http' import mimedb from 'mime-db' import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios' -import * as types from 'axios' +import * as Axios from 'axios' declare module 'cordis' { interface Context { @@ -133,11 +133,11 @@ export class Quester { const name = 'file' + (ext ? '.' + ext : '') return { mime, filename: name, data: base64ToArrayBuffer(base64) } } - let [, name] = this.resolve(url).match(/.+\/([^/?]*)(?=\?)?/) + let [, name] = this.resolve(url).match(/.+\/([^/?]*)(?=\?)?/)! const { headers, data } = await this.axios(url, { method: 'GET', responseType: 'arraybuffer', - timeout: +options.timeout || undefined, + timeout: +options.timeout! || undefined, }) const mime = headers['content-type'] if (!name.includes('.')) { @@ -165,9 +165,9 @@ export class Quester { } export namespace Quester { - export type Method = types.Method - export type AxiosResponse = types.AxiosResponse - export type AxiosRequestConfig = types.AxiosRequestConfig + export type Method = Axios.Method + export type AxiosResponse = Axios.AxiosResponse + export type AxiosRequestConfig = Axios.AxiosRequestConfig export type CreateAgent = (opts: string) => Agent export const agents: Dict = Object.create(null) @@ -201,14 +201,21 @@ export namespace Quester { export function create(this: typeof Quester, config: Quester.Config = {}) { const request = async (config: AxiosRequestConfig = {}) => { const options = http.prepare() + const error = new Error() as Axios.AxiosError return axios({ ...options, ...config, - url: http.resolve(config.url), + url: http.resolve(config.url!), headers: { ...options.headers, ...config.headers, }, + }).catch((cause) => { + if (!axios.isAxiosError(cause)) throw cause + Object.assign(error, cause) + error.isAxiosError = true + error.cause = cause + throw error }) } diff --git a/packages/axios/tsconfig.json b/packages/axios/tsconfig.json index 42cbe593..6f11f324 100644 --- a/packages/axios/tsconfig.json +++ b/packages/axios/tsconfig.json @@ -3,6 +3,8 @@ "compilerOptions": { "rootDir": "src", "outDir": "lib", + "strict": true, + "noImplicitAny": false, }, "include": [ "src",