UNPKG

feathers-fetch-service

Version:
63 lines (45 loc) 1.53 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _nodeFetch = require('node-fetch'); var _nodeFetch2 = _interopRequireDefault(_nodeFetch); var _lodash = require('lodash.omit'); var _lodash2 = _interopRequireDefault(_lodash); var _Base = require('./Base'); var _Base2 = _interopRequireDefault(_Base); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const ignoreFieldsInHeader = ['content-length']; class FetchService extends _Base2.default { constructor(settings) { super(settings); this.connection = _nodeFetch2.default; } request(options) { const fetchOptions = Object.assign({}, options); fetchOptions.headers = (0, _lodash2.default)(fetchOptions.headers, ignoreFieldsInHeader); fetchOptions.headers = Object.assign({ Accept: 'application/json' }, this.options.headers, fetchOptions.headers); if (options.body) { fetchOptions.body = JSON.stringify(options.body); } // const f = this.connection return (0, _nodeFetch2.default)(options.url, fetchOptions).then(this.checkStatus).then(response => { if (response.status === 204) { return null; } return response.json(); }); } static checkStatus(response) { if (response.ok) { return response; } return response.json().then(error => { error.response = response; // eslint-disable-line no-param-reassign throw error; }); } } exports.default = FetchService;