diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a438ab..cff0bd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 2.4.3 + +- fix: overriding options + ### 2.4.2 - fix: mjs typings export diff --git a/i18nextHttpBackend.js b/i18nextHttpBackend.js index 163320a..80c0b94 100644 --- a/i18nextHttpBackend.js +++ b/i18nextHttpBackend.js @@ -30,6 +30,8 @@ var _utils = require("./utils.js"); var _request = _interopRequireDefault(require("./request.js")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } +function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } +function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } @@ -82,7 +84,7 @@ var Backend = function () { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; this.services = services; - this.options = (0, _utils.defaults)(options, this.options || {}, getDefaults()); + this.options = _objectSpread(_objectSpread(_objectSpread({}, getDefaults()), this.options || {}), options); this.allOptions = allOptions; if (this.services && this.options.reloadInterval) { setInterval(function () { @@ -222,6 +224,11 @@ var _utils = require("./utils.js"); var fetchNode = _interopRequireWildcard(require("./getFetch.js")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } +function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } +function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } +function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } +function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } +function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var fetchApi; if (typeof fetch === 'function') { @@ -285,13 +292,13 @@ var requestWithFetch = function requestWithFetch(options, url, payload, callback if (options.queryStringParams) { url = addQueryString(url, options.queryStringParams); } - var headers = (0, _utils.defaults)({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders); + var headers = _objectSpread({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders); if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) { headers['User-Agent'] = "i18next-http-backend (node/".concat(global.process.version, "; ").concat(global.process.platform, " ").concat(global.process.arch, ")"); } if (payload) headers['Content-Type'] = 'application/json'; var reqOptions = typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions; - var fetchOptions = (0, _utils.defaults)({ + var fetchOptions = _objectSpread({ method: payload ? 'POST' : 'GET', body: payload ? options.stringify(payload) : undefined, headers: headers diff --git a/i18nextHttpBackend.min.js b/i18nextHttpBackend.min.js index 18a1ad9..8f92deb 100644 --- a/i18nextHttpBackend.min.js +++ b/i18nextHttpBackend.min.js @@ -1 +1 @@ -!function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).i18nextHttpBackend=e()}(function(){return function o(i,r,s){function a(t,e){if(!r[t]){if(!i[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(u)return u(t,!0);throw(e=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",e}n=r[t]={exports:{}},i[t][0].call(n.exports,function(e){return a(i[t][1][e]||e)},n,n.exports,o,i,r,s)}return r[t].exports}for(var u="function"==typeof require&&require,e=0;e { @@ -35,7 +35,7 @@ class Backend { init (services, options = {}, allOptions = {}) { this.services = services - this.options = defaults(options, this.options || {}, getDefaults()) + this.options = { ...getDefaults(), ...(this.options || {}), ...options } this.allOptions = allOptions if (this.services && this.options.reloadInterval) { setInterval(() => this.reload(), this.options.reloadInterval) diff --git a/lib/request.js b/lib/request.js index 54cffcd..46e8b4f 100644 --- a/lib/request.js +++ b/lib/request.js @@ -1,4 +1,4 @@ -import { defaults, hasXMLHttpRequest } from './utils.js' +import { hasXMLHttpRequest } from './utils.js' import * as fetchNode from './getFetch.cjs' let fetchApi @@ -64,17 +64,20 @@ const requestWithFetch = (options, url, payload, callback) => { if (options.queryStringParams) { url = addQueryString(url, options.queryStringParams) } - const headers = defaults({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders) + const headers = { + ...(typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders) + } if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) { headers['User-Agent'] = `i18next-http-backend (node/${global.process.version}; ${global.process.platform} ${global.process.arch})` } if (payload) headers['Content-Type'] = 'application/json' const reqOptions = typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions - const fetchOptions = defaults(omitFetchOptions ? {} : reqOptions, { + const fetchOptions = { method: payload ? 'POST' : 'GET', body: payload ? options.stringify(payload) : undefined, - headers - }) + headers, + ...(omitFetchOptions ? {} : reqOptions) + } try { fetchIt(url, fetchOptions, callback) } catch (e) { diff --git a/test/fixtures/server.js b/test/fixtures/server.js index 6274c90..aace61b 100644 --- a/test/fixtures/server.js +++ b/test/fixtures/server.js @@ -12,6 +12,17 @@ const server = (done) => { res.jsonp(req.query) }) + js.patch('/locales/it/testns', (req, res) => { + res.jsonp({ + via: 'patch' + }) + }) + + js.patch('/create/it/testns', (req, res) => { + expect(req.body).not.to.eql({}) + res.jsonp() + }) + js.get('/locales/en/test', (req, res) => { res.jsonp({ key: 'passing' diff --git a/test/http.spec.js b/test/http.spec.js index 97a1dc2..c57aa4d 100644 --- a/test/http.spec.js +++ b/test/http.spec.js @@ -165,7 +165,7 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'} }) }) - describe('with custom request option', () => { + describe('with custom request function option', () => { let backend, requestCall before(() => { @@ -217,6 +217,47 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'} }) }) + if (!hasXMLHttpRequest()) { + describe('with custom request options', () => { + let backend + + before(() => { + backend = new Http( + { + interpolator: i18next.services.interpolator + }, + { + loadPath: 'http://localhost:5001/locales/{{lng}}/{{ns}}', + addPath: 'http://localhost:5001/create/{{lng}}/{{ns}}', + requestOptions: { + method: 'PATCH' + } + } + ) + }) + + describe('#read', () => { + it('should read data', (done) => { + backend.read('it', 'testns', function (err, data) { + expect(err).not.to.be.ok() + expect(data).to.eql({ via: 'patch' }) + done() + }) + }) + }) + + describe('#create', () => { + it('should write data', (done) => { + backend.create('it', 'testns', 'new.key', 'new value', (dataArray, resArray) => { + expect(dataArray).to.eql([null]) + expect(resArray).to.eql([ { status: 200, data: '' } ]) + done() + }) + }) + }) + }) + } + describe('with loadPath function returning falsy', () => { let backend let calledLanguages = []