-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(CON-462): replace superagent with built-in fetch (#66)
* chore(CON-462): replace superagent with built-in fetch * deps: fix package-lock * chore: fix lint warnings
- Loading branch information
1 parent
fc04be8
commit f4ceb60
Showing
28 changed files
with
4,060 additions
and
2,358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
20.17.0 | ||
20.18.0 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
export enum ErrorType { | ||
HTTP = 'http', // Generic HTTP error | ||
CLIENT = 'client', // Generic client error | ||
REQUEST = 'request', // Error preparing API request | ||
RESPONSE = 'response', // Error response from API | ||
PARSE = 'parse', // Error parsing API resource | ||
} | ||
|
||
export class FetchError extends Error { | ||
static NAME = 'FetchError'; | ||
|
||
static isFetchError = (error: unknown): error is FetchError => { | ||
return error !== null && typeof error === 'object' && error instanceof FetchError; | ||
}; | ||
|
||
type: ErrorType; | ||
status: number; | ||
statusText: string; | ||
|
||
constructor(status: number, statusText: string) { | ||
super(statusText); | ||
this.name = FetchError.NAME; | ||
this.type = ErrorType.HTTP; | ||
this.status = status; | ||
this.statusText = statusText; | ||
} | ||
} | ||
|
||
export class FftSdkError extends Error { | ||
static NAME = 'SdkError'; | ||
|
||
static isSdkError(error: unknown): error is FftSdkError { | ||
return ( | ||
error !== null && | ||
typeof error === 'object' && | ||
'name' in error && | ||
'type' in error && | ||
[(FftSdkError.NAME, FftApiError.NAME)].includes(error.name as string) && | ||
Object.values(ErrorType).includes(error.type as ErrorType) | ||
); | ||
} | ||
|
||
type: ErrorType; | ||
source?: Error; | ||
|
||
constructor(error: { message: string; type?: ErrorType }) { | ||
super(error.message); | ||
this.name = FftSdkError.NAME; | ||
this.type = error.type || ErrorType.CLIENT; | ||
} | ||
} | ||
|
||
export class FftApiError extends FftSdkError { | ||
static NAME = 'ApiError'; | ||
|
||
static isApiError(error: unknown): error is FftApiError { | ||
return FftSdkError.isSdkError(error) && error.name === FftApiError.NAME && error.type === ErrorType.RESPONSE; | ||
} | ||
|
||
status?: number; | ||
statusText?: string; | ||
|
||
constructor(error: { message: string }) { | ||
super({ ...error, type: ErrorType.RESPONSE }); | ||
this.name = FftApiError.NAME; | ||
} | ||
} | ||
|
||
const isRequestError = (error: unknown): error is TypeError => { | ||
return error !== null && typeof error === 'object' && error instanceof TypeError; | ||
}; | ||
|
||
export const handleError = (error: unknown): never => { | ||
if (FftSdkError.isSdkError(error) || FftApiError.isApiError(error)) { | ||
throw error; | ||
} | ||
|
||
let sdkError; | ||
if (error instanceof Error) { | ||
sdkError = new FftSdkError({ message: error.message }); | ||
sdkError.source = error; | ||
} else { | ||
sdkError = new FftSdkError({ message: 'An error occurred' }); | ||
} | ||
|
||
if (FetchError.isFetchError(error)) { | ||
const apiError = new FftApiError(sdkError); | ||
apiError.type = ErrorType.RESPONSE; | ||
apiError.status = error.status; | ||
apiError.statusText = error.statusText; | ||
if (!apiError.message && apiError.statusText) { | ||
apiError.message = apiError.statusText; | ||
} | ||
sdkError = apiError; | ||
} else if (isRequestError(error)) { | ||
sdkError.type = ErrorType.REQUEST; | ||
} else { | ||
sdkError.type = ErrorType.CLIENT; | ||
} | ||
|
||
throw sdkError; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,83 @@ | ||
import superagent from 'superagent'; | ||
import { HTTP_TIMEOUT_MS } from './constants'; | ||
import { USER_AGENT } from '../projectConstants'; | ||
import { BasicHttpClient, HttpRequestConfiguration, HttpResult, ResponseType } from './models'; | ||
import { BasicHttpClient, HttpRequestConfiguration, HttpResult } from './models'; | ||
import { serializeWithDatesAsIsoString } from './serialize'; | ||
import { ErrorType, FetchError, FftSdkError } from './error'; | ||
|
||
export class HttpClient implements BasicHttpClient { | ||
private shouldLogHttpRequestAndResponse: boolean; | ||
|
||
constructor(shouldLogHttpRequestAndResponse?: boolean) { | ||
this.shouldLogHttpRequestAndResponse = shouldLogHttpRequestAndResponse ?? false; | ||
} | ||
|
||
public async request<TDto>(config: HttpRequestConfiguration): Promise<HttpResult<TDto>> { | ||
const request = superagent(config.method, config.url) | ||
.set('Content-Type', 'application/json') | ||
.set('User-Agent', USER_AGENT) | ||
.timeout(HTTP_TIMEOUT_MS) | ||
.responseType(config.responseType ?? ResponseType.DEFAULT) | ||
.retry(config.retries); | ||
const url = new URL(config.url); | ||
const requestHeaders = new Headers(); | ||
requestHeaders.set('Content-Type', 'application/json'); | ||
requestHeaders.set('User-Agent', USER_AGENT); | ||
|
||
if (config.customHeaders) { | ||
request.set(config.customHeaders); | ||
Object.entries(config?.customHeaders).forEach(([key, value]) => { | ||
requestHeaders.set(key, String(value)); | ||
}); | ||
} | ||
|
||
if (config.params) { | ||
request.query(config.params); | ||
Object.entries(config?.params).forEach(([name, value]) => { | ||
url.searchParams.append(name, String(value)); | ||
}); | ||
} | ||
|
||
// eslint-disable-next-line no-undef | ||
const requestOptions: RequestInit = { | ||
headers: requestHeaders, | ||
method: config.method, | ||
body: config.body ? JSON.stringify(config.body, serializeWithDatesAsIsoString) : undefined, | ||
}; | ||
|
||
if (AbortSignal?.timeout) { | ||
requestOptions.signal = AbortSignal.timeout(HTTP_TIMEOUT_MS); | ||
} | ||
|
||
if (this.shouldLogHttpRequestAndResponse) { | ||
console.debug( | ||
`Sending request. Url: ${request.url}, Method: ${request.method}. Params: ${JSON.stringify( | ||
config.params | ||
)}, Body: ${JSON.stringify(config.body)}` | ||
); | ||
console.debug(`Sending request. Url: ${config.url}, Method: ${config.method}`, [ | ||
{ | ||
params: url.searchParams, | ||
body: requestOptions.body, | ||
headers: requestOptions.headers, | ||
}, | ||
]); | ||
} | ||
|
||
const response = await request | ||
.send(config.body) | ||
.serialize((body) => JSON.stringify(body, serializeWithDatesAsIsoString)); | ||
const fetchClient = config?.fetch || fetch; | ||
|
||
const response = await fetchClient(url, requestOptions); | ||
|
||
const responseBody = | ||
response.body && response.status !== 204 | ||
? await response.json().catch(() => { | ||
if (response.ok) { | ||
throw new FftSdkError({ message: 'Error parsing API response body', type: ErrorType.PARSE }); | ||
} | ||
}) | ||
: undefined; | ||
|
||
if (this.shouldLogHttpRequestAndResponse) { | ||
console.debug( | ||
`Received response. Url: ${request.url}, Method: ${request.method} - Response Status: ${ | ||
response.statusCode | ||
}. Body: ${JSON.stringify(response.body)}` | ||
); | ||
console.debug(`Received response. Url: ${url}, Method: ${config.method} - Response Status: ${response.status}`, [ | ||
{ | ||
body: responseBody, | ||
}, | ||
]); | ||
} | ||
|
||
if (!response.ok) { | ||
throw new FetchError(response.status, response.statusText); | ||
} | ||
|
||
return { | ||
statusCode: response.statusCode, | ||
body: response.body as TDto, | ||
statusCode: response.status, | ||
body: responseBody as TDto, | ||
}; | ||
} | ||
|
||
constructor(shouldLogHttpRequestAndResponse?: boolean) { | ||
this.shouldLogHttpRequestAndResponse = shouldLogHttpRequestAndResponse ?? false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.