Skip to content

Commit

Permalink
parseLoadPayload for POST request (#110)
Browse files Browse the repository at this point in the history
* parseLoadPayload for POST request added

* comment updated

* parseLoadPayload arguments updated

---------

Co-authored-by: Ruslan Shulga <[email protected]>
  • Loading branch information
nycruslan and Ruslan Shulga committed Mar 14, 2023
1 parent 1760cac commit 9261fc5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 returned it will send a POST request
parseLoadPayload: function(languages, namespaces) { return undefined },

// allow cross domain requests
crossDomain: false,
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 returned it will send a POST request
*/
parseLoadPayload?(
languages: string[],
namespaces: string[]
): { [key: string]: any } | undefined;
/**
* allow cross domain requests
*/
Expand Down
7 changes: 6 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand Down Expand Up @@ -66,7 +67,11 @@ class Backend {
}

loadUrl (url, callback, languages, namespaces) {
this.options.request(this.options, url, undefined, (err, res) => {
const lng = (typeof languages === 'string') ? [languages] : languages
const ns = (typeof namespaces === 'string') ? [namespaces] : namespaces
// parseLoadPayload — default undefined
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 */)
if (!res && err && err.message && err.message.indexOf('Failed to fetch') > -1) return callback('failed loading ' + url + ': ' + err.message, true /* retry */)
Expand Down

0 comments on commit 9261fc5

Please sign in to comment.