i18next-http-middleware 3.2.1 → 3.3.0

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.
@@ -1,95 +1,74 @@
1
- var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
2
1
 
2
+ var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
3
3
  var serializeCookie = function serializeCookie(name, val, options) {
4
4
  var opt = options || {};
5
5
  opt.path = opt.path || '/';
6
6
  var value = encodeURIComponent(val);
7
7
  var str = name + '=' + value;
8
-
9
8
  if (opt.maxAge > 0) {
10
9
  var maxAge = opt.maxAge - 0;
11
10
  if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
12
11
  str += '; Max-Age=' + Math.floor(maxAge);
13
12
  }
14
-
15
13
  if (opt.domain) {
16
14
  if (!fieldContentRegExp.test(opt.domain)) {
17
15
  throw new TypeError('option domain is invalid');
18
16
  }
19
-
20
17
  str += '; Domain=' + opt.domain;
21
18
  }
22
-
23
19
  if (opt.path) {
24
20
  if (!fieldContentRegExp.test(opt.path)) {
25
21
  throw new TypeError('option path is invalid');
26
22
  }
27
-
28
23
  str += '; Path=' + opt.path;
29
24
  }
30
-
31
25
  if (opt.expires) {
32
26
  if (typeof opt.expires.toUTCString !== 'function') {
33
27
  throw new TypeError('option expires is invalid');
34
28
  }
35
-
36
29
  str += '; Expires=' + opt.expires.toUTCString();
37
30
  }
38
-
39
31
  if (opt.httpOnly) str += '; HttpOnly';
40
32
  if (opt.secure) str += '; Secure';
41
-
42
33
  if (opt.sameSite) {
43
34
  var sameSite = typeof opt.sameSite === 'string' ? opt.sameSite.toLowerCase() : opt.sameSite;
44
-
45
35
  switch (sameSite) {
46
36
  case true:
47
37
  str += '; SameSite=Strict';
48
38
  break;
49
-
50
39
  case 'lax':
51
40
  str += '; SameSite=Lax';
52
41
  break;
53
-
54
42
  case 'strict':
55
43
  str += '; SameSite=Strict';
56
44
  break;
57
-
58
45
  case 'none':
59
46
  str += '; SameSite=None';
60
47
  break;
61
-
62
48
  default:
63
49
  throw new TypeError('option sameSite is invalid');
64
50
  }
65
51
  }
66
-
67
52
  return str;
68
53
  };
69
-
70
54
  export default {
71
55
  name: 'cookie',
72
56
  lookup: function lookup(req, res, options) {
73
57
  var found;
74
-
75
58
  if (options.lookupCookie && typeof req !== 'undefined') {
76
59
  var cookies = options.getCookies(req);
77
60
  if (cookies) found = cookies[options.lookupCookie];
78
61
  }
79
-
80
62
  return found;
81
63
  },
82
64
  cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
83
65
  var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
84
-
85
66
  if (options.lookupCookie && req !== 'undefined') {
86
67
  var expirationDate = options.cookieExpirationDate;
87
-
88
68
  if (!expirationDate) {
89
69
  expirationDate = new Date();
90
70
  expirationDate.setFullYear(expirationDate.getFullYear() + 1);
91
71
  }
92
-
93
72
  var cookieOptions = {
94
73
  expires: expirationDate,
95
74
  domain: options.cookieDomain,
@@ -3,36 +3,29 @@ export default {
3
3
  name: 'header',
4
4
  lookup: function lookup(req, res, options) {
5
5
  var found;
6
-
7
6
  if (typeof req !== 'undefined') {
8
7
  var headers = options.getHeaders(req);
9
8
  if (!headers) return found;
10
9
  var locales = [];
11
10
  var acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language'];
12
-
13
11
  if (acceptLanguage) {
14
- var lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
15
-
12
+ var lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi;
16
13
  if (acceptLanguage.indexOf('-') > 0) {
17
14
  var foundSpecialCase = specialCases.find(function (s) {
18
15
  return acceptLanguage.toLowerCase().indexOf("-".concat(s)) > 0;
19
16
  });
20
- if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
17
+ if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi;
21
18
  }
22
-
23
19
  var lngs = [];
24
20
  var i;
25
21
  var m;
26
22
  var rgx = options.lookupHeaderRegex || lookupRegex;
27
-
28
23
  do {
29
24
  m = rgx.exec(acceptLanguage);
30
-
31
25
  if (m) {
32
26
  var lng = m[1];
33
27
  var weight = m[5] || '1';
34
28
  var q = Number(weight);
35
-
36
29
  if (lng && !isNaN(q)) {
37
30
  lngs.push({
38
31
  lng: lng,
@@ -41,19 +34,15 @@ export default {
41
34
  }
42
35
  }
43
36
  } while (m);
44
-
45
37
  lngs.sort(function (a, b) {
46
38
  return b.q - a.q;
47
39
  });
48
-
49
40
  for (i = 0; i < lngs.length; i++) {
50
41
  locales.push(lngs[i].lng);
51
42
  }
52
-
53
43
  if (locales.length) found = locales;
54
44
  }
55
45
  }
56
-
57
46
  return found;
58
47
  }
59
48
  };
@@ -2,28 +2,22 @@ export default {
2
2
  name: 'path',
3
3
  lookup: function lookup(req, res, options) {
4
4
  var found;
5
-
6
5
  if (req === undefined) {
7
6
  return found;
8
7
  }
9
-
10
8
  if (options.lookupPath !== undefined && req.params) {
11
9
  found = options.getParams(req)[options.lookupPath];
12
10
  }
13
-
14
11
  if (!found && typeof options.lookupFromPathIndex === 'number' && options.getOriginalUrl(req)) {
15
12
  var path = options.getOriginalUrl(req).split('?')[0];
16
13
  var parts = path.split('/');
17
-
18
14
  if (parts[0] === '') {
19
15
  parts.shift();
20
16
  }
21
-
22
17
  if (parts.length > options.lookupFromPathIndex) {
23
18
  found = parts[options.lookupFromPathIndex];
24
19
  }
25
20
  }
26
-
27
21
  return found;
28
22
  }
29
23
  };
@@ -2,24 +2,19 @@ export default {
2
2
  name: 'querystring',
3
3
  lookup: function lookup(req, res, options) {
4
4
  var found;
5
-
6
5
  if (options.lookupQuerystring !== undefined && typeof req !== 'undefined') {
7
6
  if (options.getQuery(req)) {
8
7
  found = options.getQuery(req)[options.lookupQuerystring];
9
8
  }
10
-
11
9
  if (!found && options.getUrl(req) && options.getUrl(req).indexOf('?')) {
12
10
  var lastPartOfUri = options.getUrl(req).substring(options.getUrl(req).indexOf('?'));
13
-
14
11
  if (typeof URLSearchParams !== 'undefined') {
15
12
  var urlParams = new URLSearchParams(lastPartOfUri);
16
13
  found = urlParams.get(options.lookupQuerystring);
17
14
  } else {
18
15
  var indexOfQsStart = lastPartOfUri.indexOf("".concat(options.lookupQuerystring, "="));
19
-
20
16
  if (indexOfQsStart > -1) {
21
17
  var restOfUri = lastPartOfUri.substring(options.lookupQuerystring.length + 2);
22
-
23
18
  if (restOfUri.indexOf('&') < 0) {
24
19
  found = restOfUri;
25
20
  } else {
@@ -29,7 +24,6 @@ export default {
29
24
  }
30
25
  }
31
26
  }
32
-
33
27
  return found;
34
28
  }
35
29
  };
@@ -1,19 +1,15 @@
1
1
  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); }
2
-
3
2
  export default {
4
3
  name: 'session',
5
4
  lookup: function lookup(req, res, options) {
6
5
  var found;
7
-
8
6
  if (options.lookupSession !== undefined && _typeof(req) && options.getSession(req)) {
9
7
  found = options.getSession(req)[options.lookupSession];
10
8
  }
11
-
12
9
  return found;
13
10
  },
14
11
  cacheUserLanguage: function cacheUserLanguage(req, res, lng) {
15
12
  var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
16
-
17
13
  if (options.lookupSession && req && options.getSession(req)) {
18
14
  options.getSession(req)[options.lookupSession] = lng;
19
15
  }
package/esm/utils.js CHANGED
@@ -2,15 +2,12 @@ export function setPath(object, path, newValue) {
2
2
  var stack;
3
3
  if (typeof path !== 'string') stack = [].concat(path);
4
4
  if (typeof path === 'string') stack = path.split('.');
5
-
6
5
  while (stack.length > 1) {
7
6
  var _key = stack.shift();
8
-
9
7
  if (_key.indexOf('###') > -1) _key = _key.replace(/###/g, '.');
10
8
  if (!object[_key]) object[_key] = {};
11
9
  object = object[_key];
12
10
  }
13
-
14
11
  var key = stack.shift();
15
12
  if (key.indexOf('###') > -1) key = key.replace(/###/g, '.');
16
13
  object[key] = newValue;
@@ -41,7 +38,6 @@ export function extend(obj) {
41
38
  export function removeLngFromUrl(url, lookupFromPathIndex) {
42
39
  var first = '';
43
40
  var pos = lookupFromPathIndex;
44
-
45
41
  if (url[0] === '/') {
46
42
  pos++;
47
43
  first = '/';
package/lib/index.js CHANGED
@@ -176,13 +176,24 @@ export function getResourcesHandler (i18next, options = {}) {
176
176
  options.setHeader(res, 'Cache-Control', 'no-cache')
177
177
  }
178
178
 
179
- const languages = options.getQuery(req)[options.lngParam || 'lng']
179
+ // first check query
180
+ let languages = options.getQuery(req)[options.lngParam || 'lng']
180
181
  ? options.getQuery(req)[options.lngParam || 'lng'].split(' ')
181
182
  : []
182
- const namespaces = options.getQuery(req)[options.nsParam || 'ns']
183
+ let namespaces = options.getQuery(req)[options.nsParam || 'ns']
183
184
  ? options.getQuery(req)[options.nsParam || 'ns'].split(' ')
184
185
  : []
185
186
 
187
+ // then check route params
188
+ if (languages.length === 0 && namespaces.length === 0) {
189
+ languages = options.getParams(req)[options.lngParam || 'lng']
190
+ ? options.getParams(req)[options.lngParam || 'lng'].split(' ')
191
+ : []
192
+ namespaces = options.getParams(req)[options.nsParam || 'ns']
193
+ ? options.getParams(req)[options.nsParam || 'ns'].split(' ')
194
+ : []
195
+ }
196
+
186
197
  // extend ns
187
198
  namespaces.forEach(ns => {
188
199
  if (i18next.options.ns && i18next.options.ns.indexOf(ns) < 0) {
@@ -14,10 +14,10 @@ export default {
14
14
  const acceptLanguage = options.lookupHeader ? headers[options.lookupHeader] : headers['accept-language']
15
15
 
16
16
  if (acceptLanguage) {
17
- let lookupRegex = /(([a-z]{2})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi
17
+ let lookupRegex = /(([a-z]{2,3})-?([A-Z]{2})?)\s*;?\s*(q=([0-9.]+))?/gi
18
18
  if (acceptLanguage.indexOf('-') > 0) {
19
19
  const foundSpecialCase = specialCases.find((s) => acceptLanguage.toLowerCase().indexOf(`-${s}`) > 0)
20
- if (foundSpecialCase) lookupRegex = /(([a-z]{2})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi
20
+ if (foundSpecialCase) lookupRegex = /(([a-z]{2,3})-?([A-Z]{4})?)\s*;?\s*(q=([0-9.]+))?/gi
21
21
  }
22
22
  const lngs = []; let i; let m
23
23
  const rgx = options.lookupHeaderRegex || lookupRegex
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-http-middleware",
3
- "version": "3.2.1",
3
+ "version": "3.3.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./cjs/index.js",
@@ -21,27 +21,27 @@
21
21
  "module": "./esm/index.js",
22
22
  "dependencies": {},
23
23
  "devDependencies": {
24
- "@babel/cli": "7.16.0",
25
- "@babel/core": "7.16.5",
26
- "@babel/preset-env": "7.16.5",
27
- "@hapi/hapi": "^20.2.1",
28
- "@types/express-serve-static-core": "^4.17.27",
24
+ "@babel/cli": "7.19.3",
25
+ "@babel/core": "7.19.6",
26
+ "@babel/preset-env": "7.19.4",
27
+ "@hapi/hapi": "^20.2.2",
28
+ "@types/express-serve-static-core": "^4.17.31",
29
29
  "babel-plugin-add-module-exports": "1.0.4",
30
- "eslint": "7.29.0",
31
- "eslint-config-standard": "16.0.3",
32
- "eslint-plugin-import": "2.25.3",
33
- "eslint-plugin-node": "11.1.0",
34
- "eslint-plugin-promise": "5.1.0",
30
+ "eslint": "8.26.0",
31
+ "eslint-config-standard": "17.0.0",
32
+ "eslint-plugin-import": "2.26.0",
33
+ "eslint-plugin-n": "15.3.0",
34
+ "eslint-plugin-promise": "6.1.1",
35
35
  "eslint-plugin-require-path-exists": "1.1.9",
36
36
  "eslint-plugin-standard": "5.0.0",
37
37
  "expect.js": "0.3.1",
38
- "express": "4.17.2",
39
- "fastify": "3.25.2",
40
- "i18next": "21.6.3",
41
- "mocha": "9.1.3",
42
- "supertest": "6.1.6",
43
- "tsd": "0.19.0",
44
- "uglify-js": "3.14.5"
38
+ "express": "4.18.2",
39
+ "fastify": "4.9.2",
40
+ "i18next": "22.0.2",
41
+ "mocha": "10.1.0",
42
+ "supertest": "6.3.1",
43
+ "tsd": "0.24.1",
44
+ "uglify-js": "3.17.4"
45
45
  },
46
46
  "description": "i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.",
47
47
  "keywords": [