@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.
- package/CHANGELOG.md +166 -1
- package/README.md +1 -1
- package/out/__test__/jobProgress.test.d.ts +2 -0
- package/out/__test__/jobProgress.test.d.ts.map +1 -0
- package/out/__test__/jobProgress.test.js +110 -0
- package/out/__test__/queue.test.d.ts +2 -0
- package/out/__test__/queue.test.d.ts.map +1 -0
- package/out/__test__/queue.test.js +179 -0
- package/out/__test__/queueResponse.test.d.ts +2 -0
- package/out/__test__/queueResponse.test.d.ts.map +1 -0
- package/out/__test__/queueResponse.test.js +14 -0
- package/out/__test__/utils.d.ts +7 -0
- package/out/__test__/utils.d.ts.map +1 -0
- package/out/__test__/utils.js +37 -0
- package/out/errors.d.ts +9 -0
- package/out/errors.d.ts.map +1 -1
- package/out/errors.js +19 -1
- package/out/index.d.ts +3 -0
- package/out/index.d.ts.map +1 -1
- package/out/index.js +15 -0
- package/out/jobProgress.d.ts +9 -0
- package/out/jobProgress.d.ts.map +1 -0
- package/out/jobProgress.js +36 -0
- package/out/queries.d.ts +4 -2
- package/out/queries.d.ts.map +1 -1
- package/out/queries.js +13 -8
- package/out/queue.d.ts +4 -4
- package/out/queue.d.ts.map +1 -1
- package/out/queue.js +25 -25
- package/out/queueResponse.d.ts +5 -0
- package/out/queueResponse.d.ts.map +1 -0
- package/out/queueResponse.js +12 -0
- package/out/text.d.ts +14 -0
- package/out/text.d.ts.map +1 -0
- package/out/text.js +16 -0
- package/out/types.d.ts +11 -4
- package/out/types.d.ts.map +1 -1
- package/out/validators.d.ts +10 -4
- package/out/validators.d.ts.map +1 -1
- package/out/validators.js +69 -18
- package/package.json +7 -2
- package/src/__test__/jobProgress.test.ts +149 -0
- package/src/__test__/queue.test.ts +236 -0
- package/src/__test__/queueResponse.test.ts +14 -0
- package/src/__test__/utils.ts +47 -0
- package/src/errors.ts +18 -0
- package/src/index.ts +14 -0
- package/src/jobProgress.ts +48 -0
- package/src/queries.ts +15 -9
- package/src/queue.ts +28 -29
- package/src/queueResponse.ts +7 -0
- package/src/text.ts +15 -0
- package/src/types.ts +12 -4
- package/src/validators.ts +98 -31
- package/tsconfig.json +12 -9
- package/tsconfig.tsbuildinfo +280 -229
- package/out/__test__/index.test.d.ts +0 -2
- package/out/__test__/index.test.d.ts.map +0 -1
- package/out/__test__/index.test.js +0 -126
- 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 {
|
|
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 =
|
|
17
|
+
const MAXIMUM_EVENTS = 50;
|
|
14
18
|
const MAXIMUM_PAYLOAD_SIZE_KB = 200;
|
|
15
19
|
|
|
16
|
-
export const
|
|
20
|
+
export const validateQueueKey = (queueName: string) => {
|
|
17
21
|
if (!queueName || !VALID_QUEUE_NAME_PATTERN.test(queueName)) {
|
|
18
|
-
throw new InvalidQueueNameError(
|
|
22
|
+
throw new InvalidQueueNameError(Text.error.invalidQueueName);
|
|
19
23
|
}
|
|
20
24
|
};
|
|
21
25
|
|
|
22
|
-
export const
|
|
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(
|
|
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(
|
|
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(
|
|
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,
|
|
61
|
+
export const validateAPIResponse = async (response: APIResponse, expectedSuccessStatus: number) => {
|
|
38
62
|
if (response.status === 429) {
|
|
39
|
-
throw new RateLimitError(
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
+
}
|