@bett3r-dev/pv3-types 1.0.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 (122) hide show
  1. package/build/authentication.d.ts +261 -0
  2. package/build/authentication.d.ts.map +1 -0
  3. package/build/authentication.js +108 -0
  4. package/build/authentication.js.map +1 -0
  5. package/build/cache.d.ts +101 -0
  6. package/build/cache.d.ts.map +1 -0
  7. package/build/cache.js +3 -0
  8. package/build/cache.js.map +1 -0
  9. package/build/clusterAdministration.d.ts +642 -0
  10. package/build/clusterAdministration.d.ts.map +1 -0
  11. package/build/clusterAdministration.js +177 -0
  12. package/build/clusterAdministration.js.map +1 -0
  13. package/build/configuration.d.ts +66 -0
  14. package/build/configuration.d.ts.map +1 -0
  15. package/build/configuration.js +3 -0
  16. package/build/configuration.js.map +1 -0
  17. package/build/database.d.ts +120 -0
  18. package/build/database.d.ts.map +1 -0
  19. package/build/database.js +41 -0
  20. package/build/database.js.map +1 -0
  21. package/build/endpoints.d.ts +183 -0
  22. package/build/endpoints.d.ts.map +1 -0
  23. package/build/endpoints.js +78 -0
  24. package/build/endpoints.js.map +1 -0
  25. package/build/evenstore.d.ts +26 -0
  26. package/build/evenstore.d.ts.map +1 -0
  27. package/build/evenstore.js +12 -0
  28. package/build/evenstore.js.map +1 -0
  29. package/build/eventsourcing/aggregateBuilder.d.ts +10 -0
  30. package/build/eventsourcing/aggregateBuilder.d.ts.map +1 -0
  31. package/build/eventsourcing/aggregateBuilder.js +3 -0
  32. package/build/eventsourcing/aggregateBuilder.js.map +1 -0
  33. package/build/eventsourcing/commandBuilder.d.ts +58 -0
  34. package/build/eventsourcing/commandBuilder.d.ts.map +1 -0
  35. package/build/eventsourcing/commandBuilder.js +6 -0
  36. package/build/eventsourcing/commandBuilder.js.map +1 -0
  37. package/build/eventsourcing/commandHandlers.d.ts +104 -0
  38. package/build/eventsourcing/commandHandlers.d.ts.map +1 -0
  39. package/build/eventsourcing/commandHandlers.js +28 -0
  40. package/build/eventsourcing/commandHandlers.js.map +1 -0
  41. package/build/eventsourcing/eventHandlers.d.ts +102 -0
  42. package/build/eventsourcing/eventHandlers.d.ts.map +1 -0
  43. package/build/eventsourcing/eventHandlers.js +33 -0
  44. package/build/eventsourcing/eventHandlers.js.map +1 -0
  45. package/build/eventsourcing/eventHandlersBuilder.d.ts +36 -0
  46. package/build/eventsourcing/eventHandlersBuilder.d.ts.map +1 -0
  47. package/build/eventsourcing/eventHandlersBuilder.js +13 -0
  48. package/build/eventsourcing/eventHandlersBuilder.js.map +1 -0
  49. package/build/eventsourcing/externalSystemBuilder.d.ts +14 -0
  50. package/build/eventsourcing/externalSystemBuilder.d.ts.map +1 -0
  51. package/build/eventsourcing/externalSystemBuilder.js +3 -0
  52. package/build/eventsourcing/externalSystemBuilder.js.map +1 -0
  53. package/build/eventsourcing/index.d.ts +386 -0
  54. package/build/eventsourcing/index.d.ts.map +1 -0
  55. package/build/eventsourcing/index.js +47 -0
  56. package/build/eventsourcing/index.js.map +1 -0
  57. package/build/eventsourcing/invariants.d.ts +19 -0
  58. package/build/eventsourcing/invariants.d.ts.map +1 -0
  59. package/build/eventsourcing/invariants.js +3 -0
  60. package/build/eventsourcing/invariants.js.map +1 -0
  61. package/build/eventsourcing/utils.d.ts +4 -0
  62. package/build/eventsourcing/utils.d.ts.map +1 -0
  63. package/build/eventsourcing/utils.js +3 -0
  64. package/build/eventsourcing/utils.js.map +1 -0
  65. package/build/index.d.ts +16 -0
  66. package/build/index.d.ts.map +1 -0
  67. package/build/index.js +32 -0
  68. package/build/index.js.map +1 -0
  69. package/build/infrastructure.d.ts +40 -0
  70. package/build/infrastructure.d.ts.map +1 -0
  71. package/build/infrastructure.js +11 -0
  72. package/build/infrastructure.js.map +1 -0
  73. package/build/logger.d.ts +32 -0
  74. package/build/logger.d.ts.map +1 -0
  75. package/build/logger.js +29 -0
  76. package/build/logger.js.map +1 -0
  77. package/build/outboxManager.d.ts +38 -0
  78. package/build/outboxManager.d.ts.map +1 -0
  79. package/build/outboxManager.js +21 -0
  80. package/build/outboxManager.js.map +1 -0
  81. package/build/ports.d.ts +39 -0
  82. package/build/ports.d.ts.map +1 -0
  83. package/build/ports.js +3 -0
  84. package/build/ports.js.map +1 -0
  85. package/build/routesAuth.d.ts +58 -0
  86. package/build/routesAuth.d.ts.map +1 -0
  87. package/build/routesAuth.js +37 -0
  88. package/build/routesAuth.js.map +1 -0
  89. package/build/typedEventEmitter.d.ts +23 -0
  90. package/build/typedEventEmitter.d.ts.map +1 -0
  91. package/build/typedEventEmitter.js +55 -0
  92. package/build/typedEventEmitter.js.map +1 -0
  93. package/build/validation.d.ts +64 -0
  94. package/build/validation.d.ts.map +1 -0
  95. package/build/validation.js +57 -0
  96. package/build/validation.js.map +1 -0
  97. package/package.json +15 -0
  98. package/src/authentication.ts +273 -0
  99. package/src/cache.ts +109 -0
  100. package/src/clusterAdministration.ts +216 -0
  101. package/src/configuration.ts +71 -0
  102. package/src/database.ts +157 -0
  103. package/src/endpoints.ts +198 -0
  104. package/src/evenstore.ts +27 -0
  105. package/src/eventsourcing/aggregateBuilder.ts +11 -0
  106. package/src/eventsourcing/commandBuilder.ts +64 -0
  107. package/src/eventsourcing/commandHandlers.ts +137 -0
  108. package/src/eventsourcing/eventHandlers.ts +118 -0
  109. package/src/eventsourcing/eventHandlersBuilder.ts +42 -0
  110. package/src/eventsourcing/externalSystemBuilder.ts +20 -0
  111. package/src/eventsourcing/index.ts +111 -0
  112. package/src/eventsourcing/invariants.ts +27 -0
  113. package/src/eventsourcing/utils.ts +4 -0
  114. package/src/index.ts +15 -0
  115. package/src/infrastructure.ts +41 -0
  116. package/src/logger.ts +45 -0
  117. package/src/outboxManager.ts +36 -0
  118. package/src/ports.ts +42 -0
  119. package/src/routesAuth.ts +58 -0
  120. package/src/typedEventEmitter.ts +73 -0
  121. package/src/validation.ts +89 -0
  122. package/tsconfig.json +15 -0
@@ -0,0 +1,273 @@
1
+ import S, { mergeMultipleSchemas, mergeSchemas, omitFromSchema } from '@bett3r-dev/jsonschema-definer';
2
+ import { RoutesAuthManagerType } from './routesAuth';
3
+ import { ClusterRoutesConfigType } from './clusterAdministration';
4
+ import { AdapterBase, PortEmitter } from './ports';
5
+ import { Ports } from './ports';
6
+
7
+ //*******************************************
8
+ // Schemas
9
+ //*******************************************
10
+ /**
11
+ * @category Schemas
12
+ */
13
+ export const AuthenticationIdSchema = S.shape({
14
+ id: S.string()
15
+ });
16
+ /**
17
+ * @category Schemas
18
+ */
19
+ export const AuthenticationEmailSchema = S.shape({
20
+ email: S.string().format( 'email' )
21
+ });
22
+ export type AuthenticationEmail = typeof AuthenticationEmailSchema.type;
23
+
24
+ /**
25
+ * @category Schemas
26
+ */
27
+ export const AuthenticationPasswordSchema = S.shape({
28
+ password: S.string()
29
+ });
30
+ export type AuthenticationPassword = typeof AuthenticationPasswordSchema.type;
31
+
32
+ /**
33
+ * @category Schemas
34
+ */
35
+ export const AuthenticationRoleSchema = S.shape({
36
+ role: S.string(),
37
+ });
38
+
39
+ /**
40
+ * @category Schemas
41
+ */
42
+ export const AuthenticationRolesSchema = S.shape({
43
+ roles: S.array().items( S.string()),
44
+ });
45
+ export type AuthenticationRolesType = typeof AuthenticationRolesSchema.type;
46
+
47
+ /**
48
+ * @category Schemas
49
+ */
50
+ export const AuthenticationProviderIdSchema = S.shape({
51
+ providerId: S.string(),
52
+ providerName: S.string(),
53
+ });
54
+ export type AuthenticationProviderId = typeof AuthenticationProviderIdSchema.type
55
+
56
+ /**
57
+ * @category Schemas
58
+ */
59
+ export const AuthenticationProviderUserSchema = mergeSchemas( AuthenticationEmailSchema, AuthenticationProviderIdSchema );
60
+
61
+ /**
62
+ * @category Schemas
63
+ */
64
+ export const UserLoginSchema = mergeSchemas( AuthenticationEmailSchema, AuthenticationPasswordSchema );
65
+ export type UserLogin = typeof UserLoginSchema.type
66
+
67
+ /**
68
+ * @category Schemas
69
+ */
70
+ export const AuthenticationRegistrationSchema = mergeSchemas( UserLoginSchema, AuthenticationRolesSchema );
71
+ export type AuthenticationRegistrationType = typeof AuthenticationRegistrationSchema.type;
72
+
73
+ /**
74
+ * @category Schemas
75
+ */
76
+ export const AuthenticationCustomAttributesSchema = S.shape({
77
+ attributes: S.shape({}).additionalProperties( true ).optional()
78
+ });
79
+ export type AuthenticationCustomAttributesType = typeof AuthenticationCustomAttributesSchema.type;
80
+
81
+ /**
82
+ * @category Schemas
83
+ */
84
+ export const AuthenticationUserSchema = mergeMultipleSchemas( AuthenticationIdSchema, AuthenticationEmailSchema, AuthenticationRolesSchema, AuthenticationCustomAttributesSchema );
85
+ export type AuthenticationUser = typeof AuthenticationUserSchema.type;
86
+
87
+
88
+ /**
89
+ * @category Schemas
90
+ */
91
+ export const AuthenticationCreateUserCommandSchema = mergeSchemas( AuthenticationUserSchema, AuthenticationPasswordSchema );
92
+ export type AuthenticationCreateUserCommand = typeof AuthenticationCreateUserCommandSchema.type
93
+
94
+ export const AuthenticationUpdateUserCommandSchema = mergeSchemas( omitFromSchema( AuthenticationCreateUserCommandSchema, [ 'password', 'id', 'roles' ]).partial(), S.shape({ providerId: S.string() }));
95
+ export type AuthenticationUpdateUserCommand = typeof AuthenticationUpdateUserCommandSchema.type;
96
+
97
+ /**
98
+ * @category Schemas
99
+ */
100
+ export const AuthenticationResponseSchema = mergeSchemas( AuthenticationRolesSchema, S.shape({
101
+ 'expires_in': S.number(),
102
+ 'refresh_expires_in': S.number(),
103
+ 'not-before-policy': S.number(),
104
+ 'access_token': S.string(),
105
+ 'refresh_token': S.string(),
106
+ 'token_type': S.string(),
107
+ 'session_state': S.string(),
108
+ 'scope': S.string(),
109
+ }));
110
+ export type AuthenticationResponseType = typeof AuthenticationResponseSchema.type;
111
+
112
+
113
+ //*******************************************
114
+ // Identity Provider
115
+ //*******************************************
116
+
117
+
118
+ /**
119
+ * Logs a user in the Application.
120
+ * @category Authentication Port
121
+ * @param email Email of the user
122
+ * @param password Password of the user
123
+ */
124
+ export declare function loginUser ( email:string, password:string ): Promise<AuthenticationResponseType>;
125
+
126
+ /**
127
+ * Inits the Recover Password process for the user with the given email.
128
+ * @category Authentication Port
129
+ * @param email Email of the user
130
+ */
131
+ export declare function recoverPassword ( email:string ): Promise<void>;
132
+
133
+ /**
134
+ * Marks the user email address as validated.
135
+ * @category Authentication Port
136
+ * @param email Email of the user
137
+ */
138
+ export declare function verifyEmail ( email: string ): Promise<void>;
139
+
140
+ /**
141
+ * Logs out a user from the application
142
+ * @category Authentication Port
143
+ * @param token Session Token to mark as logged out
144
+ */
145
+ export declare function logoutUser ( token: string ): Promise<void>;
146
+
147
+ /**
148
+ * Refreshes the user sesssion given the refresh token.
149
+ * @category Authentication Port
150
+ * @param refreshToken Session Refresh Token
151
+ */
152
+ export declare function refreshToken ( refreshToken: string ): Promise<AuthenticationResponseType>;
153
+
154
+ /**
155
+ * Changes the password of the user
156
+ * @category Authentication Port
157
+ * @param email User Email Address
158
+ * @param password New Password for the User
159
+ */
160
+ export declare function resetPassword ( email:string, password:string ): Promise<void>;
161
+
162
+ /**
163
+ * Creates a new Role in the Application in the Authentication Provider
164
+ * @category Authentication Port
165
+ * @param name The name of the Role To Create
166
+ */
167
+ export declare function createRole ( name: string ): Promise<void>;
168
+
169
+ /**
170
+ * Fetches from the Authentication Provider the list of existing Roles for the given tenant or the default one.
171
+ * @category Authentication Port
172
+ * @param tenant [Optional] The tenant for the role list to be retrieved
173
+ */
174
+ export declare function getRoles ( tenant?: string ): Promise<void>;
175
+
176
+ /**
177
+ * Deletes a Role from the Authentication Provider.
178
+ * @category Authentication Port
179
+ * @param name The name of the Role to Delete
180
+ */
181
+ export declare function deleteRole ( name: string ): Promise<void>;
182
+
183
+ /**
184
+ * Creates a new User in the Authentication Provider
185
+ * @category Authentication Port
186
+ * @param id Application Id of the User to create
187
+ * @param email The Email address of the user
188
+ * @param roles The roles to create the user with
189
+ * @param attributes Additional Attributes to add to the auth token
190
+ */
191
+ export declare function createUser ( id:string, email:string, password:string, roles: string[], attributes?: Record<string, any> ): Promise<string>;
192
+ /**
193
+ * Creates a new User in the Authentication Provider
194
+ * @category Authentication Port
195
+ * @param id Provider Id of the User to create
196
+ * @param email The Email address of the user
197
+ * @param attributes Additional Attributes to add to the auth token
198
+ */
199
+ export declare function updateUser ( providerId:string, email?:string, attributes?: Record<string, any> ): Promise<string>;
200
+
201
+ /**
202
+ * Assigns a list of roles to a given user
203
+ * @category Authentication Port
204
+ * @param email The User Email Address
205
+ * @param roles The Roles to assign to the user
206
+ */
207
+ export declare function assignUserRoles ( email:string, roles: string[]): Promise<void>;
208
+
209
+ /**
210
+ * Removes a list of roles from a given user
211
+ * @category Authentication Port
212
+ * @param email The User Email Address
213
+ * @param roles The roles to remove from the user
214
+ */
215
+ export declare function removeUserRoles ( email:string, roles: string[]): Promise<void>;
216
+
217
+ /**
218
+ * Performs the Authorization process for a request to a resource given the user Access Token.
219
+ * This function is the response of the `authorizeRequest` curried execution.
220
+ * @param accessToken JWT Access Token of the request
221
+ * @param method The HTTP method accessing the resource
222
+ * @param uri The resource uri
223
+ * @see {@link authorizeRequest}
224
+ */
225
+ export declare function authorizeRequestFunction( accessToken: string, method: string, uri: string ): Promise<void>
226
+
227
+ /**
228
+ * Curried function that will receive the routeManager as parameter and
229
+ * return another function with the actual authorization functionality
230
+ * @category Authentication Port
231
+ * @param routeManager The route manager to use to perform the authorization process of the resource requested.
232
+ * @see {@link authorizeRequestFunction}
233
+ */
234
+ export declare function authorizeRequest ( routeManager: RoutesAuthManagerType ): typeof authorizeRequestFunction
235
+
236
+ /**
237
+ * @category Main Types
238
+ */
239
+ export type AuthenticationType = {
240
+ name: string,
241
+ loginUser: typeof loginUser
242
+ recoverPassword: typeof recoverPassword
243
+ verifyEmail: typeof verifyEmail
244
+ logoutUser: typeof logoutUser
245
+ refreshToken: typeof refreshToken
246
+ resetPassword: typeof resetPassword
247
+ createRole: typeof createRole
248
+ getRoles: typeof getRoles
249
+ deleteRole: typeof deleteRole
250
+ createUser: typeof createUser
251
+ updateUser: typeof updateUser
252
+ assignUserRoles: typeof assignUserRoles
253
+ removeUserRoles: typeof removeUserRoles
254
+ authorizeRequest: typeof authorizeRequest
255
+ }
256
+
257
+ /**
258
+ * @category Main Types
259
+ */
260
+ export type AuthenticationPort = PortEmitter & AuthenticationType & {
261
+ RouteManager: ( ports: Pick<Ports, 'database'|'eventstore'>, config: ClusterRoutesConfigType, eventstore?: Ports['eventstore']) => RoutesAuthManagerType
262
+ };
263
+
264
+ /**
265
+ * Defines the concrete implementation for the Authentication Port.
266
+ *
267
+ * __start__ Stablishes the connection with the Authentication Provider with a Service Account with administrative
268
+ * Priviledges that will effect all the operations.
269
+ *
270
+ * __stop__ Disconnects the Service Account from the Authentication Provider
271
+ * @category Main Types
272
+ */
273
+ export type AuthenticationAdapter = AuthenticationType & AdapterBase
package/src/cache.ts ADDED
@@ -0,0 +1,109 @@
1
+ import { AdapterBase, PortEmitter } from './ports';
2
+
3
+ /**
4
+ * @category Main Types
5
+ */
6
+ export type CacheAdapter<T = any> = CacheType & AdapterBase<T> & {
7
+ flushAll: () => Promise<void>
8
+ }
9
+
10
+ /**
11
+ * @category Main Types
12
+ */
13
+ export type CachePort = PortEmitter & CacheType;
14
+
15
+ /**
16
+ * @hidden
17
+ */
18
+ export declare function set<T extends Record<string, any>>( key: string ) : ( value: T, expirationSeconds?: number | boolean ) => Promise<T>
19
+ /**
20
+ * Stores a key value pair in cache
21
+ * @category Cache Port
22
+ * @param key The key to store in cache
23
+ * @param value The value to store in the key. Bear in mind that the value will be JSON stringified
24
+ * @param expirationSeconds TTL for the cache, in case of `false` passed, the cache would not expire
25
+ */
26
+ export declare function set<T>( key: string, value: T, expirationSeconds?: number | false ): Promise<T>
27
+ /**
28
+ * @category Cache Port
29
+ * @param key
30
+ */
31
+ export declare function get<T>( key: string ): Promise<T>
32
+ /**
33
+ * @category Cache Port
34
+ * @param key
35
+ */
36
+ export declare function remove( key: string ): Promise<void>
37
+ /**
38
+ * @category Cache Port
39
+ * @param key
40
+ * @param value
41
+ */
42
+ export declare function incrementBy( key:string, value: number ): Promise<number>
43
+ /**
44
+ * @category Cache Port
45
+ * @param function
46
+ */
47
+ export declare function memoize<F extends Function>( func: F, expirationSeconds?: number | false ): F
48
+
49
+ /**
50
+ * @category Cache Port
51
+ * @param pattern
52
+ */
53
+ export declare function keys( pattern: string ): Promise<string[]>
54
+
55
+ //*******************************************
56
+ // HASHES
57
+ //*******************************************
58
+ /**
59
+ * HEXISTS
60
+ * @category Cache Port
61
+ * */
62
+ export declare function hashExists( key: string, hash:string ): Promise<boolean>
63
+ /** HGET */
64
+ export declare function hashGet<T>( key: string, hash:string ): Promise<T>
65
+ /**
66
+ * HGExTALL
67
+ * It will always return a Record<string, string>, further coerce of types must be implemented by client
68
+ * @category Cache Port
69
+ */
70
+ export declare function hashGetAll( key: string ): Promise<Record<string, string>>
71
+ /**
72
+ * HSET
73
+ * @category Cache Port
74
+ * */
75
+ export declare function hashSet<T>( key: string, hash:string, value: T ): Promise<boolean>
76
+ /**
77
+ * HSETNX
78
+ * @category Cache Port
79
+ * */
80
+ export declare function hashSetIfNotExists<T>( key: string, hash:string, value: T ): Promise<boolean>
81
+ /**
82
+ * HINCRBY
83
+ * @category Cache Port
84
+ * */
85
+ export declare function hashIncrementBy( key: string, hash:string, value: number ): Promise<boolean>
86
+ /**
87
+ * HDEL
88
+ * @category Cache Port
89
+ * */
90
+ export declare function hashDel( key: string, hash:string ): Promise<boolean>
91
+
92
+ /**
93
+ * @category Main Types
94
+ */
95
+ export type CacheType = {
96
+ set: typeof set
97
+ get: typeof get
98
+ keys: typeof keys
99
+ memoize: typeof memoize
100
+ remove: typeof remove
101
+ incrementBy: typeof incrementBy
102
+ hashExists: typeof hashExists
103
+ hashGet: typeof hashGet
104
+ hashGetAll: typeof hashGetAll
105
+ hashSet: typeof hashSet
106
+ hashSetIfNotExists: typeof hashSetIfNotExists
107
+ hashIncrementBy: typeof hashIncrementBy
108
+ hashDel: typeof hashDel
109
+ }
@@ -0,0 +1,216 @@
1
+ import S, { mergeSchemas, omitFromSchema } from '@bett3r-dev/jsonschema-definer';
2
+ import { CommittedEvent, Event } from '@bett3r-dev/pv3-types';
3
+
4
+ export type ClusterStructureType = {
5
+ getCurrentState: ( id?: string ) => Promise<StructureState>,
6
+ getStructureRevision: ( version:number ) => Promise<StructureState | null>,
7
+ subscribeToStructureChanges: ( onStructureChanges: ( change: StructureState ) => void ) => Promise<{ unsubscribe: () => void }>,
8
+ stop: () => Promise<void>,
9
+ } & {
10
+ [key in keyof CommandTypes]: ( payload: CommandTypes[key], expectedVersion?: number ) => Promise<StructureState>;
11
+ }
12
+
13
+ export type StructureState = {
14
+ state: StructureAggregateType,
15
+ version: number,
16
+ }
17
+
18
+ export type StructureCommittedEventType = CommittedEvent<StructureAggregateType & {
19
+ change: {
20
+ broker: {host: string};
21
+ service: {host: string};
22
+ subscriptions: {subscriptions: Record<string,number>};
23
+ }
24
+ }>
25
+
26
+ export const StructureServiceSchema = S.shape({
27
+ hosts: S.array().items( S.string()),
28
+ port: S.number().default( 1984 ).optional(),
29
+ channel: S.string().enum( 'tcp', 'http', 'local' ).default( 'http' ).optional(),
30
+ isExternal: S.boolean().default( false ).optional(),
31
+ possibleSubscriptions: S.array().items( S.string()).optional(),
32
+ possibleModules: S.shape({}).additionalProperties( S.array().items( S.string())).optional(),
33
+ isService: S.boolean().optional(),
34
+ isBroker: S.boolean().optional(),
35
+ endpoints: S.array().items( S.string()).optional(),
36
+ openApiEndpoint: S.string().optional().default( '/documentation/json' ),
37
+ produces: S.shape({}).additionalProperties( S.array().items( S.string())).optional(),
38
+ consumes: S.shape({}).additionalProperties( S.array().items( S.string())).optional(),
39
+ });
40
+ export type StructureServiceType = typeof StructureServiceSchema.type;
41
+
42
+ export const RetryableStatuses = [ 429, 503, 504 ];
43
+
44
+ export const StructureSubscriptionSchema = S.shape({
45
+ producers: S.array().items( S.string()),
46
+ module: S.string(),
47
+ consumer: S.string(),
48
+ load: S.number().optional(),
49
+ eventstore: S.string().optional(),
50
+ events: S.array().items( S.string()).optional(),
51
+ manualDataSplits: S.number().minimum( 1 ).optional(),
52
+ mirrorDataSplitsWithConsumers: S.boolean().optional().default( true ),
53
+ customDataSplits: S.array().items( S.array().items( S.string())).optional(),
54
+ streams: S.string().optional(),
55
+ endpoint: S.string().optional(),
56
+ retryAfterSecs: S.number().optional(),
57
+ retryableStatuses: S.array().items( S.number()).default( RetryableStatuses ).optional(),
58
+ dataSplits: S.shape({
59
+ custom: S.array().items( S.string()),
60
+ manual: S.array().items( S.string()),
61
+ automatic: S.array().items( S.string()),
62
+ }).partial().optional(),
63
+ });
64
+ export type StructureSubscriptionType = typeof StructureSubscriptionSchema.type;
65
+
66
+ export const StructureBrokerSubscriptionSchema = S.shape({
67
+ host: S.string(),
68
+ index: S.number(),
69
+ custom: S.array().items( S.string()).optional()
70
+ });
71
+ export type StructureBrokerSubscriptionType = typeof StructureBrokerSubscriptionSchema.type;
72
+
73
+ export const StructureBrokerSchema = S.shape({
74
+ host: S.string(),
75
+ subscriptions: S.shape({}).additionalProperties(
76
+ S.shape({}).additionalProperties( StructureBrokerSubscriptionSchema )
77
+ ),
78
+ });
79
+ export type StructureBrokerType = typeof StructureBrokerSchema.type;
80
+
81
+ export const StructureSubscriptionsSchema = S.shape({})
82
+ .additionalProperties( StructureSubscriptionSchema );
83
+
84
+ export const StructureServicesSchema = S.shape({})
85
+ .additionalProperties( StructureServiceSchema );
86
+ export type StructureServicesType = typeof StructureServicesSchema.type;
87
+
88
+ export const StructureBrokersSchema = S.shape({})
89
+ .additionalProperties( omitFromSchema( StructureBrokerSchema, ['host']));
90
+
91
+ export const StructureAggregateSchema = S.shape({
92
+ subscriptions: StructureSubscriptionsSchema,
93
+ services: StructureServicesSchema,
94
+ brokers: StructureBrokersSchema,
95
+ clusterRevision: S.number(),
96
+ servicesRevisionAck: S.array().items( S.string())
97
+ });
98
+ export type StructureAggregateType = typeof StructureAggregateSchema.type;
99
+
100
+ export const AddSubscriptionsCommandSchema = S.shape({
101
+ subscriptions: StructureSubscriptionsSchema, doNotRebalance: S.boolean().optional()
102
+ });
103
+ export const EditSubscriptionCommandSchema = S.shape({
104
+ id: S.string(),
105
+ subscription: StructureSubscriptionSchema.partial(),
106
+ doNotRebalance: S.boolean().optional()
107
+ });
108
+ export const DeleteSubscriptionCommandSchema = S.shape({
109
+ id: S.string(),
110
+ doNotRebalance: S.boolean().optional()
111
+ });
112
+
113
+ export const UpsertHostCommandSchema = mergeSchemas( omitFromSchema( StructureServiceSchema, ['hosts']), S.shape({
114
+ service: S.string(),
115
+ host: S.string(),
116
+ doNotRebalance: S.boolean().optional()
117
+ }));
118
+ export type UpsertHostCommandType = typeof UpsertHostCommandSchema.type;
119
+
120
+ export const RemoveHostCommandSchema = S.shape({ host: S.string() });
121
+
122
+ export const SetStructureUpsertHostCommandSchema = mergeSchemas( StructureServiceSchema, S.shape({
123
+ service: S.string(),
124
+ }));
125
+ export const SetStructureCommandSchema = S.shape({
126
+ upsertHosts: S.array().items( UpsertHostCommandSchema ),
127
+ removeHosts: S.array().items( S.string()),
128
+ subscriptions: StructureSubscriptionsSchema.optional()
129
+ });
130
+
131
+ export const RebalanceSubscriptionsCommandSchema = S.shape({})
132
+ .additionalProperties( omitFromSchema( StructureBrokerSchema, ['host']));
133
+ export type RebalanceSubscriptionsCommandType = typeof RebalanceSubscriptionsCommandSchema.type;
134
+
135
+ export const RemoveBrokerCommandSchema = S.shape({ host: S.string() });
136
+
137
+ export const AckStructureSchema = S.shape({ host: S.string(), revision: S.number() });
138
+
139
+ export type CommandTypes = {
140
+ UpsertHost: typeof UpsertHostCommandSchema.type
141
+ RemoveHost: typeof RemoveHostCommandSchema.type
142
+ RebalanceSubscriptions: typeof RebalanceSubscriptionsCommandSchema.type
143
+ AddSubscriptions: typeof AddSubscriptionsCommandSchema.type
144
+ EditSubscription: typeof EditSubscriptionCommandSchema.type
145
+ DeleteSubscription: typeof DeleteSubscriptionCommandSchema.type
146
+ SetStructure: typeof SetStructureCommandSchema.type
147
+ AckStructure: typeof AckStructureSchema.type
148
+ }
149
+
150
+ //*******************************************
151
+ // EVENTS
152
+ //*******************************************
153
+
154
+ export const structureEvents = {
155
+ HostUpserted: () => Event({
156
+ schema: S.shape({
157
+ services: StructureServicesSchema,
158
+ brokers: StructureBrokersSchema.optional(),
159
+ doNotRebalance: S.boolean().optional(),
160
+ })
161
+ }),
162
+ HostRemoved: () => Event({
163
+ schema: S.shape({
164
+ brokers: StructureBrokersSchema,
165
+ host: S.string(),
166
+ service: S.string()
167
+ })
168
+ }),
169
+ SubscriptionsRebalanced: () => Event({
170
+ schema: S.shape({
171
+ brokers: StructureBrokersSchema,
172
+ })
173
+ }),
174
+ SubscriptionsAdded: () => Event({
175
+ schema: S.shape({
176
+ brokers: StructureBrokersSchema.optional(),
177
+ subscriptions: StructureSubscriptionsSchema,
178
+ doNotRebalance: S.boolean().optional(),
179
+ })
180
+ }),
181
+ SubscriptionEdited: () => Event({
182
+ schema: S.shape({
183
+ brokers: StructureBrokersSchema.optional(),
184
+ subscriptions: StructureSubscriptionsSchema,
185
+ doNotRebalance: S.boolean().optional(),
186
+ })
187
+ }),
188
+ SubscriptionDeleted: () => Event({
189
+ schema: S.shape({
190
+ id: S.string(),
191
+ doNotRebalance: S.boolean().optional(),
192
+ brokers: StructureBrokersSchema.optional(),
193
+ })
194
+ }),
195
+ StructureSet: () => Event({
196
+ schema: StructureAggregateSchema
197
+ }),
198
+ StructureAcked: () => Event({
199
+ schema: AckStructureSchema
200
+ })
201
+ };
202
+
203
+
204
+ export const ClusterRoutesConfigSchema = S.shape({
205
+ clusterRoutesAggregateCollection: S.string().optional(),
206
+ clusterRoutesEventstoreCollection: S.string().optional(),
207
+ clusterRoutesEventstoreStream: S.string().optional(),
208
+ });
209
+ export type ClusterRoutesConfigType = typeof ClusterRoutesConfigSchema.type;
210
+
211
+ export const ClusterStructureConfigSchema = S.shape({
212
+ structureEventstoreStream: S.string().default( 'environment' ).optional(),
213
+ structureAggregateCollection: S.string().default( 'cluster_infrastructure' ).optional(),
214
+ structureEventstoreCollection: S.string().default( 'cluster_eventstore' ).optional(),
215
+ });
216
+ export type ClusterStructureConfigType = typeof ClusterStructureConfigSchema.type;
@@ -0,0 +1,71 @@
1
+ import { ObjectSchema } from '@bett3r-dev/jsonschema-definer';
2
+ import { AdapterBase, PortEmitter, PortEmitterEvents } from './ports';
3
+ import { TypedEventEmitter } from './typedEventEmitter';
4
+
5
+ export type ConfigurationPortEmitterEvents = PortEmitterEvents & {
6
+ argsApplied: {config: any},
7
+ }
8
+
9
+ /**
10
+ * Patches the configuration of the system with a specified object
11
+ * @param config The object to patch the configuration with
12
+ */
13
+ export declare function applyConfig( config: Record<string, any> ): Promise<Record<string, any>>
14
+
15
+ /**
16
+ * @category Main Types
17
+ */
18
+ export type ConfigurationAdapter = AdapterBase & {
19
+ applyConfig: typeof applyConfig
20
+ }
21
+
22
+ /**
23
+ * Returns a configuration for an specific module in the system
24
+ * @category Configuration Port
25
+ * @param name The name of the key in the config file structure
26
+ * @param schema The schema of the specific configuration piece
27
+ */
28
+ export declare function getModuleConfig <CONFIG extends object>( name:string, schema: ObjectSchema<CONFIG, true> | ObjectSchema<CONFIG, false> ): {
29
+ config: CONFIG,
30
+ onArgsApplied: ( callback: ( config: CONFIG ) => void ) => void
31
+ }
32
+
33
+ export type CliArgDefinition = {
34
+ type: 'string' | 'number' | 'boolean',
35
+ name: string,
36
+ alias?: string,
37
+ description?: string,
38
+ default?: any
39
+ }
40
+
41
+ export type CliCommandDefinition = {
42
+ /** The name of the command, accepts positional arguments. Use <> for required and [] for optionals. Optionals might optionally accept and array like [optional..]
43
+ * @example
44
+ * 'test <required|requiredAlias> [optional1] [optional2..]'
45
+ */
46
+ name: string,
47
+ alias?: string,
48
+ description?: string,
49
+ /** The descriptions for the positional arguements of the command. MUST BE SPECIFIED IN THE NAME FIRST */
50
+ arguments?: CliArgDefinition[],
51
+ handler: ( args: Record<string, any> ) => void;
52
+ }
53
+
54
+ /**
55
+ * @category Main Types
56
+ */
57
+ export type ConfigurationType = {
58
+ _getConfig: () => Record<string, any>,
59
+ getModuleConfig: typeof getModuleConfig,
60
+ getModules: () => string[],
61
+ getArgs: () => Promise<Record<string, any>>,
62
+ applyArgs: () => Promise<Record<string, any>>,
63
+ emitter: TypedEventEmitter<ConfigurationPortEmitterEvents>,
64
+ configureCliArg: ( options: CliArgDefinition ) => void,
65
+ configureCliCommand: ( options: CliCommandDefinition ) => void,
66
+ }
67
+
68
+ /**
69
+ * @category Main Types
70
+ */
71
+ export type ConfigurationPort = Omit<PortEmitter, 'emitter'> & ConfigurationType