Skip to content

Commit 21b3c57

Browse files
committed
cleanup defaults()
1 parent 728bb16 commit 21b3c57

File tree

7 files changed

+78
-12
lines changed

7 files changed

+78
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### 2.4.3
2+
3+
- fix: overriding options
4+
15
### 2.4.2
26

37
- fix: mjs typings export

i18nextHttpBackend.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var _utils = require("./utils.js");
3030
var _request = _interopRequireDefault(require("./request.js"));
3131
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3232
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); }
33+
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; }
34+
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; }
3335
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3436
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); } }
3537
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 () {
8284
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
8385
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
8486
this.services = services;
85-
this.options = (0, _utils.defaults)(options, this.options || {}, getDefaults());
87+
this.options = _objectSpread(_objectSpread(_objectSpread({}, getDefaults()), this.options || {}), options);
8688
this.allOptions = allOptions;
8789
if (this.services && this.options.reloadInterval) {
8890
setInterval(function () {
@@ -222,6 +224,11 @@ var _utils = require("./utils.js");
222224
var fetchNode = _interopRequireWildcard(require("./getFetch.js"));
223225
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); }
224226
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; }
227+
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; }
228+
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; }
229+
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; }
230+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
231+
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); }
225232
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); }
226233
var fetchApi;
227234
if (typeof fetch === 'function') {
@@ -285,13 +292,13 @@ var requestWithFetch = function requestWithFetch(options, url, payload, callback
285292
if (options.queryStringParams) {
286293
url = addQueryString(url, options.queryStringParams);
287294
}
288-
var headers = (0, _utils.defaults)({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders);
295+
var headers = _objectSpread({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders);
289296
if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) {
290297
headers['User-Agent'] = "i18next-http-backend (node/".concat(global.process.version, "; ").concat(global.process.platform, " ").concat(global.process.arch, ")");
291298
}
292299
if (payload) headers['Content-Type'] = 'application/json';
293300
var reqOptions = typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions;
294-
var fetchOptions = (0, _utils.defaults)({
301+
var fetchOptions = _objectSpread({
295302
method: payload ? 'POST' : 'GET',
296303
body: payload ? options.stringify(payload) : undefined,
297304
headers: headers

i18nextHttpBackend.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaults, makePromise } from './utils.js'
1+
import { makePromise } from './utils.js'
22
import request from './request.js'
33

44
const getDefaults = () => {
@@ -35,7 +35,7 @@ class Backend {
3535

3636
init (services, options = {}, allOptions = {}) {
3737
this.services = services
38-
this.options = defaults(options, this.options || {}, getDefaults())
38+
this.options = { ...getDefaults(), ...(this.options || {}), ...options }
3939
this.allOptions = allOptions
4040
if (this.services && this.options.reloadInterval) {
4141
setInterval(() => this.reload(), this.options.reloadInterval)

lib/request.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defaults, hasXMLHttpRequest } from './utils.js'
1+
import { hasXMLHttpRequest } from './utils.js'
22
import * as fetchNode from './getFetch.cjs'
33

44
let fetchApi
@@ -64,17 +64,20 @@ const requestWithFetch = (options, url, payload, callback) => {
6464
if (options.queryStringParams) {
6565
url = addQueryString(url, options.queryStringParams)
6666
}
67-
const headers = defaults({}, typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders)
67+
const headers = {
68+
...(typeof options.customHeaders === 'function' ? options.customHeaders() : options.customHeaders)
69+
}
6870
if (typeof window === 'undefined' && typeof global !== 'undefined' && typeof global.process !== 'undefined' && global.process.versions && global.process.versions.node) {
6971
headers['User-Agent'] = `i18next-http-backend (node/${global.process.version}; ${global.process.platform} ${global.process.arch})`
7072
}
7173
if (payload) headers['Content-Type'] = 'application/json'
7274
const reqOptions = typeof options.requestOptions === 'function' ? options.requestOptions(payload) : options.requestOptions
73-
const fetchOptions = defaults(omitFetchOptions ? {} : reqOptions, {
75+
const fetchOptions = {
7476
method: payload ? 'POST' : 'GET',
7577
body: payload ? options.stringify(payload) : undefined,
76-
headers
77-
})
78+
headers,
79+
...(omitFetchOptions ? {} : reqOptions)
80+
}
7881
try {
7982
fetchIt(url, fetchOptions, callback)
8083
} catch (e) {

test/fixtures/server.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ const server = (done) => {
1212
res.jsonp(req.query)
1313
})
1414

15+
js.patch('/locales/it/testns', (req, res) => {
16+
res.jsonp({
17+
via: 'patch'
18+
})
19+
})
20+
21+
js.patch('/create/it/testns', (req, res) => {
22+
expect(req.body).not.to.eql({})
23+
res.jsonp()
24+
})
25+
1526
js.get('/locales/en/test', (req, res) => {
1627
res.jsonp({
1728
key: 'passing'

test/http.spec.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
165165
})
166166
})
167167

168-
describe('with custom request option', () => {
168+
describe('with custom request function option', () => {
169169
let backend, requestCall
170170

171171
before(() => {
@@ -217,6 +217,47 @@ describe(`http backend using ${hasXMLHttpRequest() ? 'XMLHttpRequest' : 'fetch'}
217217
})
218218
})
219219

220+
if (!hasXMLHttpRequest()) {
221+
describe('with custom request options', () => {
222+
let backend
223+
224+
before(() => {
225+
backend = new Http(
226+
{
227+
interpolator: i18next.services.interpolator
228+
},
229+
{
230+
loadPath: 'http://localhost:5001/locales/{{lng}}/{{ns}}',
231+
addPath: 'http://localhost:5001/create/{{lng}}/{{ns}}',
232+
requestOptions: {
233+
method: 'PATCH'
234+
}
235+
}
236+
)
237+
})
238+
239+
describe('#read', () => {
240+
it('should read data', (done) => {
241+
backend.read('it', 'testns', function (err, data) {
242+
expect(err).not.to.be.ok()
243+
expect(data).to.eql({ via: 'patch' })
244+
done()
245+
})
246+
})
247+
})
248+
249+
describe('#create', () => {
250+
it('should write data', (done) => {
251+
backend.create('it', 'testns', 'new.key', 'new value', (dataArray, resArray) => {
252+
expect(dataArray).to.eql([null])
253+
expect(resArray).to.eql([ { status: 200, data: '' } ])
254+
done()
255+
})
256+
})
257+
})
258+
})
259+
}
260+
220261
describe('with loadPath function returning falsy', () => {
221262
let backend
222263
let calledLanguages = []

0 commit comments

Comments
 (0)