@open-mercato/webhooks 0.6.7-develop.6862.1.c11a64ce0a → 0.6.7
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/.turbo/turbo-build.log +1 -1
- package/dist/modules/webhooks/__integration__/TC-WEBHOOK-003.spec.js +0 -15
- package/dist/modules/webhooks/__integration__/TC-WEBHOOK-003.spec.js.map +2 -2
- package/dist/modules/webhooks/api/inbound/[endpointId]/route.js +7 -152
- package/dist/modules/webhooks/api/inbound/[endpointId]/route.js.map +3 -3
- package/dist/modules/webhooks/backend/webhooks/[id]/page.js +1 -2
- package/dist/modules/webhooks/backend/webhooks/[id]/page.js.map +2 -2
- package/dist/modules/webhooks/backend/webhooks/page.js +1 -2
- package/dist/modules/webhooks/backend/webhooks/page.js.map +2 -2
- package/dist/modules/webhooks/data/entities.js +1 -101
- package/dist/modules/webhooks/data/entities.js.map +2 -2
- package/dist/modules/webhooks/encryption.js +0 -7
- package/dist/modules/webhooks/encryption.js.map +2 -2
- package/dist/modules/webhooks/events.js +0 -2
- package/dist/modules/webhooks/events.js.map +2 -2
- package/dist/modules/webhooks/lib/queue.js +0 -47
- package/dist/modules/webhooks/lib/queue.js.map +2 -2
- package/dist/modules/webhooks/widgets/injection/integration-deliveries/widget.client.js +1 -2
- package/dist/modules/webhooks/widgets/injection/integration-deliveries/widget.client.js.map +2 -2
- package/generated/entities.ids.generated.ts +1 -3
- package/generated/entity-fields-registry.ts +0 -28
- package/package.json +11 -12
- package/src/modules/webhooks/__integration__/TC-WEBHOOK-003.spec.ts +0 -16
- package/src/modules/webhooks/api/inbound/[endpointId]/__tests__/route.test.ts +1 -99
- package/src/modules/webhooks/api/inbound/[endpointId]/route.ts +7 -178
- package/src/modules/webhooks/backend/webhooks/[id]/page.tsx +1 -2
- package/src/modules/webhooks/backend/webhooks/page.tsx +1 -2
- package/src/modules/webhooks/data/entities.ts +0 -84
- package/src/modules/webhooks/encryption.ts +0 -7
- package/src/modules/webhooks/events.ts +0 -2
- package/src/modules/webhooks/i18n/de.json +1 -1
- package/src/modules/webhooks/i18n/en.json +1 -1
- package/src/modules/webhooks/i18n/es.json +1 -1
- package/src/modules/webhooks/i18n/pl.json +1 -1
- package/src/modules/webhooks/lib/queue.ts +0 -60
- package/src/modules/webhooks/migrations/.snapshot-open-mercato.json +339 -908
- package/src/modules/webhooks/widgets/injection/integration-deliveries/widget.client.tsx +1 -2
- package/dist/modules/webhooks/extension-points.js +0 -15
- package/dist/modules/webhooks/extension-points.js.map +0 -7
- package/dist/modules/webhooks/generators.js +0 -57
- package/dist/modules/webhooks/generators.js.map +0 -7
- package/dist/modules/webhooks/lib/inbound-dispatch.js +0 -105
- package/dist/modules/webhooks/lib/inbound-dispatch.js.map +0 -7
- package/dist/modules/webhooks/lib/inbound-registry.js +0 -76
- package/dist/modules/webhooks/lib/inbound-registry.js.map +0 -7
- package/dist/modules/webhooks/lib/module-webhook-registry.js +0 -12
- package/dist/modules/webhooks/lib/module-webhook-registry.js.map +0 -7
- package/dist/modules/webhooks/migrations/Migration20260617141327_webhooks.js +0 -16
- package/dist/modules/webhooks/migrations/Migration20260617141327_webhooks.js.map +0 -7
- package/dist/modules/webhooks/workers/inbound-dispatch.js +0 -29
- package/dist/modules/webhooks/workers/inbound-dispatch.js.map +0 -7
- package/generated/entities/inbound_endpoint_config_entity/index.ts +0 -8
- package/generated/entities/webhook_ingestion_entity/index.ts +0 -16
- package/src/modules/webhooks/api/inbound/[endpointId]/__tests__/route.unify.test.ts +0 -149
- package/src/modules/webhooks/extension-points.ts +0 -12
- package/src/modules/webhooks/generators.ts +0 -57
- package/src/modules/webhooks/i18n/ko.json +0 -161
- package/src/modules/webhooks/lib/__tests__/inbound-dispatch.test.ts +0 -186
- package/src/modules/webhooks/lib/__tests__/inbound-registry.test.ts +0 -102
- package/src/modules/webhooks/lib/__tests__/module-webhook-registry.test.ts +0 -53
- package/src/modules/webhooks/lib/inbound-dispatch.ts +0 -147
- package/src/modules/webhooks/lib/inbound-registry.ts +0 -92
- package/src/modules/webhooks/lib/module-webhook-registry.ts +0 -33
- package/src/modules/webhooks/migrations/Migration20260617141327_webhooks.ts +0 -16
- package/src/modules/webhooks/workers/inbound-dispatch.ts +0 -31
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
-
import { findOneWithDecryption } from '@open-mercato/shared/lib/encryption/find'
|
|
3
|
-
import type {
|
|
4
|
-
WebhookHandlerContext,
|
|
5
|
-
WebhookHandlerPayload,
|
|
6
|
-
WebhookHandlerResult,
|
|
7
|
-
} from '@open-mercato/shared/lib/webhooks'
|
|
8
|
-
import { WebhookIngestionEntity } from '../data/entities'
|
|
9
|
-
import { emitWebhooksEvent } from '../events'
|
|
10
|
-
import { resolveWebhookHandlers } from './inbound-registry'
|
|
11
|
-
|
|
12
|
-
const MAX_HANDLER_RESULTS = 50
|
|
13
|
-
const MAX_ERROR_MESSAGE_LENGTH = 1024
|
|
14
|
-
|
|
15
|
-
export type InboundDispatchJob = {
|
|
16
|
-
ingestionId: string
|
|
17
|
-
sourceKey: string
|
|
18
|
-
eventType: string
|
|
19
|
-
tenantId: string
|
|
20
|
-
organizationId: string
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function truncateError(message: string): string {
|
|
24
|
-
return message.length > MAX_ERROR_MESSAGE_LENGTH
|
|
25
|
-
? `${message.slice(0, MAX_ERROR_MESSAGE_LENGTH)}…`
|
|
26
|
-
: message
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function moduleOfHandler(handlerId: string): string {
|
|
30
|
-
const separatorIndex = handlerId.indexOf(':')
|
|
31
|
-
return separatorIndex > 0 ? handlerId.slice(0, separatorIndex) : handlerId
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Process a queued inbound-dispatch job: load the ingestion, run every matching
|
|
36
|
-
* handler independently (a failing handler never blocks the others), record
|
|
37
|
-
* per-handler results, and emit lifecycle events. The payload and headers are
|
|
38
|
-
* read from the (decrypted) ingestion row rather than carried on the job, so the
|
|
39
|
-
* queue never stores the raw webhook body. Idempotent — a job whose ingestion is
|
|
40
|
-
* already `processed` returns early, and a retry after a partial failure re-runs
|
|
41
|
-
* only the handlers that have not already succeeded.
|
|
42
|
-
*/
|
|
43
|
-
export async function processInboundDispatchJob(
|
|
44
|
-
em: EntityManager,
|
|
45
|
-
job: InboundDispatchJob,
|
|
46
|
-
ctx: WebhookHandlerContext,
|
|
47
|
-
): Promise<void> {
|
|
48
|
-
const startedAtMs = Date.now()
|
|
49
|
-
const ingestion = await findOneWithDecryption(
|
|
50
|
-
em,
|
|
51
|
-
WebhookIngestionEntity,
|
|
52
|
-
{
|
|
53
|
-
id: job.ingestionId,
|
|
54
|
-
tenantId: job.tenantId,
|
|
55
|
-
organizationId: job.organizationId,
|
|
56
|
-
},
|
|
57
|
-
{},
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
if (!ingestion || ingestion.status === 'processed') return
|
|
61
|
-
|
|
62
|
-
ingestion.status = 'processing'
|
|
63
|
-
await em.flush()
|
|
64
|
-
|
|
65
|
-
const handlers = resolveWebhookHandlers(job.sourceKey, job.eventType)
|
|
66
|
-
const payload: WebhookHandlerPayload = {
|
|
67
|
-
data: ingestion.payload,
|
|
68
|
-
eventType: job.eventType,
|
|
69
|
-
sourceKey: job.sourceKey,
|
|
70
|
-
headers: ingestion.headers ?? {},
|
|
71
|
-
ingestionId: job.ingestionId,
|
|
72
|
-
tenantId: job.tenantId,
|
|
73
|
-
organizationId: job.organizationId,
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const previousResults = Array.isArray(ingestion.handlerResults) ? ingestion.handlerResults : []
|
|
77
|
-
const alreadySucceeded = new Map(
|
|
78
|
-
previousResults
|
|
79
|
-
.filter((result) => result?.status === 'success')
|
|
80
|
-
.map((result) => [result.handlerId, result]),
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
const results: WebhookHandlerResult[] = []
|
|
84
|
-
let failedCount = 0
|
|
85
|
-
|
|
86
|
-
for (const entry of handlers) {
|
|
87
|
-
const previousSuccess = alreadySucceeded.get(entry.meta.id)
|
|
88
|
-
if (previousSuccess) {
|
|
89
|
-
results.push(previousSuccess)
|
|
90
|
-
continue
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const handlerStartedMs = Date.now()
|
|
94
|
-
const handlerStartedAt = new Date(handlerStartedMs).toISOString()
|
|
95
|
-
try {
|
|
96
|
-
const mod = await entry.handler()
|
|
97
|
-
await mod.default(payload, ctx)
|
|
98
|
-
results.push({
|
|
99
|
-
handlerId: entry.meta.id,
|
|
100
|
-
module: moduleOfHandler(entry.meta.id),
|
|
101
|
-
status: 'success',
|
|
102
|
-
durationMs: Date.now() - handlerStartedMs,
|
|
103
|
-
startedAt: handlerStartedAt,
|
|
104
|
-
})
|
|
105
|
-
} catch (error) {
|
|
106
|
-
failedCount += 1
|
|
107
|
-
const message = truncateError(error instanceof Error ? error.message : String(error))
|
|
108
|
-
results.push({
|
|
109
|
-
handlerId: entry.meta.id,
|
|
110
|
-
module: moduleOfHandler(entry.meta.id),
|
|
111
|
-
status: 'failed',
|
|
112
|
-
errorMessage: message,
|
|
113
|
-
durationMs: Date.now() - handlerStartedMs,
|
|
114
|
-
startedAt: handlerStartedAt,
|
|
115
|
-
})
|
|
116
|
-
await emitWebhooksEvent('webhooks.inbound.handler_failed', {
|
|
117
|
-
ingestionId: job.ingestionId,
|
|
118
|
-
sourceKey: job.sourceKey,
|
|
119
|
-
eventType: job.eventType,
|
|
120
|
-
handlerId: entry.meta.id,
|
|
121
|
-
errorMessage: message,
|
|
122
|
-
tenantId: job.tenantId,
|
|
123
|
-
organizationId: job.organizationId,
|
|
124
|
-
})
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
ingestion.handlerCount = handlers.length
|
|
129
|
-
ingestion.handlerResults = results.slice(0, MAX_HANDLER_RESULTS)
|
|
130
|
-
ingestion.status = failedCount > 0 ? 'failed' : 'processed'
|
|
131
|
-
ingestion.processedAt = new Date()
|
|
132
|
-
ingestion.durationMs = Date.now() - startedAtMs
|
|
133
|
-
ingestion.errorMessage = failedCount > 0
|
|
134
|
-
? `${failedCount}/${handlers.length} handlers failed`
|
|
135
|
-
: null
|
|
136
|
-
await em.flush()
|
|
137
|
-
|
|
138
|
-
await emitWebhooksEvent('webhooks.inbound.processed', {
|
|
139
|
-
ingestionId: job.ingestionId,
|
|
140
|
-
sourceKey: job.sourceKey,
|
|
141
|
-
eventType: job.eventType,
|
|
142
|
-
handlerCount: handlers.length,
|
|
143
|
-
failedCount,
|
|
144
|
-
tenantId: job.tenantId,
|
|
145
|
-
organizationId: job.organizationId,
|
|
146
|
-
})
|
|
147
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { matchWebhookEventPattern } from '@open-mercato/shared/lib/events/patterns'
|
|
2
|
-
import type {
|
|
3
|
-
WebhookHandlerRegistryEntry,
|
|
4
|
-
WebhookSourceConfig,
|
|
5
|
-
} from '@open-mercato/shared/lib/webhooks'
|
|
6
|
-
|
|
7
|
-
const WEBHOOK_SOURCES_KEY = '__openMercatoWebhookSources__'
|
|
8
|
-
const WEBHOOK_HANDLERS_KEY = '__openMercatoWebhookHandlers__'
|
|
9
|
-
|
|
10
|
-
type GlobalState = typeof globalThis & {
|
|
11
|
-
[WEBHOOK_SOURCES_KEY]?: Map<string, WebhookSourceConfig>
|
|
12
|
-
[WEBHOOK_HANDLERS_KEY]?: WebhookHandlerRegistryEntry[]
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function getSourceRegistry(): Map<string, WebhookSourceConfig> {
|
|
16
|
-
const globalState = globalThis as GlobalState
|
|
17
|
-
if (!globalState[WEBHOOK_SOURCES_KEY]) {
|
|
18
|
-
globalState[WEBHOOK_SOURCES_KEY] = new Map<string, WebhookSourceConfig>()
|
|
19
|
-
}
|
|
20
|
-
return globalState[WEBHOOK_SOURCES_KEY]
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function getHandlerRegistry(): WebhookHandlerRegistryEntry[] {
|
|
24
|
-
const globalState = globalThis as GlobalState
|
|
25
|
-
if (!globalState[WEBHOOK_HANDLERS_KEY]) {
|
|
26
|
-
globalState[WEBHOOK_HANDLERS_KEY] = []
|
|
27
|
-
}
|
|
28
|
-
return globalState[WEBHOOK_HANDLERS_KEY]
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function registerWebhookSource(config: WebhookSourceConfig): () => void {
|
|
32
|
-
const registry = getSourceRegistry()
|
|
33
|
-
registry.set(config.key, config)
|
|
34
|
-
return () => {
|
|
35
|
-
if (registry.get(config.key) === config) registry.delete(config.key)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function setWebhookSources(configs: WebhookSourceConfig[]): void {
|
|
40
|
-
const registry = getSourceRegistry()
|
|
41
|
-
registry.clear()
|
|
42
|
-
for (const config of configs) registry.set(config.key, config)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function getWebhookSource(sourceKey: string): WebhookSourceConfig | undefined {
|
|
46
|
-
return getSourceRegistry().get(sourceKey)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export function listWebhookSources(): WebhookSourceConfig[] {
|
|
50
|
-
return Array.from(getSourceRegistry().values())
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function clearWebhookSources(): void {
|
|
54
|
-
getSourceRegistry().clear()
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function registerWebhookHandler(entry: WebhookHandlerRegistryEntry): () => void {
|
|
58
|
-
const registry = getHandlerRegistry()
|
|
59
|
-
registry.push(entry)
|
|
60
|
-
return () => {
|
|
61
|
-
const index = registry.indexOf(entry)
|
|
62
|
-
if (index >= 0) registry.splice(index, 1)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function setWebhookHandlers(entries: WebhookHandlerRegistryEntry[]): void {
|
|
67
|
-
const registry = getHandlerRegistry()
|
|
68
|
-
registry.length = 0
|
|
69
|
-
registry.push(...entries)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function listWebhookHandlers(): WebhookHandlerRegistryEntry[] {
|
|
73
|
-
return [...getHandlerRegistry()]
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function clearWebhookHandlers(): void {
|
|
77
|
-
getHandlerRegistry().length = 0
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Resolve the handlers that match an inbound webhook's source key and event type.
|
|
82
|
-
* Event matching reuses the outbound prefix-wildcard semantics (`*`, `payment_intent.*`).
|
|
83
|
-
*/
|
|
84
|
-
export function resolveWebhookHandlers(
|
|
85
|
-
sourceKey: string,
|
|
86
|
-
eventType: string,
|
|
87
|
-
): WebhookHandlerRegistryEntry[] {
|
|
88
|
-
return getHandlerRegistry().filter((entry) => {
|
|
89
|
-
if (entry.meta.source !== sourceKey) return false
|
|
90
|
-
return matchWebhookEventPattern(eventType, entry.meta.event)
|
|
91
|
-
})
|
|
92
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
WebhookHandlerRegistryEntry,
|
|
3
|
-
WebhookSourceConfig,
|
|
4
|
-
} from '@open-mercato/shared/lib/webhooks'
|
|
5
|
-
import { setWebhookHandlers, setWebhookSources } from './inbound-registry'
|
|
6
|
-
|
|
7
|
-
export type WebhookSourceModuleEntry = {
|
|
8
|
-
moduleId: string
|
|
9
|
-
sources: WebhookSourceConfig[]
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export type WebhookHandlerModuleEntry = {
|
|
13
|
-
moduleId: string
|
|
14
|
-
handlers: WebhookHandlerRegistryEntry[]
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Bootstrap-time registration for module-declared webhook sources.
|
|
19
|
-
* Driven by the `webhooks.sources` generator plugin via
|
|
20
|
-
* `bootstrap-registrations.generated.ts` — modules contribute a
|
|
21
|
-
* `webhook-sources.ts` and are wired here without bootstrap.ts edits.
|
|
22
|
-
*/
|
|
23
|
-
export function registerWebhookSourceEntries(entries: WebhookSourceModuleEntry[]): void {
|
|
24
|
-
setWebhookSources(entries.flatMap((entry) => entry.sources ?? []))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Bootstrap-time registration for module-declared webhook handlers.
|
|
29
|
-
* Driven by the `webhooks.handlers` generator plugin.
|
|
30
|
-
*/
|
|
31
|
-
export function registerWebhookHandlerEntries(entries: WebhookHandlerModuleEntry[]): void {
|
|
32
|
-
setWebhookHandlers(entries.flatMap((entry) => entry.handlers ?? []))
|
|
33
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Migration } from '@mikro-orm/migrations';
|
|
2
|
-
|
|
3
|
-
export class Migration20260617141327_webhooks extends Migration {
|
|
4
|
-
|
|
5
|
-
override up(): void | Promise<void> {
|
|
6
|
-
this.addSql(`create table "webhook_inbound_configs" ("id" uuid not null default gen_random_uuid(), "source_key" text not null, "is_active" boolean not null default true, "integration_id" text null, "organization_id" uuid not null, "tenant_id" uuid not null, "created_at" timestamptz not null, "updated_at" timestamptz not null, primary key ("id"));`);
|
|
7
|
-
this.addSql(`create index "webhook_inbound_configs_source_key_is_active_index" on "webhook_inbound_configs" ("source_key", "is_active");`);
|
|
8
|
-
this.addSql(`alter table "webhook_inbound_configs" add constraint "webhook_inbound_configs_source_scope_unique" unique ("source_key", "organization_id", "tenant_id");`);
|
|
9
|
-
|
|
10
|
-
this.addSql(`create table "webhook_ingestions" ("id" uuid not null default gen_random_uuid(), "source_key" text not null, "event_type" text not null, "external_message_id" text null, "payload" jsonb not null, "headers" jsonb null, "status" text not null default 'received', "error_message" text null, "processed_at" timestamptz null, "handler_count" int not null default 0, "handler_results" jsonb null, "duration_ms" int null, "organization_id" uuid not null, "tenant_id" uuid not null, "created_at" timestamptz not null, "updated_at" timestamptz not null, primary key ("id"));`);
|
|
11
|
-
this.addSql(`create index "webhook_ingestions_external_message_id_index" on "webhook_ingestions" ("external_message_id");`);
|
|
12
|
-
this.addSql(`create index "webhook_ingestions_organization_id_tenant_id_created_at_index" on "webhook_ingestions" ("organization_id", "tenant_id", "created_at");`);
|
|
13
|
-
this.addSql(`create index "webhook_ingestions_source_key_status_created_at_index" on "webhook_ingestions" ("source_key", "status", "created_at");`);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
-
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
3
|
-
import { processInboundDispatchJob, type InboundDispatchJob } from '../lib/inbound-dispatch'
|
|
4
|
-
|
|
5
|
-
const logger = createLogger('webhooks')
|
|
6
|
-
|
|
7
|
-
export const metadata = {
|
|
8
|
-
queue: 'webhook-inbound-dispatch',
|
|
9
|
-
id: 'webhooks:inbound-dispatch-worker',
|
|
10
|
-
concurrency: 5,
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default async function handler(
|
|
14
|
-
job: { data: InboundDispatchJob },
|
|
15
|
-
ctx: { resolve: <T = unknown>(name: string) => T },
|
|
16
|
-
) {
|
|
17
|
-
const em = (ctx.resolve('em') as EntityManager).fork()
|
|
18
|
-
try {
|
|
19
|
-
await processInboundDispatchJob(em, job.data, {
|
|
20
|
-
resolve: <T,>(name: string) => ctx.resolve(name) as T,
|
|
21
|
-
})
|
|
22
|
-
} catch (error) {
|
|
23
|
-
logger.error('Inbound dispatch job processing failed', {
|
|
24
|
-
ingestionId: job.data.ingestionId,
|
|
25
|
-
sourceKey: job.data.sourceKey,
|
|
26
|
-
tenantId: job.data.tenantId,
|
|
27
|
-
err: error,
|
|
28
|
-
})
|
|
29
|
-
throw error
|
|
30
|
-
}
|
|
31
|
-
}
|