@legible-sync/example-eda 1.2.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.
Files changed (118) hide show
  1. package/.eslintrc.js +16 -0
  2. package/README.md +103 -0
  3. package/__tests__/integration/ecommerce-flow.test.ts +247 -0
  4. package/__tests__/unit/EventBus.test.ts +154 -0
  5. package/__tests__/unit/PluginManager.test.ts +111 -0
  6. package/__tests__/unit/concepts/User.test.ts +130 -0
  7. package/dist/core/EventBus.d.ts +16 -0
  8. package/dist/core/EventBus.d.ts.map +1 -0
  9. package/dist/core/EventBus.js +44 -0
  10. package/dist/core/EventBus.js.map +1 -0
  11. package/dist/core/PluginManager.d.ts +16 -0
  12. package/dist/core/PluginManager.d.ts.map +1 -0
  13. package/dist/core/PluginManager.js +37 -0
  14. package/dist/core/PluginManager.js.map +1 -0
  15. package/dist/index.d.ts +2 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +145 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/plugins/analytics/concepts/Analytics.d.ts +3 -0
  20. package/dist/plugins/analytics/concepts/Analytics.d.ts.map +1 -0
  21. package/dist/plugins/analytics/concepts/Analytics.js +43 -0
  22. package/dist/plugins/analytics/concepts/Analytics.js.map +1 -0
  23. package/dist/plugins/analytics/index.d.ts +3 -0
  24. package/dist/plugins/analytics/index.d.ts.map +1 -0
  25. package/dist/plugins/analytics/index.js +16 -0
  26. package/dist/plugins/analytics/index.js.map +1 -0
  27. package/dist/plugins/analytics/syncs/analytics-events.sync.d.ts +3 -0
  28. package/dist/plugins/analytics/syncs/analytics-events.sync.d.ts.map +1 -0
  29. package/dist/plugins/analytics/syncs/analytics-events.sync.js +101 -0
  30. package/dist/plugins/analytics/syncs/analytics-events.sync.js.map +1 -0
  31. package/dist/plugins/inventory/concepts/Inventory.d.ts +3 -0
  32. package/dist/plugins/inventory/concepts/Inventory.d.ts.map +1 -0
  33. package/dist/plugins/inventory/concepts/Inventory.js +60 -0
  34. package/dist/plugins/inventory/concepts/Inventory.js.map +1 -0
  35. package/dist/plugins/inventory/index.d.ts +3 -0
  36. package/dist/plugins/inventory/index.d.ts.map +1 -0
  37. package/dist/plugins/inventory/index.js +15 -0
  38. package/dist/plugins/inventory/index.js.map +1 -0
  39. package/dist/plugins/notifications/concepts/Notification.d.ts +3 -0
  40. package/dist/plugins/notifications/concepts/Notification.d.ts.map +1 -0
  41. package/dist/plugins/notifications/concepts/Notification.js +42 -0
  42. package/dist/plugins/notifications/concepts/Notification.js.map +1 -0
  43. package/dist/plugins/notifications/index.d.ts +3 -0
  44. package/dist/plugins/notifications/index.d.ts.map +1 -0
  45. package/dist/plugins/notifications/index.js +15 -0
  46. package/dist/plugins/notifications/index.js.map +1 -0
  47. package/dist/plugins/orders/concepts/Order.d.ts +3 -0
  48. package/dist/plugins/orders/concepts/Order.d.ts.map +1 -0
  49. package/dist/plugins/orders/concepts/Order.js +98 -0
  50. package/dist/plugins/orders/concepts/Order.js.map +1 -0
  51. package/dist/plugins/orders/index.d.ts +3 -0
  52. package/dist/plugins/orders/index.d.ts.map +1 -0
  53. package/dist/plugins/orders/index.js +16 -0
  54. package/dist/plugins/orders/index.js.map +1 -0
  55. package/dist/plugins/orders/syncs/order-workflow.sync.d.ts +3 -0
  56. package/dist/plugins/orders/syncs/order-workflow.sync.d.ts.map +1 -0
  57. package/dist/plugins/orders/syncs/order-workflow.sync.js +168 -0
  58. package/dist/plugins/orders/syncs/order-workflow.sync.js.map +1 -0
  59. package/dist/plugins/payments/concepts/Payment.d.ts +3 -0
  60. package/dist/plugins/payments/concepts/Payment.d.ts.map +1 -0
  61. package/dist/plugins/payments/concepts/Payment.js +156 -0
  62. package/dist/plugins/payments/concepts/Payment.js.map +1 -0
  63. package/dist/plugins/payments/index.d.ts +3 -0
  64. package/dist/plugins/payments/index.d.ts.map +1 -0
  65. package/dist/plugins/payments/index.js +16 -0
  66. package/dist/plugins/payments/index.js.map +1 -0
  67. package/dist/plugins/payments/syncs/payment-workflow.sync.d.ts +3 -0
  68. package/dist/plugins/payments/syncs/payment-workflow.sync.d.ts.map +1 -0
  69. package/dist/plugins/payments/syncs/payment-workflow.sync.js +264 -0
  70. package/dist/plugins/payments/syncs/payment-workflow.sync.js.map +1 -0
  71. package/dist/plugins/products/concepts/Product.d.ts +3 -0
  72. package/dist/plugins/products/concepts/Product.d.ts.map +1 -0
  73. package/dist/plugins/products/concepts/Product.js +85 -0
  74. package/dist/plugins/products/concepts/Product.js.map +1 -0
  75. package/dist/plugins/products/index.d.ts +3 -0
  76. package/dist/plugins/products/index.d.ts.map +1 -0
  77. package/dist/plugins/products/index.js +16 -0
  78. package/dist/plugins/products/index.js.map +1 -0
  79. package/dist/plugins/products/syncs/product-events.sync.d.ts +3 -0
  80. package/dist/plugins/products/syncs/product-events.sync.d.ts.map +1 -0
  81. package/dist/plugins/products/syncs/product-events.sync.js +77 -0
  82. package/dist/plugins/products/syncs/product-events.sync.js.map +1 -0
  83. package/dist/plugins/users/concepts/User.d.ts +3 -0
  84. package/dist/plugins/users/concepts/User.d.ts.map +1 -0
  85. package/dist/plugins/users/concepts/User.js +81 -0
  86. package/dist/plugins/users/concepts/User.js.map +1 -0
  87. package/dist/plugins/users/index.d.ts +3 -0
  88. package/dist/plugins/users/index.d.ts.map +1 -0
  89. package/dist/plugins/users/index.js +16 -0
  90. package/dist/plugins/users/index.js.map +1 -0
  91. package/dist/plugins/users/syncs/user-events.sync.d.ts +3 -0
  92. package/dist/plugins/users/syncs/user-events.sync.d.ts.map +1 -0
  93. package/dist/plugins/users/syncs/user-events.sync.js +75 -0
  94. package/dist/plugins/users/syncs/user-events.sync.js.map +1 -0
  95. package/package.json +40 -0
  96. package/src/core/EventBus.ts +55 -0
  97. package/src/core/PluginManager.ts +51 -0
  98. package/src/index.ts +169 -0
  99. package/src/plugins/analytics/concepts/Analytics.ts +53 -0
  100. package/src/plugins/analytics/index.ts +15 -0
  101. package/src/plugins/analytics/syncs/analytics-events.sync.ts +103 -0
  102. package/src/plugins/inventory/concepts/Inventory.ts +73 -0
  103. package/src/plugins/inventory/index.ts +14 -0
  104. package/src/plugins/notifications/concepts/Notification.ts +49 -0
  105. package/src/plugins/notifications/index.ts +14 -0
  106. package/src/plugins/orders/concepts/Order.ts +118 -0
  107. package/src/plugins/orders/index.ts +15 -0
  108. package/src/plugins/orders/syncs/order-workflow.sync.ts +173 -0
  109. package/src/plugins/payments/concepts/Payment.ts +186 -0
  110. package/src/plugins/payments/index.ts +15 -0
  111. package/src/plugins/payments/syncs/payment-workflow.sync.ts +274 -0
  112. package/src/plugins/products/concepts/Product.ts +102 -0
  113. package/src/plugins/products/index.ts +15 -0
  114. package/src/plugins/products/syncs/product-events.sync.ts +78 -0
  115. package/src/plugins/users/concepts/User.ts +97 -0
  116. package/src/plugins/users/index.ts +15 -0
  117. package/src/plugins/users/syncs/user-events.sync.ts +76 -0
  118. package/tsconfig.json +9 -0
@@ -0,0 +1,97 @@
1
+ // plugins/users/concepts/User.ts
2
+ import { Concept } from '@legible-sync/core';
3
+ import { v4 as uuidv4 } from 'uuid';
4
+
5
+ export const User: Concept = {
6
+ state: {
7
+ users: new Map<string, any>(),
8
+ emails: new Set<string>(),
9
+ usernames: new Set<string>(),
10
+ },
11
+
12
+ async execute(action: string, input: any) {
13
+ const state = this.state;
14
+
15
+ switch (action) {
16
+ case 'register': {
17
+ const { username, email, password } = input;
18
+
19
+ // Validation
20
+ if (!username || !email || !password) {
21
+ throw new Error('Username, email, and password are required');
22
+ }
23
+
24
+ if (state.emails.has(email)) {
25
+ throw new Error('Email already registered');
26
+ }
27
+
28
+ if (state.usernames.has(username)) {
29
+ throw new Error('Username already taken');
30
+ }
31
+
32
+ // Create user
33
+ const userId = uuidv4();
34
+ const user = {
35
+ id: userId,
36
+ username,
37
+ email,
38
+ createdAt: new Date(),
39
+ status: 'active'
40
+ };
41
+
42
+ state.users.set(userId, user);
43
+ state.emails.add(email);
44
+ state.usernames.add(username);
45
+
46
+ return { userId, user };
47
+ }
48
+
49
+ case 'get': {
50
+ const { userId } = input;
51
+ const user = state.users.get(userId);
52
+ if (!user) {
53
+ throw new Error('User not found');
54
+ }
55
+ return { user };
56
+ }
57
+
58
+ case 'update': {
59
+ const { userId, updates } = input;
60
+ const user = state.users.get(userId);
61
+ if (!user) {
62
+ throw new Error('User not found');
63
+ }
64
+
65
+ // Update user
66
+ const updatedUser = { ...user, ...updates, updatedAt: new Date() };
67
+ state.users.set(userId, updatedUser);
68
+
69
+ return { user: updatedUser };
70
+ }
71
+
72
+ case 'deactivate': {
73
+ const { userId } = input;
74
+ const user = state.users.get(userId);
75
+ if (!user) {
76
+ throw new Error('User not found');
77
+ }
78
+
79
+ user.status = 'inactive';
80
+ user.deactivatedAt = new Date();
81
+ state.users.set(userId, user);
82
+
83
+ return { user };
84
+ }
85
+
86
+ case 'reset': {
87
+ state.users.clear();
88
+ state.emails.clear();
89
+ state.usernames.clear();
90
+ return { reset: true };
91
+ }
92
+
93
+ default:
94
+ throw new Error(`Unknown action: ${action}`);
95
+ }
96
+ }
97
+ };
@@ -0,0 +1,15 @@
1
+ // plugins/users/index.ts
2
+ import { Plugin } from '../../core/PluginManager';
3
+ import { User } from './concepts/User';
4
+ import { userEventSyncs } from './syncs/user-events.sync';
5
+
6
+ export const usersPlugin: Plugin = {
7
+ name: 'users',
8
+ concepts: {
9
+ User
10
+ },
11
+ syncs: userEventSyncs,
12
+ initialize: async (_engine) => {
13
+ console.log('👤 Users plugin initialized');
14
+ }
15
+ };
@@ -0,0 +1,76 @@
1
+ // plugins/users/syncs/user-events.sync.ts
2
+ import { SyncRule } from '@legible-sync/core';
3
+
4
+ export const userEventSyncs: SyncRule[] = [
5
+ // When a user registers, publish user.registered event
6
+ {
7
+ name: "PublishUserRegisteredEvent",
8
+ when: [
9
+ {
10
+ concept: "User",
11
+ action: "register"
12
+ }
13
+ ],
14
+ then: [
15
+ {
16
+ concept: "EventBus",
17
+ action: "publish",
18
+ input: {
19
+ event: "user.registered",
20
+ data: {
21
+ userId: "?userId",
22
+ username: "?user.username",
23
+ email: "?user.email"
24
+ }
25
+ }
26
+ }
27
+ ]
28
+ },
29
+
30
+ // When a user is updated, publish user.updated event
31
+ {
32
+ name: "PublishUserUpdatedEvent",
33
+ when: [
34
+ {
35
+ concept: "User",
36
+ action: "update"
37
+ }
38
+ ],
39
+ then: [
40
+ {
41
+ concept: "EventBus",
42
+ action: "publish",
43
+ input: {
44
+ event: "user.updated",
45
+ data: {
46
+ userId: "?userId",
47
+ updates: "?updates"
48
+ }
49
+ }
50
+ }
51
+ ]
52
+ },
53
+
54
+ // When a user is deactivated, publish user.deactivated event
55
+ {
56
+ name: "PublishUserDeactivatedEvent",
57
+ when: [
58
+ {
59
+ concept: "User",
60
+ action: "deactivate"
61
+ }
62
+ ],
63
+ then: [
64
+ {
65
+ concept: "EventBus",
66
+ action: "publish",
67
+ input: {
68
+ event: "user.deactivated",
69
+ data: {
70
+ userId: "?userId"
71
+ }
72
+ }
73
+ }
74
+ ]
75
+ }
76
+ ];
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "dist"]
9
+ }