@my-devkit/firebase 1.0.194 → 1.0.196

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 (75) hide show
  1. package/.eslintrc.js +3 -3
  2. package/dist/app-factory.js +9 -9
  3. package/dist/app-factory.js.map +1 -1
  4. package/dist/bus.js +1 -4
  5. package/dist/bus.js.map +1 -1
  6. package/dist/constants.js +1 -1
  7. package/dist/context.js +1 -1
  8. package/dist/decorators/controller/body.js.map +1 -1
  9. package/dist/decorators/controller/body.spec.js.map +1 -1
  10. package/dist/decorators/controller/controller.js +1 -1
  11. package/dist/decorators/controller/controller.js.map +1 -1
  12. package/dist/decorators/controller/get.js +1 -1
  13. package/dist/decorators/controller/param.js.map +1 -1
  14. package/dist/decorators/controller/param.spec.js.map +1 -1
  15. package/dist/decorators/controller/post.js +1 -1
  16. package/dist/decorators/controller/query.js.map +1 -1
  17. package/dist/decorators/controller/query.spec.js.map +1 -1
  18. package/dist/decorators/handler/command-handler.js.map +1 -1
  19. package/dist/decorators/handler/command-handler.spec.js.map +1 -1
  20. package/dist/decorators/handler/event-handler.js.map +1 -1
  21. package/dist/decorators/handler/event-handler.spec.js.map +1 -1
  22. package/dist/decorators/injectable.js +1 -1
  23. package/dist/decorators/injectable.js.map +1 -1
  24. package/dist/decorators/module.js +1 -1
  25. package/dist/decorators/module.js.map +1 -1
  26. package/dist/decorators/transactional-client.js +1 -1
  27. package/dist/decorators/transactional-client.js.map +1 -1
  28. package/dist/firestore-client.js +10 -10
  29. package/dist/firestore-client.js.map +1 -1
  30. package/dist/handler-helper.js.map +1 -1
  31. package/dist/injector.js +1 -1
  32. package/dist/injector.js.map +1 -1
  33. package/dist/reflect.js +7 -7
  34. package/dist/reflect.js.map +1 -1
  35. package/dist/sanity-check/module.js +1 -1
  36. package/dist/server/middlewares/authentication-middleware.js +1 -3
  37. package/dist/server/middlewares/authentication-middleware.js.map +1 -1
  38. package/dist/server/middlewares/create-context-middleware.js.map +1 -1
  39. package/dist/server/middlewares/error-middleware.js.map +1 -1
  40. package/dist/server/middlewares/headers-middleware.js.map +1 -1
  41. package/dist/server/server.js.map +1 -1
  42. package/package.json +4 -4
  43. package/src/app-factory.ts +31 -69
  44. package/src/bus.ts +5 -22
  45. package/src/constants.ts +1 -1
  46. package/src/context.ts +1 -1
  47. package/src/decorators/controller/body.spec.ts +3 -9
  48. package/src/decorators/controller/body.ts +3 -9
  49. package/src/decorators/controller/controller.ts +1 -1
  50. package/src/decorators/controller/get.ts +1 -1
  51. package/src/decorators/controller/param.spec.ts +5 -14
  52. package/src/decorators/controller/param.ts +2 -7
  53. package/src/decorators/controller/post.ts +1 -1
  54. package/src/decorators/controller/query.spec.ts +4 -10
  55. package/src/decorators/controller/query.ts +2 -7
  56. package/src/decorators/handler/command-handler.spec.ts +2 -6
  57. package/src/decorators/handler/command-handler.ts +3 -9
  58. package/src/decorators/handler/event-handler.spec.ts +2 -6
  59. package/src/decorators/handler/event-handler.ts +3 -9
  60. package/src/decorators/injectable.ts +1 -1
  61. package/src/decorators/module.ts +1 -1
  62. package/src/decorators/transactional-client.ts +1 -1
  63. package/src/execution-mode-enum.ts +1 -1
  64. package/src/firestore-client.ts +32 -79
  65. package/src/handler-helper.ts +5 -18
  66. package/src/injector.ts +1 -1
  67. package/src/interfaces/from-array.ts +1 -2
  68. package/src/reflect.ts +11 -20
  69. package/src/request-method.enum.ts +1 -1
  70. package/src/sanity-check/module.ts +1 -1
  71. package/src/server/middlewares/authentication-middleware.ts +4 -14
  72. package/src/server/middlewares/create-context-middleware.ts +1 -5
  73. package/src/server/middlewares/error-middleware.ts +1 -5
  74. package/src/server/middlewares/headers-middleware.ts +1 -5
  75. package/src/server/server.ts +10 -32
@@ -56,9 +56,7 @@ describe('Given a CommandHandler decorator', () => {
56
56
 
57
57
  reflect(TestClass).getHandlerConfiguration();
58
58
  } catch (error) {
59
- expect(error.message).equal(
60
- 'TestClass.create should have a command as argument!'
61
- );
59
+ expect(error.message).equal('TestClass.create should have a command as argument!');
62
60
  }
63
61
  });
64
62
  });
@@ -80,9 +78,7 @@ describe('Given a CommandHandler decorator', () => {
80
78
  }
81
79
  }
82
80
  it('Then we should retrieve information via reflect', async () => {
83
- const commandHandlers = Array.from(
84
- reflect(TestClass).getHandlerConfiguration().commandHandlers.values()
85
- );
81
+ const commandHandlers = Array.from(reflect(TestClass).getHandlerConfiguration().commandHandlers.values());
86
82
 
87
83
  expect(commandHandlers.length).equal(2, 'Wrong route count detected');
88
84
 
@@ -7,21 +7,15 @@ export function CommandHandler(): MethodDecorator {
7
7
  const paramTypes = reflect(target, propertyKey).getParameters();
8
8
 
9
9
  if (paramTypes.length === 0) {
10
- throw new Error(
11
- `${target.constructor.name}.${propertyKey.toString()} should have one argument!`
12
- );
10
+ throw new Error(`${target.constructor.name}.${propertyKey.toString()} should have one argument!`);
13
11
  }
14
12
 
15
13
  if (paramTypes.length > 1) {
16
- throw new Error(
17
- `${target.constructor.name}.${propertyKey.toString()} should have only one argument!`
18
- );
14
+ throw new Error(`${target.constructor.name}.${propertyKey.toString()} should have only one argument!`);
19
15
  }
20
16
 
21
17
  if (!(paramTypes[0].prototype instanceof Command)) {
22
- throw new Error(
23
- `${target.constructor.name}.${propertyKey.toString()} should have a command as argument!`
24
- );
18
+ throw new Error(`${target.constructor.name}.${propertyKey.toString()} should have a command as argument!`);
25
19
  }
26
20
 
27
21
  reflect(target, propertyKey).registerCommandHandler(<any>paramTypes[0].prototype);
@@ -57,9 +57,7 @@ describe('Given a EventHandler decorator', () => {
57
57
 
58
58
  reflect(TestClass).getHandlerConfiguration();
59
59
  } catch (error) {
60
- expect(error.message).equal(
61
- 'TestClass.created should have an event as argument!'
62
- );
60
+ expect(error.message).equal('TestClass.created should have an event as argument!');
63
61
  }
64
62
  });
65
63
  });
@@ -81,9 +79,7 @@ describe('Given a EventHandler decorator', () => {
81
79
  }
82
80
  }
83
81
  it('Then we should retrieve information via reflect', async () => {
84
- const eventHandlers = Array.from(
85
- reflect(TestClass).getHandlerConfiguration().eventHandlers.values()
86
- );
82
+ const eventHandlers = Array.from(reflect(TestClass).getHandlerConfiguration().eventHandlers.values());
87
83
 
88
84
  expect(eventHandlers.length).equal(2, 'Wrong route count detected');
89
85
 
@@ -8,21 +8,15 @@ export function EventHandler(mode: ExecutionMode): MethodDecorator {
8
8
  const paramTypes = reflect(target, propertyKey).getParameters();
9
9
 
10
10
  if (paramTypes.length === 0) {
11
- throw new Error(
12
- `${target.constructor.name}.${propertyKey.toString()} should have one argument!`
13
- );
11
+ throw new Error(`${target.constructor.name}.${propertyKey.toString()} should have one argument!`);
14
12
  }
15
13
 
16
14
  if (paramTypes.length > 1) {
17
- throw new Error(
18
- `${target.constructor.name}.${propertyKey.toString()} should have only one argument!`
19
- );
15
+ throw new Error(`${target.constructor.name}.${propertyKey.toString()} should have only one argument!`);
20
16
  }
21
17
 
22
18
  if (!(paramTypes[0].prototype instanceof Event)) {
23
- throw new Error(
24
- `${target.constructor.name}.${propertyKey.toString()} should have an event as argument!`
25
- );
19
+ throw new Error(`${target.constructor.name}.${propertyKey.toString()} should have an event as argument!`);
26
20
  }
27
21
 
28
22
  reflect(target, propertyKey).registerEventHandler(<any>paramTypes[0].prototype, mode);
@@ -1,7 +1,7 @@
1
1
  import { reflect } from '../reflect';
2
2
 
3
3
  export function Injectable(): ClassDecorator {
4
- return target => {
4
+ return (target) => {
5
5
  reflect(target).registerInjectable();
6
6
  };
7
7
  }
@@ -19,7 +19,7 @@ export interface ModuleOptions {
19
19
  }
20
20
 
21
21
  export function Module(options: ModuleOptions): ClassDecorator {
22
- return target => {
22
+ return (target) => {
23
23
  for (const key in options) {
24
24
  if (options.hasOwnProperty(key)) {
25
25
  Reflect.defineMetadata(key, options[key], target);
@@ -1,7 +1,7 @@
1
1
  import { reflect } from '../reflect';
2
2
 
3
3
  export function TransactionalClient(): ClassDecorator {
4
- return target => {
4
+ return (target) => {
5
5
  reflect(target).registerTransactionalClient();
6
6
  };
7
7
  }
@@ -1,4 +1,4 @@
1
1
  export enum ExecutionMode {
2
2
  Synchronous = <any>'Synchronous',
3
- Asynchronous = <any>'Asynchronous'
3
+ Asynchronous = <any>'Asynchronous',
4
4
  }
@@ -1,12 +1,4 @@
1
- import {
2
- _orderBy,
3
- Cacheable,
4
- deserialize,
5
- Document,
6
- Logger,
7
- NotFoundError,
8
- serialize
9
- } from '@my-devkit/core';
1
+ import { _orderBy, Cacheable, deserialize, Document, Logger, NotFoundError, serialize } from '@my-devkit/core';
10
2
  import { getFirestore } from 'firebase-admin/firestore';
11
3
 
12
4
  import { Injectable, TransactionalClient } from './decorators';
@@ -55,14 +47,12 @@ export class FirestoreClient implements ITransactionalClient {
55
47
  public async _findWhere<T extends Document>(
56
48
  collection: string,
57
49
  whereClauses: [string, FirestoreClient.Operator, any | any[]][] = [],
58
- options?: FirestoreClient.QueryOptions
50
+ options?: FirestoreClient.QueryOptions,
59
51
  ): Promise<T> {
60
52
  const documents = await this._getWhere<T>(collection, whereClauses, options);
61
53
 
62
54
  if (documents.length > 1) {
63
- throw new Error(
64
- `Transactional Repository: Too many documents found (${documents.length})`
65
- );
55
+ throw new Error(`Transactional Repository: Too many documents found (${documents.length})`);
66
56
  }
67
57
 
68
58
  return documents[0] || null;
@@ -78,21 +68,16 @@ export class FirestoreClient implements ITransactionalClient {
78
68
  return result;
79
69
  }
80
70
 
81
- public async _getAll<T extends Document>(
82
- collection: string,
83
- options?: FirestoreClient.QueryOptions
84
- ): Promise<T[]> {
71
+ public async _getAll<T extends Document>(collection: string, options?: FirestoreClient.QueryOptions): Promise<T[]> {
85
72
  return this._getWhere(collection, [], options);
86
73
  }
87
74
 
88
75
  public async _getWhere<T extends Document>(
89
76
  collection: string,
90
77
  whereClauses: [string, FirestoreClient.Operator, any | any[]][] = [],
91
- options?: FirestoreClient.QueryOptions
78
+ options?: FirestoreClient.QueryOptions,
92
79
  ): Promise<T[]> {
93
- Logger.info(
94
- `Repository: getWhere ${collection} whereClauses: ${JSON.stringify(whereClauses)}`
95
- );
80
+ Logger.info(`Repository: getWhere ${collection} whereClauses: ${JSON.stringify(whereClauses)}`);
96
81
  const documentMap = await this.queryCollection<T>(collection, whereClauses, options);
97
82
 
98
83
  for (const operation of Array.from(this.operations.values())) {
@@ -102,36 +87,26 @@ export class FirestoreClient implements ITransactionalClient {
102
87
  case FirestoreClient.Action.Create:
103
88
  if (this.doesDocumentMatchQuery(operation.document, collection, whereClauses)) {
104
89
  documentMap.set(operation.document._path, <T>operation.document);
105
- Logger.info(
106
- `Repository: Document ${operation.document._path} added to result`
107
- );
90
+ Logger.info(`Repository: Document ${operation.document._path} added to result`);
108
91
  }
109
92
  break;
110
93
  case FirestoreClient.Action.Update:
111
94
  if (this.doesDocumentMatchQuery(operation.document, collection, whereClauses)) {
112
- Logger.info(
113
- `Repository: Document ${operation.document._path} ${exists ? 'updated from' : 'added to'} result`
114
- );
95
+ Logger.info(`Repository: Document ${operation.document._path} ${exists ? 'updated from' : 'added to'} result`);
115
96
  documentMap.set(operation.document._path, <T>operation.document);
116
97
  } else if (exists) {
117
98
  documentMap.delete(operation.document._path);
118
- Logger.info(
119
- `Repository: Document ${operation.document._path} removed from result`
120
- );
99
+ Logger.info(`Repository: Document ${operation.document._path} removed from result`);
121
100
  }
122
101
  break;
123
102
  case FirestoreClient.Action.Delete:
124
103
  if (exists) {
125
104
  documentMap.delete(operation.document._path);
126
- Logger.info(
127
- `Repository: Document ${operation.document._path} removed from result`
128
- );
105
+ Logger.info(`Repository: Document ${operation.document._path} removed from result`);
129
106
  }
130
107
  break;
131
108
  default:
132
- throw new Error(
133
- `FirestoreClient: Unhandled existing operation ${operation.action} on ${operation.document._path}`
134
- );
109
+ throw new Error(`FirestoreClient: Unhandled existing operation ${operation.action} on ${operation.document._path}`);
135
110
  }
136
111
  }
137
112
 
@@ -156,27 +131,21 @@ export class FirestoreClient implements ITransactionalClient {
156
131
  if (existingOperation) {
157
132
  switch (existingOperation.action) {
158
133
  case FirestoreClient.Action.Create:
159
- throw new Error(
160
- `FirestoreClient: can't create twice document ${document._path}`
161
- );
134
+ throw new Error(`FirestoreClient: can't create twice document ${document._path}`);
162
135
  case FirestoreClient.Action.Update:
163
- throw new Error(
164
- `FirestoreClient: can't create already existing document ${document._path}`
165
- );
136
+ throw new Error(`FirestoreClient: can't create already existing document ${document._path}`);
166
137
  case FirestoreClient.Action.Delete:
167
138
  existingOperation.action = FirestoreClient.Action.Update;
168
139
  existingOperation.document = document;
169
140
  break;
170
141
  default:
171
- throw new Error(
172
- `FirestoreClient: Unhandled existing operation ${existingOperation.action} on ${document._path}`
173
- );
142
+ throw new Error(`FirestoreClient: Unhandled existing operation ${existingOperation.action} on ${document._path}`);
174
143
  }
175
144
  } else {
176
145
  this.operations.set(document._path, {
177
146
  document,
178
147
  action: FirestoreClient.Action.Create,
179
- committed: false
148
+ committed: false,
180
149
  });
181
150
  }
182
151
  }
@@ -191,19 +160,15 @@ export class FirestoreClient implements ITransactionalClient {
191
160
  existingOperation.document = document;
192
161
  break;
193
162
  case FirestoreClient.Action.Delete:
194
- throw new Error(
195
- `FirestoreClient: can't update deleted document ${document._path}`
196
- );
163
+ throw new Error(`FirestoreClient: can't update deleted document ${document._path}`);
197
164
  default:
198
- throw new Error(
199
- `FirestoreClient: Unhandled existing operation ${existingOperation.action} on ${document._path}`
200
- );
165
+ throw new Error(`FirestoreClient: Unhandled existing operation ${existingOperation.action} on ${document._path}`);
201
166
  }
202
167
  } else {
203
168
  this.operations.set(document._path, {
204
169
  document,
205
170
  action: FirestoreClient.Action.Update,
206
- committed: false
171
+ committed: false,
207
172
  });
208
173
  }
209
174
  }
@@ -221,26 +186,20 @@ export class FirestoreClient implements ITransactionalClient {
221
186
  existingOperation.action = FirestoreClient.Action.Delete;
222
187
  break;
223
188
  case FirestoreClient.Action.Delete:
224
- throw new Error(
225
- `FirestoreClient: can't delete already deleted document ${document._path}`
226
- );
189
+ throw new Error(`FirestoreClient: can't delete already deleted document ${document._path}`);
227
190
  default:
228
- throw new Error(
229
- `FirestoreClient: Unhandled existing operation ${existingOperation.action} on ${document._path}`
230
- );
191
+ throw new Error(`FirestoreClient: Unhandled existing operation ${existingOperation.action} on ${document._path}`);
231
192
  }
232
193
  } else {
233
194
  this.operations.set(document._path, {
234
195
  document,
235
196
  action: FirestoreClient.Action.Delete,
236
- committed: false
197
+ committed: false,
237
198
  });
238
199
  }
239
200
  }
240
201
 
241
- private getCollectionReference(
242
- collection: string
243
- ): FirebaseFirestore.CollectionReference | FirebaseFirestore.Query {
202
+ private getCollectionReference(collection: string): FirebaseFirestore.CollectionReference | FirebaseFirestore.Query {
244
203
  if (collection.startsWith('**/')) {
245
204
  return this.db.collectionGroup(collection.replace('**/', ''));
246
205
  } else {
@@ -252,11 +211,11 @@ export class FirestoreClient implements ITransactionalClient {
252
211
  private async queryCollection<T extends Document>(
253
212
  collection: string,
254
213
  whereClauses: [string, FirestoreClient.Operator, any | any[]][] = [],
255
- options: FirestoreClient.QueryOptions
214
+ options: FirestoreClient.QueryOptions,
256
215
  ): Promise<Map<string, T>> {
257
216
  let query = this.getCollectionReference(collection);
258
217
 
259
- whereClauses.forEach(where => {
218
+ whereClauses.forEach((where) => {
260
219
  query = query.where(where[0], where[1], where[2]);
261
220
  });
262
221
 
@@ -271,7 +230,7 @@ export class FirestoreClient implements ITransactionalClient {
271
230
  const querySnapshot = await query.get();
272
231
 
273
232
  const documents = new Map<string, T>();
274
- querySnapshot.forEach(doc => {
233
+ querySnapshot.forEach((doc) => {
275
234
  const document = deserialize<T>(doc.data());
276
235
  documents.set(document._path, document);
277
236
  });
@@ -288,13 +247,13 @@ export class FirestoreClient implements ITransactionalClient {
288
247
  private doesDocumentMatchQuery<T extends Document>(
289
248
  document: T,
290
249
  collection: string,
291
- whereClauses: [string, FirestoreClient.Operator, any | any[]][]
250
+ whereClauses: [string, FirestoreClient.Operator, any | any[]][],
292
251
  ): boolean {
293
252
  const serializedDocument = serialize(document);
294
253
 
295
254
  return (
296
255
  document._path.startsWith(`${collection}/`) &&
297
- whereClauses.every(wc => {
256
+ whereClauses.every((wc) => {
298
257
  const documentValue = serializedDocument[wc[0]];
299
258
  const operator = wc[1];
300
259
  const expectedValue = wc[2];
@@ -313,23 +272,17 @@ export class FirestoreClient implements ITransactionalClient {
313
272
  case '<=':
314
273
  return documentValue <= expectedValue;
315
274
  case 'array-contains':
316
- return (
317
- Array.isArray(documentValue) && documentValue.includes(expectedValue)
318
- );
275
+ return Array.isArray(documentValue) && documentValue.includes(expectedValue);
319
276
  case 'array-contains-any':
320
277
  return (
321
278
  Array.isArray(documentValue) &&
322
279
  Array.isArray(expectedValue) &&
323
- documentValue.some(v => expectedValue.includes(v))
280
+ documentValue.some((v) => expectedValue.includes(v))
324
281
  );
325
282
  case 'in':
326
- return (
327
- Array.isArray(expectedValue) && expectedValue.includes(documentValue)
328
- );
283
+ return Array.isArray(expectedValue) && expectedValue.includes(documentValue);
329
284
  case 'not-in':
330
- return (
331
- Array.isArray(expectedValue) && !expectedValue.includes(documentValue)
332
- );
285
+ return Array.isArray(expectedValue) && !expectedValue.includes(documentValue);
333
286
  default:
334
287
  throw new Error(`Operator ${operator} not handled!`);
335
288
  }
@@ -360,7 +313,7 @@ export namespace FirestoreClient {
360
313
  export enum Action {
361
314
  Create = <any>'Create',
362
315
  Update = <any>'Update',
363
- Delete = <any>'Delete'
316
+ Delete = <any>'Delete',
364
317
  }
365
318
  export type Operator = FirebaseFirestore.WhereFilterOp;
366
319
 
@@ -6,10 +6,7 @@ import { ExecutionMode } from './execution-mode-enum';
6
6
  import { FirestoreClient } from './firestore-client';
7
7
 
8
8
  export class HandlerHelper {
9
- public static async create<A extends Aggregate, R>(
10
- aggregate: A,
11
- callback: (a: A) => Promise<R>
12
- ): Promise<R> {
9
+ public static async create<A extends Aggregate, R>(aggregate: A, callback: (a: A) => Promise<R>): Promise<R> {
13
10
  const result = await callback(aggregate);
14
11
 
15
12
  this.client.createDocument(aggregate);
@@ -18,15 +15,10 @@ export class HandlerHelper {
18
15
  return result;
19
16
  }
20
17
 
21
- public static async update<A extends Aggregate, R>(
22
- aggregateOrId: A | string,
23
- callback: (a: A) => Promise<R>
24
- ): Promise<R> {
18
+ public static async update<A extends Aggregate, R>(aggregateOrId: A | string, callback: (a: A) => Promise<R>): Promise<R> {
25
19
  const aggregate = await this.getAggregate(aggregateOrId);
26
20
  if (aggregate.events.length > 0) {
27
- Logger.warn(
28
- `${aggregate._type} ${aggregate.id} has ${aggregate.events.length} events!!`
29
- );
21
+ Logger.warn(`${aggregate._type} ${aggregate.id} has ${aggregate.events.length} events!!`);
30
22
  }
31
23
  const hashBefore = aggregate.hash;
32
24
 
@@ -36,19 +28,14 @@ export class HandlerHelper {
36
28
  this.client.updateDocument(aggregate);
37
29
  await this.publishEvents(aggregate);
38
30
  } else {
39
- Logger.debug(
40
- `${aggregate._type} ${aggregate.id} didn't change, ${aggregate.events.length} events skipped`
41
- );
31
+ Logger.debug(`${aggregate._type} ${aggregate.id} didn't change, ${aggregate.events.length} events skipped`);
42
32
  aggregate.clearEvents();
43
33
  }
44
34
 
45
35
  return result;
46
36
  }
47
37
 
48
- public static async delete<A extends Aggregate, R>(
49
- aggregateOrId: A | string,
50
- callback: (a: A) => Promise<R>
51
- ): Promise<R> {
38
+ public static async delete<A extends Aggregate, R>(aggregateOrId: A | string, callback: (a: A) => Promise<R>): Promise<R> {
52
39
  const aggregate = await this.getAggregate(aggregateOrId);
53
40
 
54
41
  const result = await callback(aggregate);
package/src/injector.ts CHANGED
@@ -13,7 +13,7 @@ export class Injector {
13
13
 
14
14
  if (!this.instances.has(injectableId)) {
15
15
  const tokens = reflect(target).getParameters();
16
- const injections = tokens.map(token => this.resolve<any>(token));
16
+ const injections = tokens.map((token) => this.resolve<any>(token));
17
17
  const instance = new target(...injections);
18
18
  this.instances.set(injectableId, instance);
19
19
 
@@ -1,2 +1 @@
1
- export type FromArray<T extends ReadonlyArray<unknown>> =
2
- T extends ReadonlyArray<infer FromArray> ? FromArray : never;
1
+ export type FromArray<T extends ReadonlyArray<unknown>> = T extends ReadonlyArray<infer FromArray> ? FromArray : never;
package/src/reflect.ts CHANGED
@@ -15,7 +15,7 @@ class ReflectHelper<T extends ReflectTarget<any>> {
15
15
 
16
16
  constructor(
17
17
  private target: T,
18
- private methodName: string | symbol
18
+ private methodName: string | symbol,
19
19
  ) {}
20
20
 
21
21
  public getParameters(): Type[] {
@@ -42,7 +42,7 @@ class ReflectHelper<T extends ReflectTarget<any>> {
42
42
  if (!this.hasMetaData(this.CONTROLLER_META_DATA)) {
43
43
  this.defineMetaData<ControllerConfiguration>(this.CONTROLLER_META_DATA, {
44
44
  basePath: null,
45
- routes: []
45
+ routes: [],
46
46
  });
47
47
  }
48
48
 
@@ -58,10 +58,7 @@ class ReflectHelper<T extends ReflectTarget<any>> {
58
58
  this.getControllerRoute().requestMethod = route.requestMethod;
59
59
  }
60
60
 
61
- public registerControllerRouteArgumentInjector<T>(
62
- injector: RequestArgumentInjector<T>,
63
- index: number
64
- ): void {
61
+ public registerControllerRouteArgumentInjector<T>(injector: RequestArgumentInjector<T>, index: number): void {
65
62
  this.getControllerRoute().argumentInjectors[index] = injector;
66
63
  }
67
64
 
@@ -69,7 +66,7 @@ class ReflectHelper<T extends ReflectTarget<any>> {
69
66
  if (!this.hasMetaData(this.HANDLER_META_DATA)) {
70
67
  this.defineMetaData<HandlerConfiguration>(this.HANDLER_META_DATA, {
71
68
  commandHandlers: new Map<string, CommandHandler>(),
72
- eventHandlers: new Map<string, EventHandler>()
69
+ eventHandlers: new Map<string, EventHandler>(),
73
70
  });
74
71
  }
75
72
 
@@ -79,33 +76,30 @@ class ReflectHelper<T extends ReflectTarget<any>> {
79
76
  public registerCommandHandler<C extends Command>(command: new () => C): void {
80
77
  this.getHandlerConfiguration().commandHandlers.set(this.methodName.toString(), {
81
78
  methodName: this.methodName.toString(),
82
- command
79
+ command,
83
80
  });
84
81
  }
85
82
 
86
- public registerEventHandler<E extends Event>(
87
- event: new () => E,
88
- executionMode: ExecutionMode
89
- ): void {
83
+ public registerEventHandler<E extends Event>(event: new () => E, executionMode: ExecutionMode): void {
90
84
  this.getHandlerConfiguration().eventHandlers.set(this.methodName.toString(), {
91
85
  methodName: this.methodName.toString(),
92
86
  executionMode,
93
- event
87
+ event,
94
88
  });
95
89
  }
96
90
 
97
91
  private getControllerRoute(): ControllerRoute {
98
92
  const config = this.getControllerConfiguration();
99
- if (!config.routes.find(r => r.methodName === this.methodName.toString())) {
93
+ if (!config.routes.find((r) => r.methodName === this.methodName.toString())) {
100
94
  config.routes.push({
101
95
  methodName: this.methodName.toString(),
102
96
  path: null,
103
97
  requestMethod: null,
104
- argumentInjectors: []
98
+ argumentInjectors: [],
105
99
  });
106
100
  }
107
101
 
108
- return config.routes.find(r => r.methodName === this.methodName.toString());
102
+ return config.routes.find((r) => r.methodName === this.methodName.toString());
109
103
  }
110
104
 
111
105
  private getMetaData<M>(key: string): M {
@@ -163,10 +157,7 @@ export interface HandlerConfiguration {
163
157
 
164
158
  export type RequestArgumentInjector<T = any> = (req: Server.Request) => T;
165
159
 
166
- export function reflect<T extends ReflectTarget<any>>(
167
- target: T,
168
- methodName?: string | symbol
169
- ): ReflectHelper<T> {
160
+ export function reflect<T extends ReflectTarget<any>>(target: T, methodName?: string | symbol): ReflectHelper<T> {
170
161
  return new ReflectHelper(target, methodName);
171
162
  }
172
163
 
@@ -1,4 +1,4 @@
1
1
  export enum RequestMethod {
2
2
  GET = 'GET',
3
- POST = 'POST'
3
+ POST = 'POST',
4
4
  }
@@ -3,6 +3,6 @@ import { Module } from '../decorators';
3
3
  import { SanityCheckController } from './controller';
4
4
 
5
5
  @Module({
6
- controllers: [SanityCheckController]
6
+ controllers: [SanityCheckController],
7
7
  })
8
8
  export class SanityCheckModule {}
@@ -5,14 +5,8 @@ import { Context } from '../../context';
5
5
  import { Server } from '../server';
6
6
 
7
7
  export function AuthenticationMiddleware(auth: Auth, publicPaths: string[]): Server.RequestHandler {
8
- return async (
9
- request: Server.Request,
10
- _response: Server.Response,
11
- next: Server.NextFunction
12
- ): Promise<void> => {
13
- const authorizationHeader = request.headers.authorization
14
- ? request.headers.authorization.toString()
15
- : null;
8
+ return async (request: Server.Request, _response: Server.Response, next: Server.NextFunction): Promise<void> => {
9
+ const authorizationHeader = request.headers.authorization ? request.headers.authorization.toString() : null;
16
10
  Logger.info(`${request.method} ${request.path}: Authentication check`);
17
11
 
18
12
  if (publicPaths.includes(request.path)) {
@@ -31,9 +25,7 @@ export function AuthenticationMiddleware(auth: Auth, publicPaths: string[]): Ser
31
25
  Context.user = await auth.getUser(userId);
32
26
  next();
33
27
  } catch (error) {
34
- throw new UnauthorizedError(
35
- `Error while verifying token ${error.message ? error.message : null}`
36
- );
28
+ throw new UnauthorizedError(`Error while verifying token ${error.message ? error.message : null}`);
37
29
  }
38
30
  } else if (authorizationHeader.startsWith('Basic')) {
39
31
  const idToken = authorizationHeader.split('Basic ')[1];
@@ -45,9 +37,7 @@ export function AuthenticationMiddleware(auth: Auth, publicPaths: string[]): Ser
45
37
  Context.user = await auth.getUser(userId);
46
38
  next();
47
39
  } catch (error) {
48
- throw new UnauthorizedError(
49
- `Error while verifying token ${error.message ? error.message : null}`
50
- );
40
+ throw new UnauthorizedError(`Error while verifying token ${error.message ? error.message : null}`);
51
41
  }
52
42
  } else {
53
43
  throw new UnauthorizedError('Incorrect authorization header');
@@ -5,11 +5,7 @@ import { Context } from '../../context';
5
5
  import { Server } from '../server';
6
6
 
7
7
  export function CreateContextMiddleware(bus: Bus): Server.RequestHandler {
8
- return async (
9
- request: Server.Request,
10
- _response: Server.Response,
11
- next: Server.NextFunction
12
- ): Promise<void> => {
8
+ return async (request: Server.Request, _response: Server.Response, next: Server.NextFunction): Promise<void> => {
13
9
  Context.create(bus);
14
10
  Context.correlationId = request.header('X-Correlation-ID') || guid();
15
11
 
@@ -4,11 +4,7 @@ import { Context } from '../../context';
4
4
  import { Server } from '../server';
5
5
 
6
6
  export function ErrorMiddleWare(): Server.ErrorHandler {
7
- return async (
8
- error: any,
9
- _request: Server.Request,
10
- response: Server.Response
11
- ): Promise<void> => {
7
+ return async (error: any, _request: Server.Request, response: Server.Response): Promise<void> => {
12
8
  if (!(error instanceof Error)) {
13
9
  error = new Error(error);
14
10
  }
@@ -2,11 +2,7 @@ import { Context } from '../../context';
2
2
  import { Server } from '../server';
3
3
 
4
4
  export function HeadersMiddleWare(): Server.RequestHandler {
5
- return async (
6
- _request: Server.Request,
7
- response: Server.Response,
8
- next: Server.NextFunction
9
- ): Promise<void> => {
5
+ return async (_request: Server.Request, response: Server.Response, next: Server.NextFunction): Promise<void> => {
10
6
  response.removeHeader('X-Powered-By'); // Remove verbose express
11
7
  response.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); // HTTP 1.1.
12
8
  response.setHeader('Pragma', 'no-cache'); // HTTP 1.0.