Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional chaining to ensure compatibility with mocking library #682

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions src/model/ApiError.ts
Original file line number Diff line number Diff line change
@@ -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,
host: axiosError.request.host,
path: axiosError.request.path,
protocol: axiosError.request?.protocol,
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
}
}

Expand Down