UNPKG

http-service-lib

Version:

Reusable Http Service and implementations

28 lines (27 loc) 1.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FetchHttpPutService = void 0; const HttpResponse_1 = require("../../../entities/HttpResponse"); const HttpError_1 = require("../../../errors/HttpError"); const FetchHttpService_1 = require("./FetchHttpService"); class FetchHttpPutService extends FetchHttpService_1.FetchHttpService { async put(request) { try { const response = await fetch(request.url, { headers: request.headers, method: 'PUT', body: this.parseRequestBody(request), credentials: 'include', }); if (!response.ok) throw new HttpError_1.HttpError(await response.text(), response.status); return new HttpResponse_1.HttpResponse(response.status, await this.parseResponseBody(response)); } catch (error) { if (error instanceof HttpError_1.HttpError) throw error; throw new HttpError_1.HttpError('Could not reach endpoint', 404); } } } exports.FetchHttpPutService = FetchHttpPutService;