-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
125 lines (101 loc) · 3.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const defaults = {
loadPath: '/locales/{{lng}}/{{ns}}.json',
addPath: '/locales/add/{{lng}}/{{ns}}',
multiSeparator: '+',
allowMultiLoading: false,
// eslint-disable-next-line n/no-unsupported-features/node-builtins
fetch: typeof fetch === 'undefined' ? undefined : fetch,
parse: JSON.parse,
stringify: JSON.stringify,
requestOptions: {},
};
const arrify = (val) => (Array.isArray(val) ? val : [val]);
const normalize = (funcOrVal, ...args) => (typeof funcOrVal === 'function' ? funcOrVal(...args) : funcOrVal);
class BackendError extends Error {
retry = null;
constructor(message, retry = false) {
super(message);
this.retry = retry;
}
}
export default class Backend {
constructor(services, options) {
this.init(services, options);
}
type = 'backend';
static type = 'backend';
init(services, options = {}) {
this.services = services;
this.options = {
...defaults,
...this.options,
...options,
};
}
getLoadPath(languages, namespaces) {
return normalize(this.options.loadPath, languages, namespaces);
}
read(language, namespace, callback) {
const loadPath = this.getLoadPath(language, namespace);
const url = this.services.interpolator.interpolate(loadPath, { lng: language, ns: namespace });
this.loadUrl(url, callback);
}
readMulti(languages, namespaces, callback) {
const loadPath = this.getLoadPath(languages, namespaces);
const { multiSeparator } = this.options;
const url = this.services.interpolator.interpolate(loadPath, {
lng: languages.join(multiSeparator),
ns: namespaces.join(multiSeparator),
});
this.loadUrl(url, callback);
}
loadUrl(url, callback) {
const { fetch, requestOptions, parse } = this.options;
fetch(url, requestOptions)
.then((response) => {
const { ok, status } = response;
if (!ok) {
const retry = status >= 500 && status < 600; // don't retry for 4xx codes
throw new BackendError(`failed loading ${url}`, retry);
}
return response.text();
}, () => {
throw new BackendError(`failed loading ${url}`);
})
.then((data) => {
try {
return callback(null, parse(data, url));
} catch {
throw new BackendError(`failed parsing ${url} to json`, false);
}
})
.catch((e) => {
if (e instanceof BackendError) {
callback(e.message, e.retry);
}
});
}
create(languages, namespace, key, fallbackValue) {
const payload = {
[key]: fallbackValue || '',
};
arrify(languages).forEach((lng) => {
const {
addPath,
requestOptions,
fetch,
stringify,
} = this.options;
const url = this.services.interpolator.interpolate(addPath, { lng, ns: namespace });
try {
fetch(url, {
method: 'POST',
body: stringify(payload),
...requestOptions,
});
} catch (e) {
console.error(e); // eslint-disable-line no-console
}
});
}
}