-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da6cea0
commit b007d72
Showing
10 changed files
with
264 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import fetch, { UnfetchResponse } from "unfetch"; | ||
import { GenericRecord, IHttpConfig } from "./common"; | ||
import { InvalidResponseError } from "../../utils/errors"; | ||
|
||
export namespace Network { | ||
/** | ||
* Performs a request and returns a JSON object with the response | ||
*/ | ||
|
||
export async function get(config: IHttpConfig, path: string, data?: GenericRecord) { | ||
const { url, headers } = config; | ||
const endpoint: URL = new URL(path, url); | ||
for (const [key, value] of Object.entries(data ?? {})) { | ||
if (value != null) { | ||
endpoint.searchParams.set(key, String(value)); | ||
} | ||
} | ||
const response: UnfetchResponse = await fetch(endpoint.href, { | ||
method: "GET", | ||
headers | ||
}); | ||
if (!response.ok) { | ||
throw new InvalidResponseError(response); | ||
} | ||
return response.json(); | ||
} | ||
|
||
export async function post(config: IHttpConfig, path: string, data?: any) { | ||
const { url, headers } = config; | ||
const endpoint: URL = new URL(path, url); | ||
const response: UnfetchResponse = await fetch(endpoint.href, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
...headers | ||
}, | ||
body: JSON.stringify(data) | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new InvalidResponseError(response); | ||
} | ||
return response.json(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { UnfetchResponse } from "unfetch"; | ||
|
||
export class InvalidEmailParamError extends Error { | ||
constructor() { | ||
super("The param does not email"); | ||
} | ||
} | ||
|
||
export class MismatchApproveAddressError extends Error { | ||
constructor() { | ||
super("Customer and approver mismatch"); | ||
} | ||
} | ||
|
||
export class UnregisteredEmailError extends Error { | ||
constructor() { | ||
super("Unregistered email error"); | ||
} | ||
} | ||
export class InsufficientBalanceError extends Error { | ||
constructor() { | ||
super("Insufficient balance error"); | ||
} | ||
} | ||
|
||
export class NoHttpModuleError extends Error { | ||
constructor() { | ||
super("A Http Module is needed"); | ||
} | ||
} | ||
export class ClientError extends Error { | ||
public response: UnfetchResponse; | ||
constructor(res: UnfetchResponse) { | ||
super(res.statusText); | ||
this.name = "ClientError"; | ||
this.response = res; | ||
} | ||
} | ||
|
||
export class InvalidResponseError extends ClientError { | ||
constructor(res: UnfetchResponse) { | ||
super(res); | ||
this.message = "Invalid response"; | ||
} | ||
} | ||
export class MissingBodyeError extends ClientError { | ||
constructor(res: UnfetchResponse) { | ||
super(res); | ||
this.message = "Missing response body"; | ||
} | ||
} | ||
export class BodyParseError extends ClientError { | ||
constructor(res: UnfetchResponse) { | ||
super(res); | ||
this.message = "Error parsing body"; | ||
} | ||
} |
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,111 @@ | ||
/** | ||
* Contains definition for error types | ||
* | ||
* Copyright: | ||
* Copyright (c) 2022 BOSAGORA Foundation All rights reserved. | ||
* | ||
* License: | ||
* MIT License. See LICENSE for details. | ||
*/ | ||
|
||
/** | ||
* The class used when a network error occurs | ||
*/ | ||
export class NetworkError extends Error { | ||
/** | ||
* The status code | ||
*/ | ||
public status: number; | ||
|
||
/** | ||
* The status text | ||
*/ | ||
public statusText: string; | ||
|
||
/** | ||
* The message of response | ||
*/ | ||
public statusMessage: string; | ||
|
||
/** | ||
* Constructor | ||
* @param status The status code | ||
* @param statusText The status text | ||
* @param statusMessage The message of response | ||
*/ | ||
constructor(status: number, statusText: string, statusMessage: string) { | ||
super(statusText); | ||
this.name = "NetworkError"; | ||
this.status = status; | ||
this.statusText = statusText; | ||
this.statusMessage = statusMessage; | ||
} | ||
} | ||
|
||
/** | ||
* When status code is 404 | ||
*/ | ||
export class NotFoundError extends NetworkError { | ||
/** | ||
* Constructor | ||
* @param status The status code | ||
* @param statusText The status text | ||
* @param statusMessage The message of response | ||
*/ | ||
constructor(status: number, statusText: string, statusMessage: string) { | ||
super(status, statusText, statusMessage); | ||
this.name = "NotFoundError"; | ||
} | ||
} | ||
|
||
/** | ||
* When status code is 400 | ||
*/ | ||
export class BadRequestError extends NetworkError { | ||
/** | ||
* Constructor | ||
* @param status The status code | ||
* @param statusText The status text | ||
* @param statusMessage The message of response | ||
*/ | ||
constructor(status: number, statusText: string, statusMessage: string) { | ||
super(status, statusText, statusMessage); | ||
this.name = "BadRequestError"; | ||
} | ||
} | ||
|
||
/** | ||
* It is a function that handles errors that occur during communication | ||
* with a server for easy use. | ||
* @param error This is why the error occurred | ||
* @returns The instance of Error | ||
*/ | ||
export function handleNetworkError(error: any): Error { | ||
if ( | ||
error.response !== undefined && | ||
error.response.status !== undefined && | ||
error.response.statusText !== undefined | ||
) { | ||
let statusMessage: string; | ||
if (error.response.data !== undefined) { | ||
if (typeof error.response.data === "string") statusMessage = error.response.data; | ||
else if (typeof error.response.data === "object" && error.response.data.statusMessage !== undefined) | ||
statusMessage = error.response.data.statusMessage; | ||
else if (typeof error.response.data === "object" && error.response.data.errorMessage !== undefined) | ||
statusMessage = error.response.data.errorMessage; | ||
else statusMessage = error.response.data.toString(); | ||
} else statusMessage = ""; | ||
|
||
switch (error.response.status) { | ||
case 400: | ||
return new BadRequestError(error.response.status, error.response.statusText, statusMessage); | ||
case 404: | ||
return new NotFoundError(error.response.status, error.response.statusText, statusMessage); | ||
default: | ||
return new NetworkError(error.response.status, error.response.statusText, statusMessage); | ||
} | ||
} else { | ||
if (error.message !== undefined) return new Error(error.message); | ||
else return new Error("An unknown error has occurred."); | ||
} | ||
} |
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