From 875a8e3fe853c26c15ffe0a43d3258358cf52b3a Mon Sep 17 00:00:00 2001 From: Ruslan Shulga Date: Mon, 13 Mar 2023 16:38:54 -0400 Subject: [PATCH 1/3] parseLoadPayload for POST request added --- README.md | 8 ++++++-- index.d.ts | 8 ++++++++ lib/index.js | 7 ++++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5d818e7..e121611 100644 --- a/README.md +++ b/README.md @@ -120,8 +120,12 @@ for plain browser: // here it removes the letter a from the json (bad idea) parse: function(data) { return data.replace(/a/g, ''); }, - //parse data before it has been sent by addPath - parsePayload: function(namespace, key, fallbackValue) { return { key } }, + // parse data before it has been sent by addPath + parsePayload: function(namespace, key, fallbackValue) { return { key: fallbackValue || "" } }, + + // parse data before it has been sent by loadPath + // if value return it will send a POST request + parseLoadPayload: function(languages, namespaces) { return undefined }, // allow cross domain requests crossDomain: false, diff --git a/index.d.ts b/index.d.ts index eff91bd..e3cdf87 100644 --- a/index.d.ts +++ b/index.d.ts @@ -46,6 +46,14 @@ export interface HttpBackendOptions { key: string, fallbackValue?: string ): { [key: string]: any }; + /** + * parse data before it has been sent by loadPath + * if value return it will send a POST request + */ + parseLoadPayload?( + languages: string[], + namespaces: string[] + ): { [key: string]: any } | undefined; /** * allow cross domain requests */ diff --git a/lib/index.js b/lib/index.js index e757b97..6802e09 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,6 +9,7 @@ const getDefaults = () => { parse: data => JSON.parse(data), stringify: JSON.stringify, parsePayload: (namespace, key, fallbackValue) => ({ [key]: fallbackValue || '' }), + parseLoadPayload: (languages, namespaces) => undefined, request, reloadInterval: typeof window !== 'undefined' ? false : 60 * 60 * 1000, customHeaders: {}, @@ -66,7 +67,11 @@ class Backend { } loadUrl (url, callback, languages, namespaces) { - this.options.request(this.options, url, undefined, (err, res) => { + if (typeof languages === 'string') languages = [languages] + if (typeof namespaces === 'string') namespaces = [namespaces] + // parseLoadPayload — default undefined + const payload = this.options.parseLoadPayload(languages, namespaces) + this.options.request(this.options, url, payload, (err, res) => { if (res && ((res.status >= 500 && res.status < 600) || !res.status)) return callback('failed loading ' + url + '; status code: ' + res.status, true /* retry */) if (res && res.status >= 400 && res.status < 500) return callback('failed loading ' + url + '; status code: ' + res.status, false /* no retry */) if (!res && err && err.message && err.message.indexOf('Failed to fetch') > -1) return callback('failed loading ' + url + ': ' + err.message, true /* retry */) From fe9d1b2054e34fc9ee5c651f0498c3653716d804 Mon Sep 17 00:00:00 2001 From: Ruslan Shulga Date: Mon, 13 Mar 2023 16:43:13 -0400 Subject: [PATCH 2/3] comment updated --- README.md | 2 +- index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e121611..fdc2098 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ for plain browser: parsePayload: function(namespace, key, fallbackValue) { return { key: fallbackValue || "" } }, // parse data before it has been sent by loadPath - // if value return it will send a POST request + // if value returned it will send a POST request parseLoadPayload: function(languages, namespaces) { return undefined }, // allow cross domain requests diff --git a/index.d.ts b/index.d.ts index e3cdf87..a43db17 100644 --- a/index.d.ts +++ b/index.d.ts @@ -48,7 +48,7 @@ export interface HttpBackendOptions { ): { [key: string]: any }; /** * parse data before it has been sent by loadPath - * if value return it will send a POST request + * if value returned it will send a POST request */ parseLoadPayload?( languages: string[], From ea3cd60a3acd05aa3074e7351569b021428534db Mon Sep 17 00:00:00 2001 From: Ruslan Shulga Date: Mon, 13 Mar 2023 17:28:24 -0400 Subject: [PATCH 3/3] parseLoadPayload arguments updated --- lib/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6802e09..2c80171 100644 --- a/lib/index.js +++ b/lib/index.js @@ -67,10 +67,10 @@ class Backend { } loadUrl (url, callback, languages, namespaces) { - if (typeof languages === 'string') languages = [languages] - if (typeof namespaces === 'string') namespaces = [namespaces] + const lng = (typeof languages === 'string') ? [languages] : languages + const ns = (typeof namespaces === 'string') ? [namespaces] : namespaces // parseLoadPayload — default undefined - const payload = this.options.parseLoadPayload(languages, namespaces) + const payload = this.options.parseLoadPayload(lng, ns) this.options.request(this.options, url, payload, (err, res) => { if (res && ((res.status >= 500 && res.status < 600) || !res.status)) return callback('failed loading ' + url + '; status code: ' + res.status, true /* retry */) if (res && res.status >= 400 && res.status < 500) return callback('failed loading ' + url + '; status code: ' + res.status, false /* no retry */)