1 import {
2 ResourceEntry,
3 ResourceTypeHandler,
4 SubscriberResourceData,
5 } from "../types"
6
7 export class SubscriberHandler
8 implements ResourceTypeHandler<SubscriberResourceData>
9 {
10 readonly type = "subscriber"
11
12 validate(data: SubscriberResourceData): void {
13 if (!data.id) {
14 throw new Error(
15 `Subscriber registration requires id. Received: ${JSON.stringify(data)}`
16 )
17 }
18
19 if (!data.sourcePath) {
20 throw new Error(
21 `Subscriber registration requires sourcePath. Received: ${JSON.stringify(
22 data
23 )}`
24 )
25 }
26
27 if (!data.subscriberId) {
28 throw new Error(
29 `Subscriber registration requires subscriberId. Received: ${JSON.stringify(
30 data
31 )}`
32 )
33 }
34
35 if (!data.events) {
36 throw new Error(
37 `Subscriber registration requires events. Received: ${JSON.stringify(
38 data
39 )}`
40 )
41 }
42
43 if (!Array.isArray(data.events)) {
44 throw new Error(
45 `Subscriber registration requires events to be an array. Received: ${JSON.stringify(
46 data
47 )}`
48 )
49 }
50 }
51
52 resolveSourcePath(data: SubscriberResourceData): string {
53 return data.sourcePath
54 }
55
56 createEntry(data: SubscriberResourceData): ResourceEntry {
57 return {
58 id: data.id,
59 subscriberId: data.subscriberId,
60 events: data.events,
61 }
62 }
63
64 getInverseKey(data: SubscriberResourceData): string {
65 return `${this.type}:${data.subscriberId}`
66 }
67 }