Skip to content

Commit

Permalink
cleanup defaults()
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jan 31, 2024
1 parent 728bb16 commit 21b3c57
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
### 2.4.3

- fix: overriding options

### 2.4.2

- fix: mjs typings export
Expand Down
13 changes: 10 additions & 3 deletions i18nextHttpBackend.js
Expand Up @@ -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; }
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion i18nextHttpBackend.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/index.js
@@ -1,4 +1,4 @@
import { defaults, makePromise } from './utils.js'
import { makePromise } from './utils.js'
import request from './request.js'

const getDefaults = () => {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions 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
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/server.js
Expand Up @@ -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'
Expand Down
43 changes: 42 additions & 1 deletion test/http.spec.js
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit 21b3c57

Please sign in to comment.