@merkaly/nest 0.1.8 → 0.2.1
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 +40 -21
- package/dist/events/aliexpress.d.ts +10 -0
- package/dist/events/aliexpress.d.ts.map +1 -0
- package/dist/events/aliexpress.js +12 -0
- package/dist/events/aliexpress.js.map +1 -0
- package/dist/events/index.d.ts +2 -2
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +2 -2
- package/dist/events/index.js.map +1 -1
- package/dist/events/merkaly-event.d.ts +96 -0
- package/dist/events/merkaly-event.d.ts.map +1 -0
- package/dist/events/merkaly-event.js +49 -0
- package/dist/events/merkaly-event.js.map +1 -0
- package/dist/rabbit/events.consumer.d.ts +19 -0
- package/dist/rabbit/events.consumer.d.ts.map +1 -0
- package/dist/rabbit/events.consumer.js +96 -0
- package/dist/rabbit/events.consumer.js.map +1 -0
- package/dist/rabbit/events.publisher.d.ts +8 -8
- package/dist/rabbit/events.publisher.d.ts.map +1 -1
- package/dist/rabbit/events.publisher.js +35 -41
- package/dist/rabbit/events.publisher.js.map +1 -1
- package/dist/rabbit/index.d.ts +1 -0
- package/dist/rabbit/index.d.ts.map +1 -1
- package/dist/rabbit/index.js +1 -0
- package/dist/rabbit/index.js.map +1 -1
- package/dist/rabbit/rabbit.consumer.d.ts +7 -10
- package/dist/rabbit/rabbit.consumer.d.ts.map +1 -1
- package/dist/rabbit/rabbit.consumer.js +38 -13
- package/dist/rabbit/rabbit.consumer.js.map +1 -1
- package/dist/rabbit/rabbit.module.d.ts +4 -2
- package/dist/rabbit/rabbit.module.d.ts.map +1 -1
- package/dist/rabbit/rabbit.module.js +35 -15
- package/dist/rabbit/rabbit.module.js.map +1 -1
- package/dist/rabbit/rabbit.options.d.ts +25 -5
- package/dist/rabbit/rabbit.options.d.ts.map +1 -1
- package/dist/rabbit/rabbit.options.js +6 -2
- package/dist/rabbit/rabbit.options.js.map +1 -1
- package/package.json +2 -2
- package/dist/events/webhook-event.d.ts +0 -12
- package/dist/events/webhook-event.d.ts.map +0 -1
- package/dist/events/webhook-event.js +0 -11
- package/dist/events/webhook-event.js.map +0 -1
- package/dist/events/webhook-patterns.d.ts +0 -52
- package/dist/events/webhook-patterns.d.ts.map +0 -1
- package/dist/events/webhook-patterns.js +0 -49
- package/dist/events/webhook-patterns.js.map +0 -1
package/README.md
CHANGED
|
@@ -41,8 +41,9 @@ import {
|
|
|
41
41
|
LoggerModule, LoggerService, // logger
|
|
42
42
|
requestLogger, requestOrigin, requestContext, bodyParsers, bodyParserReason, // uma linha por request
|
|
43
43
|
AbstractExceptionFilter, ExceptionsFilter, rememberError, // filtros (motivo do erro para o log)
|
|
44
|
-
RabbitModule, EventsPublisher,
|
|
45
|
-
|
|
44
|
+
RabbitModule, EventsPublisher, EventsConsumer, EventRejectedException, // eventos
|
|
45
|
+
rabbitConsumerOptions, assertConsumerTopology, OnMerkalyEvent,
|
|
46
|
+
MerkalyEvent, MerkalyEventType, MerkalyProducer, EventDataMap, createMerkalyEvent, AliexpressOrderStatus,
|
|
46
47
|
SignatureGuard, safeCompare, hmacDigest, verifyHmac,
|
|
47
48
|
} from '@merkaly/nest';
|
|
48
49
|
```
|
|
@@ -54,9 +55,8 @@ import {
|
|
|
54
55
|
imports: [
|
|
55
56
|
LoggerModule.forRoot({ service: 'webhooks.merkaly.io' }), // global
|
|
56
57
|
RabbitModule.forRoot({
|
|
57
|
-
url: env.string('RABBITMQ_URL'), // sem url:
|
|
58
|
-
|
|
59
|
-
routes: { [WebhookPatterns.asaas.PAYMENT_CONFIRMED]: 'payments.merkaly.io-queue' }, // opcional
|
|
58
|
+
url: env.string('RABBITMQ_URL'), // sem url: emit rejeita com 503
|
|
59
|
+
producer: MerkalyProducer.WEBHOOKS, // vai em todo evento
|
|
60
60
|
timeout: env.number('RABBITMQ_PUBLISH_TIMEOUT', 5000),
|
|
61
61
|
required: env.isProduction, // produção não sobe sem url
|
|
62
62
|
}),
|
|
@@ -68,7 +68,7 @@ export class AppModule {}
|
|
|
68
68
|
|
|
69
69
|
`RabbitModule.forRootAsync({ imports, inject, useFactory })` aceita a config injetada.
|
|
70
70
|
|
|
71
|
-
**
|
|
71
|
+
**Exchange e filas**: o produtor publica no exchange topic `merkaly.events` com o tipo do evento como routing key e nunca conhece uma fila. Cada consumidor declara a sua fila, com o nome do repositório (`api.merkaly.io`, `payments.merkaly.io`), e o Nest a liga a cada tipo que ele escuta. Um consumidor novo não toca no produtor.
|
|
72
72
|
|
|
73
73
|
**Health check**: o pacote não registra controllers. O Swagger servido em `/` responde 200 e funciona como health check.
|
|
74
74
|
|
|
@@ -114,32 +114,51 @@ LoggerModule.forRootAsync({
|
|
|
114
114
|
|
|
115
115
|
### Eventos
|
|
116
116
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
- Asaas: `asaas.<evento em minúsculas>` (ex.: `asaas.payment_received`).
|
|
120
|
-
- Stripe: tipo pontuado original (ex.: `stripe.invoice.paid`, `stripe.customer.subscription.updated`).
|
|
121
|
-
- Auth0: nome legível do código de log (ex.: `auth0.success_signup`, `auth0.success_login`, `auth0.user_deleted`) e `auth0.user_login` da Action.
|
|
122
|
-
- AliExpress: tópico do `message_type` (53 `aliexpress.dropshipper_order_status_update`, 18 `aliexpress.waybill_changed`, 15 `aliexpress.product_deleted`); status do pedido DS em `AliexpressOrderStatus`.
|
|
117
|
+
Contrato completo em [`docs/events.md`](docs/events.md). Eventos de negócio normalizados (`payment.received`, nunca `asaas.payment_received`): se o provedor muda, o consumidor não muda.
|
|
123
118
|
|
|
124
119
|
```ts
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
);
|
|
120
|
+
// produtor: data tipado pelo tipo; resolve com o evento ou ServiceUnavailableException (o provedor reintenta)
|
|
121
|
+
await this.events.emit(MerkalyEventType.PAYMENT_RECEIVED, { ...paymentRef, receivedAt }, {
|
|
122
|
+
id: `asaas:${body.id}`, source: 'asaas', organization, occurredAt, raw: { type: body.event, payload },
|
|
123
|
+
});
|
|
129
124
|
```
|
|
130
125
|
|
|
131
|
-
Consumidor
|
|
126
|
+
Consumidor:
|
|
132
127
|
|
|
133
128
|
```ts
|
|
134
|
-
|
|
129
|
+
// main.ts
|
|
130
|
+
const consumer = { url, queue: 'api.merkaly.io' };
|
|
131
|
+
await assertConsumerTopology(consumer); // cria .retry e .dlq
|
|
132
|
+
app.connectMicroservice(rabbitConsumerOptions(consumer));
|
|
135
133
|
await app.startAllMicroservices();
|
|
136
134
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
135
|
+
// módulo: `around` roda cada mensagem dentro do contexto do app (ex.: CLS com a organização)
|
|
136
|
+
RabbitModule.forConsumerAsync({
|
|
137
|
+
inject: [ClsService],
|
|
138
|
+
useFactory: (cls: ClsService) => ({
|
|
139
|
+
queue: 'api.merkaly.io',
|
|
140
|
+
around: (event, next) => cls.run(() => { cls.set('identity', event.organization); return next(); }),
|
|
141
|
+
}),
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// handler
|
|
145
|
+
@OnMerkalyEvent(MerkalyEventType.PAYMENT_RECEIVED)
|
|
146
|
+
received(@Payload() event: MerkalyEvent<MerkalyEventType.PAYMENT_RECEIVED>, @Ctx() context: RmqContext) {
|
|
147
|
+
return this.consumer.handle(event, context, (e) => this.payments.refresh(e.data.saleId));
|
|
140
148
|
}
|
|
141
149
|
```
|
|
142
150
|
|
|
151
|
+
`@OnMerkalyEvent(...types)` aceita vários tipos no mesmo handler. `EventsConsumer.handle` fecha a mensagem e loga uma linha (`ORIGIN` = provedor, `SCOPE` = organização, `KIND` = `EVENT`):
|
|
152
|
+
|
|
153
|
+
| Resultado | O que acontece |
|
|
154
|
+
| --- | --- |
|
|
155
|
+
| Sucesso | `ack`. |
|
|
156
|
+
| `EventRejectedException` (venda inexistente, valor diferente...) | `ack` + `WARN`: reintentar não resolve. |
|
|
157
|
+
| Outro erro (Mongo, rede, provedor fora) | Vai para `<fila>.retry` (30 s) e volta; depois de 5 tentativas, `<fila>.dlq`. |
|
|
158
|
+
| Malformado ou `version` maior que a do handler | `<fila>.dlq` direto. |
|
|
159
|
+
|
|
160
|
+
A fila principal descarta para a `.dlq` o que ficar 7 dias sem consumo ou passar de 10.000 mensagens.
|
|
161
|
+
|
|
143
162
|
### Env
|
|
144
163
|
|
|
145
164
|
`env.string`, `env.number`, `env.boolean`, `env.list`, `env.required`, `env.isProduction`. Carrega o `.env` (dotenv) no primeiro acesso. Valor obrigatório ausente ou inválido: **erro em produção**, aviso no log fora dela.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const AliexpressOrderStatus: {
|
|
2
|
+
readonly CREATED: "OrderCreated";
|
|
3
|
+
readonly PAYMENT_AUTHORIZED: "PaymentAuthorized";
|
|
4
|
+
readonly PAYMENT_FAILED: "paymentFailedEvent";
|
|
5
|
+
readonly SHIPPED: "OrderShipped";
|
|
6
|
+
readonly CONFIRMED: "OrderConfirmed";
|
|
7
|
+
readonly CLOSED: "OrderClosed";
|
|
8
|
+
};
|
|
9
|
+
export type AliexpressOrderStatus = (typeof AliexpressOrderStatus)[keyof typeof AliexpressOrderStatus];
|
|
10
|
+
//# sourceMappingURL=aliexpress.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aliexpress.d.ts","sourceRoot":"","sources":["../../src/events/aliexpress.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,qBAAqB;;;;;;;CAOxB,CAAC;AAEX,MAAM,MAAM,qBAAqB,GAC/B,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,OAAO,qBAAqB,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AliexpressOrderStatus = void 0;
|
|
4
|
+
exports.AliexpressOrderStatus = {
|
|
5
|
+
CREATED: 'OrderCreated',
|
|
6
|
+
PAYMENT_AUTHORIZED: 'PaymentAuthorized',
|
|
7
|
+
PAYMENT_FAILED: 'paymentFailedEvent',
|
|
8
|
+
SHIPPED: 'OrderShipped',
|
|
9
|
+
CONFIRMED: 'OrderConfirmed',
|
|
10
|
+
CLOSED: 'OrderClosed',
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=aliexpress.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aliexpress.js","sourceRoot":"","sources":["../../src/events/aliexpress.ts"],"names":[],"mappings":";;;AACa,QAAA,qBAAqB,GAAG;IACnC,OAAO,EAAE,cAAc;IACvB,kBAAkB,EAAE,mBAAmB;IACvC,cAAc,EAAE,oBAAoB;IACpC,OAAO,EAAE,cAAc;IACvB,SAAS,EAAE,gBAAgB;IAC3B,MAAM,EAAE,aAAa;CACb,CAAC"}
|
package/dist/events/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
1
|
+
export * from './aliexpress';
|
|
2
|
+
export * from './merkaly-event';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/events/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/events/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC"}
|
package/dist/events/index.js
CHANGED
|
@@ -14,6 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./
|
|
18
|
-
__exportStar(require("./
|
|
17
|
+
__exportStar(require("./aliexpress"), exports);
|
|
18
|
+
__exportStar(require("./merkaly-event"), exports);
|
|
19
19
|
//# sourceMappingURL=index.js.map
|
package/dist/events/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/events/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/events/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B;AAC7B,kDAAgC"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
export declare enum MerkalyEventType {
|
|
2
|
+
PAYMENT_CONFIRMED = "payment.confirmed",
|
|
3
|
+
PAYMENT_RECEIVED = "payment.received",
|
|
4
|
+
PAYMENT_FAILED = "payment.failed",
|
|
5
|
+
PAYMENT_OVERDUE = "payment.overdue",
|
|
6
|
+
PAYMENT_CANCELLED = "payment.cancelled",
|
|
7
|
+
PAYMENT_REFUNDED = "payment.refunded",
|
|
8
|
+
PAYMENT_CHARGEBACK_OPENED = "payment.chargeback.opened",
|
|
9
|
+
PAYMENT_CHARGEBACK_UPDATED = "payment.chargeback.updated",
|
|
10
|
+
PAYMENT_ACCOUNT_APPROVED = "payment.account.approved",
|
|
11
|
+
PAYMENT_ACCOUNT_REJECTED = "payment.account.rejected",
|
|
12
|
+
PAYMENT_ACCOUNT_STATUS_CHANGED = "payment.account.status_changed"
|
|
13
|
+
}
|
|
14
|
+
export declare enum MerkalyProducer {
|
|
15
|
+
WEBHOOKS = "webhooks.merkaly.io",
|
|
16
|
+
API = "api.merkaly.io",
|
|
17
|
+
PAYMENTS = "payments.merkaly.io"
|
|
18
|
+
}
|
|
19
|
+
export type MerkalyEventSource = 'asaas' | 'stripe' | 'auth0' | 'aliexpress';
|
|
20
|
+
export type PaymentProvider = 'asaas';
|
|
21
|
+
export type PaymentMethod = 'PIX' | 'CARD' | 'BOLETO';
|
|
22
|
+
export interface PaymentRef {
|
|
23
|
+
provider: PaymentProvider;
|
|
24
|
+
paymentId: string;
|
|
25
|
+
saleId: string | null;
|
|
26
|
+
method: PaymentMethod;
|
|
27
|
+
amount: number;
|
|
28
|
+
netAmount: number | null;
|
|
29
|
+
}
|
|
30
|
+
export type PaymentAccountSection = 'general' | 'commercialInfo' | 'documentation' | 'bankAccountInfo';
|
|
31
|
+
export type PaymentAccountStatus = 'PENDING' | 'AWAITING_APPROVAL' | 'APPROVED' | 'REJECTED' | 'EXPIRING_SOON' | 'EXPIRED';
|
|
32
|
+
export interface PaymentAccountRef {
|
|
33
|
+
provider: PaymentProvider;
|
|
34
|
+
accountId: string;
|
|
35
|
+
}
|
|
36
|
+
export interface EventDataMap {
|
|
37
|
+
[MerkalyEventType.PAYMENT_CONFIRMED]: PaymentRef & {
|
|
38
|
+
confirmedAt: string;
|
|
39
|
+
};
|
|
40
|
+
[MerkalyEventType.PAYMENT_RECEIVED]: PaymentRef & {
|
|
41
|
+
receivedAt: string;
|
|
42
|
+
};
|
|
43
|
+
[MerkalyEventType.PAYMENT_FAILED]: PaymentRef & {
|
|
44
|
+
reason: 'card_refused' | 'risk_rejected';
|
|
45
|
+
};
|
|
46
|
+
[MerkalyEventType.PAYMENT_OVERDUE]: PaymentRef & {
|
|
47
|
+
dueDate: string;
|
|
48
|
+
};
|
|
49
|
+
[MerkalyEventType.PAYMENT_CANCELLED]: PaymentRef;
|
|
50
|
+
[MerkalyEventType.PAYMENT_REFUNDED]: PaymentRef & {
|
|
51
|
+
refundedAmount: number;
|
|
52
|
+
partial: boolean;
|
|
53
|
+
};
|
|
54
|
+
[MerkalyEventType.PAYMENT_CHARGEBACK_OPENED]: PaymentRef & {
|
|
55
|
+
reason: string | null;
|
|
56
|
+
};
|
|
57
|
+
[MerkalyEventType.PAYMENT_CHARGEBACK_UPDATED]: PaymentRef & {
|
|
58
|
+
stage: 'disputed' | 'awaiting_reversal';
|
|
59
|
+
};
|
|
60
|
+
[MerkalyEventType.PAYMENT_ACCOUNT_APPROVED]: PaymentAccountRef;
|
|
61
|
+
[MerkalyEventType.PAYMENT_ACCOUNT_REJECTED]: PaymentAccountRef;
|
|
62
|
+
[MerkalyEventType.PAYMENT_ACCOUNT_STATUS_CHANGED]: PaymentAccountRef & {
|
|
63
|
+
section: PaymentAccountSection;
|
|
64
|
+
status: PaymentAccountStatus;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export interface MerkalyRawEvent {
|
|
68
|
+
type: string;
|
|
69
|
+
payload?: unknown;
|
|
70
|
+
truncated?: boolean;
|
|
71
|
+
}
|
|
72
|
+
export interface MerkalyEvent<K extends MerkalyEventType = MerkalyEventType> {
|
|
73
|
+
id: string;
|
|
74
|
+
type: K;
|
|
75
|
+
version: number;
|
|
76
|
+
producer: MerkalyProducer;
|
|
77
|
+
source?: MerkalyEventSource;
|
|
78
|
+
organization?: string;
|
|
79
|
+
occurredAt: string;
|
|
80
|
+
publishedAt: string;
|
|
81
|
+
correlationId?: string;
|
|
82
|
+
data: EventDataMap[K];
|
|
83
|
+
raw?: MerkalyRawEvent;
|
|
84
|
+
}
|
|
85
|
+
export interface MerkalyEventMeta {
|
|
86
|
+
id?: string;
|
|
87
|
+
version?: number;
|
|
88
|
+
source?: MerkalyEventSource;
|
|
89
|
+
organization?: string;
|
|
90
|
+
occurredAt?: string;
|
|
91
|
+
correlationId?: string;
|
|
92
|
+
raw?: MerkalyRawEvent;
|
|
93
|
+
}
|
|
94
|
+
export declare const RAW_PAYLOAD_LIMIT: number;
|
|
95
|
+
export declare function createMerkalyEvent<K extends MerkalyEventType>(producer: MerkalyProducer, type: K, data: EventDataMap[K], meta?: MerkalyEventMeta): MerkalyEvent<K>;
|
|
96
|
+
//# sourceMappingURL=merkaly-event.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merkaly-event.d.ts","sourceRoot":"","sources":["../../src/events/merkaly-event.ts"],"names":[],"mappings":"AAGA,oBAAY,gBAAgB;IAC1B,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C;IAChB,QAAQ,EAAE,eAAe,CAAC;IAC1B,MAAM,CAAC,EAAE,kBAAkB,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;IACtB,GAAG,CAAC,EAAE,eAAe,CAAC;CACvB;AAED,MAAM,WAAW,gBAAgB;IAC/B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,kBAAkB,CAAC;IAC5B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,GAAG,CAAC,EAAE,eAAe,CAAC;CACvB;AAED,eAAO,MAAM,iBAAiB,QAAY,CAAC;AAW3C,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,gBAAgB,EAC3D,QAAQ,EAAE,eAAe,EACzB,IAAI,EAAE,CAAC,EACP,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EACrB,IAAI,GAAE,gBAAqB,GAC1B,YAAY,CAAC,CAAC,CAAC,CAgBjB"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RAW_PAYLOAD_LIMIT = exports.MerkalyProducer = exports.MerkalyEventType = void 0;
|
|
4
|
+
exports.createMerkalyEvent = createMerkalyEvent;
|
|
5
|
+
const crypto_1 = require("crypto");
|
|
6
|
+
var MerkalyEventType;
|
|
7
|
+
(function (MerkalyEventType) {
|
|
8
|
+
MerkalyEventType["PAYMENT_CONFIRMED"] = "payment.confirmed";
|
|
9
|
+
MerkalyEventType["PAYMENT_RECEIVED"] = "payment.received";
|
|
10
|
+
MerkalyEventType["PAYMENT_FAILED"] = "payment.failed";
|
|
11
|
+
MerkalyEventType["PAYMENT_OVERDUE"] = "payment.overdue";
|
|
12
|
+
MerkalyEventType["PAYMENT_CANCELLED"] = "payment.cancelled";
|
|
13
|
+
MerkalyEventType["PAYMENT_REFUNDED"] = "payment.refunded";
|
|
14
|
+
MerkalyEventType["PAYMENT_CHARGEBACK_OPENED"] = "payment.chargeback.opened";
|
|
15
|
+
MerkalyEventType["PAYMENT_CHARGEBACK_UPDATED"] = "payment.chargeback.updated";
|
|
16
|
+
MerkalyEventType["PAYMENT_ACCOUNT_APPROVED"] = "payment.account.approved";
|
|
17
|
+
MerkalyEventType["PAYMENT_ACCOUNT_REJECTED"] = "payment.account.rejected";
|
|
18
|
+
MerkalyEventType["PAYMENT_ACCOUNT_STATUS_CHANGED"] = "payment.account.status_changed";
|
|
19
|
+
})(MerkalyEventType || (exports.MerkalyEventType = MerkalyEventType = {}));
|
|
20
|
+
var MerkalyProducer;
|
|
21
|
+
(function (MerkalyProducer) {
|
|
22
|
+
MerkalyProducer["WEBHOOKS"] = "webhooks.merkaly.io";
|
|
23
|
+
MerkalyProducer["API"] = "api.merkaly.io";
|
|
24
|
+
MerkalyProducer["PAYMENTS"] = "payments.merkaly.io";
|
|
25
|
+
})(MerkalyProducer || (exports.MerkalyProducer = MerkalyProducer = {}));
|
|
26
|
+
exports.RAW_PAYLOAD_LIMIT = 16 * 1024;
|
|
27
|
+
function capRaw(raw) {
|
|
28
|
+
if (!raw?.payload)
|
|
29
|
+
return raw;
|
|
30
|
+
const size = Buffer.byteLength(JSON.stringify(raw.payload) ?? '');
|
|
31
|
+
return size > exports.RAW_PAYLOAD_LIMIT ? { type: raw.type, truncated: true } : raw;
|
|
32
|
+
}
|
|
33
|
+
function createMerkalyEvent(producer, type, data, meta = {}) {
|
|
34
|
+
const publishedAt = new Date().toISOString();
|
|
35
|
+
return {
|
|
36
|
+
id: meta.id ?? (0, crypto_1.randomUUID)(),
|
|
37
|
+
type,
|
|
38
|
+
version: meta.version ?? 1,
|
|
39
|
+
producer,
|
|
40
|
+
source: meta.source,
|
|
41
|
+
organization: meta.organization || undefined,
|
|
42
|
+
occurredAt: meta.occurredAt ?? publishedAt,
|
|
43
|
+
publishedAt,
|
|
44
|
+
correlationId: meta.correlationId,
|
|
45
|
+
data,
|
|
46
|
+
raw: capRaw(meta.raw),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=merkaly-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merkaly-event.js","sourceRoot":"","sources":["../../src/events/merkaly-event.ts"],"names":[],"mappings":";;;AA6GA,gDAqBC;AAlID,mCAAoC;AAGpC,IAAY,gBAYX;AAZD,WAAY,gBAAgB;IAC1B,2DAAuC,CAAA;IACvC,yDAAqC,CAAA;IACrC,qDAAiC,CAAA;IACjC,uDAAmC,CAAA;IACnC,2DAAuC,CAAA;IACvC,yDAAqC,CAAA;IACrC,2EAAuD,CAAA;IACvD,6EAAyD,CAAA;IACzD,yEAAqD,CAAA;IACrD,yEAAqD,CAAA;IACrD,qFAAiE,CAAA;AACnE,CAAC,EAZW,gBAAgB,gCAAhB,gBAAgB,QAY3B;AAED,IAAY,eAIX;AAJD,WAAY,eAAe;IACzB,mDAAgC,CAAA;IAChC,yCAAsB,CAAA;IACtB,mDAAgC,CAAA;AAClC,CAAC,EAJW,eAAe,+BAAf,eAAe,QAI1B;AA6EY,QAAA,iBAAiB,GAAG,EAAE,GAAG,IAAI,CAAC;AAG3C,SAAS,MAAM,CAAC,GAAqB;IACnC,IAAI,CAAC,GAAG,EAAE,OAAO;QAAE,OAAO,GAAG,CAAC;IAE9B,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;IAElE,OAAO,IAAI,GAAG,yBAAiB,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;AAC9E,CAAC;AAED,SAAgB,kBAAkB,CAChC,QAAyB,EACzB,IAAO,EACP,IAAqB,EACrB,OAAyB,EAAE;IAE3B,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IAE7C,OAAO;QACL,EAAE,EAAE,IAAI,CAAC,EAAE,IAAI,IAAA,mBAAU,GAAE;QAC3B,IAAI;QACJ,OAAO,EAAE,IAAI,CAAC,OAAO,IAAI,CAAC;QAC1B,QAAQ;QACR,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,YAAY,EAAE,IAAI,CAAC,YAAY,IAAI,SAAS;QAC5C,UAAU,EAAE,IAAI,CAAC,UAAU,IAAI,WAAW;QAC1C,WAAW;QACX,aAAa,EAAE,IAAI,CAAC,aAAa;QACjC,IAAI;QACJ,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC;KACtB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { RmqContext } from '@nestjs/microservices';
|
|
2
|
+
import { MerkalyEvent, MerkalyEventType } from '../events';
|
|
3
|
+
import { LoggerService } from '../logger';
|
|
4
|
+
import { EventsConsumerOptions } from './rabbit.options';
|
|
5
|
+
export declare class EventRejectedException extends Error {
|
|
6
|
+
}
|
|
7
|
+
export interface HandleOptions {
|
|
8
|
+
version?: number;
|
|
9
|
+
}
|
|
10
|
+
export declare class EventsConsumer {
|
|
11
|
+
private readonly options;
|
|
12
|
+
private readonly logger;
|
|
13
|
+
constructor(options: EventsConsumerOptions, logger?: LoggerService);
|
|
14
|
+
handle<K extends MerkalyEventType>(event: MerkalyEvent<K>, context: RmqContext, handler: (event: MerkalyEvent<K>) => Promise<unknown>, { version }?: HandleOptions): Promise<void>;
|
|
15
|
+
private ack;
|
|
16
|
+
private deadLetter;
|
|
17
|
+
private retry;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=events.consumer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.consumer.d.ts","sourceRoot":"","sources":["../../src/rabbit/events.consumer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAkB,MAAM,WAAW,CAAC;AAG1D,OAAO,EAAqC,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAM5F,qBAAa,sBAAuB,SAAQ,KAAK;CAAG;AAEpD,MAAM,WAAW,aAAa;IAC5B,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,qBACa,cAAc;IAIG,OAAO,CAAC,QAAQ,CAAC,OAAO;IAHpD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;gBAGM,OAAO,EAAE,qBAAqB,EAC7D,MAAM,CAAC,EAAE,aAAa;IAM7B,MAAM,CAAC,CAAC,SAAS,gBAAgB,EACtC,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,EACtB,OAAO,EAAE,UAAU,EACnB,OAAO,EAAE,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,EACrD,EAAE,OAAW,EAAE,GAAE,aAAkB,GAClC,OAAO,CAAC,IAAI,CAAC;IAoChB,OAAO,CAAC,GAAG;IAKX,OAAO,CAAC,UAAU;IASlB,OAAO,CAAC,KAAK;CAiBd"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.EventsConsumer = exports.EventRejectedException = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const logger_1 = require("../logger");
|
|
18
|
+
const events_publisher_1 = require("./events.publisher");
|
|
19
|
+
const rabbit_consumer_1 = require("./rabbit.consumer");
|
|
20
|
+
const rabbit_options_1 = require("./rabbit.options");
|
|
21
|
+
const GROUP = 'EVENT';
|
|
22
|
+
const RETRIES_HEADER = 'x-merkaly-retries';
|
|
23
|
+
class EventRejectedException extends Error {
|
|
24
|
+
}
|
|
25
|
+
exports.EventRejectedException = EventRejectedException;
|
|
26
|
+
let EventsConsumer = class EventsConsumer {
|
|
27
|
+
options;
|
|
28
|
+
logger;
|
|
29
|
+
constructor(options, logger) {
|
|
30
|
+
this.options = options;
|
|
31
|
+
this.logger = logger ?? new logger_1.LoggerService();
|
|
32
|
+
}
|
|
33
|
+
handle(event, context, handler, { version = 1 } = {}) {
|
|
34
|
+
const started = Date.now();
|
|
35
|
+
const scope = {
|
|
36
|
+
origin: event?.source ?? event?.producer ?? 'unknown',
|
|
37
|
+
scope: () => event?.organization,
|
|
38
|
+
};
|
|
39
|
+
const line = (text) => `${event?.type} (${event?.id}) ${text} ${Date.now() - started}ms`;
|
|
40
|
+
return logger_1.requestContext.run(scope, async () => {
|
|
41
|
+
if (!event?.id || !event?.type)
|
|
42
|
+
return this.deadLetter(context, line('malformed'));
|
|
43
|
+
if (event.version > version)
|
|
44
|
+
return this.deadLetter(context, line(`unknown version ${event.version}`));
|
|
45
|
+
const next = () => handler(event);
|
|
46
|
+
const run = this.options.around ? () => this.options.around(event, next) : next;
|
|
47
|
+
return Promise.resolve()
|
|
48
|
+
.then(run)
|
|
49
|
+
.then(() => {
|
|
50
|
+
this.ack(context);
|
|
51
|
+
this.logger.log({ group: GROUP, message: line('ok') });
|
|
52
|
+
})
|
|
53
|
+
.catch((error) => {
|
|
54
|
+
if (error instanceof EventRejectedException) {
|
|
55
|
+
this.ack(context);
|
|
56
|
+
return this.logger.warn({ group: GROUP, message: line(`rejected "${error.message}"`) });
|
|
57
|
+
}
|
|
58
|
+
return this.retry(context, line(`failed "${(0, events_publisher_1.describeError)(error)}"`), error);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
ack(context) {
|
|
63
|
+
context.getChannelRef().ack(context.getMessage());
|
|
64
|
+
}
|
|
65
|
+
deadLetter(context, message, trace) {
|
|
66
|
+
context.getChannelRef().nack(context.getMessage(), false, false);
|
|
67
|
+
this.logger.error({
|
|
68
|
+
group: GROUP,
|
|
69
|
+
message: `${message} -> ${(0, rabbit_consumer_1.deadLetterQueue)(this.options.queue)}`,
|
|
70
|
+
trace,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
retry(context, message, trace) {
|
|
74
|
+
const msg = context.getMessage();
|
|
75
|
+
const attempt = Number(msg.properties.headers?.[RETRIES_HEADER] ?? 0) + 1;
|
|
76
|
+
const max = this.options.retries ?? rabbit_options_1.DEFAULT_RETRIES;
|
|
77
|
+
if (attempt > max)
|
|
78
|
+
return this.deadLetter(context, `${message}, ${max} retries`, trace);
|
|
79
|
+
const channel = context.getChannelRef();
|
|
80
|
+
channel.sendToQueue((0, rabbit_consumer_1.retryQueue)(this.options.queue), msg.content, {
|
|
81
|
+
...msg.properties,
|
|
82
|
+
headers: { ...msg.properties.headers, [RETRIES_HEADER]: attempt },
|
|
83
|
+
persistent: true,
|
|
84
|
+
});
|
|
85
|
+
channel.ack(msg);
|
|
86
|
+
this.logger.error({ group: GROUP, message: `${message}, retry ${attempt}/${max}`, trace });
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
exports.EventsConsumer = EventsConsumer;
|
|
90
|
+
exports.EventsConsumer = EventsConsumer = __decorate([
|
|
91
|
+
(0, common_1.Injectable)(),
|
|
92
|
+
__param(0, (0, common_1.Inject)(rabbit_options_1.CONSUMER_OPTIONS)),
|
|
93
|
+
__param(1, (0, common_1.Optional)()),
|
|
94
|
+
__metadata("design:paramtypes", [Object, logger_1.LoggerService])
|
|
95
|
+
], EventsConsumer);
|
|
96
|
+
//# sourceMappingURL=events.consumer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.consumer.js","sourceRoot":"","sources":["../../src/rabbit/events.consumer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAA8D;AAG9D,sCAA0D;AAC1D,yDAAmD;AACnD,uDAAgE;AAChE,qDAA4F;AAE5F,MAAM,KAAK,GAAG,OAAO,CAAC;AACtB,MAAM,cAAc,GAAG,mBAAmB,CAAC;AAG3C,MAAa,sBAAuB,SAAQ,KAAK;CAAG;AAApD,wDAAoD;AAO7C,IAAM,cAAc,GAApB,MAAM,cAAc;IAIoB;IAH5B,MAAM,CAAgB;IAEvC,YAC6C,OAA8B,EAC7D,MAAsB;QADS,YAAO,GAAP,OAAO,CAAuB;QAGzE,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,IAAI,sBAAa,EAAE,CAAC;IAC9C,CAAC;IAGM,MAAM,CACX,KAAsB,EACtB,OAAmB,EACnB,OAAqD,EACrD,EAAE,OAAO,GAAG,CAAC,KAAoB,EAAE;QAEnC,MAAM,OAAO,GAAG,IAAI,CAAC,GAA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CAAC,GAAG,CAAC,CAAC;QAC1E,MAAM,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,gCAAe,CAAC;QAEpD,IAAI,OAAO,GAAG,GAAG;YAAE,OAAO,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,OAAO,KAAK,GAAG,UAAU,EAAE,KAAK,CAAC,CAAC;QAExF,MAAM,OAAO,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;QAExC,OAAO,CAAC,WAAW,CAAC,IAAA,4BAAU,EAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,GAAG,CAAC,OAAO,EAAE;YAC/D,GAAG,GAAG,CAAC,UAAU;YACjB,OAAO,EAAE,EAAE,GAAG,GAAG,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC,cAAc,CAAC,EAAE,OAAO,EAAE;YACjE,UAAU,EAAE,IAAI;SACjB,CAAC,CAAC;QACH,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACjB,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,OAAO,WAAW,OAAO,IAAI,GAAG,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;IAC7F,CAAC;CACF,CAAA;AAnFY,wCAAc;yBAAd,cAAc;IAD1B,IAAA,mBAAU,GAAE;IAKR,WAAA,IAAA,eAAM,EAAC,iCAAgB,CAAC,CAAA;IACxB,WAAA,IAAA,iBAAQ,GAAE,CAAA;6CAAU,sBAAa;GALzB,cAAc,CAmF1B"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import { OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common';
|
|
2
|
-
import {
|
|
2
|
+
import { EventDataMap, MerkalyEvent, MerkalyEventMeta, MerkalyEventType } from '../events';
|
|
3
3
|
import { LoggerService } from '../logger';
|
|
4
|
-
import { BrokerStatus,
|
|
4
|
+
import { BrokerStatus, RabbitClient, RabbitModuleOptions } from './rabbit.options';
|
|
5
|
+
export declare const describeError: (error: any) => string;
|
|
5
6
|
export declare class EventsPublisher implements OnApplicationBootstrap, OnApplicationShutdown {
|
|
6
7
|
private readonly options;
|
|
7
|
-
private readonly
|
|
8
|
-
private readonly clients;
|
|
8
|
+
private readonly client;
|
|
9
9
|
private readonly logger;
|
|
10
10
|
private brokerStatus;
|
|
11
|
-
constructor(options: RabbitModuleOptions,
|
|
11
|
+
constructor(options: RabbitModuleOptions, client: RabbitClient | null, logger?: LoggerService);
|
|
12
12
|
get status(): BrokerStatus;
|
|
13
|
+
private get exchange();
|
|
13
14
|
onApplicationBootstrap(): void | Promise<any>;
|
|
14
|
-
onApplicationShutdown():
|
|
15
|
-
|
|
16
|
-
private client;
|
|
15
|
+
onApplicationShutdown(): any;
|
|
16
|
+
emit<K extends MerkalyEventType>(type: K, data: EventDataMap[K], meta?: MerkalyEventMeta): Promise<MerkalyEvent<K>>;
|
|
17
17
|
}
|
|
18
18
|
//# sourceMappingURL=events.publisher.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.publisher.d.ts","sourceRoot":"","sources":["../../src/rabbit/events.publisher.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,sBAAsB,EACtB,qBAAqB,EAGtB,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"events.publisher.d.ts","sourceRoot":"","sources":["../../src/rabbit/events.publisher.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,sBAAsB,EACtB,qBAAqB,EAGtB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,gBAAgB,EAChB,gBAAgB,EAEjB,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EACL,YAAY,EAKZ,YAAY,EACZ,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAE1B,eAAO,MAAM,aAAa,GAAI,OAAO,GAAG,KAAG,MAGkB,CAAC;AAI9D,qBACa,eAAgB,YAAW,sBAAsB,EAAE,qBAAqB;IAKzD,OAAO,CAAC,QAAQ,CAAC,OAAO;IACzB,OAAO,CAAC,QAAQ,CAAC,MAAM;IALhD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAgB;IACvC,OAAO,CAAC,YAAY,CAAe;gBAGQ,OAAO,EAAE,mBAAmB,EAC7B,MAAM,EAAE,YAAY,GAAG,IAAI,EACvD,MAAM,CAAC,EAAE,aAAa;IAMpC,IAAW,MAAM,iBAEhB;IAED,OAAO,KAAK,QAAQ,GAEnB;IAEM,sBAAsB;IAkBtB,qBAAqB;IAKrB,IAAI,CAAC,CAAC,SAAS,gBAAgB,EACpC,IAAI,EAAE,CAAC,EACP,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EACrB,IAAI,CAAC,EAAE,gBAAgB,GACtB,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;CAuB5B"}
|
|
@@ -11,84 +11,78 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var EventsPublisher_1;
|
|
15
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.EventsPublisher = void 0;
|
|
15
|
+
exports.EventsPublisher = exports.describeError = void 0;
|
|
17
16
|
const common_1 = require("@nestjs/common");
|
|
18
17
|
const rxjs_1 = require("rxjs");
|
|
18
|
+
const events_1 = require("../events");
|
|
19
19
|
const logger_1 = require("../logger");
|
|
20
20
|
const rabbit_options_1 = require("./rabbit.options");
|
|
21
|
-
const
|
|
21
|
+
const describeError = (error) => error?.message ??
|
|
22
22
|
error?.err?.message ??
|
|
23
23
|
(typeof error === 'string' ? error : JSON.stringify(error));
|
|
24
|
-
|
|
24
|
+
exports.describeError = describeError;
|
|
25
|
+
const GROUP = 'EventsPublisher';
|
|
26
|
+
let EventsPublisher = class EventsPublisher {
|
|
25
27
|
options;
|
|
26
|
-
|
|
27
|
-
clients = new Map();
|
|
28
|
+
client;
|
|
28
29
|
logger;
|
|
29
30
|
brokerStatus;
|
|
30
|
-
constructor(options,
|
|
31
|
+
constructor(options, client, logger) {
|
|
31
32
|
this.options = options;
|
|
32
|
-
this.
|
|
33
|
+
this.client = client;
|
|
33
34
|
this.logger = logger ?? new logger_1.LoggerService();
|
|
34
|
-
this.brokerStatus =
|
|
35
|
+
this.brokerStatus = client ? 'connecting' : 'disabled';
|
|
35
36
|
}
|
|
36
37
|
get status() {
|
|
37
38
|
return this.brokerStatus;
|
|
38
39
|
}
|
|
40
|
+
get exchange() {
|
|
41
|
+
return this.options.exchange ?? rabbit_options_1.MERKALY_EXCHANGE;
|
|
42
|
+
}
|
|
39
43
|
onApplicationBootstrap() {
|
|
40
|
-
if (!this.
|
|
44
|
+
if (!this.client) {
|
|
41
45
|
return this.logger.warn({
|
|
42
|
-
group:
|
|
43
|
-
message: 'RabbitMQ url is not set: publishing disabled, every
|
|
44
|
-
type: 'event',
|
|
46
|
+
group: GROUP,
|
|
47
|
+
message: 'RabbitMQ url is not set: publishing disabled, every emit will fail',
|
|
45
48
|
});
|
|
46
49
|
}
|
|
47
|
-
|
|
48
|
-
client.status?.subscribe((status) => {
|
|
50
|
+
this.client.status?.subscribe((status) => {
|
|
49
51
|
this.brokerStatus = status === 'connected' ? 'connected' : 'disconnected';
|
|
50
52
|
});
|
|
51
|
-
return client.connect().catch((error) => {
|
|
53
|
+
return this.client.connect().catch((error) => {
|
|
52
54
|
this.brokerStatus = 'disconnected';
|
|
53
|
-
this.logger.error({
|
|
54
|
-
group: EventsPublisher_1.name,
|
|
55
|
-
message: `Broker unreachable: ${describe(error)}`,
|
|
56
|
-
type: 'event',
|
|
57
|
-
});
|
|
55
|
+
this.logger.error({ group: GROUP, message: `Broker unreachable: ${(0, exports.describeError)(error)}` });
|
|
58
56
|
});
|
|
59
57
|
}
|
|
60
58
|
onApplicationShutdown() {
|
|
61
|
-
return
|
|
59
|
+
return this.client?.close();
|
|
62
60
|
}
|
|
63
|
-
|
|
64
|
-
if (!this.
|
|
61
|
+
emit(type, data, meta) {
|
|
62
|
+
if (!this.client) {
|
|
65
63
|
return Promise.reject(new common_1.ServiceUnavailableException('Event broker is not configured'));
|
|
66
64
|
}
|
|
67
|
-
const
|
|
65
|
+
const event = (0, events_1.createMerkalyEvent)(this.options.producer, type, data, meta);
|
|
68
66
|
const ms = this.options.timeout ?? rabbit_options_1.DEFAULT_PUBLISH_TIMEOUT;
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
const line = `${type} (${event.id}) -> ${this.exchange}`;
|
|
68
|
+
return (0, rxjs_1.lastValueFrom)(this.client.emit(type, event).pipe((0, rxjs_1.timeout)(ms)), {
|
|
69
|
+
defaultValue: undefined,
|
|
70
|
+
})
|
|
71
|
+
.then(() => {
|
|
72
|
+
this.logger.log({ group: GROUP, message: line });
|
|
73
|
+
return event;
|
|
74
|
+
})
|
|
73
75
|
.catch((error) => {
|
|
74
|
-
this.logger.error({
|
|
75
|
-
|
|
76
|
-
message: `${pattern} (${event.id}) -> ${queue} failed: ${describe(error)}`,
|
|
77
|
-
});
|
|
78
|
-
throw new common_1.ServiceUnavailableException(`Could not publish ${pattern}`);
|
|
76
|
+
this.logger.error({ group: GROUP, message: `${line} failed: ${(0, exports.describeError)(error)}` });
|
|
77
|
+
throw new common_1.ServiceUnavailableException(`Could not publish ${type}`);
|
|
79
78
|
});
|
|
80
79
|
}
|
|
81
|
-
client(queue) {
|
|
82
|
-
if (!this.clients.has(queue))
|
|
83
|
-
this.clients.set(queue, this.factory(queue));
|
|
84
|
-
return this.clients.get(queue);
|
|
85
|
-
}
|
|
86
80
|
};
|
|
87
81
|
exports.EventsPublisher = EventsPublisher;
|
|
88
|
-
exports.EventsPublisher = EventsPublisher =
|
|
82
|
+
exports.EventsPublisher = EventsPublisher = __decorate([
|
|
89
83
|
(0, common_1.Injectable)(),
|
|
90
84
|
__param(0, (0, common_1.Inject)(rabbit_options_1.RABBIT_OPTIONS)),
|
|
91
|
-
__param(1, (0, common_1.Inject)(rabbit_options_1.
|
|
85
|
+
__param(1, (0, common_1.Inject)(rabbit_options_1.RABBIT_CLIENT)),
|
|
92
86
|
__param(2, (0, common_1.Optional)()),
|
|
93
87
|
__metadata("design:paramtypes", [Object, Object, logger_1.LoggerService])
|
|
94
88
|
], EventsPublisher);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"events.publisher.js","sourceRoot":"","sources":["../../src/rabbit/events.publisher.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"events.publisher.js","sourceRoot":"","sources":["../../src/rabbit/events.publisher.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAOwB;AACxB,+BAA8C;AAC9C,sCAMmB;AACnB,sCAA0C;AAC1C,qDAQ0B;AAEnB,MAAM,aAAa,GAAG,CAAC,KAAU,EAAU,EAAE,CAClD,KAAK,EAAE,OAAO;IACd,KAAK,EAAE,GAAG,EAAE,OAAO;IACnB,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;AAHjD,QAAA,aAAa,iBAGoC;AAE9D,MAAM,KAAK,GAAG,iBAAiB,CAAC;AAGzB,IAAM,eAAe,GAArB,MAAM,eAAe;IAKiB;IACD;IALzB,MAAM,CAAgB;IAC/B,YAAY,CAAe;IAEnC,YAC2C,OAA4B,EAC7B,MAA2B,EACvD,MAAsB;QAFO,YAAO,GAAP,OAAO,CAAqB;QAC7B,WAAM,GAAN,MAAM,CAAqB;QAGnE,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,IAAI,sBAAa,EAAE,CAAC;QAC5C,IAAI,CAAC,YAAY,GAAG,MAAM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1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rabbit/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rabbit/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC"}
|
package/dist/rabbit/index.js
CHANGED
|
@@ -14,6 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./events.consumer"), exports);
|
|
17
18
|
__exportStar(require("./events.publisher"), exports);
|
|
18
19
|
__exportStar(require("./rabbit.consumer"), exports);
|
|
19
20
|
__exportStar(require("./rabbit.module"), exports);
|
package/dist/rabbit/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rabbit/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,oDAAkC;AAClC,kDAAgC;AAChC,mDAAiC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/rabbit/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,qDAAmC;AACnC,oDAAkC;AAClC,kDAAgC;AAChC,mDAAiC"}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
prefetchCount?: number;
|
|
7
|
-
}
|
|
1
|
+
import { RmqOptions } from '@nestjs/microservices';
|
|
2
|
+
import { MerkalyEventType } from '../events';
|
|
3
|
+
import { RabbitConsumerOptions } from './rabbit.options';
|
|
4
|
+
export declare const retryQueue: (queue: string) => string;
|
|
5
|
+
export declare const deadLetterQueue: (queue: string) => string;
|
|
8
6
|
export declare function rabbitConsumerOptions(options: RabbitConsumerOptions): RmqOptions;
|
|
9
|
-
export declare
|
|
10
|
-
export declare
|
|
11
|
-
export declare function nackMessage(context: RmqContext, requeue?: boolean): void;
|
|
7
|
+
export declare function assertConsumerTopology(options: RabbitConsumerOptions): Promise<void>;
|
|
8
|
+
export declare const OnMerkalyEvent: (...types: MerkalyEventType[]) => MethodDecorator;
|
|
12
9
|
//# sourceMappingURL=rabbit.consumer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rabbit.consumer.d.ts","sourceRoot":"","sources":["../../src/rabbit/rabbit.consumer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,UAAU,
|
|
1
|
+
{"version":3,"file":"rabbit.consumer.d.ts","sourceRoot":"","sources":["../../src/rabbit/rabbit.consumer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,UAAU,EAAa,MAAM,uBAAuB,CAAC;AAE5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAyC,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAIhG,eAAO,MAAM,UAAU,GAAI,OAAO,MAAM,WAAqB,CAAC;AAE9D,eAAO,MAAM,eAAe,GAAI,OAAO,MAAM,WAAmB,CAAC;AAGjE,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,qBAAqB,GAAG,UAAU,CAuBhF;AAGD,wBAAsB,sBAAsB,CAAC,OAAO,EAAE,qBAAqB,iBAgB1E;AAGD,eAAO,MAAM,cAAc,GAAI,GAAG,OAAO,gBAAgB,EAAE,oBAAuC,CAAC"}
|
|
@@ -1,30 +1,55 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.OnMerkalyEvent = exports.deadLetterQueue = exports.retryQueue = void 0;
|
|
4
4
|
exports.rabbitConsumerOptions = rabbitConsumerOptions;
|
|
5
|
-
exports.
|
|
6
|
-
exports.nackMessage = nackMessage;
|
|
5
|
+
exports.assertConsumerTopology = assertConsumerTopology;
|
|
7
6
|
const microservices_1 = require("@nestjs/microservices");
|
|
7
|
+
const amqplib_1 = require("amqplib");
|
|
8
|
+
const rabbit_options_1 = require("./rabbit.options");
|
|
9
|
+
const WEEK = 7 * 24 * 60 * 60 * 1000;
|
|
10
|
+
const retryQueue = (queue) => `${queue}.retry`;
|
|
11
|
+
exports.retryQueue = retryQueue;
|
|
12
|
+
const deadLetterQueue = (queue) => `${queue}.dlq`;
|
|
13
|
+
exports.deadLetterQueue = deadLetterQueue;
|
|
8
14
|
function rabbitConsumerOptions(options) {
|
|
9
15
|
return {
|
|
10
16
|
transport: microservices_1.Transport.RMQ,
|
|
11
17
|
options: {
|
|
12
18
|
urls: [options.url],
|
|
13
19
|
queue: options.queue,
|
|
14
|
-
|
|
20
|
+
exchange: options.exchange ?? rabbit_options_1.MERKALY_EXCHANGE,
|
|
21
|
+
exchangeType: 'topic',
|
|
22
|
+
wildcards: true,
|
|
15
23
|
noAck: false,
|
|
16
24
|
prefetchCount: options.prefetchCount ?? 10,
|
|
25
|
+
queueOptions: {
|
|
26
|
+
durable: true,
|
|
27
|
+
arguments: {
|
|
28
|
+
'x-message-ttl': WEEK,
|
|
29
|
+
'x-max-length': 10_000,
|
|
30
|
+
'x-overflow': 'drop-head',
|
|
31
|
+
'x-dead-letter-exchange': '',
|
|
32
|
+
'x-dead-letter-routing-key': (0, exports.deadLetterQueue)(options.queue),
|
|
33
|
+
},
|
|
34
|
+
},
|
|
17
35
|
},
|
|
18
36
|
};
|
|
19
37
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
channel.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
38
|
+
async function assertConsumerTopology(options) {
|
|
39
|
+
const connection = await (0, amqplib_1.connect)(options.url);
|
|
40
|
+
const channel = await connection.createChannel();
|
|
41
|
+
await channel.assertQueue((0, exports.deadLetterQueue)(options.queue), { durable: true });
|
|
42
|
+
await channel.assertQueue((0, exports.retryQueue)(options.queue), {
|
|
43
|
+
durable: true,
|
|
44
|
+
arguments: {
|
|
45
|
+
'x-message-ttl': options.retryDelay ?? rabbit_options_1.DEFAULT_RETRY_DELAY,
|
|
46
|
+
'x-dead-letter-exchange': '',
|
|
47
|
+
'x-dead-letter-routing-key': options.queue,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
await channel.close();
|
|
51
|
+
await connection.close();
|
|
29
52
|
}
|
|
53
|
+
const OnMerkalyEvent = (...types) => (0, microservices_1.EventPattern)(types, microservices_1.Transport.RMQ);
|
|
54
|
+
exports.OnMerkalyEvent = OnMerkalyEvent;
|
|
30
55
|
//# sourceMappingURL=rabbit.consumer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rabbit.consumer.js","sourceRoot":"","sources":["../../src/rabbit/rabbit.consumer.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"rabbit.consumer.js","sourceRoot":"","sources":["../../src/rabbit/rabbit.consumer.ts"],"names":[],"mappings":";;;AAYA,sDAuBC;AAGD,wDAgBC;AAtDD,yDAA4E;AAC5E,qCAAkC;AAElC,qDAAgG;AAEhG,MAAM,IAAI,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAE9B,MAAM,UAAU,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,GAAG,KAAK,QAAQ,CAAC;AAAjD,QAAA,UAAU,cAAuC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BAAwB,EAAE,EAAE;YAC5B,2BAA2B,EAAE,OAAO,CAAC,KAAK;SAC3C;KACF,CAAC,CAAC;IAEH,MAAM,OAAO,CAAC,KAAK,EAAE,CAAC;IACtB,MAAM,UAAU,CAAC,KAAK,EAAE,CAAC;AAC3B,CAAC;AAGM,MAAM,cAAc,GAAG,CAAC,GAAG,KAAyB,EAAE,EAAE,CAAC,IAAA,4BAAY,EAAC,KAAK,EAAE,yBAAS,CAAC,GAAG,CAAC,CAAC;AAAtF,QAAA,cAAc,kBAAwE"}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { DynamicModule } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
export declare function
|
|
2
|
+
import { EventsConsumerAsyncOptions, EventsConsumerOptions, RabbitClient, RabbitModuleAsyncOptions, RabbitModuleOptions } from './rabbit.options';
|
|
3
|
+
export declare function createRabbitClient(options: RabbitModuleOptions): RabbitClient | null;
|
|
4
4
|
export declare class RabbitModule {
|
|
5
5
|
static forRoot(options: RabbitModuleOptions): DynamicModule;
|
|
6
6
|
static forRootAsync(options: RabbitModuleAsyncOptions): DynamicModule;
|
|
7
|
+
static forConsumer(options: EventsConsumerOptions): DynamicModule;
|
|
8
|
+
static forConsumerAsync(options: EventsConsumerAsyncOptions): DynamicModule;
|
|
7
9
|
}
|
|
8
10
|
//# sourceMappingURL=rabbit.module.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rabbit.module.d.ts","sourceRoot":"","sources":["../../src/rabbit/rabbit.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAA4B,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"rabbit.module.d.ts","sourceRoot":"","sources":["../../src/rabbit/rabbit.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAA4B,MAAM,gBAAgB,CAAC;AAIzE,OAAO,EAEL,0BAA0B,EAC1B,qBAAqB,EAIrB,YAAY,EACZ,wBAAwB,EACxB,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAG1B,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,mBAAmB,GAAG,YAAY,GAAG,IAAI,CAiBpF;AAQD,qBAEa,YAAY;IACvB,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa;IAQ3D,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE,wBAAwB,GAAG,aAAa;IAarE,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,qBAAqB,GAAG,aAAa;IAQjE,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,0BAA0B,GAAG,aAAa;CAW5E"}
|
|
@@ -8,37 +8,39 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
var RabbitModule_1;
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.RabbitModule = void 0;
|
|
11
|
-
exports.
|
|
11
|
+
exports.createRabbitClient = createRabbitClient;
|
|
12
12
|
const common_1 = require("@nestjs/common");
|
|
13
13
|
const microservices_1 = require("@nestjs/microservices");
|
|
14
|
+
const events_consumer_1 = require("./events.consumer");
|
|
14
15
|
const events_publisher_1 = require("./events.publisher");
|
|
15
16
|
const rabbit_options_1 = require("./rabbit.options");
|
|
16
|
-
function
|
|
17
|
-
|
|
18
|
-
if (!url) {
|
|
17
|
+
function createRabbitClient(options) {
|
|
18
|
+
if (!options.url) {
|
|
19
19
|
if (options.required)
|
|
20
20
|
throw new Error('RabbitMQ url is required');
|
|
21
21
|
return null;
|
|
22
22
|
}
|
|
23
|
-
return
|
|
23
|
+
return microservices_1.ClientProxyFactory.create({
|
|
24
24
|
transport: microservices_1.Transport.RMQ,
|
|
25
|
-
options: {
|
|
25
|
+
options: {
|
|
26
|
+
urls: [options.url],
|
|
27
|
+
exchange: options.exchange ?? rabbit_options_1.MERKALY_EXCHANGE,
|
|
28
|
+
exchangeType: 'topic',
|
|
29
|
+
wildcards: true,
|
|
30
|
+
persistent: true,
|
|
31
|
+
},
|
|
26
32
|
});
|
|
27
33
|
}
|
|
28
|
-
const
|
|
29
|
-
provide: rabbit_options_1.
|
|
30
|
-
useFactory:
|
|
34
|
+
const clientProvider = {
|
|
35
|
+
provide: rabbit_options_1.RABBIT_CLIENT,
|
|
36
|
+
useFactory: createRabbitClient,
|
|
31
37
|
inject: [rabbit_options_1.RABBIT_OPTIONS],
|
|
32
38
|
};
|
|
33
39
|
let RabbitModule = RabbitModule_1 = class RabbitModule {
|
|
34
40
|
static forRoot(options) {
|
|
35
41
|
return {
|
|
36
42
|
module: RabbitModule_1,
|
|
37
|
-
providers: [
|
|
38
|
-
{ provide: rabbit_options_1.RABBIT_OPTIONS, useValue: options },
|
|
39
|
-
clientFactoryProvider,
|
|
40
|
-
events_publisher_1.EventsPublisher,
|
|
41
|
-
],
|
|
43
|
+
providers: [{ provide: rabbit_options_1.RABBIT_OPTIONS, useValue: options }, clientProvider, events_publisher_1.EventsPublisher],
|
|
42
44
|
exports: [events_publisher_1.EventsPublisher],
|
|
43
45
|
};
|
|
44
46
|
}
|
|
@@ -48,12 +50,30 @@ let RabbitModule = RabbitModule_1 = class RabbitModule {
|
|
|
48
50
|
imports: options.imports ?? [],
|
|
49
51
|
providers: [
|
|
50
52
|
{ provide: rabbit_options_1.RABBIT_OPTIONS, useFactory: options.useFactory, inject: options.inject ?? [] },
|
|
51
|
-
|
|
53
|
+
clientProvider,
|
|
52
54
|
events_publisher_1.EventsPublisher,
|
|
53
55
|
],
|
|
54
56
|
exports: [events_publisher_1.EventsPublisher],
|
|
55
57
|
};
|
|
56
58
|
}
|
|
59
|
+
static forConsumer(options) {
|
|
60
|
+
return {
|
|
61
|
+
module: RabbitModule_1,
|
|
62
|
+
providers: [{ provide: rabbit_options_1.CONSUMER_OPTIONS, useValue: options }, events_consumer_1.EventsConsumer],
|
|
63
|
+
exports: [events_consumer_1.EventsConsumer],
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
static forConsumerAsync(options) {
|
|
67
|
+
return {
|
|
68
|
+
module: RabbitModule_1,
|
|
69
|
+
imports: options.imports ?? [],
|
|
70
|
+
providers: [
|
|
71
|
+
{ provide: rabbit_options_1.CONSUMER_OPTIONS, useFactory: options.useFactory, inject: options.inject ?? [] },
|
|
72
|
+
events_consumer_1.EventsConsumer,
|
|
73
|
+
],
|
|
74
|
+
exports: [events_consumer_1.EventsConsumer],
|
|
75
|
+
};
|
|
76
|
+
}
|
|
57
77
|
};
|
|
58
78
|
exports.RabbitModule = RabbitModule;
|
|
59
79
|
exports.RabbitModule = RabbitModule = RabbitModule_1 = __decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rabbit.module.js","sourceRoot":"","sources":["../../src/rabbit/rabbit.module.ts"],"names":[],"mappings":";;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"rabbit.module.js","sourceRoot":"","sources":["../../src/rabbit/rabbit.module.ts"],"names":[],"mappings":";;;;;;;;;;AAiBA,gDAiBC;AAlCD,2CAAyE;AACzE,yDAAsE;AACtE,uDAAmD;AACnD,yDAAqD;AACrD,qDAU0B;AAG1B,SAAgB,kBAAkB,CAAC,OAA4B;IAC7D,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC;QACjB,IAAI,OAAO,CAAC,QAAQ;YAAE,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAElE,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO,kCAAkB,CAAC,MAAM,CAAC;QAC/B,SAAS,EAAE,yBAAS,CAAC,GAAG;QACxB,OAAO,EAAE;YACP,IAAI,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC;YACnB,QAAQ,EAAE,OAAO,CAAC,QAAQ,IAAI,iCAAgB;YAC9C,YAAY,EAAE,OAAO;YACrB,SAAS,EAAE,IAAI;YACf,UAAU,EAAE,IAAI;SACjB;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,cAAc,GA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AI,EAAE,EAAE;gBAC3F,gCAAc;aACf;YACD,OAAO,EAAE,CAAC,gCAAc,CAAC;SAC1B,CAAC;IACJ,CAAC;CACF,CAAA;AAzCY,oCAAY;uBAAZ,YAAY;IAFxB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,YAAY,CAyCxB"}
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import { ModuleMetadata } from '@nestjs/common';
|
|
2
2
|
import { ClientProxy } from '@nestjs/microservices';
|
|
3
|
+
import { MerkalyEvent, MerkalyProducer } from '../events';
|
|
3
4
|
export declare const RABBIT_OPTIONS: unique symbol;
|
|
4
|
-
export declare const
|
|
5
|
+
export declare const RABBIT_CLIENT: unique symbol;
|
|
6
|
+
export declare const CONSUMER_OPTIONS: unique symbol;
|
|
7
|
+
export declare const MERKALY_EXCHANGE = "merkaly.events";
|
|
8
|
+
export declare const DEFAULT_PUBLISH_TIMEOUT = 5000;
|
|
9
|
+
export declare const DEFAULT_RETRIES = 5;
|
|
10
|
+
export declare const DEFAULT_RETRY_DELAY = 30000;
|
|
5
11
|
export interface RabbitModuleOptions {
|
|
6
12
|
url?: string;
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
producer: MerkalyProducer;
|
|
14
|
+
exchange?: string;
|
|
9
15
|
timeout?: number;
|
|
10
16
|
required?: boolean;
|
|
11
17
|
}
|
|
@@ -13,7 +19,21 @@ export interface RabbitModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'
|
|
|
13
19
|
useFactory: (...args: any[]) => RabbitModuleOptions | Promise<RabbitModuleOptions>;
|
|
14
20
|
inject?: any[];
|
|
15
21
|
}
|
|
16
|
-
export
|
|
22
|
+
export interface RabbitConsumerOptions {
|
|
23
|
+
url: string;
|
|
24
|
+
queue: string;
|
|
25
|
+
exchange?: string;
|
|
26
|
+
prefetchCount?: number;
|
|
27
|
+
retries?: number;
|
|
28
|
+
retryDelay?: number;
|
|
29
|
+
}
|
|
30
|
+
export interface EventsConsumerOptions extends Pick<RabbitConsumerOptions, 'queue' | 'retries'> {
|
|
31
|
+
around?: (event: MerkalyEvent, next: () => Promise<unknown>) => Promise<unknown>;
|
|
32
|
+
}
|
|
33
|
+
export interface EventsConsumerAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
|
|
34
|
+
useFactory: (...args: any[]) => EventsConsumerOptions | Promise<EventsConsumerOptions>;
|
|
35
|
+
inject?: any[];
|
|
36
|
+
}
|
|
37
|
+
export type RabbitClient = ClientProxy;
|
|
17
38
|
export type BrokerStatus = 'disabled' | 'connecting' | 'connected' | 'disconnected';
|
|
18
|
-
export declare const DEFAULT_PUBLISH_TIMEOUT = 5000;
|
|
19
39
|
//# sourceMappingURL=rabbit.options.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rabbit.options.d.ts","sourceRoot":"","sources":["../../src/rabbit/rabbit.options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"rabbit.options.d.ts","sourceRoot":"","sources":["../../src/rabbit/rabbit.options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAE1D,eAAO,MAAM,cAAc,eAA2B,CAAC;AACvD,eAAO,MAAM,aAAa,eAA0B,CAAC;AACrD,eAAO,MAAM,gBAAgB,eAA6B,CAAC;AAE3D,eAAO,MAAM,gBAAgB,mBAAmB,CAAC;AACjD,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAC5C,eAAO,MAAM,eAAe,IAAI,CAAC;AACjC,eAAO,MAAM,mBAAmB,QAAS,CAAC;AAE1C,MAAM,WAAW,mBA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 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_PUBLISH_TIMEOUT = exports.
|
|
3
|
+
exports.DEFAULT_RETRY_DELAY = exports.DEFAULT_RETRIES = exports.DEFAULT_PUBLISH_TIMEOUT = exports.MERKALY_EXCHANGE = exports.CONSUMER_OPTIONS = exports.RABBIT_CLIENT = exports.RABBIT_OPTIONS = void 0;
|
|
4
4
|
exports.RABBIT_OPTIONS = Symbol('RABBIT_OPTIONS');
|
|
5
|
-
exports.
|
|
5
|
+
exports.RABBIT_CLIENT = Symbol('RABBIT_CLIENT');
|
|
6
|
+
exports.CONSUMER_OPTIONS = Symbol('CONSUMER_OPTIONS');
|
|
7
|
+
exports.MERKALY_EXCHANGE = 'merkaly.events';
|
|
6
8
|
exports.DEFAULT_PUBLISH_TIMEOUT = 5000;
|
|
9
|
+
exports.DEFAULT_RETRIES = 5;
|
|
10
|
+
exports.DEFAULT_RETRY_DELAY = 30_000;
|
|
7
11
|
//# sourceMappingURL=rabbit.options.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rabbit.options.js","sourceRoot":"","sources":["../../src/rabbit/rabbit.options.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"rabbit.options.js","sourceRoot":"","sources":["../../src/rabbit/rabbit.options.ts"],"names":[],"mappings":";;;AAIa,QAAA,cAAc,GAAG,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAC1C,QAAA,aAAa,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;AACxC,QAAA,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;AAE9C,QAAA,gBAAgB,GAAG,gBAAgB,CAAC;AACpC,QAAA,uBAAuB,GAAG,IAAI,CAAC;AAC/B,QAAA,eAAe,GAAG,CAAC,CAAC;AACpB,QAAA,mBAAmB,GAAG,MAAM,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkaly/nest",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Shared NestJS runtime and tooling for Merkaly backend apps: pinned versions plus RabbitMQ events, logger, filters, config and signatures",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"eslint-config-prettier": "^10.1.8",
|
|
46
46
|
"eslint-plugin-import": "^2.32.0",
|
|
47
47
|
"eslint-plugin-prettier": "^5.5.4",
|
|
48
|
-
"express": "^5.
|
|
48
|
+
"express": "^5.3.0",
|
|
49
49
|
"globals": "^17.13.0",
|
|
50
50
|
"nestjs-console": "^10.0.0",
|
|
51
51
|
"prettier": "^3.9.3",
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type WebhookSource = 'asaas' | 'stripe' | 'auth0' | 'aliexpress';
|
|
2
|
-
export interface WebhookEvent<T = unknown> {
|
|
3
|
-
id: string;
|
|
4
|
-
source: WebhookSource;
|
|
5
|
-
type: string;
|
|
6
|
-
receivedAt: string;
|
|
7
|
-
organization?: string;
|
|
8
|
-
data: T;
|
|
9
|
-
}
|
|
10
|
-
export declare function toPattern(source: WebhookSource, event: string): string;
|
|
11
|
-
export declare function createWebhookEvent<T>(event: Omit<WebhookEvent<T>, 'receivedAt'>): WebhookEvent<T>;
|
|
12
|
-
//# sourceMappingURL=webhook-event.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webhook-event.d.ts","sourceRoot":"","sources":["../../src/events/webhook-event.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,GAAG,YAAY,CAAC;AAExE,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO;IACvC,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,aAAa,CAAC;IACtB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,EAAE,CAAC,CAAC;CACT;AAED,wBAAgB,SAAS,CAAC,MAAM,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,UAE7D;AAED,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAEjG"}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.toPattern = toPattern;
|
|
4
|
-
exports.createWebhookEvent = createWebhookEvent;
|
|
5
|
-
function toPattern(source, event) {
|
|
6
|
-
return `${source}.${event.toLowerCase()}`;
|
|
7
|
-
}
|
|
8
|
-
function createWebhookEvent(event) {
|
|
9
|
-
return { ...event, receivedAt: new Date().toISOString() };
|
|
10
|
-
}
|
|
11
|
-
//# sourceMappingURL=webhook-event.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webhook-event.js","sourceRoot":"","sources":["../../src/events/webhook-event.ts"],"names":[],"mappings":";;AAWA,8BAEC;AAED,gDAEC;AAND,SAAgB,SAAS,CAAC,MAAqB,EAAE,KAAa;IAC5D,OAAO,GAAG,MAAM,IAAI,KAAK,CAAC,WAAW,EAAE,EAAE,CAAC;AAC5C,CAAC;AAED,SAAgB,kBAAkB,CAAI,KAA0C;IAC9E,OAAO,EAAE,GAAG,KAAK,EAAE,UAAU,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE,CAAC;AAC5D,CAAC"}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
export declare const WebhookPatterns: {
|
|
2
|
-
readonly asaas: {
|
|
3
|
-
readonly PAYMENT_CREATED: "asaas.payment_created";
|
|
4
|
-
readonly PAYMENT_AUTHORIZED: "asaas.payment_authorized";
|
|
5
|
-
readonly PAYMENT_UPDATED: "asaas.payment_updated";
|
|
6
|
-
readonly PAYMENT_CONFIRMED: "asaas.payment_confirmed";
|
|
7
|
-
readonly PAYMENT_RECEIVED: "asaas.payment_received";
|
|
8
|
-
readonly PAYMENT_OVERDUE: "asaas.payment_overdue";
|
|
9
|
-
readonly PAYMENT_DELETED: "asaas.payment_deleted";
|
|
10
|
-
readonly PAYMENT_REFUNDED: "asaas.payment_refunded";
|
|
11
|
-
readonly PAYMENT_PARTIALLY_REFUNDED: "asaas.payment_partially_refunded";
|
|
12
|
-
readonly PAYMENT_CHARGEBACK_REQUESTED: "asaas.payment_chargeback_requested";
|
|
13
|
-
readonly PAYMENT_CHARGEBACK_DISPUTE: "asaas.payment_chargeback_dispute";
|
|
14
|
-
readonly ACCOUNT_APPROVED: "asaas.account_status_general_approval_approved";
|
|
15
|
-
readonly ACCOUNT_REJECTED: "asaas.account_status_general_approval_rejected";
|
|
16
|
-
};
|
|
17
|
-
readonly stripe: {
|
|
18
|
-
readonly SUBSCRIPTION_CREATED: "stripe.customer.subscription.created";
|
|
19
|
-
readonly SUBSCRIPTION_UPDATED: "stripe.customer.subscription.updated";
|
|
20
|
-
readonly SUBSCRIPTION_DELETED: "stripe.customer.subscription.deleted";
|
|
21
|
-
readonly INVOICE_PAID: "stripe.invoice.paid";
|
|
22
|
-
readonly INVOICE_PAYMENT_FAILED: "stripe.invoice.payment_failed";
|
|
23
|
-
};
|
|
24
|
-
readonly auth0: {
|
|
25
|
-
readonly SUCCESS_SIGNUP: "auth0.success_signup";
|
|
26
|
-
readonly SUCCESS_LOGIN: "auth0.success_login";
|
|
27
|
-
readonly FAILED_LOGIN: "auth0.failed_login";
|
|
28
|
-
readonly USER_DELETED: "auth0.user_deleted";
|
|
29
|
-
readonly BLOCKED_ACCOUNT: "auth0.blocked_account";
|
|
30
|
-
readonly USER_LOGIN: "auth0.user_login";
|
|
31
|
-
};
|
|
32
|
-
readonly aliexpress: {
|
|
33
|
-
readonly DS_ORDER_STATUS_UPDATE: "aliexpress.dropshipper_order_status_update";
|
|
34
|
-
readonly WAYBILL_CHANGED: "aliexpress.waybill_changed";
|
|
35
|
-
readonly PRODUCT_DELETED: "aliexpress.product_deleted";
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
export declare const AliexpressOrderStatus: {
|
|
39
|
-
readonly CREATED: "OrderCreated";
|
|
40
|
-
readonly PAYMENT_AUTHORIZED: "PaymentAuthorized";
|
|
41
|
-
readonly PAYMENT_FAILED: "paymentFailedEvent";
|
|
42
|
-
readonly SHIPPED: "OrderShipped";
|
|
43
|
-
readonly CONFIRMED: "OrderConfirmed";
|
|
44
|
-
readonly CLOSED: "OrderClosed";
|
|
45
|
-
};
|
|
46
|
-
type Values<T> = T[keyof T];
|
|
47
|
-
export type WebhookPattern = Values<{
|
|
48
|
-
[S in keyof typeof WebhookPatterns]: Values<(typeof WebhookPatterns)[S]>;
|
|
49
|
-
}>;
|
|
50
|
-
export type AliexpressOrderStatus = Values<typeof AliexpressOrderStatus>;
|
|
51
|
-
export {};
|
|
52
|
-
//# sourceMappingURL=webhook-patterns.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webhook-patterns.d.ts","sourceRoot":"","sources":["../../src/events/webhook-patterns.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoClB,CAAC;AAEX,eAAO,MAAM,qBAAqB;;;;;;;CAOxB,CAAC;AAEX,KAAK,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAE5B,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC;KACjC,CAAC,IAAI,MAAM,OAAO,eAAe,GAAG,MAAM,CAAC,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;CACzE,CAAC,CAAC;AAEH,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,OAAO,qBAAqB,CAAC,CAAC"}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AliexpressOrderStatus = exports.WebhookPatterns = void 0;
|
|
4
|
-
exports.WebhookPatterns = {
|
|
5
|
-
asaas: {
|
|
6
|
-
PAYMENT_CREATED: 'asaas.payment_created',
|
|
7
|
-
PAYMENT_AUTHORIZED: 'asaas.payment_authorized',
|
|
8
|
-
PAYMENT_UPDATED: 'asaas.payment_updated',
|
|
9
|
-
PAYMENT_CONFIRMED: 'asaas.payment_confirmed',
|
|
10
|
-
PAYMENT_RECEIVED: 'asaas.payment_received',
|
|
11
|
-
PAYMENT_OVERDUE: 'asaas.payment_overdue',
|
|
12
|
-
PAYMENT_DELETED: 'asaas.payment_deleted',
|
|
13
|
-
PAYMENT_REFUNDED: 'asaas.payment_refunded',
|
|
14
|
-
PAYMENT_PARTIALLY_REFUNDED: 'asaas.payment_partially_refunded',
|
|
15
|
-
PAYMENT_CHARGEBACK_REQUESTED: 'asaas.payment_chargeback_requested',
|
|
16
|
-
PAYMENT_CHARGEBACK_DISPUTE: 'asaas.payment_chargeback_dispute',
|
|
17
|
-
ACCOUNT_APPROVED: 'asaas.account_status_general_approval_approved',
|
|
18
|
-
ACCOUNT_REJECTED: 'asaas.account_status_general_approval_rejected',
|
|
19
|
-
},
|
|
20
|
-
stripe: {
|
|
21
|
-
SUBSCRIPTION_CREATED: 'stripe.customer.subscription.created',
|
|
22
|
-
SUBSCRIPTION_UPDATED: 'stripe.customer.subscription.updated',
|
|
23
|
-
SUBSCRIPTION_DELETED: 'stripe.customer.subscription.deleted',
|
|
24
|
-
INVOICE_PAID: 'stripe.invoice.paid',
|
|
25
|
-
INVOICE_PAYMENT_FAILED: 'stripe.invoice.payment_failed',
|
|
26
|
-
},
|
|
27
|
-
auth0: {
|
|
28
|
-
SUCCESS_SIGNUP: 'auth0.success_signup',
|
|
29
|
-
SUCCESS_LOGIN: 'auth0.success_login',
|
|
30
|
-
FAILED_LOGIN: 'auth0.failed_login',
|
|
31
|
-
USER_DELETED: 'auth0.user_deleted',
|
|
32
|
-
BLOCKED_ACCOUNT: 'auth0.blocked_account',
|
|
33
|
-
USER_LOGIN: 'auth0.user_login',
|
|
34
|
-
},
|
|
35
|
-
aliexpress: {
|
|
36
|
-
DS_ORDER_STATUS_UPDATE: 'aliexpress.dropshipper_order_status_update',
|
|
37
|
-
WAYBILL_CHANGED: 'aliexpress.waybill_changed',
|
|
38
|
-
PRODUCT_DELETED: 'aliexpress.product_deleted',
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
exports.AliexpressOrderStatus = {
|
|
42
|
-
CREATED: 'OrderCreated',
|
|
43
|
-
PAYMENT_AUTHORIZED: 'PaymentAuthorized',
|
|
44
|
-
PAYMENT_FAILED: 'paymentFailedEvent',
|
|
45
|
-
SHIPPED: 'OrderShipped',
|
|
46
|
-
CONFIRMED: 'OrderConfirmed',
|
|
47
|
-
CLOSED: 'OrderClosed',
|
|
48
|
-
};
|
|
49
|
-
//# sourceMappingURL=webhook-patterns.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"webhook-patterns.js","sourceRoot":"","sources":["../../src/events/webhook-patterns.ts"],"names":[],"mappings":";;;AAAa,QAAA,eAAe,GAAG;IAC7B,KAAK,EAAE;QACL,eAAe,EAAE,uBAAuB;QACxC,kBAAkB,EAAE,0BAA0B;QAC9C,eAAe,EAAE,uBAAuB;QACxC,iBAAiB,EAAE,yBAAyB;QAC5C,gBAAgB,EAAE,wBAAwB;QAC1C,eAAe,EAAE,uBAAuB;QACxC,eAAe,EAAE,uBAAuB;QACxC,gBAAgB,EAAE,wBAAwB;QAC1C,0BAA0B,EAAE,kCAAkC;QAC9D,4BAA4B,EAAE,oCAAoC;QAClE,0BAA0B,EAAE,kCAAkC;QAC9D,gBAAgB,EAAE,gDAAgD;QAClE,gBAAgB,EAAE,gDAAgD;KACnE;IACD,MAAM,EAAE;QACN,oBAAoB,EAAE,sCAAsC;QAC5D,oBAAoB,EAAE,sCAAsC;QAC5D,oBAAoB,EAAE,sCAAsC;QAC5D,YAAY,EAAE,qBAAqB;QACnC,sBAAsB,EAAE,+BAA+B;KACxD;IACD,KAAK,EAAE;QACL,cAAc,EAAE,sBAAsB;QACtC,aAAa,EAAE,qBAAqB;QACpC,YAAY,EAAE,oBAAoB;QAClC,YAAY,EAAE,oBAAoB;QAClC,eAAe,EAAE,uBAAuB;QACxC,UAAU,EAAE,kBAAkB;KAC/B;IACD,UAAU,EAAE;QACV,sBAAsB,EAAE,4CAA4C;QACpE,eAAe,EAAE,4BAA4B;QAC7C,eAAe,EAAE,4BAA4B;KAC9C;CACO,CAAC;AAEE,QAAA,qBAAqB,GAAG;IACnC,OAAO,EAAE,cAAc;IACvB,kBAAkB,EAAE,mBAAmB;IACvC,cAAc,EAAE,oBAAoB;IACpC,OAAO,EAAE,cAAc;IACvB,SAAS,EAAE,gBAAgB;IAC3B,MAAM,EAAE,aAAa;CACb,CAAC"}
|