UNPKG

fsm-sdk

Version:

Node.JS sdk to interface with SAP Field Service Management APIs.

53 lines 2.71 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpService = void 0; const polyfills_1 = require("../../polyfills"); class HttpService { constructor(_config, _logger = console) { this._config = _config; this._logger = _logger; } request(uri, options) { if (!uri) { throw new Error('URI is required for HTTP request'); } if (this._config.debug) { this._logger.log(`[httpRequest] outgoing ${uri} options[${JSON.stringify(options, null, 2)}]`); } return polyfills_1.fetch(uri, options) .then((response) => __awaiter(this, void 0, void 0, function* () { const contentType = response.headers.get('content-type'); const isJson = contentType && contentType.includes('application/json'); const content = yield (!!response.json && !!response.text ? isJson && options.method !== 'DELETE' // some APIs return text/plain for DELETE requests, as a workaround don't parse JSON for DELETE ? response.json() : response.text() : Promise.resolve(null)); if (!response.ok && [304, 302].indexOf(response.status || -1) === -1) { throw { uri: uri, statusCode: response.status, message: response.statusText, error: content, response: response, options: Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, options.headers), { Authorization: '<<hidden>>' }) }) }; } if (this._config.debug) { this._logger.log(`[httpRequest] incoming going options[${JSON.stringify(options, null, 2)}] response[${JSON.stringify(content, null, 2)}]`); } return content; })); } } exports.HttpService = HttpService; //# sourceMappingURL=http-service.js.map