7-segment-timer.client
Version:
This is the client for the 7 Segment Timer.
106 lines (105 loc) • 3.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpClient = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
class HttpClient {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
async get(url, options) {
const prefixedUrl = this.buildUrl(url);
const headers = this.buildHeaders(options);
const request = {
method: 'GET',
headers: headers,
};
const response = await node_fetch_1.default(prefixedUrl, request);
const body = await response.text();
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
const parsedResponse = this.tryParseStringtoJson(body);
return parsedResponse;
}
async post(url, data, options) {
const prefixedUrl = this.buildUrl(url);
const headers = this.buildHeaders(options);
const request = {
method: 'POST',
headers: headers,
body: data ? JSON.stringify(data) : undefined,
};
const response = await node_fetch_1.default(prefixedUrl, request);
const body = await response.text();
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
const parsedResponse = this.tryParseStringtoJson(body);
return parsedResponse;
}
async put(url, data, options) {
const prefixedUrl = this.buildUrl(url);
const headers = this.buildHeaders(options);
const request = {
method: 'PUT',
headers: headers,
body: JSON.stringify(data),
};
const response = await node_fetch_1.default(prefixedUrl, request);
const body = await response.text();
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
const parsedResponse = this.tryParseStringtoJson(body);
return parsedResponse;
}
async delete(url, options) {
const prefixedUrl = this.buildUrl(url);
const headers = this.buildHeaders(options);
const request = {
method: 'DELETE',
headers: headers,
};
const response = await node_fetch_1.default(prefixedUrl, request);
const body = await response.text();
if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}
const parsedResponse = this.tryParseStringtoJson(body);
return parsedResponse;
}
buildUrl(url) {
if (this.baseUrl.endsWith('/') && url.startsWith('/')) {
return `${this.baseUrl}${url.replace('/', '')}`;
}
else if (!this.baseUrl.endsWith('/') && !url.startsWith('/')) {
return `${this.baseUrl}/${url}`;
}
return `${this.baseUrl}${url}`;
}
buildHeaders(options) {
const headers = {
'Content-Type': 'application/json',
};
if (options && options.headers !== undefined) {
const optionHeaders = Object.keys(options.headers);
for (const header of optionHeaders) {
headers[header] = options.headers[header];
}
}
return headers;
}
// tslint:disable-next-line:no-any
tryParseStringtoJson(result) {
try {
return JSON.parse(result);
}
catch (error) {
return result;
}
}
}
exports.HttpClient = HttpClient;