i18next-http-middleware 3.3.1 → 3.4.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.
package/README.md CHANGED
@@ -128,6 +128,36 @@ server.route({
128
128
 
129
129
  ```
130
130
 
131
+ ### Koa usage
132
+
133
+ ```js
134
+ var i18next = require('i18next')
135
+ var middleware = require('i18next-http-middleware')
136
+ const Koa = require('koa')
137
+ const router = require('@koa/router')()
138
+
139
+ i18next.use(middleware.LanguageDetector).init({
140
+ preload: ['en', 'de', 'it'],
141
+ ...otherOptions
142
+ })
143
+
144
+ var app = new Koa()
145
+ app.use(i18nextMiddleware.koaPlugin(i18next, {
146
+ ignoreRoutes: ['/foo'] // or function(req, res, options, i18next) { /* return true to ignore */ }
147
+ }))
148
+
149
+ // in your request handler
150
+ router.get('/myRoute', ctx => {
151
+ ctx.body = JSON.stringify({
152
+ 'ctx.language': ctx.language,
153
+ 'ctx.i18n.language': ctx.i18n.language,
154
+ 'ctx.i18n.languages': ctx.i18n.languages,
155
+ 'ctx.i18n.languages[0]': ctx.i18n.languages[0],
156
+ 'ctx.t("home.title")': ctx.t('home.title')
157
+ }, null, 2)
158
+ })
159
+ ```
160
+
131
161
  ### Deno usage
132
162
 
133
163
  #### abc
@@ -14,7 +14,7 @@ var _httpFunctions = require("./httpFunctions.js");
14
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
15
  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); }
16
16
  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; }
17
- 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); }
17
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
18
18
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
19
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, _toPropertyKey(descriptor.key), descriptor); } }
20
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; }
@@ -123,6 +123,5 @@ var LanguageDetector = function () {
123
123
  return LanguageDetector;
124
124
  }();
125
125
  LanguageDetector.type = 'languageDetector';
126
- var _default = LanguageDetector;
127
- exports.default = _default;
126
+ var _default = exports.default = LanguageDetector;
128
127
  module.exports = exports.default;
@@ -4,35 +4,31 @@ 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
- var getPath = function getPath(req) {
7
+ var getPath = exports.getPath = function getPath(req) {
8
8
  if (req.path) return req.path;
9
9
  if (req.raw && req.raw.path) return req.raw.path;
10
10
  if (req.url) return req.url;
11
11
  console.log('no possibility found to get path');
12
12
  };
13
- exports.getPath = getPath;
14
- var getUrl = function getUrl(req) {
13
+ var getUrl = exports.getUrl = function getUrl(req) {
15
14
  if (req.url && req.url.href) return req.url.href;
16
15
  if (req.url) return req.url;
17
16
  if (req.raw && req.raw.url) return req.raw.url;
18
17
  console.log('no possibility found to get url');
19
18
  };
20
- exports.getUrl = getUrl;
21
- var setUrl = function setUrl(req, url) {
19
+ var setUrl = exports.setUrl = function setUrl(req, url) {
22
20
  if (req.url) {
23
21
  req.url = url;
24
22
  return;
25
23
  }
26
24
  console.log('no possibility found to get url');
27
25
  };
28
- exports.setUrl = setUrl;
29
- var getOriginalUrl = function getOriginalUrl(req) {
26
+ var getOriginalUrl = exports.getOriginalUrl = function getOriginalUrl(req) {
30
27
  if (req.originalUrl) return req.originalUrl;
31
28
  if (req.raw && req.raw.originalUrl) return req.raw.originalUrl;
32
29
  return getUrl(req);
33
30
  };
34
- exports.getOriginalUrl = getOriginalUrl;
35
- var getQuery = function getQuery(req) {
31
+ var getQuery = exports.getQuery = function getQuery(req) {
36
32
  if (req.query && typeof req.query.entries === 'function' && typeof Object.fromEntries === 'function' && typeof req.query[Symbol.iterator] === 'function') {
37
33
  return Object.fromEntries(req.query);
38
34
  }
@@ -40,26 +36,24 @@ var getQuery = function getQuery(req) {
40
36
  if (req.searchParams) return req.searchParams;
41
37
  if (req.raw && req.raw.query) return req.raw.query;
42
38
  if (req.ctx && req.ctx.queryParams) return req.ctx.queryParams;
39
+ if (req.url && req.url.searchParams) return req.url.searchParams;
43
40
  var url = req.url || req.raw && req.raw.url;
44
41
  if (url && url.indexOf('?') < 0) return {};
45
42
  console.log('no possibility found to get query');
46
43
  return {};
47
44
  };
48
- exports.getQuery = getQuery;
49
- var getParams = function getParams(req) {
45
+ var getParams = exports.getParams = function getParams(req) {
50
46
  if (req.params) return req.params;
51
47
  if (req.raw && req.raw.params) return req.raw.params;
52
48
  if (req.ctx && req.ctx.params) return req.ctx.params;
53
49
  console.log('no possibility found to get params');
54
50
  return {};
55
51
  };
56
- exports.getParams = getParams;
57
- var getHeaders = function getHeaders(req) {
52
+ var getHeaders = exports.getHeaders = function getHeaders(req) {
58
53
  if (req.headers) return req.headers;
59
54
  console.log('no possibility found to get headers');
60
55
  };
61
- exports.getHeaders = getHeaders;
62
- var getCookies = function getCookies(req) {
56
+ var getCookies = exports.getCookies = function getCookies(req) {
63
57
  if (req.cookies) return req.cookies;
64
58
  if (getHeaders(req)) {
65
59
  var list = {};
@@ -72,8 +66,7 @@ var getCookies = function getCookies(req) {
72
66
  }
73
67
  console.log('no possibility found to get cookies');
74
68
  };
75
- exports.getCookies = getCookies;
76
- var getBody = function getBody(req) {
69
+ var getBody = exports.getBody = function getBody(req) {
77
70
  if (req.ctx && typeof req.ctx.body === 'function') {
78
71
  return req.ctx.body.bind(req.ctx);
79
72
  }
@@ -81,19 +74,18 @@ var getBody = function getBody(req) {
81
74
  if (req.json) return req.json;
82
75
  if (req.body) return req.body;
83
76
  if (req.payload) return req.payload;
77
+ if (req.request && req.request.body) return req.request.body;
84
78
  console.log('no possibility found to get body');
85
79
  return {};
86
80
  };
87
- exports.getBody = getBody;
88
- var getHeader = function getHeader(res, name) {
81
+ var getHeader = exports.getHeader = function getHeader(res, name) {
89
82
  if (res.getHeader) return res.getHeader(name);
90
83
  if (res.headers) return res.headers[name];
91
84
  if (getHeaders(res) && getHeaders(res)[name]) return getHeaders(res)[name];
92
85
  console.log('no possibility found to get header');
93
86
  return undefined;
94
87
  };
95
- exports.getHeader = getHeader;
96
- var setHeader = function setHeader(res, name, value) {
88
+ var setHeader = exports.setHeader = function setHeader(res, name, value) {
97
89
  if (res._headerSent || res.headersSent) return;
98
90
  if (typeof res.setHeader === 'function') return res.setHeader(name, value);
99
91
  if (typeof res.header === 'function') return res.header(name, value);
@@ -103,33 +95,32 @@ var setHeader = function setHeader(res, name, value) {
103
95
  if (res.headers && typeof res.headers.set === 'function') {
104
96
  return res.headers.set(name, value);
105
97
  }
98
+ if (typeof res.set === 'function') {
99
+ return res.set(name, value);
100
+ }
106
101
  console.log('no possibility found to set header');
107
102
  };
108
- exports.setHeader = setHeader;
109
- var setContentType = function setContentType(res, type) {
103
+ var setContentType = exports.setContentType = function setContentType(res, type) {
110
104
  if (typeof res.contentType === 'function') return res.contentType(type);
111
105
  if (typeof res.type === 'function') return res.type(type);
112
106
  setHeader(res, 'Content-Type', type);
113
107
  };
114
- exports.setContentType = setContentType;
115
- var setStatus = function setStatus(res, code) {
108
+ var setStatus = exports.setStatus = function setStatus(res, code) {
116
109
  if (typeof res.status === 'function') return res.status(code);
117
110
  if (res.status) return res.status = code;
118
111
  console.log('no possibility found to set status');
119
112
  };
120
- exports.setStatus = setStatus;
121
- var send = function send(res, body) {
113
+ var send = exports.send = function send(res, body) {
122
114
  if (typeof res.send === 'function') return res.send(body);
115
+ if (res.request && res.response && res.app) res.body = body;
123
116
  return body;
124
117
  };
125
- exports.send = send;
126
- var getSession = function getSession(req) {
118
+ var getSession = exports.getSession = function getSession(req) {
127
119
  if (req.session) return req.session;
128
120
  if (req.raw && req.raw.session) return req.raw.session;
129
121
  console.log('no possibility found to get session');
130
122
  };
131
- exports.getSession = getSession;
132
- var extendOptionsWithDefaults = function extendOptionsWithDefaults() {
123
+ var extendOptionsWithDefaults = exports.extendOptionsWithDefaults = function extendOptionsWithDefaults() {
133
124
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
134
125
  options.getPath = options.getPath || getPath;
135
126
  options.getOriginalUrl = options.getOriginalUrl || getOriginalUrl;
@@ -147,5 +138,4 @@ var extendOptionsWithDefaults = function extendOptionsWithDefaults() {
147
138
  options.setStatus = options.setStatus || setStatus;
148
139
  options.send = options.send || send;
149
140
  return options;
150
- };
151
- exports.extendOptionsWithDefaults = extendOptionsWithDefaults;
141
+ };
package/cjs/index.d.ts CHANGED
@@ -75,6 +75,8 @@ type IgnoreRoutesFunction = (
75
75
 
76
76
  export function handle(i18next: I18next, options?: HandleOptions): Handler;
77
77
 
78
+ export function koaPlugin(i18next: I18next, options?: HandleOptions): (context: unknown, next: Function) => any;
79
+
78
80
  export function plugin(
79
81
  instance: any,
80
82
  options: HandleOptions & { i18next?: I18next },
package/cjs/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
 
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
3
4
  Object.defineProperty(exports, "__esModule", {
4
5
  value: true
5
6
  });
@@ -9,6 +10,7 @@ exports.default = void 0;
9
10
  exports.getResourcesHandler = getResourcesHandler;
10
11
  exports.handle = handle;
11
12
  exports.hapiPlugin = void 0;
13
+ exports.koaPlugin = koaPlugin;
12
14
  exports.missingKeyHandler = missingKeyHandler;
13
15
  exports.plugin = plugin;
14
16
  var utils = _interopRequireWildcard(require("./utils.js"));
@@ -17,14 +19,15 @@ var _httpFunctions = require("./httpFunctions.js");
17
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
20
  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); }
19
21
  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; }
20
- 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); }
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; }
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; }
22
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
23
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
23
24
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
24
25
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
25
26
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
26
- var LanguageDetector = _LanguageDetector.default;
27
- exports.LanguageDetector = LanguageDetector;
27
+ function _regeneratorRuntime() { "use strict"; _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
28
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
29
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
30
+ var LanguageDetector = exports.LanguageDetector = _LanguageDetector.default;
28
31
  var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
29
32
  if (!res) {
30
33
  if (req.request && req.response) {
@@ -36,6 +39,12 @@ var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
36
39
  res = req;
37
40
  if (!next) next = function next() {};
38
41
  }
42
+ } else if (!next && typeof res === 'function' && req.req && req.res) {
43
+ return {
44
+ req: req,
45
+ res: req,
46
+ next: res
47
+ };
39
48
  }
40
49
  return {
41
50
  req: req,
@@ -121,7 +130,33 @@ function plugin(instance, options, next) {
121
130
  });
122
131
  return next();
123
132
  }
124
- var hapiPlugin = {
133
+ function koaPlugin(i18next, options) {
134
+ var middleware = handle(i18next, options);
135
+ return function () {
136
+ var _koaMiddleware = _asyncToGenerator(_regeneratorRuntime().mark(function _callee(ctx, next) {
137
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
138
+ while (1) switch (_context.prev = _context.next) {
139
+ case 0:
140
+ _context.next = 2;
141
+ return new Promise(function (resolve) {
142
+ return middleware(ctx, ctx, resolve);
143
+ });
144
+ case 2:
145
+ _context.next = 4;
146
+ return next();
147
+ case 4:
148
+ case "end":
149
+ return _context.stop();
150
+ }
151
+ }, _callee);
152
+ }));
153
+ function koaMiddleware(_x, _x2) {
154
+ return _koaMiddleware.apply(this, arguments);
155
+ }
156
+ return koaMiddleware;
157
+ }();
158
+ }
159
+ var hapiPlugin = exports.hapiPlugin = {
125
160
  name: 'i18next-http-middleware',
126
161
  version: '1',
127
162
  register: function register(server, options) {
@@ -135,7 +170,6 @@ var hapiPlugin = {
135
170
  });
136
171
  }
137
172
  };
138
- exports.hapiPlugin = hapiPlugin;
139
173
  plugin[Symbol.for('skip-override')] = true;
140
174
  function getResourcesHandler(i18next) {
141
175
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
@@ -237,13 +271,13 @@ function addRoute(i18next, route, lngs, app, verb, fc) {
237
271
  app[verb || 'get'].apply(app, routes.concat(callbacks));
238
272
  }
239
273
  }
240
- var _default = {
274
+ var _default = exports.default = {
241
275
  plugin: plugin,
242
276
  hapiPlugin: hapiPlugin,
277
+ koaPlugin: koaPlugin,
243
278
  handle: handle,
244
279
  getResourcesHandler: getResourcesHandler,
245
280
  missingKeyHandler: missingKeyHandler,
246
281
  addRoute: addRoute,
247
282
  LanguageDetector: LanguageDetector
248
- };
249
- exports.default = _default;
283
+ };
@@ -56,7 +56,7 @@ var serializeCookie = function serializeCookie(name, val, options) {
56
56
  }
57
57
  return str;
58
58
  };
59
- var _default = {
59
+ var _default = exports.default = {
60
60
  name: 'cookie',
61
61
  lookup: function lookup(req, res, options) {
62
62
  var found;
@@ -94,5 +94,4 @@ var _default = {
94
94
  }
95
95
  }
96
96
  };
97
- exports.default = _default;
98
97
  module.exports = exports.default;
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var specialCases = ['hans', 'hant', 'latn', 'cyrl', 'cans', 'mong', 'arab'];
8
- var _default = {
8
+ var _default = exports.default = {
9
9
  name: 'header',
10
10
  lookup: function lookup(req, res, options) {
11
11
  var found;
@@ -52,5 +52,4 @@ var _default = {
52
52
  return found;
53
53
  }
54
54
  };
55
- exports.default = _default;
56
55
  module.exports = exports.default;
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = {
7
+ var _default = exports.default = {
8
8
  name: 'path',
9
9
  lookup: function lookup(req, res, options) {
10
10
  var found;
@@ -27,5 +27,4 @@ var _default = {
27
27
  return found;
28
28
  }
29
29
  };
30
- exports.default = _default;
31
30
  module.exports = exports.default;
@@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = {
7
+ var _default = exports.default = {
8
8
  name: 'querystring',
9
9
  lookup: function lookup(req, res, options) {
10
10
  var found;
@@ -33,5 +33,4 @@ var _default = {
33
33
  return found;
34
34
  }
35
35
  };
36
- exports.default = _default;
37
36
  module.exports = exports.default;
@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- 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); }
8
- var _default = {
7
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
8
+ var _default = exports.default = {
9
9
  name: 'session',
10
10
  lookup: function lookup(req, res, options) {
11
11
  var found;
@@ -21,5 +21,4 @@ var _default = {
21
21
  }
22
22
  }
23
23
  };
24
- exports.default = _default;
25
24
  module.exports = exports.default;
@@ -1,4 +1,4 @@
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); }
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
2
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
3
3
  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, _toPropertyKey(descriptor.key), descriptor); } }
4
4
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
@@ -30,6 +30,7 @@ export var getQuery = function getQuery(req) {
30
30
  if (req.searchParams) return req.searchParams;
31
31
  if (req.raw && req.raw.query) return req.raw.query;
32
32
  if (req.ctx && req.ctx.queryParams) return req.ctx.queryParams;
33
+ if (req.url && req.url.searchParams) return req.url.searchParams;
33
34
  var url = req.url || req.raw && req.raw.url;
34
35
  if (url && url.indexOf('?') < 0) return {};
35
36
  console.log('no possibility found to get query');
@@ -67,6 +68,7 @@ export var getBody = function getBody(req) {
67
68
  if (req.json) return req.json;
68
69
  if (req.body) return req.body;
69
70
  if (req.payload) return req.payload;
71
+ if (req.request && req.request.body) return req.request.body;
70
72
  console.log('no possibility found to get body');
71
73
  return {};
72
74
  };
@@ -87,6 +89,9 @@ export var setHeader = function setHeader(res, name, value) {
87
89
  if (res.headers && typeof res.headers.set === 'function') {
88
90
  return res.headers.set(name, value);
89
91
  }
92
+ if (typeof res.set === 'function') {
93
+ return res.set(name, value);
94
+ }
90
95
  console.log('no possibility found to set header');
91
96
  };
92
97
  export var setContentType = function setContentType(res, type) {
@@ -101,6 +106,7 @@ export var setStatus = function setStatus(res, code) {
101
106
  };
102
107
  export var send = function send(res, body) {
103
108
  if (typeof res.send === 'function') return res.send(body);
109
+ if (res.request && res.response && res.app) res.body = body;
104
110
  return body;
105
111
  };
106
112
  export var getSession = function getSession(req) {
package/esm/index.d.ts CHANGED
@@ -75,6 +75,8 @@ type IgnoreRoutesFunction = (
75
75
 
76
76
  export function handle(i18next: I18next, options?: HandleOptions): Handler;
77
77
 
78
+ export function koaPlugin(i18next: I18next, options?: HandleOptions): (context: unknown, next: Function) => any;
79
+
78
80
  export function plugin(
79
81
  instance: any,
80
82
  options: HandleOptions & { i18next?: I18next },
package/esm/index.js CHANGED
@@ -1,9 +1,12 @@
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
- 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; }
3
- 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; }
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
4
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
5
  function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
6
6
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
7
+ function _regeneratorRuntime() { "use strict"; _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
8
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
9
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
7
10
  import * as utils from './utils.js';
8
11
  import LD from './LanguageDetector.js';
9
12
  import { extendOptionsWithDefaults } from './httpFunctions.js';
@@ -19,6 +22,12 @@ var checkForCombinedReqRes = function checkForCombinedReqRes(req, res, next) {
19
22
  res = req;
20
23
  if (!next) next = function next() {};
21
24
  }
25
+ } else if (!next && typeof res === 'function' && req.req && req.res) {
26
+ return {
27
+ req: req,
28
+ res: req,
29
+ next: res
30
+ };
22
31
  }
23
32
  return {
24
33
  req: req,
@@ -104,6 +113,32 @@ export function plugin(instance, options, next) {
104
113
  });
105
114
  return next();
106
115
  }
116
+ export function koaPlugin(i18next, options) {
117
+ var middleware = handle(i18next, options);
118
+ return function () {
119
+ var _koaMiddleware = _asyncToGenerator(_regeneratorRuntime().mark(function _callee(ctx, next) {
120
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
121
+ while (1) switch (_context.prev = _context.next) {
122
+ case 0:
123
+ _context.next = 2;
124
+ return new Promise(function (resolve) {
125
+ return middleware(ctx, ctx, resolve);
126
+ });
127
+ case 2:
128
+ _context.next = 4;
129
+ return next();
130
+ case 4:
131
+ case "end":
132
+ return _context.stop();
133
+ }
134
+ }, _callee);
135
+ }));
136
+ function koaMiddleware(_x, _x2) {
137
+ return _koaMiddleware.apply(this, arguments);
138
+ }
139
+ return koaMiddleware;
140
+ }();
141
+ }
107
142
  export var hapiPlugin = {
108
143
  name: 'i18next-http-middleware',
109
144
  version: '1',
@@ -222,6 +257,7 @@ export function addRoute(i18next, route, lngs, app, verb, fc) {
222
257
  export default {
223
258
  plugin: plugin,
224
259
  hapiPlugin: hapiPlugin,
260
+ koaPlugin: koaPlugin,
225
261
  handle: handle,
226
262
  getResourcesHandler: getResourcesHandler,
227
263
  missingKeyHandler: missingKeyHandler,
@@ -1,4 +1,4 @@
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); }
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
2
  export default {
3
3
  name: 'session',
4
4
  lookup: function lookup(req, res, options) {
package/index.d.ts CHANGED
@@ -75,6 +75,8 @@ type IgnoreRoutesFunction = (
75
75
 
76
76
  export function handle(i18next: I18next, options?: HandleOptions): Handler;
77
77
 
78
+ export function koaPlugin(i18next: I18next, options?: HandleOptions): (context: unknown, next: Function) => any;
79
+
78
80
  export function plugin(
79
81
  instance: any,
80
82
  options: HandleOptions & { i18next?: I18next },
@@ -35,6 +35,7 @@ export const getQuery = (req) => {
35
35
  if (req.searchParams) return req.searchParams
36
36
  if (req.raw && req.raw.query) return req.raw.query
37
37
  if (req.ctx && req.ctx.queryParams) return req.ctx.queryParams
38
+ if (req.url && req.url.searchParams) return req.url.searchParams
38
39
  const url = req.url || (req.raw && req.raw.url)
39
40
  if (url && url.indexOf('?') < 0) return {}
40
41
  console.log('no possibility found to get query')
@@ -73,6 +74,7 @@ export const getBody = (req) => {
73
74
  if (req.json) return req.json
74
75
  if (req.body) return req.body
75
76
  if (req.payload) return req.payload
77
+ if (req.request && req.request.body) return req.request.body
76
78
  console.log('no possibility found to get body')
77
79
  return {}
78
80
  }
@@ -93,6 +95,9 @@ export const setHeader = (res, name, value) => {
93
95
  if (res.headers && typeof res.headers.set === 'function') {
94
96
  return res.headers.set(name, value)
95
97
  }
98
+ if (typeof res.set === 'function') {
99
+ return res.set(name, value)
100
+ }
96
101
  console.log('no possibility found to set header')
97
102
  }
98
103
  export const setContentType = (res, type) => {
@@ -108,6 +113,7 @@ export const setStatus = (res, code) => {
108
113
  }
109
114
  export const send = (res, body) => {
110
115
  if (typeof res.send === 'function') return res.send(body)
116
+ if (res.request && res.response && res.app) res.body = body
111
117
  return body
112
118
  }
113
119
  export const getSession = (req) => {
package/lib/index.js CHANGED
@@ -15,6 +15,12 @@ const checkForCombinedReqRes = (req, res, next) => {
15
15
  res = req
16
16
  if (!next) next = () => {}
17
17
  }
18
+ } else if (!next && typeof res === 'function' && req.req && req.res) {
19
+ return {
20
+ req,
21
+ res: req,
22
+ next: res
23
+ }
18
24
  }
19
25
  return { req, res, next }
20
26
  }
@@ -119,6 +125,14 @@ export function plugin (instance, options, next) {
119
125
  return next()
120
126
  }
121
127
 
128
+ export function koaPlugin (i18next, options) {
129
+ const middleware = handle(i18next, options)
130
+ return async function koaMiddleware (ctx, next) {
131
+ await new Promise((resolve) => middleware(ctx, ctx, resolve))
132
+ await next()
133
+ }
134
+ }
135
+
122
136
  export const hapiPlugin = {
123
137
  name: 'i18next-http-middleware',
124
138
  version: '1',
@@ -291,6 +305,7 @@ export function addRoute (i18next, route, lngs, app, verb, fc) {
291
305
  export default {
292
306
  plugin,
293
307
  hapiPlugin,
308
+ koaPlugin,
294
309
  handle,
295
310
  getResourcesHandler,
296
311
  missingKeyHandler,
package/licence CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2022 i18next
1
+ Copyright (c) 2023 i18next
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-http-middleware",
3
- "version": "3.3.1",
3
+ "version": "3.4.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./cjs/index.js",
@@ -27,28 +27,29 @@
27
27
  }
28
28
  },
29
29
  "module": "./esm/index.js",
30
- "dependencies": {},
31
30
  "devDependencies": {
32
- "@babel/cli": "7.21.5",
33
- "@babel/core": "7.21.8",
34
- "@babel/preset-env": "7.21.5",
35
- "@hapi/hapi": "^20.2.2",
36
- "@types/express-serve-static-core": "^4.17.35",
31
+ "@babel/cli": "7.23.0",
32
+ "@babel/core": "7.23.0",
33
+ "@babel/preset-env": "7.22.20",
34
+ "@hapi/hapi": "^21.3.2",
35
+ "@types/express-serve-static-core": "^4.17.37",
36
+ "@koa/router": "12.0.0",
37
+ "koa": "2.14.2",
37
38
  "babel-plugin-add-module-exports": "1.0.4",
38
- "eslint": "8.41.0",
39
- "eslint-config-standard": "17.0.0",
40
- "eslint-plugin-import": "2.27.5",
41
- "eslint-plugin-n": "15.7.0",
39
+ "eslint": "8.50.0",
40
+ "eslint-config-standard": "17.1.0",
41
+ "eslint-plugin-import": "2.28.1",
42
+ "eslint-plugin-n": "16.1.0",
42
43
  "eslint-plugin-promise": "6.1.1",
43
44
  "eslint-plugin-require-path-exists": "1.1.9",
44
45
  "eslint-plugin-standard": "5.0.0",
45
46
  "expect.js": "0.3.1",
46
47
  "express": "4.18.2",
47
- "fastify": "4.17.0",
48
- "i18next": "22.5.0",
48
+ "fastify": "4.23.2",
49
+ "i18next": "23.5.1",
49
50
  "mocha": "10.2.0",
50
51
  "supertest": "6.3.3",
51
- "tsd": "0.28.1",
52
+ "tsd": "0.29.0",
52
53
  "uglify-js": "3.17.4"
53
54
  },
54
55
  "description": "i18next-http-middleware is a middleware to be used with Node.js web frameworks like express or Fastify and also for Deno.",