UNPKG

@sethub/sdk

Version:

<div align="center"> <h1> SetHub SDK </h1>

155 lines (154 loc) 6.45 kB
export class HttpClient { constructor(options) { var _a, _b; this.baseURL = options === null || options === void 0 ? void 0 : options.baseURL; this.requestInterceptors = (_a = options === null || options === void 0 ? void 0 : options.requestInterceptors) !== null && _a !== void 0 ? _a : []; this.responseInterceptors = (_b = options === null || options === void 0 ? void 0 : options.responseInterceptors) !== null && _b !== void 0 ? _b : []; } mountHeaders(headers) { return new Headers({ Accept: 'application/json', 'Content-Type': 'application/json', ...headers }); } async extractBodyJsonFromResponse(response) { var _a; const responseContentType = response.headers.get('Content-Type'); const isJsonResponse = (_a = responseContentType === null || responseContentType === void 0 ? void 0 : responseContentType.includes('application/json')) !== null && _a !== void 0 ? _a : false; let body = null; if (isJsonResponse) { body = (await response.json()); } return body; } async initRequest(request) { var _a; const config = { method: request.method, url: request.url, queryParams: request.queryParams, headers: this.mountHeaders(request.headers), mode: 'cors', signal: request.abortSignal }; if (!request.body) { config.headers.delete('Content-Type'); } const requestContentType = config.headers.get('Content-Type'); const isJsonBody = (_a = requestContentType === null || requestContentType === void 0 ? void 0 : requestContentType.startsWith('application/json')) !== null && _a !== void 0 ? _a : false; if (request.body && isJsonBody) { config.body = JSON.stringify(request.body); } return this.request(config); } isHttpResponse(maybeHttpResponse) { return (typeof maybeHttpResponse === 'object' && maybeHttpResponse !== null && 'ok' in maybeHttpResponse && 'statusCode' in maybeHttpResponse); } async request(config) { var _a; let configCopy = config; const url = new URL(configCopy.url, this.baseURL); const queryParamsEntires = Object.entries((_a = configCopy.queryParams) !== null && _a !== void 0 ? _a : {}); for (const [name, value] of queryParamsEntires) { url.searchParams.append(name, String(value)); } try { /** Intercepta a requisição antes de ela ser executada */ for (const interceptor of this.requestInterceptors) { configCopy = await interceptor(configCopy); } const response = await fetch(url, configCopy); const responseBody = await this.extractBodyJsonFromResponse(response); let httpResponse = { ok: response.ok, statusCode: response.status, headers: response.headers, body: responseBody }; /** Intercepta a resposta antes de devolver */ for (const interceptor of this.responseInterceptors) { httpResponse = (await interceptor(configCopy, httpResponse)); } return httpResponse; } catch (error) { if (this.isHttpResponse(error)) { return error; } if (error instanceof Error) { if (error.name === 'ConnectionRefused') { // TODO: Atribuir uma mensagem diferente } if (error.name === 'Timeout') { // TODO: Implementar timeout e atribuir uma mensagem diferente } } return { ok: false, headers: {}, statusCode: 0, body: { code: 'internal', message: 'We are unable to process your request right now, please try again later.' } }; } } get(url, options) { const request = { method: 'GET', url, headers: options === null || options === void 0 ? void 0 : options.headers, queryParams: options === null || options === void 0 ? void 0 : options.queryParams, abortSignal: options === null || options === void 0 ? void 0 : options.abortSignal }; return this.initRequest(request); } post(url, body, options) { const request = { method: 'POST', url, headers: options === null || options === void 0 ? void 0 : options.headers, queryParams: options === null || options === void 0 ? void 0 : options.queryParams, body, abortSignal: options === null || options === void 0 ? void 0 : options.abortSignal }; return this.initRequest(request); } put(url, body, options) { const request = { method: 'PUT', url, headers: options === null || options === void 0 ? void 0 : options.headers, queryParams: options === null || options === void 0 ? void 0 : options.queryParams, body, abortSignal: options === null || options === void 0 ? void 0 : options.abortSignal }; return this.initRequest(request); } patch(url, body, options) { const request = { method: 'PATCH', url, headers: options === null || options === void 0 ? void 0 : options.headers, queryParams: options === null || options === void 0 ? void 0 : options.queryParams, body, abortSignal: options === null || options === void 0 ? void 0 : options.abortSignal }; return this.initRequest(request); } delete(url, options) { const request = { method: 'DELETE', url, headers: options === null || options === void 0 ? void 0 : options.headers, queryParams: options === null || options === void 0 ? void 0 : options.queryParams, abortSignal: options === null || options === void 0 ? void 0 : options.abortSignal }; return this.initRequest(request); } }