@miguelmorales13/nestkit 0.10.0 → 0.16.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 +518 -12
- package/dist/audit/audit.module.d.ts +23 -0
- package/dist/audit/audit.options.d.ts +16 -0
- package/dist/audit/audit.port.d.ts +37 -0
- package/dist/audit/audit.service.d.ts +29 -0
- package/dist/audit/index.cjs +72 -0
- package/dist/audit/index.d.ts +7 -0
- package/dist/audit/index.js +72 -0
- package/dist/audit/sinks.d.ts +18 -0
- package/dist/auth/index.cjs +4 -3
- package/dist/auth/index.js +2 -1
- package/dist/auth/oauth/index.cjs +81 -13
- package/dist/auth/oauth/index.js +75 -7
- package/dist/auth/oauth/oauth-auth.service.d.ts +29 -1
- package/dist/auth/oauth/oauth.options.d.ts +11 -0
- package/dist/bootstrap/index.cjs +6 -5
- package/dist/bootstrap/index.js +5 -4
- package/dist/chunk-47FXU74G.cjs +205 -0
- package/dist/{chunk-54ZXIB5T.cjs → chunk-4GT66KQS.cjs} +4 -4
- package/dist/{chunk-J7TURDAL.cjs → chunk-67B7IYAV.cjs} +2 -2
- package/dist/{chunk-NVCI3CQI.cjs → chunk-C5DV4RAX.cjs} +6 -1
- package/dist/{chunk-KDAA6GFF.js → chunk-DABTRWA6.js} +12 -7
- package/dist/{chunk-ZA56XBCK.cjs → chunk-EBPEMVZU.cjs} +15 -0
- package/dist/{chunk-EYURGACO.js → chunk-JV4EAOF2.js} +1 -1
- package/dist/chunk-KP2ZHJSR.js +35 -0
- package/dist/{chunk-7I2Y7V52.js → chunk-KRC47OX6.js} +1 -1
- package/dist/chunk-LK3OINS7.js +205 -0
- package/dist/{chunk-A3B2EY4V.js → chunk-MLOQDUBN.js} +6 -1
- package/dist/chunk-TCAMLNUZ.js +27 -0
- package/dist/{chunk-V2M75FN3.cjs → chunk-U7QK24HL.cjs} +2 -2
- package/dist/{chunk-AOCF5QCZ.js → chunk-UIVBPNH4.js} +4 -4
- package/dist/{chunk-TJHRABML.cjs → chunk-UWDHAZVE.cjs} +12 -7
- package/dist/chunk-XHW4CTBS.js +57 -0
- package/dist/chunk-XX5ROVSC.cjs +57 -0
- package/dist/chunk-Y72D5L6I.cjs +27 -0
- package/dist/crud/crud.controller.d.ts +3 -1
- package/dist/crud/crud.service.d.ts +63 -4
- package/dist/crud/index.cjs +7 -2
- package/dist/crud/index.d.ts +2 -1
- package/dist/crud/index.js +6 -1
- package/dist/crud/list-query.dto.d.ts +23 -0
- package/dist/crud/repository.port.d.ts +33 -1
- package/dist/errors/index.cjs +4 -4
- package/dist/errors/index.js +4 -4
- package/dist/idempotency/idempotency.interceptor.d.ts +35 -0
- package/dist/idempotency/idempotency.module.d.ts +27 -0
- package/dist/idempotency/idempotency.options.d.ts +29 -0
- package/dist/idempotency/idempotency.port.d.ts +43 -0
- package/dist/idempotency/index.cjs +206 -0
- package/dist/idempotency/index.d.ts +7 -0
- package/dist/idempotency/index.js +206 -0
- package/dist/idempotency/memory.store.d.ts +24 -0
- package/dist/index.cjs +24 -13
- package/dist/index.js +29 -18
- package/dist/invoicing/adapters/facturama.invoicing.d.ts +50 -0
- package/dist/invoicing/adapters/facturapi.invoicing.d.ts +35 -0
- package/dist/invoicing/amounts.d.ts +52 -0
- package/dist/invoicing/index.cjs +577 -0
- package/dist/invoicing/index.d.ts +11 -0
- package/dist/invoicing/index.js +577 -0
- package/dist/invoicing/invoicing.module.d.ts +42 -0
- package/dist/invoicing/invoicing.options.d.ts +51 -0
- package/dist/invoicing/invoicing.port.d.ts +156 -0
- package/dist/invoicing/lines.d.ts +21 -0
- package/dist/invoicing/provider-http.d.ts +21 -0
- package/dist/media/index.cjs +57 -7
- package/dist/media/index.d.ts +2 -2
- package/dist/media/index.js +56 -6
- package/dist/media/media.options.d.ts +20 -0
- package/dist/media/media.service.d.ts +18 -1
- package/dist/payments/adapters/clip.terminal.d.ts +101 -0
- package/dist/payments/adapters/mercadopago.terminal.d.ts +74 -0
- package/dist/payments/index.cjs +795 -0
- package/dist/payments/index.d.ts +12 -0
- package/dist/payments/index.js +795 -0
- package/dist/payments/money.d.ts +20 -0
- package/dist/payments/payments-webhook.controller.d.ts +30 -0
- package/dist/payments/payments.module.d.ts +45 -0
- package/dist/payments/payments.options.d.ts +84 -0
- package/dist/payments/payments.port.d.ts +174 -0
- package/dist/payments/provider-http.d.ts +26 -0
- package/dist/response/api-response.d.ts +7 -0
- package/dist/response/index.cjs +12 -4
- package/dist/response/index.d.ts +2 -0
- package/dist/response/index.js +12 -4
- package/dist/response/paginated.d.ts +25 -0
- package/dist/response/response.interceptor.d.ts +4 -0
- package/dist/tracking/index.cjs +3 -3
- package/dist/tracking/index.d.ts +1 -1
- package/dist/tracking/index.js +2 -2
- package/dist/tracking/request-context.d.ts +22 -0
- package/package.json +25 -3
- package/dist/chunk-3CLYZC3T.cjs +0 -104
- package/dist/chunk-EBO6UKHL.js +0 -20
- package/dist/chunk-JOVBJDJ2.js +0 -104
- /package/dist/{chunk-MR2IFCZE.cjs → chunk-H2EHN366.cjs} +0 -0
- /package/dist/{chunk-NAK4WDKS.js → chunk-ODPSIFOB.js} +0 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Amounts cross this module as an integer number of minor units (centavos),
|
|
3
|
+
* never as a decimal.
|
|
4
|
+
*
|
|
5
|
+
* Both providers take a decimal on the wire — Mercado Pago a string
|
|
6
|
+
* (`"24.00"`), Clip a JSON number (`200.5`) — and that is exactly why the port
|
|
7
|
+
* refuses to. A float that reaches the adapter has already lost the argument:
|
|
8
|
+
* `19.99 * 3` is `59.96999999999999`, and the terminal charges what it is sent.
|
|
9
|
+
* Each adapter formats at the very edge, once, from an integer that cannot
|
|
10
|
+
* drift.
|
|
11
|
+
*/
|
|
12
|
+
export declare function assertCents(cents: number, field?: string): void;
|
|
13
|
+
/** `2450` -> `"24.50"`. String, because that is what Mercado Pago's Orders API wants. */
|
|
14
|
+
export declare function centsToDecimalString(cents: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* `"24.50"` -> `2450`, for reading amounts back off a provider's response.
|
|
17
|
+
* Goes through the string, not `Math.round(x * 100)`, so a value the provider
|
|
18
|
+
* already rounded is not re-rounded through a float.
|
|
19
|
+
*/
|
|
20
|
+
export declare function decimalToCents(value: string | number): number;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Type } from '@nestjs/common';
|
|
2
|
+
import { type TerminalPayment, type TerminalPaymentStatus, type TerminalWebhookEvent } from './payments.port.js';
|
|
3
|
+
export interface CreateTerminalPaymentsWebhookControllerOptions {
|
|
4
|
+
/** Route for the generated controller. Defaults to `payments/webhook`. */
|
|
5
|
+
path?: string;
|
|
6
|
+
/**
|
|
7
|
+
* Keyed by the settled status. Unhandled statuses are acknowledged and
|
|
8
|
+
* ignored, the same way `createStripeWebhookController` treats an event
|
|
9
|
+
* type nobody registered.
|
|
10
|
+
*/
|
|
11
|
+
handlers: Partial<Record<TerminalPaymentStatus, (payment: TerminalPayment, event: TerminalWebhookEvent) => Promise<void> | void>>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The webhook endpoint for whichever terminal provider is registered.
|
|
15
|
+
*
|
|
16
|
+
* It always does the same three things: verify the request, **look the charge
|
|
17
|
+
* up over the API**, then dispatch on the status that came back. The body is
|
|
18
|
+
* never the source of truth — Clip's notification is unsigned, so a forged
|
|
19
|
+
* "approved" would otherwise settle an unpaid order, and Mercado Pago's
|
|
20
|
+
* signed one carries an id anyway.
|
|
21
|
+
*
|
|
22
|
+
* Handlers run before the response. Both providers retry on a non-2xx and
|
|
23
|
+
* Mercado Pago gives up at 22 seconds, so keep the work short: mark the order
|
|
24
|
+
* and enqueue the rest.
|
|
25
|
+
*/
|
|
26
|
+
export declare function createTerminalPaymentsWebhookController(options: CreateTerminalPaymentsWebhookControllerOptions): Type<{
|
|
27
|
+
handleWebhook(req: unknown, query: Record<string, unknown>): Promise<{
|
|
28
|
+
received: true;
|
|
29
|
+
}>;
|
|
30
|
+
}>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { type DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { type PaymentsOptions } from './payments.options.js';
|
|
3
|
+
import { type TerminalPaymentsPort } from './payments.port.js';
|
|
4
|
+
/**
|
|
5
|
+
* Merge explicit options with the environment, so `PaymentsModule.forRoot()`
|
|
6
|
+
* with no arguments works when the environment is configured.
|
|
7
|
+
*
|
|
8
|
+
* Recognized vars: `PAYMENTS_PROVIDER`; Mercado Pago `MERCADOPAGO_ACCESS_TOKEN`,
|
|
9
|
+
* `MERCADOPAGO_WEBHOOK_SECRET`; Clip `CLIP_API_KEY`, `CLIP_SECRET_KEY`,
|
|
10
|
+
* `CLIP_WEBHOOK_SECRET`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolvePaymentsOptions(options?: PaymentsOptions): PaymentsOptions;
|
|
13
|
+
/**
|
|
14
|
+
* Build the adapter the options select, failing loudly on missing config.
|
|
15
|
+
*
|
|
16
|
+
* Exported on its own because a merchant can legitimately run both — Clip
|
|
17
|
+
* readers in one branch, Point in another. The module registers the default
|
|
18
|
+
* one under {@link TERMINAL_PAYMENTS}; a second is this function plus a
|
|
19
|
+
* provider of your own token.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createTerminalPaymentsAdapter(options: PaymentsOptions): TerminalPaymentsPort;
|
|
22
|
+
/**
|
|
23
|
+
* Card-present payments for NestJS. Register once in the root module and
|
|
24
|
+
* inject the port anywhere with
|
|
25
|
+
* `@Inject(TERMINAL_PAYMENTS) payments: TerminalPaymentsPort`.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* PaymentsModule.forRoot({ provider: 'clip' }) // reads CLIP_* from env
|
|
29
|
+
* PaymentsModule.forRoot({ provider: 'mercadopago' }) // reads MERCADOPAGO_* from env
|
|
30
|
+
* PaymentsModule.forRootAsync({ inject: [Config], useFactory: (c) => ({ provider: c.paymentsProvider }) })
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Switching providers is this one line: nothing above depends on a concrete
|
|
34
|
+
* adapter. Adding a third (Izipay, Rappi, a bank's own terminal) is a class
|
|
35
|
+
* implementing `TerminalPaymentsPort`, a case in
|
|
36
|
+
* {@link createTerminalPaymentsAdapter}, and nothing else.
|
|
37
|
+
*/
|
|
38
|
+
export declare class PaymentsModule {
|
|
39
|
+
static forRoot(options?: PaymentsOptions): DynamicModule;
|
|
40
|
+
static forRootAsync(config: {
|
|
41
|
+
imports?: any[];
|
|
42
|
+
inject?: any[];
|
|
43
|
+
useFactory: (...args: any[]) => PaymentsOptions | Promise<PaymentsOptions>;
|
|
44
|
+
}): DynamicModule;
|
|
45
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/** DI token carrying the {@link PaymentsOptions} the module was registered with. */
|
|
2
|
+
export declare const PAYMENTS_OPTIONS: unique symbol;
|
|
3
|
+
/** Which adapter backs the {@link TerminalPaymentsPort}. */
|
|
4
|
+
export type PaymentsProvider = 'mercadopago' | 'clip';
|
|
5
|
+
/**
|
|
6
|
+
* Mercado Pago Point, over the **Orders API**.
|
|
7
|
+
*
|
|
8
|
+
* Not the Payment Intents API (`/point/integration-api/payment-intents`): it
|
|
9
|
+
* still answers, but Mercado Pago builds every new Point feature on Orders and
|
|
10
|
+
* documents the move as one-way. Starting a new integration on the old one
|
|
11
|
+
* buys a migration.
|
|
12
|
+
*/
|
|
13
|
+
export interface MercadoPagoPointOptions {
|
|
14
|
+
/** Access token of the application the terminals belong to. */
|
|
15
|
+
accessToken: string;
|
|
16
|
+
/**
|
|
17
|
+
* Webhook secret from Your integrations → Webhooks. Without it
|
|
18
|
+
* `verifyWebhook` throws instead of trusting the request: an unsigned
|
|
19
|
+
* notification is a stranger's POST.
|
|
20
|
+
*/
|
|
21
|
+
webhookSecret?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Raw value for `config.point.print_on_terminal`, sent on every charge.
|
|
24
|
+
*
|
|
25
|
+
* Exists because Mercado Pago documents `no_ticket` and no other value, so
|
|
26
|
+
* nestkit will not invent the one that prints. If your account has one that
|
|
27
|
+
* works, put it here; otherwise leave it out and the terminal's own setting
|
|
28
|
+
* applies.
|
|
29
|
+
*/
|
|
30
|
+
printOnTerminal?: string;
|
|
31
|
+
/** Override for tests. Defaults to `https://api.mercadopago.com`. */
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
/** Override for tests. Defaults to the global `fetch`. */
|
|
34
|
+
fetchImpl?: typeof fetch;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Clip, over the **PinPad API** (`api.payclip.io/f2f/pinpad/v1`).
|
|
38
|
+
*
|
|
39
|
+
* Three things about it that are not obvious and cost a day each:
|
|
40
|
+
*
|
|
41
|
+
* - It has **no sandbox**. The API runs in production only, so the first real
|
|
42
|
+
* test needs a physical reader (Total 3, Ultra, Clip PinPad or Stand 2) with
|
|
43
|
+
* the Clip PinPad APK installed.
|
|
44
|
+
* - The reader must be **registered with Clip** (serial number emailed to
|
|
45
|
+
* sdk@payclip.com) before it answers anything.
|
|
46
|
+
* - Its webhook is **unsigned**, which is why {@link webhookPathSecret} exists.
|
|
47
|
+
*/
|
|
48
|
+
export interface ClipPinpadOptions {
|
|
49
|
+
/** API key from the Clip dashboard. Combined with {@link secretKey} into the Basic token. */
|
|
50
|
+
apiKey: string;
|
|
51
|
+
/** Secret key from the Clip dashboard. */
|
|
52
|
+
secretKey: string;
|
|
53
|
+
/**
|
|
54
|
+
* A hard-to-guess value you also put in the `webhook_url` you register, and
|
|
55
|
+
* which `verifyWebhook` then requires back (as the `token` query parameter
|
|
56
|
+
* or the `x-webhook-token` header).
|
|
57
|
+
*
|
|
58
|
+
* Clip signs nothing: its notification is `{ id, origin, event_type }`, so
|
|
59
|
+
* with a guessable URL anyone can make your service go look up ids. That is
|
|
60
|
+
* bounded — the answer still comes from Clip's API, never from the body —
|
|
61
|
+
* but unauthenticated work on demand is a bad trade when the fix is a random
|
|
62
|
+
* string in a URL.
|
|
63
|
+
*/
|
|
64
|
+
webhookPathSecret?: string;
|
|
65
|
+
/** Override for tests. Defaults to `https://api.payclip.io` — the PinPad host. */
|
|
66
|
+
baseUrl?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Clip's *other* host, `https://api.payclip.com`, where transactions and
|
|
69
|
+
* refunds live. Two hosts for one integration is not a typo on their side or
|
|
70
|
+
* ours: PinPad is a separate service, and a refund needs both.
|
|
71
|
+
*/
|
|
72
|
+
apiBaseUrl?: string;
|
|
73
|
+
/** Override for tests. Defaults to the global `fetch`. */
|
|
74
|
+
fetchImpl?: typeof fetch;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* What the module is registered with. Give it a provider and that provider's
|
|
78
|
+
* block; anything omitted falls back to the environment.
|
|
79
|
+
*/
|
|
80
|
+
export interface PaymentsOptions {
|
|
81
|
+
provider?: PaymentsProvider;
|
|
82
|
+
mercadopago?: MercadoPagoPointOptions;
|
|
83
|
+
clip?: ClipPinpadOptions;
|
|
84
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DI token for the card-present payments adapter. Inject with
|
|
3
|
+
* `@Inject(TERMINAL_PAYMENTS) payments: TerminalPaymentsPort`.
|
|
4
|
+
*/
|
|
5
|
+
export declare const TERMINAL_PAYMENTS: unique symbol;
|
|
6
|
+
/**
|
|
7
|
+
* The states a charge on a physical terminal actually passes through, named
|
|
8
|
+
* once so a call site never learns a provider's vocabulary.
|
|
9
|
+
*
|
|
10
|
+
* `pending` and `at_terminal` are deliberately distinct: only the first is
|
|
11
|
+
* reliably cancellable from the server. Once the terminal has picked the
|
|
12
|
+
* charge up, someone is standing in front of it with a card, and both
|
|
13
|
+
* providers either refuse the cancellation or race the customer for it.
|
|
14
|
+
*/
|
|
15
|
+
export type TerminalPaymentStatus = 'pending' | 'at_terminal' | 'approved' | 'rejected' | 'cancelled' | 'expired' | 'refunded' | 'unknown';
|
|
16
|
+
/** A terminal that can be charged on. `id` is what {@link ChargeInput.terminalId} takes. */
|
|
17
|
+
export interface Terminal {
|
|
18
|
+
/** Mercado Pago's `terminal_id`, Clip's reader serial number. */
|
|
19
|
+
id: string;
|
|
20
|
+
/**
|
|
21
|
+
* Whether the device is reachable right now.
|
|
22
|
+
*
|
|
23
|
+
* `undefined` means the provider does not report it, not that the terminal
|
|
24
|
+
* is down — Mercado Pago's list says nothing about connectivity, while Clip
|
|
25
|
+
* expires a device that has not checked in for 20 seconds. Never render
|
|
26
|
+
* `undefined` as offline.
|
|
27
|
+
*/
|
|
28
|
+
online?: boolean;
|
|
29
|
+
/** Mercado Pago only: `'pdv'` accepts API charges, `'standalone'` ignores them. */
|
|
30
|
+
operatingMode?: 'pdv' | 'standalone' | 'unknown';
|
|
31
|
+
/** The provider's own object, for anything this port does not model. */
|
|
32
|
+
raw: unknown;
|
|
33
|
+
}
|
|
34
|
+
export interface ChargeInput {
|
|
35
|
+
/** Integer minor units (centavos). See `money.ts` for why this is not a decimal. */
|
|
36
|
+
amountCents: number;
|
|
37
|
+
/** Which terminal should light up. From {@link TerminalPaymentsPort.listTerminals}. */
|
|
38
|
+
terminalId: string;
|
|
39
|
+
/**
|
|
40
|
+
* Your id for this sale. Both providers store it and hand it back, and it is
|
|
41
|
+
* the only thing that ties a settled payment to your order once a webhook
|
|
42
|
+
* arrives late or out of order.
|
|
43
|
+
*
|
|
44
|
+
* Mercado Pago caps it at 64 characters of letters, digits, `-` and `_`; the
|
|
45
|
+
* adapter checks that before sending, because the rejection it would come
|
|
46
|
+
* back with names the field and not the rule.
|
|
47
|
+
*/
|
|
48
|
+
reference: string;
|
|
49
|
+
/**
|
|
50
|
+
* Mercado Pago only. Clip's reader shows the amount and nothing else, so its
|
|
51
|
+
* adapter refuses this rather than dropping it — put the detail in
|
|
52
|
+
* `reference`, which both providers store.
|
|
53
|
+
*/
|
|
54
|
+
description?: string;
|
|
55
|
+
/** Integer minor units. Clip only — Mercado Pago's Point order has no tip field. */
|
|
56
|
+
tipCents?: number;
|
|
57
|
+
/**
|
|
58
|
+
* `false` asks the terminal not to print.
|
|
59
|
+
*
|
|
60
|
+
* `true` does **not** force a print: Mercado Pago documents exactly one
|
|
61
|
+
* value for this field (`no_ticket`) and names no other, so the adapter
|
|
62
|
+
* sends nothing and the terminal's own setting decides. Guessing a value
|
|
63
|
+
* here would be a 400 in production discovered by a cashier. Someone who
|
|
64
|
+
* knows a valid value can pass it raw as `printOnTerminal` in
|
|
65
|
+
* {@link MercadoPagoPointOptions}.
|
|
66
|
+
*/
|
|
67
|
+
printReceipt?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* How long the charge stays alive on the terminal before it expires on its
|
|
70
|
+
* own. Mercado Pago only — Clip has no per-charge expiry (its equivalent is
|
|
71
|
+
* the device-level kiosk timeout), so its adapter refuses this.
|
|
72
|
+
*/
|
|
73
|
+
expiresInMinutes?: number;
|
|
74
|
+
/**
|
|
75
|
+
* Where to notify the result. Clip takes this per charge; Mercado Pago
|
|
76
|
+
* configures it once in the panel and rejects it here.
|
|
77
|
+
*/
|
|
78
|
+
webhookUrl?: string;
|
|
79
|
+
/**
|
|
80
|
+
* Replay guard for the create call. Defaults to a fresh UUID. Pass your own
|
|
81
|
+
* (derived from `reference`) if your caller can retry the same sale.
|
|
82
|
+
*/
|
|
83
|
+
idempotencyKey?: string;
|
|
84
|
+
}
|
|
85
|
+
/** A charge, normalized. `raw` is the provider payload it was read from. */
|
|
86
|
+
export interface TerminalPayment {
|
|
87
|
+
/** The provider's id for the charge — what {@link TerminalPaymentsPort.get} takes back. */
|
|
88
|
+
id: string;
|
|
89
|
+
provider: string;
|
|
90
|
+
status: TerminalPaymentStatus;
|
|
91
|
+
/** What was asked for, in minor units. */
|
|
92
|
+
amountCents: number;
|
|
93
|
+
/** What was actually collected. `undefined` until the charge settles. */
|
|
94
|
+
paidCents?: number;
|
|
95
|
+
tipCents?: number;
|
|
96
|
+
reference?: string;
|
|
97
|
+
terminalId?: string;
|
|
98
|
+
createdAt?: Date;
|
|
99
|
+
raw: unknown;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The shape a webhook arrives in, described structurally so the module works
|
|
103
|
+
* on Fastify and Express without depending on either — same reasoning as
|
|
104
|
+
* `auth/oauth`'s `http.ts`.
|
|
105
|
+
*/
|
|
106
|
+
export interface WebhookRequestLike {
|
|
107
|
+
headers: Record<string, string | string[] | undefined>;
|
|
108
|
+
query?: Record<string, unknown>;
|
|
109
|
+
body?: unknown;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* What a verified webhook tells you: which charge moved. Deliberately not the
|
|
113
|
+
* new status — see {@link TerminalPaymentsPort.verifyWebhook}.
|
|
114
|
+
*/
|
|
115
|
+
export interface TerminalWebhookEvent {
|
|
116
|
+
provider: string;
|
|
117
|
+
/** The charge id to look up. */
|
|
118
|
+
paymentId: string;
|
|
119
|
+
/** The provider's event name, when it sends one. Informational only. */
|
|
120
|
+
action?: string;
|
|
121
|
+
raw: unknown;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* One port, several providers. Nothing above this interface knows whether the
|
|
125
|
+
* terminal is a Mercado Pago Point or a Clip reader, which is what makes
|
|
126
|
+
* swapping (or running both, one per branch) a configuration change.
|
|
127
|
+
*
|
|
128
|
+
* Adapters never silently drop an option they cannot honor — they throw. A
|
|
129
|
+
* tip that quietly disappears is a customer asking where their money went.
|
|
130
|
+
*/
|
|
131
|
+
export interface TerminalPaymentsPort {
|
|
132
|
+
/** Which adapter this is: `'mercadopago'`, `'clip'`. */
|
|
133
|
+
readonly provider: string;
|
|
134
|
+
/** The terminals this account can charge on. */
|
|
135
|
+
listTerminals(): Promise<Terminal[]>;
|
|
136
|
+
/** Push a charge to a terminal. Returns as soon as it is queued, not when it is paid. */
|
|
137
|
+
charge(input: ChargeInput): Promise<TerminalPayment>;
|
|
138
|
+
/** Read a charge's current state. The authoritative answer, always. */
|
|
139
|
+
get(id: string): Promise<TerminalPayment>;
|
|
140
|
+
/**
|
|
141
|
+
* Withdraw a charge. Only reliable while it is still `pending`; once it is
|
|
142
|
+
* `at_terminal` the provider may refuse, and this throws.
|
|
143
|
+
*
|
|
144
|
+
* Read the charge back with {@link get} afterwards if the amount matters:
|
|
145
|
+
* the cancellation response is an echo, and Clip's own documentation prints
|
|
146
|
+
* it with the amount in two different formats.
|
|
147
|
+
*/
|
|
148
|
+
cancel(id: string): Promise<TerminalPayment>;
|
|
149
|
+
/**
|
|
150
|
+
* Give the money back on a charge that already settled. Total by default,
|
|
151
|
+
* partial with `amountCents`.
|
|
152
|
+
*
|
|
153
|
+
* Mercado Pago refunds the order directly, within 90 days and only while it
|
|
154
|
+
* is `processed`. Clip refunds a *transaction*, which is not the id you
|
|
155
|
+
* hold, so its adapter resolves it from the charge's detail first — one
|
|
156
|
+
* method here, two calls there.
|
|
157
|
+
*/
|
|
158
|
+
refund(id: string, options?: {
|
|
159
|
+
amountCents?: number;
|
|
160
|
+
reason?: string;
|
|
161
|
+
}): Promise<TerminalPayment>;
|
|
162
|
+
/**
|
|
163
|
+
* Authenticate an incoming webhook and say which charge it is about.
|
|
164
|
+
*
|
|
165
|
+
* It returns an id, never a status, and that is the whole design. Mercado
|
|
166
|
+
* Pago signs its notifications and Clip does not sign at all — its payload
|
|
167
|
+
* is three fields anyone can POST at you. Trusting a body that cannot be
|
|
168
|
+
* verified is how a forged "approved" marks an unpaid order as paid, so the
|
|
169
|
+
* result of a webhook is always a lookup: verify, then `get()`.
|
|
170
|
+
*
|
|
171
|
+
* Throws `UnauthorizedAppException` when the request cannot be trusted.
|
|
172
|
+
*/
|
|
173
|
+
verifyWebhook(request: WebhookRequestLike): TerminalWebhookEvent;
|
|
174
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AppException } from '../errors/app.exception.js';
|
|
2
|
+
/**
|
|
3
|
+
* A call to the provider that came back wrong.
|
|
4
|
+
*
|
|
5
|
+
* 502, not the provider's own status: the caller's request was fine, the
|
|
6
|
+
* dependency failed. Surfacing Clip's 400 as your 400 sends the client off to
|
|
7
|
+
* fix a request that has nothing wrong with it. `details` keeps the original
|
|
8
|
+
* status and body so the real cause is still one log line away.
|
|
9
|
+
*/
|
|
10
|
+
export declare class TerminalPaymentProviderException extends AppException {
|
|
11
|
+
constructor(provider: string, status: number, body: unknown, message?: string);
|
|
12
|
+
}
|
|
13
|
+
/** Thrown when an adapter is asked for something its provider does not do. */
|
|
14
|
+
export declare class UnsupportedByProviderException extends AppException {
|
|
15
|
+
constructor(provider: string, message: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* One `fetch` wrapper both adapters share: sends JSON, parses JSON, and turns
|
|
19
|
+
* any non-2xx into {@link TerminalPaymentProviderException} with the body
|
|
20
|
+
* intact.
|
|
21
|
+
*
|
|
22
|
+
* Reading the body as text first is deliberate — a gateway error page is not
|
|
23
|
+
* JSON, and `response.json()` on it throws a parse error that hides the status
|
|
24
|
+
* that actually explains the failure.
|
|
25
|
+
*/
|
|
26
|
+
export declare function requestJson<T>(provider: string, fetchImpl: typeof fetch, url: string, init: RequestInit): Promise<T>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PageMeta } from './paginated.js';
|
|
1
2
|
/**
|
|
2
3
|
* Envelope shared by every response nestkit produces, success or error.
|
|
3
4
|
*/
|
|
@@ -8,6 +9,12 @@ export interface BaseApiResponse {
|
|
|
8
9
|
export interface ApiSuccessResponse<T> extends BaseApiResponse {
|
|
9
10
|
success: true;
|
|
10
11
|
data: T;
|
|
12
|
+
/**
|
|
13
|
+
* Present only when the controller returned a {@link Paginated} result. The
|
|
14
|
+
* items stay in `data` as a plain array — a client that does not paginate
|
|
15
|
+
* reads the same shape it always did.
|
|
16
|
+
*/
|
|
17
|
+
meta?: PageMeta;
|
|
11
18
|
}
|
|
12
19
|
export interface ApiErrorResponse extends BaseApiResponse {
|
|
13
20
|
success: false;
|
package/dist/response/index.cjs
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true});require('../chunk-
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});require('../chunk-H2EHN366.cjs');
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
5
|
-
|
|
4
|
+
var _chunkUWDHAZVEcjs = require('../chunk-UWDHAZVE.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkY72D5L6Icjs = require('../chunk-Y72D5L6I.cjs');
|
|
10
|
+
require('../chunk-EBPEMVZU.cjs');
|
|
6
11
|
require('../chunk-2REOCMUD.cjs');
|
|
7
12
|
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
exports.ResponseInterceptor = _chunkUWDHAZVEcjs.ResponseInterceptor; exports.buildPageMeta = _chunkY72D5L6Icjs.buildPageMeta; exports.isPaginated = _chunkY72D5L6Icjs.isPaginated; exports.paginated = _chunkY72D5L6Icjs.paginated;
|
package/dist/response/index.d.ts
CHANGED
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export type { ApiResponse, ApiSuccessResponse, ApiErrorResponse, BaseApiResponse } from './api-response.js';
|
|
2
2
|
export { ResponseInterceptor } from './response.interceptor.js';
|
|
3
|
+
export { paginated, isPaginated, buildPageMeta } from './paginated.js';
|
|
4
|
+
export type { Paginated, PageMeta } from './paginated.js';
|
package/dist/response/index.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
import "../chunk-
|
|
1
|
+
import "../chunk-ODPSIFOB.js";
|
|
2
2
|
import {
|
|
3
3
|
ResponseInterceptor
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import
|
|
4
|
+
} from "../chunk-DABTRWA6.js";
|
|
5
|
+
import {
|
|
6
|
+
buildPageMeta,
|
|
7
|
+
isPaginated,
|
|
8
|
+
paginated
|
|
9
|
+
} from "../chunk-TCAMLNUZ.js";
|
|
10
|
+
import "../chunk-KP2ZHJSR.js";
|
|
6
11
|
import "../chunk-4MGIQFAJ.js";
|
|
7
12
|
export {
|
|
8
|
-
ResponseInterceptor
|
|
13
|
+
ResponseInterceptor,
|
|
14
|
+
buildPageMeta,
|
|
15
|
+
isPaginated,
|
|
16
|
+
paginated
|
|
9
17
|
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** What a client needs to render a pager, and nothing more. */
|
|
2
|
+
export interface PageMeta {
|
|
3
|
+
page: number;
|
|
4
|
+
limit: number;
|
|
5
|
+
total: number;
|
|
6
|
+
pages: number;
|
|
7
|
+
hasNext: boolean;
|
|
8
|
+
hasPrevious: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* A page of results on its way out of a controller.
|
|
12
|
+
*
|
|
13
|
+
* The `ResponseInterceptor` recognises it and hoists the metadata into the
|
|
14
|
+
* envelope, so the client still gets a plain array in `data` and the paging
|
|
15
|
+
* information beside it in `meta`. Returning it from a hand-written
|
|
16
|
+
* controller works exactly the same as returning it from the generic CRUD one.
|
|
17
|
+
*/
|
|
18
|
+
export interface Paginated<T> {
|
|
19
|
+
items: T[];
|
|
20
|
+
meta: PageMeta;
|
|
21
|
+
}
|
|
22
|
+
export declare function buildPageMeta(total: number, page: number, limit: number): PageMeta;
|
|
23
|
+
/** Wrap a page of items so the interceptor can recognise it. */
|
|
24
|
+
export declare function paginated<T>(items: T[], meta: PageMeta): Paginated<T>;
|
|
25
|
+
export declare function isPaginated(value: unknown): value is Paginated<unknown>;
|
|
@@ -7,6 +7,10 @@ import type { ApiSuccessResponse } from './api-response.js';
|
|
|
7
7
|
* `app.useGlobalInterceptors(new ResponseInterceptor())` (or via
|
|
8
8
|
* `applyNestKitDefaults`). Relies on TrackingModule's RequestIdMiddleware
|
|
9
9
|
* having already populated RequestContext for the current request.
|
|
10
|
+
*
|
|
11
|
+
* A {@link Paginated} return value is unwrapped: its items become `data` and
|
|
12
|
+
* its page metadata becomes `meta`, so paging never nests an array one level
|
|
13
|
+
* deeper for clients that do not use it.
|
|
10
14
|
*/
|
|
11
15
|
export declare class ResponseInterceptor<T> implements NestInterceptor<T, ApiSuccessResponse<T>> {
|
|
12
16
|
intercept(_context: ExecutionContext, next: CallHandler<T>): Observable<ApiSuccessResponse<T>>;
|
package/dist/tracking/index.cjs
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var _chunkU7QK24HLcjs = require('../chunk-U7QK24HL.cjs');
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _chunkEBPEMVZUcjs = require('../chunk-EBPEMVZU.cjs');
|
|
9
9
|
require('../chunk-2REOCMUD.cjs');
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
exports.REQUEST_ID_HEADER =
|
|
15
|
+
exports.REQUEST_ID_HEADER = _chunkU7QK24HLcjs.REQUEST_ID_HEADER; exports.RequestContext = _chunkEBPEMVZUcjs.RequestContext; exports.RequestIdMiddleware = _chunkU7QK24HLcjs.RequestIdMiddleware; exports.TrackingModule = _chunkU7QK24HLcjs.TrackingModule;
|
package/dist/tracking/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { RequestContext } from './request-context.js';
|
|
2
|
-
export type { RequestContextStore } from './request-context.js';
|
|
2
|
+
export type { RequestContextStore, RequestActor } from './request-context.js';
|
|
3
3
|
export { RequestIdMiddleware, REQUEST_ID_HEADER } from './request-id.middleware.js';
|
|
4
4
|
export { TrackingModule } from './tracking.module.js';
|
package/dist/tracking/index.js
CHANGED
|
@@ -2,10 +2,10 @@ import {
|
|
|
2
2
|
REQUEST_ID_HEADER,
|
|
3
3
|
RequestIdMiddleware,
|
|
4
4
|
TrackingModule
|
|
5
|
-
} from "../chunk-
|
|
5
|
+
} from "../chunk-JV4EAOF2.js";
|
|
6
6
|
import {
|
|
7
7
|
RequestContext
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-KP2ZHJSR.js";
|
|
9
9
|
import "../chunk-4MGIQFAJ.js";
|
|
10
10
|
export {
|
|
11
11
|
REQUEST_ID_HEADER,
|
|
@@ -1,5 +1,17 @@
|
|
|
1
|
+
/** Who is making the request, once something has authenticated them. */
|
|
2
|
+
export interface RequestActor {
|
|
3
|
+
id: string;
|
|
4
|
+
tenantId?: string;
|
|
5
|
+
roles?: string[];
|
|
6
|
+
}
|
|
1
7
|
export interface RequestContextStore {
|
|
2
8
|
requestId: string;
|
|
9
|
+
/**
|
|
10
|
+
* Filled in by whatever authenticates the request — `JwtAuthGuard` does it —
|
|
11
|
+
* so layers that have no business receiving the user as an argument (an audit
|
|
12
|
+
* sink, a repository) can still answer "who".
|
|
13
|
+
*/
|
|
14
|
+
actor?: RequestActor;
|
|
3
15
|
}
|
|
4
16
|
/**
|
|
5
17
|
* Thin wrapper around AsyncLocalStorage so any layer (filters, interceptors,
|
|
@@ -11,4 +23,14 @@ export declare class RequestContext {
|
|
|
11
23
|
static run<T>(store: RequestContextStore, fn: () => T): T;
|
|
12
24
|
static getStore(): RequestContextStore | undefined;
|
|
13
25
|
static getRequestId(): string | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Records who this request belongs to. Mutates the current store rather than
|
|
28
|
+
* opening a new scope: a guard runs inside the middleware's scope, and
|
|
29
|
+
* calling `run` again there would hide everything set before it.
|
|
30
|
+
*
|
|
31
|
+
* Silently does nothing outside a request — a queue worker has no store, and
|
|
32
|
+
* a guard should not explode because of where it was called from.
|
|
33
|
+
*/
|
|
34
|
+
static setActor(actor: RequestActor): void;
|
|
35
|
+
static getActor(): RequestActor | undefined;
|
|
14
36
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miguelmorales13/nestkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks,
|
|
7
|
+
"description": "Reusable NestJS building blocks: i18n, error handling, standardized responses, request tracking, base entities, generic CRUD, Postgres/Supabase connection helpers, outbound Telegram/WhatsApp/Stripe/Meta clients, short-lived media hosting, batteries-included auth, Stripe webhooks, Resend/Nodemailer email, card-present terminal payments (Mercado Pago Point, Clip), and CFDI 4.0 invoicing (Facturapi, Facturama).",
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public"
|
|
10
10
|
},
|
|
@@ -140,12 +140,33 @@
|
|
|
140
140
|
"types": "./dist/storage/index.d.ts",
|
|
141
141
|
"import": "./dist/storage/index.js",
|
|
142
142
|
"require": "./dist/storage/index.cjs"
|
|
143
|
+
},
|
|
144
|
+
"./payments": {
|
|
145
|
+
"types": "./dist/payments/index.d.ts",
|
|
146
|
+
"import": "./dist/payments/index.js",
|
|
147
|
+
"require": "./dist/payments/index.cjs"
|
|
148
|
+
},
|
|
149
|
+
"./invoicing": {
|
|
150
|
+
"types": "./dist/invoicing/index.d.ts",
|
|
151
|
+
"import": "./dist/invoicing/index.js",
|
|
152
|
+
"require": "./dist/invoicing/index.cjs"
|
|
153
|
+
},
|
|
154
|
+
"./idempotency": {
|
|
155
|
+
"types": "./dist/idempotency/index.d.ts",
|
|
156
|
+
"import": "./dist/idempotency/index.js",
|
|
157
|
+
"require": "./dist/idempotency/index.cjs"
|
|
158
|
+
},
|
|
159
|
+
"./audit": {
|
|
160
|
+
"types": "./dist/audit/index.d.ts",
|
|
161
|
+
"import": "./dist/audit/index.js",
|
|
162
|
+
"require": "./dist/audit/index.cjs"
|
|
143
163
|
}
|
|
144
164
|
},
|
|
145
165
|
"scripts": {
|
|
146
166
|
"build": "tsup && tsc -p tsconfig.build.json",
|
|
147
167
|
"typecheck": "tsc --noEmit",
|
|
148
|
-
"dev": "tsup --watch"
|
|
168
|
+
"dev": "tsup --watch",
|
|
169
|
+
"test": "bun test"
|
|
149
170
|
},
|
|
150
171
|
"peerDependencies": {
|
|
151
172
|
"@nestjs/common": "^11.0.0",
|
|
@@ -224,6 +245,7 @@
|
|
|
224
245
|
"@nestjs/core": "^11.0.0",
|
|
225
246
|
"@nestjs/swagger": "^11.0.0",
|
|
226
247
|
"@supabase/supabase-js": "^2.45.0",
|
|
248
|
+
"@types/bun": "^1.4.2",
|
|
227
249
|
"@types/jsonwebtoken": "^9.0.10",
|
|
228
250
|
"@types/node": "^22.0.0",
|
|
229
251
|
"@types/nodemailer": "^8.0.1",
|