UNPKG

8.15 kBTypeScriptView Raw
1declare module 'oblak' {
2 // helpers
3 type AnyFunction<TReturn = any> = (...params) => TReturn;
4
5 namespace Tools {
6 type SettingsFunction = (obj) => any;
7 // express
8 type ExpressMiddleware<TReturn = void> = (req: any, res?: any, next?: any) => TReturn;
9 type ExpressAsyncMiddleware = ExpressMiddleware<Promise<void>>;
10
11 interface Domain {
12 settings: SettingsFunction;
13 command: {
14 only: {
15 ifExists: () => any;
16 ifNotExists: () => any;
17 ifValidatedBy: (validator: string | object | AnyFunction) => any;
18 ifState: (condition: AnyFunction) => any;
19 after: (rule: AnyFunction) => any;
20 }
21 };
22 event: {
23 settings: SettingsFunction;
24 };
25 }
26
27 interface Readmodels {
28 settings: SettingsFunction;
29 extendEvent: (obj: any) => any;
30 extendPreEvent: (obj: any) => any;
31 only: {
32 ifExists: () => any;
33 ifNotExists: () => any;
34 if: (condition: Function) => any;
35 },
36 identifier: (obj: any) => any;
37 }
38
39 interface Saga {
40 settings: SettingsFunction;
41 only: {
42 ifExists: () => any;
43 ifNotExists: () => any;
44 if: (condition: Function) => any;
45 };
46 shouldHandle: () => any;
47 identifier: () => Oblak.EventIdentityDefinition;
48 }
49
50 interface Rest {
51 services: (fn: AnyFunction) => ExpressMiddleware,
52 readmodels: {
53 one: (cmd: any, options?: any) => ExpressMiddleware;
54 list: (cmd: any, options?: any) => ExpressMiddleware;
55 },
56 command: (cmd: any, options?: any) => ExpressMiddleware,
57 async: (asyncMiddleware: ExpressAsyncMiddleware ) => ExpressMiddleware;
58 }
59 }
60
61 interface Tools {
62 saga: Tools.Saga;
63 readmodels: Tools.Readmodels;
64 domain: Tools.Domain;
65 rest: Tools.Rest;
66 }
67
68 class Oblak {
69 static tools : Tools;
70 static debug : () => Oblak;
71 use: AnyFunction;
72 addHook: AnyFunction;
73 run: AnyFunction<Promise<any>>;
74 init: AnyFunction<Promise<any>>;
75 clear: AnyFunction<Promise<any>>;
76 }
77
78 export = Oblak;
79}
80
81declare namespace Oblak {
82 interface PlainObject {
83 [key: string]: any;
84 }
85
86 interface OblakMetadataBase {
87 [key: string]: any;
88 timestamp: number;
89 correlationId: string;
90 causationId: string;
91 }
92
93 interface OblakDomainEvent {
94 payload: PlainObject;
95 metadata: OblakMetadataBase;
96 context: string;
97 name: string;
98 aggregate: {
99 name: string;
100 id: string;
101 revision;
102 };
103 fullname: string;
104 }
105
106 interface OblakReadmodelEvent {
107 payload: PlainObject;
108 metadata: OblakMetadataBase;
109 context: string;
110 aggregate: {
111 name: string;
112 id: string;
113 revision: number;
114 };
115 event: {
116 name: string;
117 id: string;
118 }
119 fullname: string;
120 }
121
122 type EventIdentityDefinition = string | ((event: OblakDomainEvent) => string);
123
124 type AggregateFunction<T> = (id?: string) => T;
125
126 namespace Data {
127 interface ReadmodelCollectionEvents {
128 create: string;
129 update: string;
130 delete: string;
131 any: string;
132 }
133
134 interface DomainEvents {
135
136 }
137
138 interface ReadModelsEvents {
139
140 }
141
142 interface Events {
143 domain: DomainEvents;
144 readmodels: ReadModelsEvents;
145 }
146
147 interface Exports {
148 events: Events;
149 }
150 }
151
152 interface Data extends Data.Exports {}
153
154 namespace Api {
155 interface Writable<TCommand> {
156 getDomain: () => Domain<TCommand>;
157 }
158
159 interface ReadableDomain {
160 getDomain: () => DomainReadable;
161 }
162
163 interface Readable {
164 getReadmodels: () => Readmodels;
165 }
166
167 interface Base {
168 getServices: () => any;
169 }
170
171 interface Domain<TCommand> {}
172 interface DomainReadable {}
173 interface Readmodels {}
174
175 type Command<TPayload, TReturn> = (payload?:TPayload, metadata?:PlainObject) => TReturn;
176 type ReadableAggregate = (id:string) => Promise<any>;
177
178 interface ReadmodelQuery<T> extends PromiseLike<T> {
179 find: () => ReadmodelQuery<T>;
180 skip: () => ReadmodelQuery<T>;
181 size: () => ReadmodelQuery<T>;
182 }
183 }
184
185 namespace Saga {
186 interface Api extends Api.Base, Api.Readable, Api.Writable<void> {}
187
188 interface Model {
189 id: string;
190 set(attribute: any, value?: any): void;
191 get(attr: string): any;
192 destroy(): void;
193 }
194
195 type ReactionMiddleware = (event: OblakDomainEvent, saga: Model, app: Api) => void | Promise<void>;
196 type Reaction = Array<ReactionMiddleware> | ReactionMiddleware;
197
198 interface Identity {
199 [key: string]: EventIdentityDefinition;
200 }
201
202 interface Reactions {
203 [key: string]: Reaction;
204 }
205
206 type Command<TPayload, TReturn> = (payload?: TPayload, metadata?: PlainObject) => TReturn;
207
208 interface Exports {
209 identity: Identity;
210 reactions: Reactions;
211 }
212 }
213 interface Saga extends Saga.Exports {}
214
215 namespace Readmodel {
216 interface Api extends Api.Base, Api.Readable {}
217
218 interface Elasticsearch6IntexMappings {
219 properties: any;
220 }
221
222 interface Elasticsearch6Intex {
223 mappings: Elasticsearch6IntexMappings;
224 }
225
226 interface Elasticsearch6RepositorySettings {
227 index: Elasticsearch6Intex;
228 }
229
230 interface MongodbRepositorySettings {
231 indexes: Array<any>;
232 }
233
234 interface Model {
235 id: string;
236 set(attribute: any, value?: any): void;
237 get(attr: string): any;
238 destroy(): void;
239 }
240
241 type ReactionMiddleware = (event: OblakDomainEvent, viewmodel: Model, app: Api) => void | Promise<void>;
242 type Reaction = Array<ReactionMiddleware> | ReactionMiddleware;
243
244 interface Identity {
245 [key: string]: EventIdentityDefinition;
246 }
247
248 interface Reactions {
249 [key: string]: Reaction;
250 }
251
252 interface OblakAggregators {
253 [key: string]: any;
254 }
255
256 interface OblakRepositorySettings {
257 aggregators: OblakAggregators;
258 }
259
260 interface RepositorySettings {
261 elasticsearch6?: Elasticsearch6RepositorySettings;
262 mongodb?: MongodbRepositorySettings;
263 oblak?: OblakRepositorySettings;
264 }
265
266 interface Exports {
267 schema?: string;
268 initialState?: PlainObject;
269 identity: Identity;
270 reactions: Reactions;
271 repositorySettings?: RepositorySettings;
272 }
273 }
274 interface Readmodel extends Readmodel.Exports {}
275
276 namespace Domain {
277 interface Api extends Api.Base, Api.Readable, Api.ReadableDomain {}
278
279 interface Command<TPayload> {
280 payload: TPayload;
281 metadata: OblakMetadataBase;
282 }
283
284 interface ModelApply {
285 (name: string, payload: PlainObject, metadata: OblakMetadataBase): void;
286 }
287
288 interface Model<TApply> {
289 id: string;
290
291 apply: TApply;
292 /**
293 * Gets an attribute of the vm.
294 * @param attr The attribute name.
295 * @return The result value.
296 *
297 * @example:
298 * aggregate.get('firstname'); // returns 'Jack'
299 */
300 get(attr: string): any;
301 /**
302 * Sets attributes for the aggregate.
303 *
304 * @example:
305 * aggregate.set('firstname', 'Jack');
306 * // or
307 * aggregate.set({
308 * firstname: 'Jack',
309 * lastname: 'X-Man'
310 * });
311 */
312 set(attribute: any, value?: any): void;
313
314 /**
315 * Marks this aggregate as destroyed.
316 */
317 destroy(): void;
318
319 /**
320 * Returns true if this aggregate is destroyed.
321 */
322 isDestroyed(): boolean;
323 }
324
325 interface GenericModelApply extends ModelApply {
326 [key: string]: (payload?: PlainObject, metadata?: OblakMetadataBase) => void;
327 }
328
329 type CommandMiddleware<TApply, TPlayload> = (command?: Command<TPlayload>, aggregate?: Model<TApply>, app?: Api) => void | Promise<void>;
330 type CommandMiddlewares<TApply, TPlayload> = Array<CommandMiddleware<TApply, TPlayload>> | CommandMiddleware<TApply, TPlayload>;
331
332 type EventMiddleware = (event?: OblakDomainEvent, aggregate?: Model<void>) => void;
333 type EVentMiddlewares = Array<EventMiddleware> | EventMiddleware;
334
335 interface Commands<TApply> {
336 [key: string]: CommandMiddlewares<TApply, PlainObject>;
337 }
338
339 interface Events {
340 [key: string]: EVentMiddlewares;
341 }
342
343 interface Aggregate extends Exports<GenericModelApply, Commands<GenericModelApply>> {
344 }
345
346 interface Exports<TApply, TCommands> {
347 /**
348 * Aggregates initial stae
349 */
350 initialState: PlainObject;
351 /**
352 * Aggregate command defintion
353 */
354 commands: TCommands;
355 /**
356 * Your aggregate event definitions
357 */
358 events: Events;
359 /**
360 * Aggreagte ID Generator fuction
361 */
362 idGenerator: (command?: Command<PlainObject>, app?: Api) => void | Promise<void>;
363 }
364 }
365 interface Aggregate extends Domain.Aggregate {}
366}