external-event.ts
TypeScript · 26 рядків
1export function normalizeExternalEvent(input: any = {}) {
2 if (!input || typeof input !== 'object' || Array.isArray(input)) {
3 throw new Error('Event body must be an object')
4 }
5 if (!String(input.event || '').trim()) throw new Error('event is required')
6 if (!String(input.entityType || '').trim()) throw new Error('entityType is required')
7 if (input.entityId === undefined || input.entityId === null || input.entityId === '') {
8 throw new Error('entityId is required')
9 }
10 if (typeof input.userId !== 'number' || !Number.isFinite(input.userId)) {
11 throw new Error('userId must be a number')
12 }
13 if (!input.payload || typeof input.payload !== 'object' || Array.isArray(input.payload)) {
14 throw new Error('payload must be an object')
15 }
16
17 return {
18 event: String(input.event).trim(),
19 entityType: String(input.entityType).trim(),
20 entityId: input.entityId,
21 userId: input.userId,
22 payload: input.payload,
23 data: input.payload
24 }
25}
26