@abinnovision/nestjs-i18n 0.0.0 → 0.1.0-beta.4

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
@@ -82,6 +82,17 @@ The locale is taken from `opts.locale`, then from `i18n.run(locale, fn)`, then r
82
82
 
83
83
  Set `middleware: false` to disable the request context middleware. Then only an explicit `locale` and `run()` apply.
84
84
 
85
+ ### Handlers outside Nest
86
+
87
+ Handlers registered on the raw HTTP adapter (e.g. better-auth) run outside the middleware, whether or not `middleware: false` is set. Wrap them with `runForRequest`:
88
+
89
+ ```typescript
90
+ const i18n = app.get(I18nService);
91
+ app.use((req, _res, next) => i18n.runForRequest(req, next));
92
+ ```
93
+
94
+ Register it before those handlers. `app.get(I18nService)` works because the module is global. The locale is still resolved from the request on every call. Callbacks from streams or `EventEmitter`s can lose the async context, same as with the middleware.
95
+
85
96
  ## Localized exceptions
86
97
 
87
98
  With `@abinnovision/nestjs-exceptions`, the `./exceptions` subpath localizes the `detail` of error responses with the `exceptions.<CODE>` messages. The exception `meta` is used as message parameters. Entity-focused exceptions also get an `entity` parameter from the `entities.<name>` message, or the raw entity name.
@@ -1,4 +1,4 @@
1
- const require_i18n_context = require("./i18n.context.cjs");
1
+ const require_i18n_service = require("./i18n.service.cjs");
2
2
  let _nestjs_common = require("@nestjs/common");
3
3
  //#region src/i18n.middleware.ts
4
4
  function _ts_decorate(decorators, target, key, desc) {
@@ -16,19 +16,19 @@ function _ts_param(paramIndex, decorator) {
16
16
  };
17
17
  }
18
18
  var I18nMiddleware = class {
19
- context;
20
- constructor(context) {
21
- this.context = context;
19
+ i18n;
20
+ constructor(i18n) {
21
+ this.i18n = i18n;
22
22
  }
23
23
  use(request, _response, next) {
24
- this.context.run({ request }, next);
24
+ this.i18n.runForRequest(request, next);
25
25
  }
26
26
  };
27
27
  I18nMiddleware = _ts_decorate([
28
28
  (0, _nestjs_common.Injectable)(),
29
- _ts_param(0, (0, _nestjs_common.Inject)(require_i18n_context.I18nContext)),
29
+ _ts_param(0, (0, _nestjs_common.Inject)(require_i18n_service.I18nService)),
30
30
  _ts_metadata("design:type", Function),
31
- _ts_metadata("design:paramtypes", [typeof require_i18n_context.I18nContext === "undefined" ? Object : require_i18n_context.I18nContext])
31
+ _ts_metadata("design:paramtypes", [typeof require_i18n_service.I18nService === "undefined" ? Object : require_i18n_service.I18nService])
32
32
  ], I18nMiddleware);
33
33
  //#endregion
34
34
  Object.defineProperty(exports, "I18nMiddleware", {
@@ -1,4 +1,4 @@
1
- import { I18nContext } from "./i18n.context.mjs";
1
+ import { I18nService } from "./i18n.service.mjs";
2
2
  import { Inject, Injectable } from "@nestjs/common";
3
3
  //#region src/i18n.middleware.ts
4
4
  function _ts_decorate(decorators, target, key, desc) {
@@ -16,19 +16,19 @@ function _ts_param(paramIndex, decorator) {
16
16
  };
17
17
  }
18
18
  var I18nMiddleware = class {
19
- context;
20
- constructor(context) {
21
- this.context = context;
19
+ i18n;
20
+ constructor(i18n) {
21
+ this.i18n = i18n;
22
22
  }
23
23
  use(request, _response, next) {
24
- this.context.run({ request }, next);
24
+ this.i18n.runForRequest(request, next);
25
25
  }
26
26
  };
27
27
  I18nMiddleware = _ts_decorate([
28
28
  Injectable(),
29
- _ts_param(0, Inject(I18nContext)),
29
+ _ts_param(0, Inject(I18nService)),
30
30
  _ts_metadata("design:type", Function),
31
- _ts_metadata("design:paramtypes", [typeof I18nContext === "undefined" ? Object : I18nContext])
31
+ _ts_metadata("design:paramtypes", [typeof I18nService === "undefined" ? Object : I18nService])
32
32
  ], I18nMiddleware);
33
33
  //#endregion
34
34
  export { I18nMiddleware };
@@ -1,7 +1,7 @@
1
1
  const require_i18n_module_config = require("./i18n.module-config.cjs");
2
2
  const require_i18n_context = require("./i18n.context.cjs");
3
- const require_i18n_middleware = require("./i18n.middleware.cjs");
4
3
  const require_i18n_service = require("./i18n.service.cjs");
4
+ const require_i18n_middleware = require("./i18n.middleware.cjs");
5
5
  let _nestjs_common = require("@nestjs/common");
6
6
  //#region src/i18n.module.ts
7
7
  function _ts_decorate(decorators, target, key, desc) {
@@ -1,7 +1,7 @@
1
1
  import { i18nModuleConfigToken } from "./i18n.module-config.mjs";
2
2
  import { I18nContext } from "./i18n.context.mjs";
3
- import { I18nMiddleware } from "./i18n.middleware.mjs";
4
3
  import { I18nService } from "./i18n.service.mjs";
4
+ import { I18nMiddleware } from "./i18n.middleware.mjs";
5
5
  import { ConfigurableModuleBuilder, Global, Inject, Module } from "@nestjs/common";
6
6
  //#region src/i18n.module.ts
7
7
  function _ts_decorate(decorators, target, key, desc) {
@@ -57,6 +57,13 @@ var I18nService = class I18nService {
57
57
  }, fn);
58
58
  }
59
59
  /**
60
+ * Runs `fn` in a new context holding the request. The locale is resolved
61
+ * from the request on every call. A locale set by an outer `run()` does
62
+ * not apply inside.
63
+ */ runForRequest(request, fn) {
64
+ return this.context.run({ request }, fn);
65
+ }
66
+ /**
60
67
  * Resolves the locale of a request with the configured resolvers.
61
68
  */ resolveLocale(request) {
62
69
  for (const resolver of this.resolvers) {
@@ -16,9 +16,12 @@ interface TranslateOptions {
16
16
  /**
17
17
  * Translates messages of the configured catalogs.
18
18
  *
19
- * The locale is taken from `opts.locale`, then from `run()`, then resolved
20
- * from the current request, then the default locale. It is resolved on
21
- * every call, so values set later in the request (e.g. `req.user`) apply.
19
+ * The locale is taken from `opts.locale`, then from the innermost `run()`,
20
+ * then resolved from the request of the innermost `runForRequest()` or
21
+ * middleware context, then the default locale. Both `run()` and
22
+ * `runForRequest()` start a new context: `run()` keeps the outer request,
23
+ * `runForRequest()` drops the outer locale. The request locale is resolved
24
+ * on every call, so values set later in the request (e.g. `req.user`) apply.
22
25
  */
23
26
  declare class I18nService {
24
27
  private readonly config;
@@ -41,6 +44,12 @@ declare class I18nService {
41
44
  * Runs `fn` with the given locale as the context locale.
42
45
  */
43
46
  run<T>(locale: Locale, fn: () => T): T;
47
+ /**
48
+ * Runs `fn` in a new context holding the request. The locale is resolved
49
+ * from the request on every call. A locale set by an outer `run()` does
50
+ * not apply inside.
51
+ */
52
+ runForRequest<T>(request: I18nRequest, fn: () => T): T;
44
53
  /**
45
54
  * Resolves the locale of a request with the configured resolvers.
46
55
  */
@@ -16,9 +16,12 @@ interface TranslateOptions {
16
16
  /**
17
17
  * Translates messages of the configured catalogs.
18
18
  *
19
- * The locale is taken from `opts.locale`, then from `run()`, then resolved
20
- * from the current request, then the default locale. It is resolved on
21
- * every call, so values set later in the request (e.g. `req.user`) apply.
19
+ * The locale is taken from `opts.locale`, then from the innermost `run()`,
20
+ * then resolved from the request of the innermost `runForRequest()` or
21
+ * middleware context, then the default locale. Both `run()` and
22
+ * `runForRequest()` start a new context: `run()` keeps the outer request,
23
+ * `runForRequest()` drops the outer locale. The request locale is resolved
24
+ * on every call, so values set later in the request (e.g. `req.user`) apply.
22
25
  */
23
26
  declare class I18nService {
24
27
  private readonly config;
@@ -41,6 +44,12 @@ declare class I18nService {
41
44
  * Runs `fn` with the given locale as the context locale.
42
45
  */
43
46
  run<T>(locale: Locale, fn: () => T): T;
47
+ /**
48
+ * Runs `fn` in a new context holding the request. The locale is resolved
49
+ * from the request on every call. A locale set by an outer `run()` does
50
+ * not apply inside.
51
+ */
52
+ runForRequest<T>(request: I18nRequest, fn: () => T): T;
44
53
  /**
45
54
  * Resolves the locale of a request with the configured resolvers.
46
55
  */
@@ -57,6 +57,13 @@ var I18nService = class I18nService {
57
57
  }, fn);
58
58
  }
59
59
  /**
60
+ * Runs `fn` in a new context holding the request. The locale is resolved
61
+ * from the request on every call. A locale set by an outer `run()` does
62
+ * not apply inside.
63
+ */ runForRequest(request, fn) {
64
+ return this.context.run({ request }, fn);
65
+ }
66
+ /**
60
67
  * Resolves the locale of a request with the configured resolvers.
61
68
  */ resolveLocale(request) {
62
69
  for (const resolver of this.resolvers) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@abinnovision/nestjs-i18n",
4
- "version": "0.0.0",
4
+ "version": "0.1.0-beta.4",
5
5
  "description": "Type-safe backend i18n for NestJS with ICU MessageFormat catalogs, request locale resolution, and localized exceptions.",
6
6
  "keywords": [
7
7
  "nestjs",
@@ -113,4 +113,4 @@
113
113
  "npm": true,
114
114
  "npmAccess": "public"
115
115
  }
116
- }
116
+ }