From 227b723337fecbaaa8c1f87ae50341b8a4228ac7 Mon Sep 17 00:00:00 2001 From: Quentin Lem Date: Tue, 9 Apr 2024 10:09:36 +0200 Subject: [PATCH 1/2] Add optional chaining to ensure compatibility with mocking library Use "request.sockect.localPort" --- src/model/ApiError.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/model/ApiError.ts b/src/model/ApiError.ts index e14bda53..e20be66f 100644 --- a/src/model/ApiError.ts +++ b/src/model/ApiError.ts @@ -37,10 +37,10 @@ export class ApiError { this.headers = axiosError.response.headers; this.request = { url: { - protocol: axiosError.request.protocol, - port: axiosError.request.agent.defaultPort, - host: axiosError.request.host, - path: axiosError.request.path, + protocol: axiosError.request?.protocol, + port: axiosError.request?.agent?.defaultPort || axiosError.request?.socket?.localPort, + host: axiosError.request?.host, + path: axiosError.request?.path, }, headers: axiosError.request.getHeaders(), method: axiosError.request.method From 4221087372c5f73a45d74b241a37ed4e51f25a1e Mon Sep 17 00:00:00 2001 From: Quentin Lem Date: Mon, 15 Apr 2024 17:08:12 +0200 Subject: [PATCH 2/2] improve apierror types --- src/model/ApiError.ts | 50 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/model/ApiError.ts b/src/model/ApiError.ts index e20be66f..5993ae7a 100644 --- a/src/model/ApiError.ts +++ b/src/model/ApiError.ts @@ -1,49 +1,51 @@ +import { AxiosError, AxiosResponseHeaders, RawAxiosResponseHeaders } from 'axios' + interface RequestUrl { - protocol: string - port: number - host: string - path: string + protocol?: string + port?: number + host?: string + path?: string } interface Request { - url: RequestUrl - headers: any - method: string + url?: RequestUrl + headers?: any + method?: string } interface Response { - statusCode: number, - body: any, - headers: any, - request: Request, + statusCode?: number, + body?: any, + headers?: any, + request?: Request, } interface ErrorResponse { - response: Response - body: any + response?: Response + body?: any } export class ApiError { - statusCode: number - body: any - headers: any - request: Request + statusCode?: number + body?: any + headers?: RawAxiosResponseHeaders | AxiosResponseHeaders + request?: Request - constructor(axiosError) { + constructor(axiosError: AxiosError) { - this.statusCode = axiosError.response.status; - this.body = axiosError.response.data; - this.headers = axiosError.response.headers; + this.statusCode = axiosError.response?.status; + this.body = axiosError.response?.data; + this.headers = axiosError.response?.headers; this.request = { url: { protocol: axiosError.request?.protocol, - port: axiosError.request?.agent?.defaultPort || axiosError.request?.socket?.localPort, + port: axiosError.request?.socket?.localPort || axiosError.request?.agent?.defaultPort, host: axiosError.request?.host, path: axiosError.request?.path, }, - headers: axiosError.request.getHeaders(), - method: axiosError.request.method + headers: axiosError.request?.getHeaders(), + method: axiosError.request?.method } }