@forge/events 0.0.0-experimental-d3a39c9 → 0.0.0-experimental-490cfcf

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 +166 -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 +280 -229
  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
package/src/validators.ts CHANGED
@@ -1,44 +1,95 @@
1
1
  import {
2
2
  InternalServerError,
3
3
  InvalidQueueNameError,
4
+ JobDoesNotExistError,
4
5
  NoEventsToPushError,
5
6
  PartialSuccessError,
6
7
  PayloadTooBigError,
7
8
  RateLimitError,
8
- TooManyEventsError
9
+ TooManyEventsError,
10
+ InvalidPushSettingsError,
11
+ InvocationLimitReachedError
9
12
  } from './errors';
10
- import { APIResponse, Payload, APIRequest } from './types';
13
+ import { Text } from './text';
14
+ import { APIResponse, CancelJobRequest, GetStatsRequest, Payload, PushRequest, PushSettings } from './types';
11
15
 
12
16
  const VALID_QUEUE_NAME_PATTERN = /^[a-zA-Z0-9-_]+$/;
13
- const MAXIMUM_EVENTS = 10;
17
+ const MAXIMUM_EVENTS = 50;
14
18
  const MAXIMUM_PAYLOAD_SIZE_KB = 200;
15
19
 
16
- export const validateQueueName = (queueName: string) => {
20
+ export const validateQueueKey = (queueName: string) => {
17
21
  if (!queueName || !VALID_QUEUE_NAME_PATTERN.test(queueName)) {
18
- throw new InvalidQueueNameError('Queue name can only contain alphanumeric, dash and underscore characters');
22
+ throw new InvalidQueueNameError(Text.error.invalidQueueName);
19
23
  }
20
24
  };
21
25
 
22
- export const validatePayloads = (payloads: Payload | Payload[]) => {
26
+ export const validatePushSettings = (settings: PushSettings) => {
27
+ if ((settings.delayInSeconds && settings.delayInSeconds > 900) || settings.delayInSeconds < 0) {
28
+ throw new InvalidPushSettingsError(Text.error.invalidDelayInSecondsSetting);
29
+ }
30
+ };
31
+
32
+ export const validatePushPayloads = (payloads: Payload | Payload[]) => {
23
33
  if (!payloads || (Array.isArray(payloads) && payloads.length === 0)) {
24
- throw new NoEventsToPushError(`No events pushed`);
34
+ throw new NoEventsToPushError(Text.error.noEventsPushed);
25
35
  }
26
36
 
27
37
  if (Array.isArray(payloads) && payloads.length > MAXIMUM_EVENTS) {
28
- throw new TooManyEventsError(`Maximum of ${MAXIMUM_EVENTS} events allowed in a single push`);
38
+ throw new TooManyEventsError(Text.error.maxEventsAllowed(MAXIMUM_EVENTS));
29
39
  }
30
40
 
31
41
  const payloadSizeKB = Buffer.byteLength(JSON.stringify(payloads)) / 1024;
32
42
  if (payloadSizeKB > MAXIMUM_PAYLOAD_SIZE_KB) {
33
- throw new PayloadTooBigError(`The maximum payload size allowed is ${MAXIMUM_PAYLOAD_SIZE_KB}KB`);
43
+ throw new PayloadTooBigError(Text.error.maxPayloadAllowed(MAXIMUM_PAYLOAD_SIZE_KB));
44
+ }
45
+ };
46
+
47
+ export const validateGetStatsPayload = (getStatsRequest: GetStatsRequest) => {
48
+ if (!getStatsRequest.jobId) {
49
+ throw new JobDoesNotExistError(Text.error.jobIdEmpty);
50
+ }
51
+ validateQueueKey(getStatsRequest.queueName);
52
+ };
53
+
54
+ export const validateCancelJobRequest = (cancelJobRequest: CancelJobRequest) => {
55
+ if (!cancelJobRequest.jobId) {
56
+ throw new JobDoesNotExistError(Text.error.jobIdEmpty);
34
57
  }
58
+ validateQueueKey(cancelJobRequest.queueName);
35
59
  };
36
60
 
37
- export const validateAPIResponse = async (response: APIResponse, requestBody: APIRequest) => {
61
+ export const validateAPIResponse = async (response: APIResponse, expectedSuccessStatus: number) => {
38
62
  if (response.status === 429) {
39
- throw new RateLimitError(`Too many requests`);
63
+ throw new RateLimitError(Text.error.rateLimitError);
40
64
  }
41
65
 
66
+ if (response.status === 405) {
67
+ throw new InvocationLimitReachedError(Text.error.invocationLimitReachedError);
68
+ }
69
+
70
+ if (response.status != expectedSuccessStatus && response.status) {
71
+ //Catch all errors from server that we have not handled
72
+ let internalServerError;
73
+
74
+ try {
75
+ const responseBody = await response.json();
76
+ const errorMessage = responseBody.message ? `: ${responseBody.message}` : '';
77
+ const errors = responseBody.errors ? `: ${responseBody.errors.join(', ')}` : ''; //Dropwizard returns an array of errors when request body validation failed
78
+ internalServerError = new InternalServerError(
79
+ `${response.status} ${response.statusText}${errorMessage}${errors}`,
80
+ responseBody.code,
81
+ responseBody.details
82
+ );
83
+ } catch (ignore) {
84
+ //response body is not a json
85
+ internalServerError = new InternalServerError(`${response.status} ${response.statusText}`, response.status);
86
+ }
87
+
88
+ throw internalServerError;
89
+ }
90
+ };
91
+
92
+ export const validatePushAPIResponse = async (response: APIResponse, requestBody: PushRequest) => {
42
93
  if (response.status === 413) {
43
94
  //Server can return this error response if it has a different max payload size and max number of events limits
44
95
  const responseBody = await response.json();
@@ -47,29 +98,45 @@ export const validateAPIResponse = async (response: APIResponse, requestBody: AP
47
98
 
48
99
  if (response.status === 202) {
49
100
  const responseBody = await response.json();
101
+ const defaultErrorMessage = 'Failed to process some events.';
102
+ const partialSuccessError = new PartialSuccessError(defaultErrorMessage, []);
103
+
50
104
  if (responseBody.failedEvents && responseBody.failedEvents.length > 0) {
51
- throw new PartialSuccessError(
52
- `Failed to process ${responseBody.failedEvents.length} events`,
53
- responseBody.failedEvents.map((failedEvent: any) => {
54
- return {
55
- errorMessage: failedEvent.errorMessage,
56
- payload: requestBody.payload[+failedEvent.index]
57
- };
58
- })
59
- );
60
- } else {
61
- throw new PartialSuccessError(`Failed to process events`, []);
105
+ partialSuccessError.message = `Failed to process ${responseBody.failedEvents.length} event(s).`;
106
+ partialSuccessError.failedEvents = responseBody.failedEvents.map((failedEvent: any) => {
107
+ return {
108
+ errorMessage: failedEvent.errorMessage,
109
+ payload: requestBody.payload[+failedEvent.index]
110
+ };
111
+ });
62
112
  }
113
+
114
+ if (responseBody.errorMessage) {
115
+ //Append any extra error message from backend
116
+ partialSuccessError.message =
117
+ partialSuccessError.message !== defaultErrorMessage
118
+ ? `${partialSuccessError.message} ${responseBody.errorMessage}`
119
+ : responseBody.errorMessage;
120
+ }
121
+
122
+ throw partialSuccessError;
63
123
  }
64
124
 
65
- if (response.status != 201 && response.status) {
66
- //Catch all errors from server that we have not handled
67
- const responseBody = await response.json();
68
- const errorMessage = responseBody.message ? `: ${responseBody.message}` : '';
69
- throw new InternalServerError(
70
- `${response.status} ${response.statusText}${errorMessage}`,
71
- responseBody.code,
72
- responseBody.details
73
- );
125
+ await validateAPIResponse(response, 201);
126
+ };
127
+
128
+ export const validateGetStatsAPIResponse = async (response: APIResponse, getStatsRequest: GetStatsRequest) => {
129
+ if (response.status === 404) {
130
+ throw new JobDoesNotExistError(Text.error.jobDoesNotExit(getStatsRequest.jobId, getStatsRequest.queueName));
131
+ }
132
+
133
+ await validateAPIResponse(response, 200);
134
+ };
135
+
136
+ export const validateCancelJobAPIResponse = async (response: APIResponse, cancelJobRequest: GetStatsRequest) => {
137
+ if (response.status === 404) {
138
+ throw new JobDoesNotExistError(Text.error.jobDoesNotExit(cancelJobRequest.jobId, cancelJobRequest.queueName));
74
139
  }
140
+
141
+ await validateAPIResponse(response, 204);
75
142
  };
package/tsconfig.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
- "extends": "../../tsconfig-base.json",
3
- "compilerOptions": {
4
- "outDir": "./out",
5
- "rootDir": "src",
6
- "composite": true
7
- },
8
- "references": []
9
- }
10
-
2
+ "extends": "../../tsconfig-base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./out",
5
+ "rootDir": "src",
6
+ "composite": true
7
+ },
8
+ "references": [
9
+ {
10
+ "path": "../forge-api"
11
+ }
12
+ ]
13
+ }