oneuptime/Common/Utils/API.ts

194 lines
5.2 KiB
TypeScript
Raw Normal View History

2022-04-08 12:07:15 +00:00
import axios, { AxiosError } from 'axios';
2022-04-10 21:21:18 +00:00
import URL from '../Types/API/URL';
2022-04-10 21:16:46 +00:00
import { JSONObjectOrArray } from '../Types/JSON';
2022-04-10 21:21:18 +00:00
import Headers from '../Types/API/Headers';
import HTTPResponse from '../Types/API/Response';
import HTTPErrorResponse from '../Types/API/ErrorResponse';
import HTTPMethod from '../Types/API/HTTPMethod';
2022-04-10 21:28:33 +00:00
import APIException from '../Types/Exception/ApiException';
2022-04-10 21:21:18 +00:00
import Protocol from '../Types/API/Protocol';
import Hostname from '../Types/API/Hostname';
import Route from '../Types/API/Route';
2022-04-08 12:07:15 +00:00
export default class API {
private _protocol: Protocol = Protocol.HTTPS;
public get protocol(): Protocol {
return this._protocol;
}
public set protocol(v: Protocol) {
this._protocol = v;
}
private _hostname: Hostname = new Hostname('localhost');
public get hostname(): Hostname {
return this._hostname;
}
public set hostname(v: Hostname) {
this._hostname = v;
}
2022-04-15 11:57:47 +00:00
public constructor(protocol: Protocol, hostname: Hostname) {
2022-04-08 12:07:15 +00:00
this.protocol = protocol;
this.hostname = hostname;
}
public async get(
path: Route,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await API.get(
new URL(this.protocol, this.hostname, path),
data,
headers
);
}
public async delete(
path: Route,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await API.delete(
new URL(this.protocol, this.hostname, path),
data,
headers
);
}
public async put(
path: Route,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await API.put(
new URL(this.protocol, this.hostname, path),
data,
headers
);
}
public async post(
path: Route,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await API.post(
new URL(this.protocol, this.hostname, path),
data,
headers
);
}
protected static handleError(
error: HTTPErrorResponse | APIException
): HTTPErrorResponse | APIException {
return error;
}
public static getDefaultHeaders(): Headers {
2022-04-14 13:57:52 +00:00
const defaultHeaders: Headers = {
2022-04-08 12:07:15 +00:00
'Access-Control-Allow-Origin': '*',
Accept: 'application/json',
'Content-Type': 'application/json;charset=UTF-8',
};
return defaultHeaders;
}
protected static getHeaders(headers?: Headers): Headers {
let defaultHeaders: Headers = this.getDefaultHeaders();
if (headers) {
defaultHeaders = {
...defaultHeaders,
...headers,
};
}
return defaultHeaders;
}
public static async get(
url: URL,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await this.fetch(HTTPMethod.GET, url, data, headers);
}
public static async delete(
url: URL,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await this.fetch(HTTPMethod.DELETE, url, data, headers);
}
public static async put(
url: URL,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await this.fetch(HTTPMethod.PUT, url, data, headers);
}
public static async post(
url: URL,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
return await this.fetch(HTTPMethod.POST, url, data, headers);
}
private static async fetch(
method: HTTPMethod,
url: URL,
data?: JSONObjectOrArray,
headers?: Headers
): Promise<HTTPResponse> {
const apiHeaders: Headers = this.getHeaders(headers);
try {
2022-04-17 13:29:14 +00:00
const result: {
2022-04-17 14:01:39 +00:00
data: JSONObjectOrArray;
status: number;
} = await axios({
method: method,
url: url.toString(),
headers: apiHeaders,
data,
});
2022-04-17 13:29:14 +00:00
const response: HTTPResponse = new HTTPResponse(
2022-04-14 13:59:38 +00:00
result.status,
result.data
);
2022-04-08 12:07:15 +00:00
return response;
} catch (e) {
2022-04-17 13:29:14 +00:00
const error: Error | AxiosError = e as Error | AxiosError;
2022-04-08 12:07:15 +00:00
let errorResponse: HTTPErrorResponse | APIException;
if (axios.isAxiosError(error)) {
2022-04-15 22:14:01 +00:00
// Do whatever you want with native error
2022-04-08 12:07:15 +00:00
errorResponse = this.getErrorResponse(error);
} else {
errorResponse = new APIException(error.message);
}
this.handleError(errorResponse);
throw errorResponse;
}
}
private static getErrorResponse(error: AxiosError): HTTPErrorResponse {
if (error.response) {
return new HTTPErrorResponse(
error.response.status,
error.response.data
);
}
throw new APIException('No error response body');
}
}