@miguelmorales13/nestkit 0.11.0 → 0.18.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.
Files changed (126) hide show
  1. package/README.md +619 -35
  2. package/dist/audit/audit.module.d.ts +23 -0
  3. package/dist/audit/audit.options.d.ts +16 -0
  4. package/dist/audit/audit.port.d.ts +37 -0
  5. package/dist/audit/audit.service.d.ts +29 -0
  6. package/dist/audit/index.cjs +72 -0
  7. package/dist/audit/index.d.ts +7 -0
  8. package/dist/audit/index.js +72 -0
  9. package/dist/audit/sinks.d.ts +18 -0
  10. package/dist/auth/index.cjs +12 -10
  11. package/dist/auth/index.js +5 -3
  12. package/dist/auth/oauth/http.d.ts +10 -3
  13. package/dist/auth/oauth/index.cjs +189 -29
  14. package/dist/auth/oauth/index.d.ts +6 -4
  15. package/dist/auth/oauth/index.js +169 -9
  16. package/dist/auth/oauth/magic-link.dto.d.ts +6 -0
  17. package/dist/auth/oauth/memory-magic-link.store.d.ts +19 -0
  18. package/dist/auth/oauth/oauth-auth.service.d.ts +46 -5
  19. package/dist/auth/oauth/oauth.controller.d.ts +5 -1
  20. package/dist/auth/oauth/oauth.options.d.ts +34 -1
  21. package/dist/auth/oauth/oauth.ports.d.ts +38 -0
  22. package/dist/bootstrap/index.cjs +9 -6
  23. package/dist/bootstrap/index.js +8 -5
  24. package/dist/{chunk-TJHRABML.cjs → chunk-3AUOOEQO.cjs} +12 -7
  25. package/dist/{chunk-NVCI3CQI.cjs → chunk-4HHZFOVC.cjs} +10 -5
  26. package/dist/{chunk-EYURGACO.js → chunk-4PFAKLT3.js} +1 -1
  27. package/dist/chunk-52IOOHNX.cjs +205 -0
  28. package/dist/chunk-6RAPVLJM.js +212 -0
  29. package/dist/{chunk-7I2Y7V52.js → chunk-A2DGK4ZL.js} +26 -3
  30. package/dist/chunk-APUFKA4K.cjs +212 -0
  31. package/dist/chunk-AXMW5JF7.cjs +83 -0
  32. package/dist/{chunk-KDAA6GFF.js → chunk-DYHSOULW.js} +12 -7
  33. package/dist/chunk-ETRUVZTJ.js +43 -0
  34. package/dist/{chunk-54ZXIB5T.cjs → chunk-FNUZGYUF.cjs} +4 -4
  35. package/dist/{chunk-FDNGAYTZ.cjs → chunk-HDFPSA2V.cjs} +6 -6
  36. package/dist/{chunk-V2M75FN3.cjs → chunk-HVHYZXBB.cjs} +2 -2
  37. package/dist/chunk-IFNYRZP2.js +205 -0
  38. package/dist/{chunk-A3B2EY4V.js → chunk-IKXSVEJ7.js} +7 -2
  39. package/dist/{chunk-YFYHLYHN.js → chunk-M3GQDRWR.js} +7 -1
  40. package/dist/{chunk-J7TURDAL.cjs → chunk-MSTXS3MU.cjs} +28 -5
  41. package/dist/chunk-PNK5WMPP.cjs +30 -0
  42. package/dist/chunk-Q5Y33L7B.cjs +19 -0
  43. package/dist/{chunk-ZA56XBCK.cjs → chunk-SDWHKPXI.cjs} +23 -0
  44. package/dist/chunk-TCAMLNUZ.js +27 -0
  45. package/dist/{chunk-AOCF5QCZ.js → chunk-UNEVXCFW.js} +2 -2
  46. package/dist/chunk-VVMRZEIW.js +30 -0
  47. package/dist/chunk-W2T2VFLC.cjs +57 -0
  48. package/dist/chunk-X4PQJZ4G.js +83 -0
  49. package/dist/chunk-Y72D5L6I.cjs +27 -0
  50. package/dist/{chunk-ORWJ7LES.js → chunk-ZFIDAX7K.js} +1 -1
  51. package/dist/chunk-ZJMLEP5H.js +57 -0
  52. package/dist/crud/crud.controller.d.ts +3 -1
  53. package/dist/crud/crud.service.d.ts +63 -4
  54. package/dist/crud/index.cjs +7 -2
  55. package/dist/crud/index.d.ts +2 -1
  56. package/dist/crud/index.js +6 -1
  57. package/dist/crud/list-query.dto.d.ts +23 -0
  58. package/dist/crud/repository.port.d.ts +33 -1
  59. package/dist/errors/app.exception.d.ts +9 -1
  60. package/dist/errors/common.exceptions.d.ts +6 -5
  61. package/dist/errors/global-exception.filter.d.ts +9 -0
  62. package/dist/errors/index.cjs +8 -5
  63. package/dist/errors/index.js +9 -6
  64. package/dist/i18n/i18n.interceptor.d.ts +16 -0
  65. package/dist/i18n/i18n.module.d.ts +49 -3
  66. package/dist/i18n/index.cjs +26 -2
  67. package/dist/i18n/index.d.ts +8 -0
  68. package/dist/i18n/index.js +25 -1
  69. package/dist/i18n/locale.d.ts +53 -0
  70. package/dist/i18n/message.d.ts +36 -0
  71. package/dist/i18n/nestjs-i18n.translator.d.ts +9 -0
  72. package/dist/i18n/runtime.d.ts +55 -0
  73. package/dist/i18n/translator.port.d.ts +29 -0
  74. package/dist/idempotency/idempotency.interceptor.d.ts +35 -0
  75. package/dist/idempotency/idempotency.module.d.ts +27 -0
  76. package/dist/idempotency/idempotency.options.d.ts +29 -0
  77. package/dist/idempotency/idempotency.port.d.ts +43 -0
  78. package/dist/idempotency/index.cjs +207 -0
  79. package/dist/idempotency/index.d.ts +7 -0
  80. package/dist/idempotency/index.js +207 -0
  81. package/dist/idempotency/memory.store.d.ts +24 -0
  82. package/dist/index.cjs +48 -15
  83. package/dist/index.js +51 -18
  84. package/dist/invoicing/adapters/facturama.invoicing.d.ts +50 -0
  85. package/dist/invoicing/adapters/facturapi.invoicing.d.ts +35 -0
  86. package/dist/invoicing/amounts.d.ts +52 -0
  87. package/dist/invoicing/index.cjs +578 -0
  88. package/dist/invoicing/index.d.ts +11 -0
  89. package/dist/invoicing/index.js +578 -0
  90. package/dist/invoicing/invoicing.module.d.ts +42 -0
  91. package/dist/invoicing/invoicing.options.d.ts +51 -0
  92. package/dist/invoicing/invoicing.port.d.ts +156 -0
  93. package/dist/invoicing/lines.d.ts +21 -0
  94. package/dist/invoicing/provider-http.d.ts +21 -0
  95. package/dist/payments/adapters/clip.terminal.d.ts +101 -0
  96. package/dist/payments/adapters/mercadopago.terminal.d.ts +74 -0
  97. package/dist/payments/index.cjs +796 -0
  98. package/dist/payments/index.d.ts +12 -0
  99. package/dist/payments/index.js +796 -0
  100. package/dist/payments/money.d.ts +20 -0
  101. package/dist/payments/payments-webhook.controller.d.ts +30 -0
  102. package/dist/payments/payments.module.d.ts +45 -0
  103. package/dist/payments/payments.options.d.ts +84 -0
  104. package/dist/payments/payments.port.d.ts +174 -0
  105. package/dist/payments/provider-http.d.ts +26 -0
  106. package/dist/response/api-response.d.ts +7 -0
  107. package/dist/response/index.cjs +12 -4
  108. package/dist/response/index.d.ts +2 -0
  109. package/dist/response/index.js +12 -4
  110. package/dist/response/paginated.d.ts +25 -0
  111. package/dist/response/response.interceptor.d.ts +4 -0
  112. package/dist/stripe/index.cjs +4 -3
  113. package/dist/stripe/index.js +3 -2
  114. package/dist/tracking/index.cjs +3 -3
  115. package/dist/tracking/index.d.ts +1 -1
  116. package/dist/tracking/index.js +2 -2
  117. package/dist/tracking/request-context.d.ts +32 -0
  118. package/package.json +25 -3
  119. package/dist/chunk-3CLYZC3T.cjs +0 -104
  120. package/dist/chunk-EBO6UKHL.js +0 -20
  121. package/dist/chunk-IYUUYCP5.js +0 -36
  122. package/dist/chunk-JOVBJDJ2.js +0 -104
  123. package/dist/chunk-M3EL5O6T.cjs +0 -36
  124. package/dist/chunk-R7BVS6CI.cjs +0 -13
  125. /package/dist/{chunk-MR2IFCZE.cjs → chunk-H2EHN366.cjs} +0 -0
  126. /package/dist/{chunk-NAK4WDKS.js → chunk-ODPSIFOB.js} +0 -0
@@ -1,4 +1,7 @@
1
- import { type DynamicModule } from '@nestjs/common';
1
+ import { type DynamicModule, type OnModuleDestroy, type OnModuleInit, type Type } from '@nestjs/common';
2
+ import { I18nService, type I18nLoader } from 'nestjs-i18n';
3
+ import { type LocaleOptions } from './locale.js';
4
+ import type { TranslatorPort } from './translator.port.js';
2
5
  export interface NestKitI18nOptions {
3
6
  /**
4
7
  * Path to the folder containing per-language JSON namespaces, relative to
@@ -8,11 +11,54 @@ export interface NestKitI18nOptions {
8
11
  path?: string;
9
12
  /** Defaults to 'es'. */
10
13
  fallbackLanguage?: string;
14
+ /** Where the request's language comes from. See {@link LocaleOptions}. */
15
+ locale?: LocaleOptions;
16
+ /**
17
+ * Namespace for the automatic lookup of an `AppException` by its `code`:
18
+ * with the default, `errors.NOT_FOUND` in `{path}/{lang}/errors.json`
19
+ * translates every `NotFoundAppException` that carries no explicit key.
20
+ * `false` turns the lookup off.
21
+ */
22
+ errorCodes?: string | false;
23
+ /**
24
+ * Pass `false` to skip registering `I18nInterceptor` globally. Responses
25
+ * are then not translated unless the consumer mounts the interceptor where
26
+ * it wants it; errors still are, since the filter reads the runtime directly.
27
+ */
28
+ interceptor?: boolean;
29
+ /**
30
+ * Reload the JSON files when they change on disk. Defaults to `true`
31
+ * outside `NODE_ENV=production`.
32
+ */
33
+ watch?: boolean;
34
+ /** A different `nestjs-i18n` loader (YAML, say). Ignored when `translator` is given. */
35
+ loader?: Type<I18nLoader>;
36
+ /**
37
+ * Bring your own backend. `nestjs-i18n` is then not wired at all: no files,
38
+ * no `I18nService` to inject — only this port, the locale resolution and
39
+ * the interceptor.
40
+ */
41
+ translator?: TranslatorPort;
11
42
  }
12
43
  /**
13
44
  * Thin wrapper over nestjs-i18n so consumers don't have to know its
14
- * loader/resolver configuration shape — just point it at a folder.
45
+ * loader/resolver configuration shape — just point it at a folder and say
46
+ * where the language comes from.
47
+ *
48
+ * Once the app starts it publishes the {@link TranslatorPort}, the locale
49
+ * resolver and the error-code namespace to the i18n runtime — the registry
50
+ * `GlobalExceptionFilter`, `translate()` and `I18nInterceptor` read, none of
51
+ * which can be injected into — and mounts the interceptor globally.
52
+ * `nestjs-i18n` itself gets a single resolver that delegates to that same
53
+ * runtime, so a consumer injecting `I18nService` directly gets the same
54
+ * language as the rest of the package.
15
55
  */
16
- export declare class I18nModule {
56
+ export declare class I18nModule implements OnModuleInit, OnModuleDestroy {
57
+ private readonly options;
58
+ private readonly i18n?;
17
59
  static forRoot(options?: NestKitI18nOptions): DynamicModule;
60
+ constructor(options: NestKitI18nOptions, i18n?: I18nService | undefined);
61
+ onModuleInit(): void;
62
+ onModuleDestroy(): void;
63
+ private requireService;
18
64
  }
@@ -1,9 +1,33 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunkM3EL5O6Tcjs = require('../chunk-M3EL5O6T.cjs');
4
+
5
+
6
+
7
+ var _chunkAPUFKA4Kcjs = require('../chunk-APUFKA4K.cjs');
8
+
9
+
10
+
11
+
12
+ var _chunkAXMW5JF7cjs = require('../chunk-AXMW5JF7.cjs');
13
+ require('../chunk-Y72D5L6I.cjs');
14
+ require('../chunk-SDWHKPXI.cjs');
15
+
16
+
17
+
18
+
19
+ var _chunkPNK5WMPPcjs = require('../chunk-PNK5WMPP.cjs');
5
20
  require('../chunk-2REOCMUD.cjs');
6
21
 
7
22
 
8
23
 
9
- exports.I18nModule = _chunkM3EL5O6Tcjs.I18nModule; exports.translateOr = _chunkM3EL5O6Tcjs.translateOr;
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
33
+ exports.I18nInterceptor = _chunkAPUFKA4Kcjs.I18nInterceptor; exports.I18nMessage = _chunkPNK5WMPPcjs.I18nMessage; exports.I18nModule = _chunkAPUFKA4Kcjs.I18nModule; exports.NestI18nTranslator = _chunkAPUFKA4Kcjs.NestI18nTranslator; exports.currentLanguage = _chunkAXMW5JF7cjs.currentLanguage; exports.isI18nMessage = _chunkPNK5WMPPcjs.isI18nMessage; exports.matchLanguage = _chunkAPUFKA4Kcjs.matchLanguage; exports.t = _chunkPNK5WMPPcjs.t; exports.translate = _chunkAXMW5JF7cjs.translate; exports.translateDeep = _chunkAXMW5JF7cjs.translateDeep; exports.translateOr = _chunkAPUFKA4Kcjs.translateOr;
@@ -1,3 +1,11 @@
1
1
  export { I18nModule } from './i18n.module.js';
2
2
  export type { NestKitI18nOptions } from './i18n.module.js';
3
+ export { I18nInterceptor } from './i18n.interceptor.js';
4
+ export { I18nMessage, t, isI18nMessage } from './message.js';
5
+ export type { TranslatorPort, TranslateOptions } from './translator.port.js';
6
+ export { NestI18nTranslator } from './nestjs-i18n.translator.js';
7
+ export { translate, translateDeep, currentLanguage } from './runtime.js';
8
+ export type { TranslateCallOptions } from './runtime.js';
9
+ export { matchLanguage } from './locale.js';
10
+ export type { LocaleOptions, LocaleSource, LocaleRequest, LocaleResolver } from './locale.js';
3
11
  export { translateOr } from './translate.helper.js';
@@ -1,9 +1,33 @@
1
1
  import {
2
+ I18nInterceptor,
2
3
  I18nModule,
4
+ NestI18nTranslator,
5
+ matchLanguage,
3
6
  translateOr
4
- } from "../chunk-IYUUYCP5.js";
7
+ } from "../chunk-6RAPVLJM.js";
8
+ import {
9
+ currentLanguage,
10
+ translate,
11
+ translateDeep
12
+ } from "../chunk-X4PQJZ4G.js";
13
+ import "../chunk-TCAMLNUZ.js";
14
+ import "../chunk-ETRUVZTJ.js";
15
+ import {
16
+ I18nMessage,
17
+ isI18nMessage,
18
+ t
19
+ } from "../chunk-VVMRZEIW.js";
5
20
  import "../chunk-4MGIQFAJ.js";
6
21
  export {
22
+ I18nInterceptor,
23
+ I18nMessage,
7
24
  I18nModule,
25
+ NestI18nTranslator,
26
+ currentLanguage,
27
+ isI18nMessage,
28
+ matchLanguage,
29
+ t,
30
+ translate,
31
+ translateDeep,
8
32
  translateOr
9
33
  };
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Structural request shape, so locale resolution works on Express and
3
+ * Fastify alike (Fastify exposes the raw headers on `raw`; both expose
4
+ * `headers` and `query` directly).
5
+ */
6
+ export interface LocaleRequest {
7
+ headers?: Record<string, string | string[] | undefined>;
8
+ query?: Record<string, unknown>;
9
+ cookies?: Record<string, string | undefined>;
10
+ }
11
+ /**
12
+ * Where the language comes from. Listed in order of precedence: the first
13
+ * source that yields a supported language wins.
14
+ *
15
+ * - `query` — `?lang=en`; the name comes from `LocaleOptions.query`.
16
+ * - `header` — a custom header such as `x-lang`; `LocaleOptions.header`.
17
+ * - `cookie` — `LocaleOptions.cookie`; read from the `Cookie` header
18
+ * directly, so it works without `cookie-parser`.
19
+ * - `accept-language` — the browser's header, with `q` weights and region
20
+ * fallback (`es-MX` matches `es`).
21
+ * - a function — anything else: the tenant's default language, a
22
+ * user preference read by a guard, a path prefix.
23
+ */
24
+ export type LocaleSource = 'query' | 'header' | 'cookie' | 'accept-language' | ((req: LocaleRequest) => string | undefined);
25
+ export interface LocaleOptions {
26
+ /** Defaults to `['query', 'header', 'cookie', 'accept-language']`. */
27
+ from?: LocaleSource[];
28
+ /** Query parameter name. Defaults to `lang`. */
29
+ query?: string;
30
+ /** Header name, case-insensitive. Defaults to `x-lang`. */
31
+ header?: string;
32
+ /** Cookie name. Defaults to `lang`. */
33
+ cookie?: string;
34
+ }
35
+ export type LocaleResolver = (req: LocaleRequest) => string | undefined;
36
+ /**
37
+ * Compiles the options into one function the interceptor and the filter can
38
+ * call per request. Every candidate goes through {@link matchLanguage}, so a
39
+ * value the backend cannot answer in is skipped rather than passed along —
40
+ * the next source gets its turn, and the fallback language is what remains.
41
+ */
42
+ export declare function buildLocaleResolver(options: LocaleOptions, supportedLanguages: () => string[]): LocaleResolver;
43
+ /**
44
+ * Picks the best supported language out of a raw value, which may be a single
45
+ * tag (`en`, `es-MX`) or a full `Accept-Language` header
46
+ * (`es-MX,es;q=0.9,en;q=0.8`). Exact match first, then the base language of a
47
+ * regional tag, in the order the client stated its preferences.
48
+ *
49
+ * With no supported list (a backend that cannot enumerate its languages) the
50
+ * first tag is returned as-is, but only if it looks like a language tag —
51
+ * that is the last line between a query string and an `Intl` constructor.
52
+ */
53
+ export declare function matchLanguage(raw: string, supported: string[]): string | undefined;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Brand shared by every copy of the package in a dependency tree — see the
3
+ * note on `Symbol.for` in `response/paginated.ts`.
4
+ */
5
+ declare const I18N_MESSAGE: unique symbol;
6
+ /**
7
+ * A message that has not been translated yet: a key, its interpolation
8
+ * arguments, and what to say if no translation exists.
9
+ *
10
+ * It is deliberately lazy. The place that knows the message (a service
11
+ * throwing, a controller returning) rarely knows the language; the place that
12
+ * knows the language (the edge: `I18nInterceptor`, `GlobalExceptionFilter`)
13
+ * has no idea which messages are inside the payload. So the message travels as
14
+ * data and is resolved once, at the edge, in the request's language.
15
+ *
16
+ * Serialises to the fallback (or the key) when it reaches `JSON.stringify`
17
+ * untranslated — an app that never imported `I18nModule` still answers with a
18
+ * readable string rather than `{ "key": ... }`.
19
+ */
20
+ export declare class I18nMessage {
21
+ readonly key: string;
22
+ readonly args?: Record<string, unknown> | undefined;
23
+ readonly fallback?: string | undefined;
24
+ readonly [I18N_MESSAGE] = true;
25
+ constructor(key: string, args?: Record<string, unknown> | undefined, fallback?: string | undefined);
26
+ toJSON(): string;
27
+ toString(): string;
28
+ }
29
+ /**
30
+ * Builds a lazy message. `t('widgets.not_found', { id }, 'Widget not found')`
31
+ * reads the way every i18n library reads, but it does not translate here —
32
+ * see {@link I18nMessage}.
33
+ */
34
+ export declare function t(key: string, args?: Record<string, unknown>, fallback?: string): I18nMessage;
35
+ export declare function isI18nMessage(value: unknown): value is I18nMessage;
36
+ export {};
@@ -0,0 +1,9 @@
1
+ import type { I18nService } from 'nestjs-i18n';
2
+ import type { TranslateOptions, TranslatorPort } from './translator.port.js';
3
+ /** {@link TranslatorPort} over `nestjs-i18n`: JSON files per language and namespace, held in memory. */
4
+ export declare class NestI18nTranslator implements TranslatorPort {
5
+ private readonly service;
6
+ constructor(service: I18nService);
7
+ translate(key: string, options?: TranslateOptions): string | undefined;
8
+ supportedLanguages(): string[];
9
+ }
@@ -0,0 +1,55 @@
1
+ import { I18nMessage } from './message.js';
2
+ import type { LocaleResolver } from './locale.js';
3
+ import type { TranslatorPort } from './translator.port.js';
4
+ /**
5
+ * What `I18nModule` leaves behind once it is wired, for the pieces that are
6
+ * not created through DI (`GlobalExceptionFilter` and `ResponseInterceptor`
7
+ * are `new`-ed in `applyNestKitDefaults`) and for code with no injector at
8
+ * all (a queue worker building an email).
9
+ *
10
+ * A module-level singleton for the same reason `RequestContext` is one — and
11
+ * the reason `splitting: true` is non-negotiable in `tsup.config.ts`: without
12
+ * it the `errors` bundle would hold a different copy of this variable than
13
+ * the `i18n` bundle that sets it.
14
+ */
15
+ export interface I18nRuntime {
16
+ translator: TranslatorPort;
17
+ fallbackLanguage: string;
18
+ resolveLocale: LocaleResolver;
19
+ /**
20
+ * Namespace under which an `AppException`'s `code` is looked up when it
21
+ * carries no explicit message key (`errors.NOT_FOUND`). `false` disables it.
22
+ */
23
+ errorCodes: string | false;
24
+ }
25
+ export declare function setI18nRuntime(next: I18nRuntime | undefined): void;
26
+ export declare function getI18nRuntime(): I18nRuntime | undefined;
27
+ export interface TranslateCallOptions {
28
+ /** Overrides the request's language — for an email in the recipient's language, say. */
29
+ lang?: string;
30
+ args?: Record<string, unknown>;
31
+ /** What to return when the key is missing; a message's own `fallback` wins over it. */
32
+ defaultValue?: string;
33
+ }
34
+ /** The language the current request speaks, or the configured fallback, or nothing. */
35
+ export declare function currentLanguage(): string | undefined;
36
+ /**
37
+ * Translates a key or an {@link I18nMessage} right now, in the current
38
+ * request's language unless `lang` says otherwise. Never throws: a missing
39
+ * key or a broken backend yields the fallback, then the key — a translation
40
+ * problem must not become the error a client sees.
41
+ */
42
+ export declare function translate(input: string | I18nMessage, options?: TranslateCallOptions): string;
43
+ /**
44
+ * Walks a value and replaces every {@link I18nMessage} inside it with its
45
+ * translation. This is what makes the interceptor generic: it does not care
46
+ * whether the controller returned an entity, a list, a page or `{ message }`.
47
+ *
48
+ * Only plain objects and arrays are entered. A class instance (a Date, a
49
+ * Buffer, a stream, an ORM entity with lazy relations) is passed through
50
+ * untouched — walking it would be unsafe, and serialising it is the
51
+ * consumer's business anyway. A page keeps its brand, so `ResponseInterceptor`
52
+ * still recognises it afterwards. Nothing is copied unless something under it
53
+ * actually changed.
54
+ */
55
+ export declare function translateDeep<T>(value: T, lang?: string): T;
@@ -0,0 +1,29 @@
1
+ export interface TranslateOptions {
2
+ /** Language to translate into. The adapter decides what happens when it is unknown. */
3
+ lang?: string;
4
+ /** Interpolation arguments — `{id}` placeholders, in the default adapter. */
5
+ args?: Record<string, unknown>;
6
+ }
7
+ /**
8
+ * What the rest of the package needs from a translation backend, and nothing
9
+ * more. `nestjs-i18n` with JSON files is the adapter that ships
10
+ * (`NestI18nTranslator`); a consumer with translations in a database, in
11
+ * `i18next`, or in memory implements these two methods and passes the object
12
+ * to `I18nModule.forRoot({ translator })`.
13
+ */
14
+ export interface TranslatorPort {
15
+ /**
16
+ * Returns the translated string, or `undefined` when the key does not exist
17
+ * in that language (after whatever fallback chain the backend applies). The
18
+ * callers decide what to show then — the message's fallback, or the key.
19
+ * Must not throw for a missing key.
20
+ */
21
+ translate(key: string, options?: TranslateOptions): string | undefined;
22
+ /**
23
+ * Languages the backend can actually answer in. Used to validate whatever
24
+ * the request asked for (`?lang=xx`) before it reaches the backend — an
25
+ * arbitrary tag from a query string must never turn into a lookup, a file
26
+ * path or an `Intl` call.
27
+ */
28
+ supportedLanguages(): string[];
29
+ }
@@ -0,0 +1,35 @@
1
+ import { type CallHandler, type ExecutionContext, type NestInterceptor } from '@nestjs/common';
2
+ import { type Observable } from 'rxjs';
3
+ import { type IdempotencyOptions } from './idempotency.options.js';
4
+ import { type IdempotencyStorePort } from './idempotency.port.js';
5
+ /**
6
+ * Replays the answer to a request that was already handled.
7
+ *
8
+ * The problem it solves is not exotic: a client on a slow connection taps
9
+ * twice, or a proxy retries a timed-out POST. Before `payments` and `invoicing`
10
+ * that meant a duplicated row; now it means a second card charge, or a second
11
+ * CFDI — and a CFDI cannot be deleted, only cancelled with a reason filed at
12
+ * the SAT.
13
+ *
14
+ * Three outcomes for a key that has been seen:
15
+ * - **finished** → the stored answer is returned, with `Idempotent-Replay: true`.
16
+ * - **still running** → 409, rather than waiting. Holding the second request
17
+ * open ties its lifetime to the first one's, and a client that retried
18
+ * because things were slow is the worst one to make wait again.
19
+ * - **same key, different body** → 422. Replaying an answer to a request nobody
20
+ * made is worse than failing: the client would read "created" about an object
21
+ * that does not exist.
22
+ *
23
+ * Only successful answers are remembered. An error releases the key so a retry
24
+ * genuinely retries — which means a handler that failed *after* its side effect
25
+ * will run it twice. That is why the side effect carries its own key too:
26
+ * `payments.charge()` and Mercado Pago's `X-Idempotency-Key` are the layer that
27
+ * survives this one.
28
+ */
29
+ export declare class IdempotencyInterceptor implements NestInterceptor {
30
+ private readonly store;
31
+ private readonly options;
32
+ constructor(store: IdempotencyStorePort, options?: IdempotencyOptions);
33
+ intercept(context: ExecutionContext, next: CallHandler): Observable<unknown>;
34
+ private mismatch;
35
+ }
@@ -0,0 +1,27 @@
1
+ import { type DynamicModule } from '@nestjs/common';
2
+ import { type IdempotencyOptions } from './idempotency.options.js';
3
+ /**
4
+ * Replay protection for the endpoints that change things.
5
+ *
6
+ * ```ts
7
+ * // app.module.ts
8
+ * IdempotencyModule.forRoot() // memoria, una sola instancia
9
+ * IdempotencyModule.forRoot({ store: new RedisIdempotencyStore(redis) })
10
+ *
11
+ * // main.ts, o en un controller con @UseInterceptors
12
+ * app.useGlobalInterceptors(app.get(IdempotencyInterceptor));
13
+ * ```
14
+ *
15
+ * Deliberately **not** wired into `applyNestKitDefaults`: it needs a store
16
+ * whose lifetime the app owns, and switching it on globally changes the
17
+ * contract of every mutating endpoint at once. Mounting it is a decision, not
18
+ * a default.
19
+ */
20
+ export declare class IdempotencyModule {
21
+ static forRoot(options?: IdempotencyOptions): DynamicModule;
22
+ static forRootAsync(config: {
23
+ imports?: any[];
24
+ inject?: any[];
25
+ useFactory: (...args: any[]) => IdempotencyOptions | Promise<IdempotencyOptions>;
26
+ }): DynamicModule;
27
+ }
@@ -0,0 +1,29 @@
1
+ import type { IdempotencyStorePort } from './idempotency.port.js';
2
+ /** DI token carrying the resolved {@link IdempotencyOptions}. */
3
+ export declare const IDEMPOTENCY_OPTIONS: unique symbol;
4
+ export interface IdempotencyOptions {
5
+ /** Header carrying the key. Defaults to `idempotency-key`, the name Stripe made standard. */
6
+ headerName?: string;
7
+ /** Methods this applies to. Defaults to POST, PUT, PATCH and DELETE — the ones that change things. */
8
+ methods?: string[];
9
+ /** How long an answer stays replayable. Defaults to 24 hours. */
10
+ ttlMs?: number;
11
+ /**
12
+ * Reject an unsafe request that arrives without a key. Off by default:
13
+ * turning it on breaks every existing client at once, so it belongs to a
14
+ * deliberate rollout.
15
+ */
16
+ require?: boolean;
17
+ /**
18
+ * Who the key belongs to. Two customers sending the same key —
19
+ * `"order-1"` is not imaginative — must not read each other's answers.
20
+ *
21
+ * Defaults to the authenticated subject (`req.user.sub`, which is what
22
+ * `JwtAuthGuard` attaches). Unauthenticated requests fall back to a shared
23
+ * scope, so on a public endpoint **set this to something per-caller** or keys
24
+ * are global.
25
+ */
26
+ scope?: (request: unknown) => string | undefined;
27
+ /** Where keys live. Defaults to an in-process store — single instance only. */
28
+ store?: IdempotencyStorePort;
29
+ }
@@ -0,0 +1,43 @@
1
+ /** DI token for the idempotency store. Inject with `@Inject(IDEMPOTENCY_STORE)`. */
2
+ export declare const IDEMPOTENCY_STORE: unique symbol;
3
+ /** What is replayed when the same key comes back. */
4
+ export interface IdempotencyRecord {
5
+ status: number;
6
+ body: unknown;
7
+ /** Hash of the request that first used this key. */
8
+ fingerprint: string;
9
+ completedAt: Date;
10
+ }
11
+ export type BeginResult =
12
+ /** The key is ours; run the handler. */
13
+ {
14
+ state: 'claimed';
15
+ }
16
+ /** Another request holds the key and has not finished. */
17
+ | {
18
+ state: 'in_progress';
19
+ fingerprint: string;
20
+ }
21
+ /** The key was used before and there is an answer to replay. */
22
+ | {
23
+ state: 'completed';
24
+ record: IdempotencyRecord;
25
+ };
26
+ /**
27
+ * Where claimed keys and their answers live.
28
+ *
29
+ * `begin` has to be **atomic**: claim-if-absent in one operation. A read
30
+ * followed by a write loses exactly the race this whole module exists for —
31
+ * two identical requests arriving together, which is what a double tap on a
32
+ * slow connection produces.
33
+ */
34
+ export interface IdempotencyStorePort {
35
+ begin(key: string, fingerprint: string, ttlMs: number): Promise<BeginResult>;
36
+ /** Store the answer and stop holding the key as in-progress. */
37
+ complete(key: string, record: IdempotencyRecord, ttlMs: number): Promise<void>;
38
+ /**
39
+ * Give the key up without an answer, so a retry can try again. Used when the
40
+ * handler failed in a way that might not fail twice.
41
+ */
42
+ release(key: string): Promise<void>;
43
+ }