@forge/events 0.0.0-experimental-e05f7a2 → 0.0.0-experimental-64caa5a

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 (60) hide show
  1. package/CHANGELOG.md +154 -1
  2. package/README.md +1 -1
  3. package/out/__test__/jobProgress.test.d.ts +2 -0
  4. package/out/__test__/jobProgress.test.d.ts.map +1 -0
  5. package/out/__test__/jobProgress.test.js +110 -0
  6. package/out/__test__/queue.test.d.ts +2 -0
  7. package/out/__test__/queue.test.d.ts.map +1 -0
  8. package/out/__test__/queue.test.js +179 -0
  9. package/out/__test__/queueResponse.test.d.ts +2 -0
  10. package/out/__test__/queueResponse.test.d.ts.map +1 -0
  11. package/out/__test__/queueResponse.test.js +14 -0
  12. package/out/__test__/utils.d.ts +7 -0
  13. package/out/__test__/utils.d.ts.map +1 -0
  14. package/out/__test__/utils.js +37 -0
  15. package/out/errors.d.ts +9 -0
  16. package/out/errors.d.ts.map +1 -1
  17. package/out/errors.js +19 -1
  18. package/out/index.d.ts +3 -0
  19. package/out/index.d.ts.map +1 -1
  20. package/out/index.js +15 -0
  21. package/out/jobProgress.d.ts +9 -0
  22. package/out/jobProgress.d.ts.map +1 -0
  23. package/out/jobProgress.js +36 -0
  24. package/out/queries.d.ts +4 -2
  25. package/out/queries.d.ts.map +1 -1
  26. package/out/queries.js +13 -8
  27. package/out/queue.d.ts +4 -4
  28. package/out/queue.d.ts.map +1 -1
  29. package/out/queue.js +25 -25
  30. package/out/queueResponse.d.ts +5 -0
  31. package/out/queueResponse.d.ts.map +1 -0
  32. package/out/queueResponse.js +12 -0
  33. package/out/text.d.ts +14 -0
  34. package/out/text.d.ts.map +1 -0
  35. package/out/text.js +16 -0
  36. package/out/types.d.ts +11 -4
  37. package/out/types.d.ts.map +1 -1
  38. package/out/validators.d.ts +10 -4
  39. package/out/validators.d.ts.map +1 -1
  40. package/out/validators.js +69 -18
  41. package/package.json +7 -2
  42. package/src/__test__/jobProgress.test.ts +149 -0
  43. package/src/__test__/queue.test.ts +236 -0
  44. package/src/__test__/queueResponse.test.ts +14 -0
  45. package/src/__test__/utils.ts +47 -0
  46. package/src/errors.ts +18 -0
  47. package/src/index.ts +14 -0
  48. package/src/jobProgress.ts +48 -0
  49. package/src/queries.ts +15 -9
  50. package/src/queue.ts +28 -29
  51. package/src/queueResponse.ts +7 -0
  52. package/src/text.ts +15 -0
  53. package/src/types.ts +12 -4
  54. package/src/validators.ts +98 -31
  55. package/tsconfig.json +12 -9
  56. package/tsconfig.tsbuildinfo +107 -20
  57. package/out/__test__/index.test.d.ts +0 -2
  58. package/out/__test__/index.test.d.ts.map +0 -1
  59. package/out/__test__/index.test.js +0 -126
  60. package/src/__test__/index.test.ts +0 -168
@@ -1,168 +0,0 @@
1
- import { Queue } from '../queue';
2
- import { Payload } from '../types';
3
- import {
4
- InternalServerError,
5
- InvalidQueueNameError,
6
- NoEventsToPushError,
7
- PartialSuccessError,
8
- PayloadTooBigError,
9
- RateLimitError,
10
- TooManyEventsError
11
- } from '../errors';
12
-
13
- const getApiClientMock = (response?: any, statusCode = 201) => {
14
- return jest.fn().mockReturnValue({
15
- created: statusCode === 201,
16
- status: statusCode,
17
- statusText: 'Status Text',
18
- json: jest.fn().mockResolvedValue(response)
19
- });
20
- };
21
-
22
- const getQueue = (apiClientMock: any, queueName: string) => new Queue({ queueName: queueName }, apiClientMock);
23
-
24
- const verifyApiClientCalledWith = (apiClientMock: jest.Mock, payloads: Payload | Payload[], name: string) => {
25
- expect(apiClientMock).toHaveBeenCalledWith(
26
- '/webhook/queue/publish/{cloudId}/{environmentId}/{appId}/{appVersion}',
27
- expect.objectContaining({
28
- method: 'POST',
29
- body: expect.any(String),
30
- headers: {
31
- 'content-type': 'application/json'
32
- }
33
- })
34
- );
35
-
36
- const [, { body }] = apiClientMock.mock.calls[0];
37
- const expectedBody = {
38
- queueName: name,
39
- schema: 'ari:cloud:ecosystem::forge/app-event',
40
- type: 'avi:forge:app:event',
41
- payload: payloads
42
- };
43
- expect(JSON.parse(body)).toEqual(expect.objectContaining(expectedBody));
44
- };
45
-
46
- describe('Queue methods', () => {
47
- describe('constructor', () => {
48
- it('should throw InvalidQueueNameError', async () => {
49
- const apiClientMock = getApiClientMock();
50
- expect(() => getQueue(apiClientMock, 'invalid name')).toThrowError(
51
- new InvalidQueueNameError('Queue name can only contain alphanumeric, dash and underscore characters')
52
- );
53
- });
54
- });
55
-
56
- describe('push', () => {
57
- it('should call the queue/publish endpoint', async () => {
58
- const apiClientMock = getApiClientMock();
59
- const queue = getQueue(apiClientMock, 'name');
60
- const payload = {
61
- page: 1
62
- };
63
- await queue.push([payload]);
64
- verifyApiClientCalledWith(apiClientMock, [payload], 'name');
65
- });
66
-
67
- it('should throw NoEventsToPushError', async () => {
68
- const apiClientMock = getApiClientMock();
69
- const queue = getQueue(apiClientMock, 'name');
70
- await expect(queue.push([])).rejects.toThrow(new NoEventsToPushError(`No events pushed`));
71
- expect(apiClientMock).toHaveBeenCalledTimes(0);
72
- });
73
-
74
- it('should throw TooManyEventsError', async () => {
75
- const apiClientMock = getApiClientMock();
76
- const queue = getQueue(apiClientMock, 'name');
77
- await expect(queue.push([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])).rejects.toThrow(
78
- new TooManyEventsError(`Maximum of 10 events allowed in a single push`)
79
- );
80
- expect(apiClientMock).toHaveBeenCalledTimes(0);
81
- });
82
-
83
- it('should throw PayloadTooBigError', async () => {
84
- const apiClientMock = getApiClientMock();
85
- const queue = getQueue(apiClientMock, 'name');
86
- await expect(queue.push('x'.repeat(201 * 1024))).rejects.toThrow(
87
- new PayloadTooBigError(`The maximum payload size allowed is 200KB`)
88
- );
89
- expect(apiClientMock).toHaveBeenCalledTimes(0);
90
- });
91
-
92
- it('should throw RateLimitError', async () => {
93
- const apiClientMock = getApiClientMock({}, 429);
94
- const queue = getQueue(apiClientMock, 'name');
95
- const payload = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
96
- await expect(queue.push(payload)).rejects.toThrow(new RateLimitError(`Too many requests`));
97
- verifyApiClientCalledWith(apiClientMock, payload, 'name');
98
- });
99
-
100
- it('should throw PartialSuccessError', async () => {
101
- const apiClientMock = getApiClientMock(
102
- {
103
- failedEvents: [
104
- {
105
- index: '0',
106
- errorMessage: 'failed-1'
107
- },
108
- {
109
- index: 2,
110
- errorMessage: 'failed-3'
111
- }
112
- ]
113
- },
114
- 202
115
- );
116
- const queue = getQueue(apiClientMock, 'name');
117
- const payload = [
118
- {
119
- content: 'payload-1'
120
- },
121
- {
122
- content: 'payload-2'
123
- },
124
- {
125
- content: 'payload-3'
126
- }
127
- ];
128
- await expect(queue.push(payload)).rejects.toThrow(
129
- new PartialSuccessError(`Failed to process 2 events`, [
130
- {
131
- errorMessage: 'failed-1',
132
- payload: {
133
- content: 'payload-1'
134
- }
135
- },
136
- {
137
- errorMessage: 'failed-3',
138
- payload: {
139
- content: 'payload-3'
140
- }
141
- }
142
- ])
143
- );
144
- verifyApiClientCalledWith(apiClientMock, payload, 'name');
145
- });
146
-
147
- it('should throw InternalServerError', async () => {
148
- const apiClientMock = getApiClientMock(
149
- {
150
- message: 'AWS SQS timed out',
151
- code: 500,
152
- details: 'The request processing has failed because of an unknown error, exception or failure'
153
- },
154
- 500
155
- );
156
- const queue = getQueue(apiClientMock, 'name');
157
- const payload = [1, 2, 3, 4, 5, 6, 7, 8, 9];
158
- await expect(queue.push(payload)).rejects.toThrow(
159
- new InternalServerError(
160
- `500 Status Text: AWS SQS timed out`,
161
- 500,
162
- 'The request processing has failed because of an unknown error, exception or failure'
163
- )
164
- );
165
- verifyApiClientCalledWith(apiClientMock, payload, 'name');
166
- });
167
- });
168
- });