i18next-http-middleware 3.2.0 → 3.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/LanguageDetector.js +0 -31
- package/cjs/httpFunctions.js +0 -40
- package/cjs/index.js +12 -59
- package/cjs/languageLookups/cookie.js +0 -22
- package/cjs/languageLookups/header.js +2 -13
- package/cjs/languageLookups/path.js +0 -6
- package/cjs/languageLookups/querystring.js +0 -6
- package/cjs/languageLookups/session.js +0 -5
- package/cjs/utils.js +0 -9
- package/esm/LanguageDetector.js +0 -20
- package/esm/httpFunctions.js +0 -8
- package/esm/index.js +12 -45
- package/esm/languageLookups/cookie.js +1 -22
- package/esm/languageLookups/header.js +2 -13
- package/esm/languageLookups/path.js +0 -6
- package/esm/languageLookups/querystring.js +0 -6
- package/esm/languageLookups/session.js +0 -4
- package/esm/utils.js +0 -4
- package/lib/index.js +1 -1
- package/lib/languageLookups/header.js +2 -2
- package/package.json +18 -18
package/cjs/LanguageDetector.js
CHANGED
|
@@ -1,38 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var utils = _interopRequireWildcard(require("./utils.js"));
|
|
11
|
-
|
|
12
9
|
var _cookie = _interopRequireDefault(require("./languageLookups/cookie.js"));
|
|
13
|
-
|
|
14
10
|
var _querystring = _interopRequireDefault(require("./languageLookups/querystring.js"));
|
|
15
|
-
|
|
16
11
|
var _path = _interopRequireDefault(require("./languageLookups/path.js"));
|
|
17
|
-
|
|
18
12
|
var _header = _interopRequireDefault(require("./languageLookups/header.js"));
|
|
19
|
-
|
|
20
13
|
var _session = _interopRequireDefault(require("./languageLookups/session.js"));
|
|
21
|
-
|
|
22
14
|
var _httpFunctions = require("./httpFunctions.js");
|
|
23
|
-
|
|
24
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
25
|
-
|
|
26
16
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
27
|
-
|
|
28
17
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
29
|
-
|
|
30
18
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
31
|
-
|
|
32
19
|
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, descriptor.key, descriptor); } }
|
|
33
|
-
|
|
34
20
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
35
|
-
|
|
36
21
|
function getDefaults() {
|
|
37
22
|
return (0, _httpFunctions.extendOptionsWithDefaults)({
|
|
38
23
|
order: ['querystring', 'cookie', 'header'],
|
|
@@ -45,19 +30,15 @@ function getDefaults() {
|
|
|
45
30
|
ignoreCase: true
|
|
46
31
|
});
|
|
47
32
|
}
|
|
48
|
-
|
|
49
33
|
var LanguageDetector = function () {
|
|
50
34
|
function LanguageDetector(services) {
|
|
51
35
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
52
36
|
var allOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
53
|
-
|
|
54
37
|
_classCallCheck(this, LanguageDetector);
|
|
55
|
-
|
|
56
38
|
this.type = 'languageDetector';
|
|
57
39
|
this.detectors = {};
|
|
58
40
|
this.init(services, options, allOptions);
|
|
59
41
|
}
|
|
60
|
-
|
|
61
42
|
_createClass(LanguageDetector, [{
|
|
62
43
|
key: "init",
|
|
63
44
|
value: function init(services) {
|
|
@@ -81,24 +62,19 @@ var LanguageDetector = function () {
|
|
|
81
62
|
key: "detect",
|
|
82
63
|
value: function detect(req, res, detectionOrder) {
|
|
83
64
|
var _this = this;
|
|
84
|
-
|
|
85
65
|
if (arguments.length < 2) return;
|
|
86
66
|
if (!detectionOrder) detectionOrder = this.options.order;
|
|
87
67
|
var found;
|
|
88
68
|
detectionOrder.forEach(function (detectorName) {
|
|
89
69
|
if (found || !_this.detectors[detectorName]) return;
|
|
90
|
-
|
|
91
70
|
var detections = _this.detectors[detectorName].lookup(req, res, _this.options);
|
|
92
|
-
|
|
93
71
|
if (!detections) return;
|
|
94
72
|
if (!Array.isArray(detections)) detections = [detections];
|
|
95
73
|
detections = detections.filter(function (d) {
|
|
96
74
|
return d !== undefined && d !== null;
|
|
97
75
|
});
|
|
98
|
-
|
|
99
76
|
if (_this.services.languageUtils.getBestMatchFromCodes) {
|
|
100
77
|
found = _this.services.languageUtils.getBestMatchFromCodes(detections);
|
|
101
|
-
|
|
102
78
|
if (found) {
|
|
103
79
|
if (_this.options.ignoreCase) {
|
|
104
80
|
if (detections.map(function (d) {
|
|
@@ -108,7 +84,6 @@ var LanguageDetector = function () {
|
|
|
108
84
|
if (detections.indexOf(found) < 0) found = undefined;
|
|
109
85
|
}
|
|
110
86
|
}
|
|
111
|
-
|
|
112
87
|
if (found) req.i18nextLookupName = detectorName;
|
|
113
88
|
} else {
|
|
114
89
|
found = detections.length > 0 ? detections[0] : null;
|
|
@@ -120,14 +95,12 @@ var LanguageDetector = function () {
|
|
|
120
95
|
if (typeof fallbacks === 'function') fallbacks = fallbacks();
|
|
121
96
|
if (typeof fallbacks === 'string') fallbacks = [fallbacks];
|
|
122
97
|
if (!fallbacks) fallbacks = [];
|
|
123
|
-
|
|
124
98
|
if (Object.prototype.toString.apply(fallbacks) === '[object Array]') {
|
|
125
99
|
found = fallbacks[0];
|
|
126
100
|
} else {
|
|
127
101
|
found = fallbacks[0] || fallbacks.default && fallbacks.default[0];
|
|
128
102
|
}
|
|
129
103
|
}
|
|
130
|
-
|
|
131
104
|
;
|
|
132
105
|
return found;
|
|
133
106
|
}
|
|
@@ -135,23 +108,19 @@ var LanguageDetector = function () {
|
|
|
135
108
|
key: "cacheUserLanguage",
|
|
136
109
|
value: function cacheUserLanguage(req, res, lng, caches) {
|
|
137
110
|
var _this2 = this;
|
|
138
|
-
|
|
139
111
|
if (arguments.length < 3) return;
|
|
140
112
|
if (!caches) caches = this.options.caches;
|
|
141
113
|
if (!caches) return;
|
|
142
114
|
caches.forEach(function (cacheName) {
|
|
143
115
|
if (_this2.detectors[cacheName] && _this2.detectors[cacheName].cacheUserLanguage && res.cachedUserLanguage !== lng) {
|
|
144
116
|
_this2.detectors[cacheName].cacheUserLanguage(req, res, lng, _this2.options);
|
|
145
|
-
|
|
146
117
|
res.cachedUserLanguage = lng;
|
|
147
118
|
}
|
|
148
119
|
});
|
|
149
120
|
}
|
|
150
121
|
}]);
|
|
151
|
-
|
|
152
122
|
return LanguageDetector;
|
|
153
123
|
}();
|
|
154
|
-
|
|
155
124
|
LanguageDetector.type = 'languageDetector';
|
|
156
125
|
var _default = LanguageDetector;
|
|
157
126
|
exports.default = _default;
|
package/cjs/httpFunctions.js
CHANGED
|
@@ -4,49 +4,38 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.setUrl = exports.setStatus = exports.setHeader = exports.setContentType = exports.send = exports.getUrl = exports.getSession = exports.getQuery = exports.getPath = exports.getParams = exports.getOriginalUrl = exports.getHeaders = exports.getHeader = exports.getCookies = exports.getBody = exports.extendOptionsWithDefaults = void 0;
|
|
7
|
-
|
|
8
7
|
var getPath = function getPath(req) {
|
|
9
8
|
if (req.path) return req.path;
|
|
10
9
|
if (req.raw && req.raw.path) return req.raw.path;
|
|
11
10
|
if (req.url) return req.url;
|
|
12
11
|
console.log('no possibility found to get path');
|
|
13
12
|
};
|
|
14
|
-
|
|
15
13
|
exports.getPath = getPath;
|
|
16
|
-
|
|
17
14
|
var getUrl = function getUrl(req) {
|
|
18
15
|
if (req.url && req.url.href) return req.url.href;
|
|
19
16
|
if (req.url) return req.url;
|
|
20
17
|
if (req.raw && req.raw.url) return req.raw.url;
|
|
21
18
|
console.log('no possibility found to get url');
|
|
22
19
|
};
|
|
23
|
-
|
|
24
20
|
exports.getUrl = getUrl;
|
|
25
|
-
|
|
26
21
|
var setUrl = function setUrl(req, url) {
|
|
27
22
|
if (req.url) {
|
|
28
23
|
req.url = url;
|
|
29
24
|
return;
|
|
30
25
|
}
|
|
31
|
-
|
|
32
26
|
console.log('no possibility found to get url');
|
|
33
27
|
};
|
|
34
|
-
|
|
35
28
|
exports.setUrl = setUrl;
|
|
36
|
-
|
|
37
29
|
var getOriginalUrl = function getOriginalUrl(req) {
|
|
38
30
|
if (req.originalUrl) return req.originalUrl;
|
|
39
31
|
if (req.raw && req.raw.originalUrl) return req.raw.originalUrl;
|
|
40
32
|
return getUrl(req);
|
|
41
33
|
};
|
|
42
|
-
|
|
43
34
|
exports.getOriginalUrl = getOriginalUrl;
|
|
44
|
-
|
|
45
35
|
var getQuery = function getQuery(req) {
|
|
46
36
|
if (req.query && typeof req.query.entries === 'function' && typeof Object.fromEntries === 'function' && typeof req.query[Symbol.iterator] === 'function') {
|
|
47
37
|
return Object.fromEntries(req.query);
|
|
48
38
|
}
|
|
49
|
-
|
|
50
39
|
if (req.query) return req.query;
|
|
51
40
|
if (req.searchParams) return req.searchParams;
|
|
52
41
|
if (req.raw && req.raw.query) return req.raw.query;
|
|
@@ -56,9 +45,7 @@ var getQuery = function getQuery(req) {
|
|
|
56
45
|
console.log('no possibility found to get query');
|
|
57
46
|
return {};
|
|
58
47
|
};
|
|
59
|
-
|
|
60
48
|
exports.getQuery = getQuery;
|
|
61
|
-
|
|
62
49
|
var getParams = function getParams(req) {
|
|
63
50
|
if (req.params) return req.params;
|
|
64
51
|
if (req.raw && req.raw.params) return req.raw.params;
|
|
@@ -66,19 +53,14 @@ var getParams = function getParams(req) {
|
|
|
66
53
|
console.log('no possibility found to get params');
|
|
67
54
|
return {};
|
|
68
55
|
};
|
|
69
|
-
|
|
70
56
|
exports.getParams = getParams;
|
|
71
|
-
|
|
72
57
|
var getHeaders = function getHeaders(req) {
|
|
73
58
|
if (req.headers) return req.headers;
|
|
74
59
|
console.log('no possibility found to get headers');
|
|
75
60
|
};
|
|
76
|
-
|
|
77
61
|
exports.getHeaders = getHeaders;
|
|
78
|
-
|
|
79
62
|
var getCookies = function getCookies(req) {
|
|
80
63
|
if (req.cookies) return req.cookies;
|
|
81
|
-
|
|
82
64
|
if (getHeaders(req)) {
|
|
83
65
|
var list = {};
|
|
84
66
|
var rc = getHeaders(req).cookie;
|
|
@@ -88,17 +70,13 @@ var getCookies = function getCookies(req) {
|
|
|
88
70
|
});
|
|
89
71
|
return list;
|
|
90
72
|
}
|
|
91
|
-
|
|
92
73
|
console.log('no possibility found to get cookies');
|
|
93
74
|
};
|
|
94
|
-
|
|
95
75
|
exports.getCookies = getCookies;
|
|
96
|
-
|
|
97
76
|
var getBody = function getBody(req) {
|
|
98
77
|
if (req.ctx && typeof req.ctx.body === 'function') {
|
|
99
78
|
return req.ctx.body.bind(req.ctx);
|
|
100
79
|
}
|
|
101
|
-
|
|
102
80
|
if (req.ctx && req.ctx.body) return req.ctx.body;
|
|
103
81
|
if (req.json) return req.json;
|
|
104
82
|
if (req.body) return req.body;
|
|
@@ -106,9 +84,7 @@ var getBody = function getBody(req) {
|
|
|
106
84
|
console.log('no possibility found to get body');
|
|
107
85
|
return {};
|
|
108
86
|
};
|
|
109
|
-
|
|
110
87
|
exports.getBody = getBody;
|
|
111
|
-
|
|
112
88
|
var getHeader = function getHeader(res, name) {
|
|
113
89
|
if (res.getHeader) return res.getHeader(name);
|
|
114
90
|
if (res.headers) return res.headers[name];
|
|
@@ -116,58 +92,43 @@ var getHeader = function getHeader(res, name) {
|
|
|
116
92
|
console.log('no possibility found to get header');
|
|
117
93
|
return undefined;
|
|
118
94
|
};
|
|
119
|
-
|
|
120
95
|
exports.getHeader = getHeader;
|
|
121
|
-
|
|
122
96
|
var setHeader = function setHeader(res, name, value) {
|
|
123
97
|
if (res._headerSent || res.headersSent) return;
|
|
124
98
|
if (typeof res.setHeader === 'function') return res.setHeader(name, value);
|
|
125
99
|
if (typeof res.header === 'function') return res.header(name, value);
|
|
126
|
-
|
|
127
100
|
if (res.responseHeaders && typeof res.responseHeaders.set === 'function') {
|
|
128
101
|
return res.responseHeaders.set(name, value);
|
|
129
102
|
}
|
|
130
|
-
|
|
131
103
|
if (res.headers && typeof res.headers.set === 'function') {
|
|
132
104
|
return res.headers.set(name, value);
|
|
133
105
|
}
|
|
134
|
-
|
|
135
106
|
console.log('no possibility found to set header');
|
|
136
107
|
};
|
|
137
|
-
|
|
138
108
|
exports.setHeader = setHeader;
|
|
139
|
-
|
|
140
109
|
var setContentType = function setContentType(res, type) {
|
|
141
110
|
if (typeof res.contentType === 'function') return res.contentType(type);
|
|
142
111
|
if (typeof res.type === 'function') return res.type(type);
|
|
143
112
|
setHeader(res, 'Content-Type', type);
|
|
144
113
|
};
|
|
145
|
-
|
|
146
114
|
exports.setContentType = setContentType;
|
|
147
|
-
|
|
148
115
|
var setStatus = function setStatus(res, code) {
|
|
149
116
|
if (typeof res.status === 'function') return res.status(code);
|
|
150
117
|
if (res.status) return res.status = code;
|
|
151
118
|
console.log('no possibility found to set status');
|
|
152
119
|
};
|
|
153
|
-
|
|
154
120
|
exports.setStatus = setStatus;
|
|
155
|
-
|
|
156
121
|
var send = function send(res, body) {
|
|
157
122
|
if (typeof res.send === 'function') return res.send(body);
|
|
158
123
|
return body;
|
|
159
124
|
};
|
|
160
|
-
|
|
161
125
|
exports.send = send;
|
|
162
|
-
|
|
163
126
|
var getSession = function getSession(req) {
|
|
164
127
|
if (req.session) return req.session;
|
|
165
128
|
if (req.raw && req.raw.session) return req.raw.session;
|
|
166
129
|
console.log('no possibility found to get session');
|
|
167
130
|
};
|
|
168
|
-
|
|
169
131
|
exports.getSession = getSession;
|
|
170
|
-
|
|
171
132
|
var extendOptionsWithDefaults = function extendOptionsWithDefaults() {
|
|
172
133
|
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
173
134
|
options.getPath = options.getPath || getPath;
|
|
@@ -187,5 +148,4 @@ var extendOptionsWithDefaults = function extendOptionsWithDefaults() {
|
|
|
187
148
|
options.send = options.send || send;
|
|
188
149
|
return options;
|
|
189
150
|
};
|
|
190
|
-
|
|
191
151
|
exports.extendOptionsWithDefaults = extendOptionsWithDefaults;
|
package/cjs/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
@@ -13,28 +12,17 @@ exports.handle = handle;
|
|
|
13
12
|
exports.hapiPlugin = void 0;
|
|
14
13
|
exports.missingKeyHandler = missingKeyHandler;
|
|
15
14
|
exports.plugin = plugin;
|
|
16
|
-
|
|
17
15
|
var utils = _interopRequireWildcard(require("./utils.js"));
|
|
18
|
-
|
|
19
16
|
var _LanguageDetector = _interopRequireDefault(require("./LanguageDetector.js"));
|
|
20
|
-
|
|
21
17
|
var _httpFunctions = require("./httpFunctions.js");
|
|
22
|
-
|
|
23
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
|
-
|
|
25
19
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
26
|
-
|
|
27
20
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
28
|
-
|
|
29
21
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
30
|
-
|
|
31
22
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
32
|
-
|
|
33
23
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
34
|
-
|
|
35
24
|
var LanguageDetector = _LanguageDetector.default;
|
|
36
25
|
exports.LanguageDetector = LanguageDetector;
|
|
37
|
-
|
|
38
26
|
var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
|
|
39
27
|
if (!res) {
|
|
40
28
|
if (req.request && req.response) {
|
|
@@ -47,82 +35,70 @@ var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
|
|
|
47
35
|
if (!next) next = function next() {};
|
|
48
36
|
}
|
|
49
37
|
}
|
|
50
|
-
|
|
51
38
|
return {
|
|
52
39
|
req: req,
|
|
53
40
|
res: res,
|
|
54
41
|
next: next
|
|
55
42
|
};
|
|
56
43
|
};
|
|
57
|
-
|
|
58
44
|
function handle(i18next) {
|
|
59
45
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
60
46
|
(0, _httpFunctions.extendOptionsWithDefaults)(options);
|
|
61
47
|
return function i18nextMiddleware(rq, rs, n) {
|
|
62
48
|
var _checkForCombinedReqR = checkForCombinedReqRes(rq, rs, n),
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
49
|
+
req = _checkForCombinedReqR.req,
|
|
50
|
+
res = _checkForCombinedReqR.res,
|
|
51
|
+
next = _checkForCombinedReqR.next;
|
|
67
52
|
if (typeof options.ignoreRoutes === 'function') {
|
|
68
53
|
if (options.ignoreRoutes(req, res, options, i18next)) {
|
|
69
54
|
return next();
|
|
70
55
|
}
|
|
71
56
|
} else {
|
|
72
57
|
var ignores = options.ignoreRoutes instanceof Array && options.ignoreRoutes || [];
|
|
73
|
-
|
|
74
58
|
for (var i = 0; i < ignores.length; i++) {
|
|
75
59
|
if (options.getPath(req).indexOf(ignores[i]) > -1) return next();
|
|
76
60
|
}
|
|
77
61
|
}
|
|
78
|
-
|
|
79
62
|
var i18n = i18next.cloneInstance({
|
|
80
63
|
initImmediate: false
|
|
81
64
|
});
|
|
82
65
|
i18n.on('languageChanged', function (lng) {
|
|
83
66
|
req.language = req.locale = req.lng = lng;
|
|
84
67
|
if (options.attachLocals) res.locals = res.locals || {};
|
|
85
|
-
|
|
86
68
|
if (res.locals) {
|
|
87
69
|
res.locals.language = lng;
|
|
88
70
|
res.locals.languageDir = i18next.dir(lng);
|
|
89
71
|
}
|
|
90
|
-
|
|
91
72
|
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
|
|
92
73
|
options.setHeader(res, 'Content-Language', lng);
|
|
93
74
|
}
|
|
94
|
-
|
|
95
75
|
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
96
|
-
|
|
97
76
|
if (i18next.services.languageDetector) {
|
|
98
77
|
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
99
78
|
}
|
|
100
79
|
});
|
|
101
80
|
var lng = req.lng;
|
|
102
|
-
|
|
103
81
|
if (!lng && i18next.services.languageDetector) {
|
|
104
82
|
lng = i18next.services.languageDetector.detect(req, res);
|
|
105
83
|
}
|
|
106
84
|
|
|
107
85
|
req.language = req.locale = req.lng = lng;
|
|
108
|
-
|
|
109
86
|
if (lng && options.getHeader(res, 'Content-Language') !== lng) {
|
|
110
87
|
options.setHeader(res, 'Content-Language', lng);
|
|
111
88
|
}
|
|
112
|
-
|
|
113
89
|
req.languages = i18next.services.languageUtils.toResolveHierarchy(lng);
|
|
114
|
-
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
|
|
115
90
|
|
|
91
|
+
i18n.changeLanguage(lng || i18next.options.fallbackLng[0]);
|
|
116
92
|
if (req.i18nextLookupName === 'path' && options.removeLngFromUrl) {
|
|
117
93
|
options.setUrl(req, utils.removeLngFromUrl(options.getUrl(req), i18next.services.languageDetector.options.lookupFromPathIndex));
|
|
118
94
|
}
|
|
119
|
-
|
|
120
95
|
var t = i18n.t.bind(i18n);
|
|
121
96
|
var exists = i18n.exists.bind(i18n);
|
|
97
|
+
|
|
122
98
|
req.i18n = i18n;
|
|
123
99
|
req.t = t;
|
|
124
|
-
if (options.attachLocals) res.locals = res.locals || {};
|
|
125
100
|
|
|
101
|
+
if (options.attachLocals) res.locals = res.locals || {};
|
|
126
102
|
if (res.locals) {
|
|
127
103
|
res.locals.t = t;
|
|
128
104
|
res.locals.exists = exists;
|
|
@@ -130,18 +106,15 @@ function handle(i18next) {
|
|
|
130
106
|
res.locals.language = lng;
|
|
131
107
|
res.locals.languageDir = i18next.dir(lng);
|
|
132
108
|
}
|
|
133
|
-
|
|
134
109
|
if (i18next.services.languageDetector) {
|
|
135
110
|
i18next.services.languageDetector.cacheUserLanguage(req, res, lng);
|
|
136
111
|
}
|
|
137
|
-
|
|
138
112
|
if (!req.lng) return next();
|
|
139
113
|
i18next.loadLanguages(req.lng, function () {
|
|
140
114
|
return next();
|
|
141
115
|
});
|
|
142
116
|
};
|
|
143
117
|
}
|
|
144
|
-
|
|
145
118
|
function plugin(instance, options, next) {
|
|
146
119
|
options.attachLocals = true;
|
|
147
120
|
var middleware = handle(options.i18next, options);
|
|
@@ -150,14 +123,13 @@ function plugin(instance, options, next) {
|
|
|
150
123
|
});
|
|
151
124
|
return next();
|
|
152
125
|
}
|
|
153
|
-
|
|
154
126
|
var hapiPlugin = {
|
|
155
127
|
name: 'i18next-http-middleware',
|
|
156
128
|
version: '1',
|
|
157
129
|
register: function register(server, options) {
|
|
158
130
|
options.attachLocals = true;
|
|
159
131
|
var middleware = handle(options.i18next, _objectSpread({}, options));
|
|
160
|
-
server.ext('
|
|
132
|
+
server.ext('onPreAuth', function (request, h) {
|
|
161
133
|
middleware(request, request.raw.res || h.response() || request.Response, function () {
|
|
162
134
|
return h.continue;
|
|
163
135
|
});
|
|
@@ -167,24 +139,20 @@ var hapiPlugin = {
|
|
|
167
139
|
};
|
|
168
140
|
exports.hapiPlugin = hapiPlugin;
|
|
169
141
|
plugin[Symbol.for('skip-override')] = true;
|
|
170
|
-
|
|
171
142
|
function getResourcesHandler(i18next) {
|
|
172
143
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
173
144
|
(0, _httpFunctions.extendOptionsWithDefaults)(options);
|
|
174
145
|
var maxAge = options.maxAge || 60 * 60 * 24 * 30;
|
|
175
146
|
return function (rq, rs) {
|
|
176
147
|
var _checkForCombinedReqR2 = checkForCombinedReqRes(rq, rs),
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
148
|
+
req = _checkForCombinedReqR2.req,
|
|
149
|
+
res = _checkForCombinedReqR2.res;
|
|
180
150
|
if (!i18next.services.backendConnector) {
|
|
181
151
|
options.setStatus(res, 404);
|
|
182
152
|
return options.send(res, 'i18next-express-middleware:: no backend configured');
|
|
183
153
|
}
|
|
184
|
-
|
|
185
154
|
var resources = {};
|
|
186
155
|
options.setContentType(res, 'application/json');
|
|
187
|
-
|
|
188
156
|
if (options.cache !== undefined ? options.cache : typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
|
|
189
157
|
options.setHeader(res, 'Cache-Control', 'public, max-age=' + maxAge);
|
|
190
158
|
options.setHeader(res, 'Expires', new Date(new Date().getTime() + maxAge * 1000).toUTCString());
|
|
@@ -192,9 +160,9 @@ function getResourcesHandler(i18next) {
|
|
|
192
160
|
options.setHeader(res, 'Pragma', 'no-cache');
|
|
193
161
|
options.setHeader(res, 'Cache-Control', 'no-cache');
|
|
194
162
|
}
|
|
195
|
-
|
|
196
163
|
var languages = options.getQuery(req)[options.lngParam || 'lng'] ? options.getQuery(req)[options.lngParam || 'lng'].split(' ') : [];
|
|
197
164
|
var namespaces = options.getQuery(req)[options.nsParam || 'ns'] ? options.getQuery(req)[options.nsParam || 'ns'].split(' ') : [];
|
|
165
|
+
|
|
198
166
|
namespaces.forEach(function (ns) {
|
|
199
167
|
if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
|
|
200
168
|
i18next.options.ns.push(ns);
|
|
@@ -210,49 +178,39 @@ function getResourcesHandler(i18next) {
|
|
|
210
178
|
return options.send(res, resources);
|
|
211
179
|
};
|
|
212
180
|
}
|
|
213
|
-
|
|
214
181
|
function missingKeyHandler(i18next) {
|
|
215
182
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
216
183
|
(0, _httpFunctions.extendOptionsWithDefaults)(options);
|
|
217
184
|
return function (rq, rs) {
|
|
218
185
|
var _checkForCombinedReqR3 = checkForCombinedReqRes(rq, rs),
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
186
|
+
req = _checkForCombinedReqR3.req,
|
|
187
|
+
res = _checkForCombinedReqR3.res;
|
|
222
188
|
var lng = options.getParams(req)[options.lngParam || 'lng'];
|
|
223
189
|
var ns = options.getParams(req)[options.nsParam || 'ns'];
|
|
224
|
-
|
|
225
190
|
if (!i18next.services.backendConnector) {
|
|
226
191
|
options.setStatus(res, 404);
|
|
227
192
|
return options.send(res, 'i18next-express-middleware:: no backend configured');
|
|
228
193
|
}
|
|
229
|
-
|
|
230
194
|
var body = options.getBody(req);
|
|
231
|
-
|
|
232
195
|
if (typeof body === 'function') {
|
|
233
196
|
var promise = body();
|
|
234
|
-
|
|
235
197
|
if (promise && typeof promise.then === 'function') {
|
|
236
198
|
return new Promise(function (resolve) {
|
|
237
199
|
promise.then(function (b) {
|
|
238
200
|
for (var m in b) {
|
|
239
201
|
i18next.services.backendConnector.saveMissing([lng], ns, m, b[m]);
|
|
240
202
|
}
|
|
241
|
-
|
|
242
203
|
resolve(options.send(res, 'ok'));
|
|
243
204
|
});
|
|
244
205
|
});
|
|
245
206
|
}
|
|
246
207
|
}
|
|
247
|
-
|
|
248
208
|
for (var m in body) {
|
|
249
209
|
i18next.services.backendConnector.saveMissing([lng], ns, m, body[m]);
|
|
250
210
|
}
|
|
251
|
-
|
|
252
211
|
return options.send(res, 'ok');
|
|
253
212
|
};
|
|
254
213
|
}
|
|
255
|
-
|
|
256
214
|
function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
257
215
|
if (typeof verb === 'function') {
|
|
258
216
|
fc = verb;
|
|
@@ -260,14 +218,11 @@ function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
260
218
|
}
|
|
261
219
|
|
|
262
220
|
var callbacks = [fc].concat(Array.prototype.slice.call(arguments, 6));
|
|
263
|
-
|
|
264
221
|
for (var i = 0, li = lngs.length; i < li; i++) {
|
|
265
222
|
var parts = String(route).split('/');
|
|
266
223
|
var locRoute = [];
|
|
267
|
-
|
|
268
224
|
for (var y = 0, ly = parts.length; y < ly; y++) {
|
|
269
225
|
var part = parts[y];
|
|
270
|
-
|
|
271
226
|
if (part === ':lng') {
|
|
272
227
|
locRoute.push(lngs[i]);
|
|
273
228
|
} else if (part.indexOf(':') === 0 || part === '') {
|
|
@@ -278,12 +233,10 @@ function addRoute(i18next, route, lngs, app, verb, fc) {
|
|
|
278
233
|
}));
|
|
279
234
|
}
|
|
280
235
|
}
|
|
281
|
-
|
|
282
236
|
var routes = [locRoute.join('/')];
|
|
283
237
|
app[verb || 'get'].apply(app, routes.concat(callbacks));
|
|
284
238
|
}
|
|
285
239
|
}
|
|
286
|
-
|
|
287
240
|
var _default = {
|
|
288
241
|
plugin: plugin,
|
|
289
242
|
hapiPlugin: hapiPlugin,
|
|
@@ -5,97 +5,75 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
7
|
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
8
|
-
|
|
9
8
|
var serializeCookie = function serializeCookie(name, val, options) {
|
|
10
9
|
var opt = options || {};
|
|
11
10
|
opt.path = opt.path || '/';
|
|
12
11
|
var value = encodeURIComponent(val);
|
|
13
12
|
var str = name + '=' + value;
|
|
14
|
-
|
|
15
13
|
if (opt.maxAge > 0) {
|
|
16
14
|
var maxAge = opt.maxAge - 0;
|
|
17
15
|
if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
|
|
18
16
|
str += '; Max-Age=' + Math.floor(maxAge);
|
|
19
17
|
}
|
|
20
|
-
|
|
21
18
|
if (opt.domain) {
|
|
22
19
|
if (!fieldContentRegExp.test(opt.domain)) {
|
|
23
20
|
throw new TypeError('option domain is invalid');
|
|
24
21
|
}
|
|
25
|
-
|
|
26
22
|
str += '; Domain=' + opt.domain;
|
|
27
23
|
}
|
|
28
|
-
|
|
29
24
|
if (opt.path) {
|
|
30
25
|
if (!fieldContentRegExp.test(opt.path)) {
|
|
31
26
|
throw new TypeError('option path is invalid');
|
|
32
27
|
}
|
|
33
|
-
|
|
34
28
|
str += '; Path=' + opt.path;
|
|
35
29
|
}
|
|
36
|
-
|
|
37
30
|
if (opt.expires) {
|
|
38
31
|
if (typeof opt.expires.toUTCString !== 'function') {
|
|
39
32
|
throw new TypeError('option expires is invalid');
|
|
40
33
|
}
|
|
41
|
-
|
|
42
34
|
str += '; Expires=' + opt.expires.toUTCString();
|
|
43
35
|
}
|
|
44
|
-
|
|
45
36
|
if (opt.httpOnly) str += '; HttpOnly';
|
|
46
37
|
if (opt.secure) str += '; Secure';
|
|
47
|
-
|
|
48
38
|
if (opt.sameSite) {
|
|
49
39
|
var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
50
|
-
|
|
51
40
|
switch (sameSite) {
|
|
52
41
|
case true:
|
|
53
42
|
str += '; SameSite=Strict';
|
|
54
43
|
break;
|
|
55
|
-
|
|
56
44
|
case 'lax':
|
|
57
45
|
str += '; SameSite=Lax';
|
|
58
46
|
break;
|
|
59
|
-
|
|
60
47
|
case 'strict':
|
|
61
48
|
str += '; SameSite=Strict';
|
|
62
49
|
break;
|
|
63
|
-
|
|
64
50
|
case 'none':
|
|
65
51
|
str += '; SameSite=None';
|
|
66
52
|
break;
|
|
67
|
-
|
|
68
53
|
default:
|
|
69
54
|
throw new TypeError('option sameSite is invalid');
|
|
70
55
|
}
|
|
71
56
|
}
|
|
72
|
-
|
|
73
57
|
return str;
|
|
74
58
|
};
|
|
75
|
-
|
|
76
59
|
var _default = {
|
|
77
60
|
name: 'cookie',
|
|
78
61
|
lookup: function lookup(req, res, options) {
|
|
79
62
|
var found;
|
|
80
|
-
|
|
81
63
|
if (options.lookupCookie && typeof req !== 'undefined') {
|
|
82
64
|
var cookies = options.getCookies(req);
|
|
83
65
|
if (cookies) found = cookies[options.lookupCookie];
|
|
84
66
|
}
|
|
85
|
-
|
|
86
67
|
return found;
|
|
87
68
|
},
|
|
88
69
|
cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
|
|
89
70
|
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
90
|
-
|
|
91
71
|
if (options.lookupCookie && req !== 'undefined') {
|
|
92
72
|
var expirationDate = options.cookieExpirationDate;
|
|
93
|
-
|
|
94
73
|
if (!expirationDate) {
|
|
95
74
|
expirationDate = new Date();
|
|
96
75
|
expirationDate.setFullYear(expirationDate.getFullYear() + 1);
|
|
97
76
|
}
|
|
98
|
-
|
|
99
77
|
var cookieOptions = {
|
|
100
78
|
expires: expirationDate,
|
|
101
79
|
domain: options.cookieDomain,
|