Skip to content

Commit

Permalink
feat(axios): support cause for axios error, fix #1002
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 30, 2023
1 parent 8f06f3b commit c5d6ff3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/axios/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
21 changes: 14 additions & 7 deletions packages/axios/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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('.')) {
Expand Down Expand Up @@ -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<Agent> = Object.create(null)
Expand Down Expand Up @@ -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
})
}

Expand Down
2 changes: 2 additions & 0 deletions packages/axios/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"strict": true,
"noImplicitAny": false,
},
"include": [
"src",
Expand Down

0 comments on commit c5d6ff3

Please sign in to comment.