Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseLoadPayload for POST request #110

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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