@forge/events
Advanced tools
Comparing version 0.0.0-experimental-64caa5a to 0.0.0-experimental-72f4150
171
CHANGELOG.md
# @forge/events | ||
## 0.0.0-experimental-64caa5a | ||
## 0.0.0-experimental-72f4150 | ||
### Minor Changes | ||
- a5cbdf6: Add NodeJS runtime support for Events API | ||
### Patch Changes | ||
- Updated dependencies [a5cbdf6] | ||
- Updated dependencies [a5cbdf6] | ||
- @forge/api@0.0.0-experimental-72f4150 | ||
## 0.8.0-next.0 | ||
### Minor Changes | ||
- dfab69c: Add NodeJS runtime support for Events API | ||
### Patch Changes | ||
- Updated dependencies [dfab69c] | ||
- @forge/api@2.10.1-next.0 | ||
## 0.7.5 | ||
### Patch Changes | ||
- Updated dependencies [37f48c5] | ||
- Updated dependencies [e0e3587] | ||
- Updated dependencies [ca8551d] | ||
- @forge/api@2.10.0 | ||
## 0.7.5-next.1 | ||
### Patch Changes | ||
- Updated dependencies [e0e3587a] | ||
- @forge/api@2.10.0-next.1 | ||
## 0.7.5-next.0 | ||
### Patch Changes | ||
- Updated dependencies [ca8551dd] | ||
- @forge/api@2.9.2-next.0 | ||
## 0.7.4 | ||
### Patch Changes | ||
- Updated dependencies [73b929a] | ||
- Updated dependencies [8d0dc10] | ||
- Updated dependencies [7a4fa35] | ||
- @forge/api@2.9.1 | ||
## 0.7.4-next.0 | ||
### Patch Changes | ||
- Updated dependencies [7a4fa35] | ||
- @forge/api@2.9.1-next.0 | ||
## 0.7.3 | ||
### Patch Changes | ||
- Updated dependencies [e5dd325] | ||
- @forge/api@2.9.0 | ||
## 0.7.3-next.0 | ||
### Patch Changes | ||
- Updated dependencies [e5dd325d] | ||
- @forge/api@2.9.0-next.0 | ||
## 0.7.2 | ||
### Patch Changes | ||
- Updated dependencies [8e2a5a6] | ||
- @forge/api@2.8.1 | ||
## 0.7.2-next.0 | ||
### Patch Changes | ||
- Updated dependencies [8e2a5a6] | ||
- @forge/api@2.8.1-next.0 | ||
## 0.7.1 | ||
### Patch Changes | ||
- Updated dependencies [95913f6] | ||
- @forge/api@2.8.0 | ||
## 0.7.1-next.0 | ||
### Patch Changes | ||
- Updated dependencies [95913f6] | ||
- @forge/api@2.8.0-next.0 | ||
## 0.7.0 | ||
### Minor Changes | ||
- e3af95e: Fix broken test for InvocationError | ||
- d49508c: Fix broken test for InvocationError | ||
- 6529a96: Add retryData into retryOptions | ||
- 5062ab4: Fix broken test for InvocationError | ||
## 0.7.0-next.0 | ||
### Minor Changes | ||
- e3af95e: Fix broken test for InvocationError | ||
- d49508c: Fix broken test for InvocationError | ||
- 6529a96: Add retryData into retryOptions | ||
- 5062ab4: Fix broken test for InvocationError | ||
## 0.6.1 | ||
### Patch Changes | ||
- 0e0a7b4: Returning object from InvocationError to make it transferrable. | ||
- Updated dependencies [b3ee297] | ||
- @forge/api@2.7.0 | ||
## 0.6.1-next.1 | ||
### Patch Changes | ||
- Updated dependencies [b3ee2973] | ||
- @forge/api@2.7.0-next.0 | ||
## 0.6.1-next.0 | ||
### Patch Changes | ||
- 0e0a7b47: Returning object from InvocationError to make it transferrable. | ||
## 0.6.0 | ||
### Minor Changes | ||
- 0859c97: Allow app developers to return InvocationError on error from lambda function | ||
## 0.6.0-next.0 | ||
### Minor Changes | ||
- 0859c971: Allow app developers to return InvocationError on error from lambda function | ||
## 0.5.3 | ||
### Patch Changes | ||
- @forge/api@2.6.1 | ||
## 0.5.3-next.0 | ||
### Patch Changes | ||
- @forge/api@2.6.1-next.0 | ||
## 0.5.2 | ||
### Patch Changes | ||
- Updated dependencies [8571c05] | ||
- @forge/api@0.0.0-experimental-64caa5a | ||
- @forge/api@2.6.0 | ||
@@ -10,0 +177,0 @@ ## 0.5.2-next.0 |
@@ -6,7 +6,11 @@ "use strict"; | ||
const jobProgress_1 = require("../jobProgress"); | ||
const getJobProgress = (apiClientMock, jobId) => new jobProgress_1.JobProgress(jobId, apiClientMock); | ||
const api_1 = require("@forge/api"); | ||
jest.mock('@forge/api', () => ({ | ||
createRequestStargateAsApp: jest.fn().mockReturnValue((0, utils_1.getMockFetchMethod)('done', 200)) | ||
})); | ||
const getJobProgress = (jobId, apiClientMock) => new jobProgress_1.JobProgress(jobId, apiClientMock); | ||
describe('JobProgress methods', () => { | ||
describe('getStats', () => { | ||
it('should call the queue/stats endpoint', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
success: 100, | ||
@@ -16,6 +20,6 @@ inProgress: 50, | ||
}, 200); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
const response = await jobProgress.getStats(); | ||
const { success, inProgress, failed } = await response.json(); | ||
utils_1.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
(0, utils_1.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: 'test-queue-name', | ||
@@ -29,9 +33,9 @@ jobId: 'test-job-id' | ||
it('should throw JobDoesNotExistError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
message: 'Job Not Found', | ||
code: 404 | ||
}, 404); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow(new errors_1.JobDoesNotExistError(`The job test-job-id was not found for the queue test-queue-name.`)); | ||
utils_1.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
(0, utils_1.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: 'test-queue-name', | ||
@@ -42,6 +46,6 @@ jobId: 'test-job-id' | ||
it('should throw RateLimitError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({}, 429); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({}, 429); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow(new errors_1.RateLimitError(`Too many requests.`)); | ||
utils_1.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
(0, utils_1.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: 'test-queue-name', | ||
@@ -52,3 +56,3 @@ jobId: 'test-job-id' | ||
it('should throw InternalServerError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
message: 'Service is not available', | ||
@@ -58,5 +62,5 @@ code: 513, | ||
}, 513); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow(new errors_1.InternalServerError(`513 Status Text: Service is not available`, 513)); | ||
utils_1.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
(0, utils_1.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: 'test-queue-name', | ||
@@ -69,6 +73,6 @@ jobId: 'test-job-id' | ||
it('should call the queue/cancel endpoint', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({}, 204); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({}, 204); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
const response = await jobProgress.cancel(); | ||
utils_1.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/cancel/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
(0, utils_1.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/cancel/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: 'test-queue-name', | ||
@@ -80,9 +84,9 @@ jobId: 'test-job-id' | ||
it('should throw JobDoesNotExistError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
message: 'Job Not Found', | ||
code: 404 | ||
}, 404); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.cancel()).rejects.toThrow(new errors_1.JobDoesNotExistError(`The job test-job-id was not found for the queue test-queue-name.`)); | ||
utils_1.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/cancel/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
(0, utils_1.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/cancel/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: 'test-queue-name', | ||
@@ -94,8 +98,8 @@ jobId: 'test-job-id' | ||
it('should throw InternalServerError when WHP returns 422 response', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
errors: ['jobId must not be null', 'queueName must not be null'] | ||
}, 422); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow(new errors_1.InternalServerError(`422 Status Text: jobId must not be null, queueName must not be null`)); | ||
utils_1.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
(0, utils_1.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: 'test-queue-name', | ||
@@ -106,3 +110,3 @@ jobId: 'test-job-id' | ||
it('should throw errors when queueName or jobId is empty', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
success: 100, | ||
@@ -112,10 +116,15 @@ inProgress: 50, | ||
}, 200); | ||
const jobProgress1 = getJobProgress(apiClientMock, 'test-queue-name#'); | ||
const jobProgress1 = getJobProgress('test-queue-name#', apiClientMock); | ||
await expect(jobProgress1.getStats()).rejects.toThrow(new errors_1.JobDoesNotExistError(`jobId cannot be empty.`)); | ||
const jobProgress2 = getJobProgress(apiClientMock, '#test-job-id'); | ||
const jobProgress2 = getJobProgress('#test-job-id', apiClientMock); | ||
await expect(jobProgress2.getStats()).rejects.toThrow(new errors_1.InvalidQueueNameError('Queue names can only contain alphanumeric characters, dashes and underscores.')); | ||
const jobProgress3 = getJobProgress(apiClientMock, ''); | ||
const jobProgress3 = getJobProgress('', apiClientMock); | ||
await expect(jobProgress3.getStats()).rejects.toThrow(new errors_1.JobDoesNotExistError(`jobId cannot be empty.`)); | ||
expect(apiClientMock).toHaveBeenCalledTimes(0); | ||
}); | ||
it('requests stargate if no api client is provided', async () => { | ||
const jobProgress = getJobProgress('test-queue-name#job-id'); | ||
await jobProgress.getStats(); | ||
expect((0, api_1.createRequestStargateAsApp)()).toBeCalledTimes(1); | ||
}); | ||
}); |
@@ -7,8 +7,12 @@ "use strict"; | ||
const jobProgress_1 = require("../jobProgress"); | ||
const getQueue = (apiClientMock, queueName) => new queue_1.Queue({ key: queueName }, apiClientMock); | ||
const api_1 = require("@forge/api"); | ||
jest.mock('@forge/api', () => ({ | ||
createRequestStargateAsApp: jest.fn().mockReturnValue((0, utils_1.getMockFetchMethod)('done', 201)) | ||
})); | ||
const getQueue = (queueName, apiClientMock) => new queue_1.Queue({ key: queueName }, apiClientMock); | ||
describe('Queue methods', () => { | ||
describe('constructor', () => { | ||
it('should throw InvalidQueueNameError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock(); | ||
expect(() => getQueue(apiClientMock, 'invalid name')).toThrowError(new errors_1.InvalidQueueNameError('Queue names can only contain alphanumeric characters, dashes and underscores.')); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)(); | ||
expect(() => getQueue('invalid name', apiClientMock)).toThrowError(new errors_1.InvalidQueueNameError('Queue names can only contain alphanumeric characters, dashes and underscores.')); | ||
}); | ||
@@ -18,4 +22,4 @@ }); | ||
it('should call the queue/publish endpoint', async () => { | ||
const apiClientMock = utils_1.getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)(); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = { | ||
@@ -25,7 +29,7 @@ page: 1 | ||
await queue.push([payload]); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, [payload], 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, [payload], 'name'); | ||
}); | ||
it('should throw InvalidPushSettingsError for delay', async () => { | ||
const apiClientMock = utils_1.getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push(1, { delayInSeconds: 901 })).rejects.toThrow(new errors_1.InvalidPushSettingsError(`The delayInSeconds setting must be between 0 and 900.`)); | ||
@@ -35,4 +39,4 @@ expect(apiClientMock).toHaveBeenCalledTimes(0); | ||
it('should throw NoEventsToPushError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push([])).rejects.toThrow(new errors_1.NoEventsToPushError(`No events pushed.`)); | ||
@@ -42,4 +46,4 @@ expect(apiClientMock).toHaveBeenCalledTimes(0); | ||
it('should throw TooManyEventsError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push([...Array(51).keys()])).rejects.toThrow(new errors_1.TooManyEventsError(`This push contains more than the 50 events allowed.`)); | ||
@@ -49,4 +53,4 @@ expect(apiClientMock).toHaveBeenCalledTimes(0); | ||
it('should throw PayloadTooBigError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push('x'.repeat(201 * 1024))).rejects.toThrow(new errors_1.PayloadTooBigError(`The maximum payload size is 200KB.`)); | ||
@@ -56,17 +60,17 @@ expect(apiClientMock).toHaveBeenCalledTimes(0); | ||
it('should throw RateLimitError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({}, 429); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({}, 429); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [...Array(10).keys()]; | ||
await expect(queue.push(payload)).rejects.toThrow(new errors_1.RateLimitError(`Too many requests.`)); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, payload, 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, payload, 'name'); | ||
}); | ||
it('should throw InvocationLimitReachedError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({}, 405); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({}, 405); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [...Array(5).keys()]; | ||
await expect(queue.push(payload)).rejects.toThrow(new errors_1.InvocationLimitReachedError(`The limit on cyclic invocation has been reached.`)); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, payload, 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, payload, 'name'); | ||
}); | ||
it('should throw PartialSuccessError when there are failed events', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
failedEvents: [ | ||
@@ -83,3 +87,3 @@ { | ||
}, 202); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [ | ||
@@ -110,9 +114,9 @@ { | ||
])); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, payload, 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, payload, 'name'); | ||
}); | ||
it('should throw PartialSuccessError when backend failed to create job stats', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
errorMessage: 'Failed to create stats for job name#12345' | ||
}, 202); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [ | ||
@@ -130,6 +134,6 @@ { | ||
await expect(queue.push(payload)).rejects.toThrow(new errors_1.PartialSuccessError(`Failed to create stats for job name#12345`, [])); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, payload, 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, payload, 'name'); | ||
}); | ||
it('should throw PartialSuccessError when there are failed events and backend failed to create job stats', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
errorMessage: 'Failed to create stats for job name#12345', | ||
@@ -143,3 +147,3 @@ failedEvents: [ | ||
}, 202); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [ | ||
@@ -161,6 +165,6 @@ { | ||
])); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, payload, 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, payload, 'name'); | ||
}); | ||
it('should throw InternalServerError', async () => { | ||
const apiClientMock = utils_1.getApiClientMock({ | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)({ | ||
message: 'AWS SQS timed out', | ||
@@ -170,19 +174,24 @@ code: 500, | ||
}, 500); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [...Array(9).keys()]; | ||
await expect(queue.push(payload)).rejects.toThrow(new errors_1.InternalServerError(`500 Status Text: AWS SQS timed out`, 500, 'The request processing has failed because of an unknown error, exception or failure')); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, payload, 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, payload, 'name'); | ||
}); | ||
it('should throw InternalServerError for error response without response body', async () => { | ||
const apiClientMock = utils_1.getApiClientMockWithoutResponseBody(504, 'Gateway Timeout'); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getApiClientMockWithoutResponseBody)(504, 'Gateway Timeout'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [1]; | ||
await expect(queue.push(1)).rejects.toThrow(new errors_1.InternalServerError(`504 Gateway Timeout`, 504)); | ||
utils_1.verifyApiClientCalledPushPathWith(apiClientMock, payload, 'name'); | ||
(0, utils_1.verifyApiClientCalledPushPathWith)(apiClientMock, payload, 'name'); | ||
}); | ||
it('requests stargate if no api client is provided', async () => { | ||
const queue = getQueue('queue'); | ||
await queue.push({ test: 'stargate' }); | ||
expect((0, api_1.createRequestStargateAsApp)()).toBeCalledTimes(1); | ||
}); | ||
}); | ||
describe('getJob', () => { | ||
it('should create a JobProgress by jobId', async () => { | ||
const apiClientMock = utils_1.getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = (0, utils_1.getMockFetchMethod)(); | ||
const queue = getQueue('name', apiClientMock); | ||
const jobProgress = queue.getJob('test-job-id'); | ||
@@ -189,0 +198,0 @@ expect(jobProgress).toEqual(new jobProgress_1.JobProgress('test-job-id', apiClientMock)); |
/// <reference types="jest" /> | ||
import { Payload } from '../types'; | ||
export declare const getApiClientMock: (response?: any, statusCode?: number) => jest.Mock<any, any>; | ||
export declare const getMockFetchMethod: (response?: any, statusCode?: number) => jest.Mock<any, any>; | ||
export declare const getApiClientMockWithoutResponseBody: (statusCode: number, statusText: string) => jest.Mock<any, any>; | ||
@@ -5,0 +5,0 @@ export declare const verifyApiClientCalledWith: (apiClientMock: jest.Mock, path: string, expectedBody: any) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.verifyApiClientCalledPushPathWith = exports.verifyApiClientCalledWith = exports.getApiClientMockWithoutResponseBody = exports.getApiClientMock = void 0; | ||
exports.getApiClientMock = (response, statusCode = 201) => { | ||
exports.verifyApiClientCalledPushPathWith = exports.verifyApiClientCalledWith = exports.getApiClientMockWithoutResponseBody = exports.getMockFetchMethod = void 0; | ||
const getMockFetchMethod = (response, statusCode = 201) => { | ||
return jest.fn().mockReturnValue({ | ||
@@ -12,3 +12,4 @@ created: statusCode === 201, | ||
}; | ||
exports.getApiClientMockWithoutResponseBody = (statusCode, statusText) => { | ||
exports.getMockFetchMethod = getMockFetchMethod; | ||
const getApiClientMockWithoutResponseBody = (statusCode, statusText) => { | ||
return jest.fn().mockReturnValue({ | ||
@@ -20,3 +21,4 @@ ok: statusCode === 200, | ||
}; | ||
exports.verifyApiClientCalledWith = (apiClientMock, path, expectedBody) => { | ||
exports.getApiClientMockWithoutResponseBody = getApiClientMockWithoutResponseBody; | ||
const verifyApiClientCalledWith = (apiClientMock, path, expectedBody) => { | ||
expect(apiClientMock).toHaveBeenCalledWith(path, expect.objectContaining({ | ||
@@ -32,4 +34,5 @@ method: 'POST', | ||
}; | ||
exports.verifyApiClientCalledPushPathWith = (apiClientMock, payloads, name) => { | ||
exports.verifyApiClientCalledWith(apiClientMock, '/webhook/queue/publish/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
exports.verifyApiClientCalledWith = verifyApiClientCalledWith; | ||
const verifyApiClientCalledPushPathWith = (apiClientMock, payloads, name) => { | ||
(0, exports.verifyApiClientCalledWith)(apiClientMock, '/webhook/queue/publish/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
queueName: name, | ||
@@ -41,1 +44,2 @@ schema: 'ari:cloud:ecosystem::forge/app-event', | ||
}; | ||
exports.verifyApiClientCalledPushPathWith = verifyApiClientCalledPushPathWith; |
@@ -5,2 +5,5 @@ export { Queue } from './queue'; | ||
export { QueueResponse } from './queueResponse'; | ||
export { InvocationError } from './invocationError'; | ||
export { InvocationErrorCode } from './invocationErrorCode'; | ||
export { RetryOptions } from './retryOptions'; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.InvocationErrorCode = exports.InvocationError = exports.QueueResponse = exports.JobProgress = exports.InvocationLimitReachedError = exports.InvalidPushSettingsError = exports.JobDoesNotExistError = exports.InternalServerError = exports.PartialSuccessError = exports.RateLimitError = exports.NoEventsToPushError = exports.PayloadTooBigError = exports.TooManyEventsError = exports.InvalidQueueNameError = exports.Queue = void 0; | ||
var queue_1 = require("./queue"); | ||
@@ -20,1 +21,5 @@ Object.defineProperty(exports, "Queue", { enumerable: true, get: function () { return queue_1.Queue; } }); | ||
Object.defineProperty(exports, "QueueResponse", { enumerable: true, get: function () { return queueResponse_1.QueueResponse; } }); | ||
var invocationError_1 = require("./invocationError"); | ||
Object.defineProperty(exports, "InvocationError", { enumerable: true, get: function () { return invocationError_1.InvocationError; } }); | ||
var invocationErrorCode_1 = require("./invocationErrorCode"); | ||
Object.defineProperty(exports, "InvocationErrorCode", { enumerable: true, get: function () { return invocationErrorCode_1.InvocationErrorCode; } }); |
@@ -6,6 +6,7 @@ "use strict"; | ||
const validators_1 = require("./validators"); | ||
const api_1 = require("@forge/api"); | ||
class JobProgress { | ||
constructor(id, apiClient) { | ||
this.id = id; | ||
this.apiClient = apiClient || global.api.asApp().__requestAtlassian; | ||
this.apiClient = apiClient || (0, api_1.createRequestStargateAsApp)(); | ||
} | ||
@@ -19,5 +20,5 @@ async getStats() { | ||
}; | ||
validators_1.validateGetStatsPayload(getStatsRequest); | ||
const response = await queries_1.post(queries_1.GET_STATS_PATH, getStatsRequest, this.apiClient); | ||
await validators_1.validateGetStatsAPIResponse(response, getStatsRequest); | ||
(0, validators_1.validateGetStatsPayload)(getStatsRequest); | ||
const response = await (0, queries_1.post)(queries_1.GET_STATS_PATH, getStatsRequest, this.apiClient); | ||
await (0, validators_1.validateGetStatsAPIResponse)(response, getStatsRequest); | ||
return response; | ||
@@ -32,5 +33,5 @@ } | ||
}; | ||
validators_1.validateCancelJobRequest(cancelJobRequest); | ||
const response = await queries_1.post(queries_1.CANCEL_JOB_PATH, cancelJobRequest, this.apiClient); | ||
await validators_1.validateCancelJobAPIResponse(response, cancelJobRequest); | ||
(0, validators_1.validateCancelJobRequest)(cancelJobRequest); | ||
const response = await (0, queries_1.post)(queries_1.CANCEL_JOB_PATH, cancelJobRequest, this.apiClient); | ||
await (0, validators_1.validateCancelJobAPIResponse)(response, cancelJobRequest); | ||
return response; | ||
@@ -37,0 +38,0 @@ } |
@@ -7,3 +7,3 @@ "use strict"; | ||
exports.CANCEL_JOB_PATH = '/webhook/queue/cancel/{cloudId}/{environmentId}/{appId}/{appVersion}'; | ||
exports.post = async (endpoint, body, apiClient) => { | ||
const post = async (endpoint, body, apiClient) => { | ||
const request = { | ||
@@ -18,1 +18,2 @@ method: 'POST', | ||
}; | ||
exports.post = post; |
@@ -9,12 +9,13 @@ "use strict"; | ||
const jobProgress_1 = require("./jobProgress"); | ||
const api_1 = require("@forge/api"); | ||
class Queue { | ||
constructor(queueParams, apiClient) { | ||
validators_1.validateQueueKey(queueParams.key); | ||
(0, validators_1.validateQueueKey)(queueParams.key); | ||
this.queueParams = queueParams; | ||
this.apiClient = apiClient || global.api.asApp().__requestAtlassian; | ||
this.apiClient = apiClient || (0, api_1.createRequestStargateAsApp)(); | ||
} | ||
async push(payloads, pushSettings) { | ||
validators_1.validatePushPayloads(payloads); | ||
(0, validators_1.validatePushPayloads)(payloads); | ||
const queueName = this.queueParams.key; | ||
const jobId = v4_1.default(); | ||
const jobId = (0, v4_1.default)(); | ||
const pushRequest = { | ||
@@ -29,3 +30,3 @@ queueName: queueName, | ||
if (pushSettings) { | ||
validators_1.validatePushSettings(pushSettings); | ||
(0, validators_1.validatePushSettings)(pushSettings); | ||
if (pushSettings.delayInSeconds) { | ||
@@ -35,4 +36,4 @@ pushRequest.delayInSeconds = pushSettings.delayInSeconds; | ||
} | ||
const response = await queries_1.post(queries_1.PUSH_PATH, pushRequest, this.apiClient); | ||
await validators_1.validatePushAPIResponse(response, pushRequest); | ||
const response = await (0, queries_1.post)(queries_1.PUSH_PATH, pushRequest, this.apiClient); | ||
await (0, validators_1.validatePushAPIResponse)(response, pushRequest); | ||
return `${queueName}#${jobId}`; | ||
@@ -39,0 +40,0 @@ } |
@@ -1,5 +0,9 @@ | ||
export declare class QueueResponse { | ||
export declare class Response { | ||
protected _retry: boolean; | ||
constructor(_retry: boolean); | ||
} | ||
export declare class QueueResponse extends Response { | ||
constructor(); | ||
retry(): void; | ||
} | ||
//# sourceMappingURL=queueResponse.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.QueueResponse = void 0; | ||
class QueueResponse { | ||
exports.QueueResponse = exports.Response = void 0; | ||
class Response { | ||
constructor(_retry) { | ||
this._retry = _retry; | ||
} | ||
} | ||
exports.Response = Response; | ||
class QueueResponse extends Response { | ||
constructor() { | ||
this._retry = false; | ||
super(false); | ||
} | ||
@@ -8,0 +14,0 @@ retry() { |
@@ -9,3 +9,3 @@ "use strict"; | ||
const MAXIMUM_PAYLOAD_SIZE_KB = 200; | ||
exports.validateQueueKey = (queueName) => { | ||
const validateQueueKey = (queueName) => { | ||
if (!queueName || !VALID_QUEUE_NAME_PATTERN.test(queueName)) { | ||
@@ -15,3 +15,4 @@ throw new errors_1.InvalidQueueNameError(text_1.Text.error.invalidQueueName); | ||
}; | ||
exports.validatePushSettings = (settings) => { | ||
exports.validateQueueKey = validateQueueKey; | ||
const validatePushSettings = (settings) => { | ||
if ((settings.delayInSeconds && settings.delayInSeconds > 900) || settings.delayInSeconds < 0) { | ||
@@ -21,3 +22,4 @@ throw new errors_1.InvalidPushSettingsError(text_1.Text.error.invalidDelayInSecondsSetting); | ||
}; | ||
exports.validatePushPayloads = (payloads) => { | ||
exports.validatePushSettings = validatePushSettings; | ||
const validatePushPayloads = (payloads) => { | ||
if (!payloads || (Array.isArray(payloads) && payloads.length === 0)) { | ||
@@ -34,15 +36,18 @@ throw new errors_1.NoEventsToPushError(text_1.Text.error.noEventsPushed); | ||
}; | ||
exports.validateGetStatsPayload = (getStatsRequest) => { | ||
exports.validatePushPayloads = validatePushPayloads; | ||
const validateGetStatsPayload = (getStatsRequest) => { | ||
if (!getStatsRequest.jobId) { | ||
throw new errors_1.JobDoesNotExistError(text_1.Text.error.jobIdEmpty); | ||
} | ||
exports.validateQueueKey(getStatsRequest.queueName); | ||
(0, exports.validateQueueKey)(getStatsRequest.queueName); | ||
}; | ||
exports.validateCancelJobRequest = (cancelJobRequest) => { | ||
exports.validateGetStatsPayload = validateGetStatsPayload; | ||
const validateCancelJobRequest = (cancelJobRequest) => { | ||
if (!cancelJobRequest.jobId) { | ||
throw new errors_1.JobDoesNotExistError(text_1.Text.error.jobIdEmpty); | ||
} | ||
exports.validateQueueKey(cancelJobRequest.queueName); | ||
(0, exports.validateQueueKey)(cancelJobRequest.queueName); | ||
}; | ||
exports.validateAPIResponse = async (response, expectedSuccessStatus) => { | ||
exports.validateCancelJobRequest = validateCancelJobRequest; | ||
const validateAPIResponse = async (response, expectedSuccessStatus) => { | ||
if (response.status === 429) { | ||
@@ -68,3 +73,4 @@ throw new errors_1.RateLimitError(text_1.Text.error.rateLimitError); | ||
}; | ||
exports.validatePushAPIResponse = async (response, requestBody) => { | ||
exports.validateAPIResponse = validateAPIResponse; | ||
const validatePushAPIResponse = async (response, requestBody) => { | ||
if (response.status === 413) { | ||
@@ -95,15 +101,18 @@ const responseBody = await response.json(); | ||
} | ||
await exports.validateAPIResponse(response, 201); | ||
await (0, exports.validateAPIResponse)(response, 201); | ||
}; | ||
exports.validateGetStatsAPIResponse = async (response, getStatsRequest) => { | ||
exports.validatePushAPIResponse = validatePushAPIResponse; | ||
const validateGetStatsAPIResponse = async (response, getStatsRequest) => { | ||
if (response.status === 404) { | ||
throw new errors_1.JobDoesNotExistError(text_1.Text.error.jobDoesNotExit(getStatsRequest.jobId, getStatsRequest.queueName)); | ||
} | ||
await exports.validateAPIResponse(response, 200); | ||
await (0, exports.validateAPIResponse)(response, 200); | ||
}; | ||
exports.validateCancelJobAPIResponse = async (response, cancelJobRequest) => { | ||
exports.validateGetStatsAPIResponse = validateGetStatsAPIResponse; | ||
const validateCancelJobAPIResponse = async (response, cancelJobRequest) => { | ||
if (response.status === 404) { | ||
throw new errors_1.JobDoesNotExistError(text_1.Text.error.jobDoesNotExit(cancelJobRequest.jobId, cancelJobRequest.queueName)); | ||
} | ||
await exports.validateAPIResponse(response, 204); | ||
await (0, exports.validateAPIResponse)(response, 204); | ||
}; | ||
exports.validateCancelJobAPIResponse = validateCancelJobAPIResponse; |
{ | ||
"name": "@forge/events", | ||
"version": "0.0.0-experimental-64caa5a", | ||
"version": "0.0.0-experimental-72f4150", | ||
"description": "Forge Async Event methods", | ||
@@ -19,5 +19,5 @@ "author": "Atlassian", | ||
"dependencies": { | ||
"@forge/api": "^0.0.0-experimental-64caa5a", | ||
"@forge/api": "^0.0.0-experimental-72f4150", | ||
"uuid": "^3.4.0" | ||
} | ||
} |
@@ -1,11 +0,16 @@ | ||
import { getApiClientMock, verifyApiClientCalledWith } from './utils'; | ||
import { getMockFetchMethod, verifyApiClientCalledWith } from './utils'; | ||
import { InternalServerError, InvalidQueueNameError, JobDoesNotExistError, RateLimitError } from '../errors'; | ||
import { JobProgress } from '../jobProgress'; | ||
import { createRequestStargateAsApp } from '@forge/api'; | ||
const getJobProgress = (apiClientMock: any, jobId: string) => new JobProgress(jobId, apiClientMock); | ||
jest.mock('@forge/api', () => ({ | ||
createRequestStargateAsApp: jest.fn().mockReturnValue(getMockFetchMethod('done', 200)) | ||
})); | ||
const getJobProgress = (jobId: string, apiClientMock?: any) => new JobProgress(jobId, apiClientMock); | ||
describe('JobProgress methods', () => { | ||
describe('getStats', () => { | ||
it('should call the queue/stats endpoint', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -18,3 +23,3 @@ success: 100, | ||
); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
const response = await jobProgress.getStats(); | ||
@@ -32,3 +37,3 @@ const { success, inProgress, failed } = await response.json(); | ||
it('should throw JobDoesNotExistError', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -40,3 +45,3 @@ message: 'Job Not Found', | ||
); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow( | ||
@@ -52,4 +57,4 @@ new JobDoesNotExistError(`The job test-job-id was not found for the queue test-queue-name.`) | ||
it('should throw RateLimitError', async () => { | ||
const apiClientMock = getApiClientMock({}, 429); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const apiClientMock = getMockFetchMethod({}, 429); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow(new RateLimitError(`Too many requests.`)); | ||
@@ -63,3 +68,3 @@ verifyApiClientCalledWith(apiClientMock, '/webhook/queue/stats/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
it('should throw InternalServerError', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -72,3 +77,3 @@ message: 'Service is not available', | ||
); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow( | ||
@@ -86,4 +91,4 @@ new InternalServerError(`513 Status Text: Service is not available`, 513) | ||
it('should call the queue/cancel endpoint', async () => { | ||
const apiClientMock = getApiClientMock({}, 204); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const apiClientMock = getMockFetchMethod({}, 204); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
const response = await jobProgress.cancel(); | ||
@@ -98,3 +103,3 @@ verifyApiClientCalledWith(apiClientMock, '/webhook/queue/cancel/{cloudId}/{environmentId}/{appId}/{appVersion}', { | ||
it('should throw JobDoesNotExistError', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -106,3 +111,3 @@ message: 'Job Not Found', | ||
); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.cancel()).rejects.toThrow( | ||
@@ -119,3 +124,3 @@ new JobDoesNotExistError(`The job test-job-id was not found for the queue test-queue-name.`) | ||
it('should throw InternalServerError when WHP returns 422 response', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -126,3 +131,3 @@ errors: ['jobId must not be null', 'queueName must not be null'] | ||
); | ||
const jobProgress = getJobProgress(apiClientMock, 'test-queue-name#test-job-id'); | ||
const jobProgress = getJobProgress('test-queue-name#test-job-id', apiClientMock); | ||
await expect(jobProgress.getStats()).rejects.toThrow( | ||
@@ -139,3 +144,3 @@ new InternalServerError(`422 Status Text: jobId must not be null, queueName must not be null`) | ||
it('should throw errors when queueName or jobId is empty', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -148,6 +153,6 @@ success: 100, | ||
); | ||
const jobProgress1 = getJobProgress(apiClientMock, 'test-queue-name#'); | ||
const jobProgress1 = getJobProgress('test-queue-name#', apiClientMock); | ||
await expect(jobProgress1.getStats()).rejects.toThrow(new JobDoesNotExistError(`jobId cannot be empty.`)); | ||
const jobProgress2 = getJobProgress(apiClientMock, '#test-job-id'); | ||
const jobProgress2 = getJobProgress('#test-job-id', apiClientMock); | ||
await expect(jobProgress2.getStats()).rejects.toThrow( | ||
@@ -157,3 +162,3 @@ new InvalidQueueNameError('Queue names can only contain alphanumeric characters, dashes and underscores.') | ||
const jobProgress3 = getJobProgress(apiClientMock, ''); | ||
const jobProgress3 = getJobProgress('', apiClientMock); | ||
await expect(jobProgress3.getStats()).rejects.toThrow(new JobDoesNotExistError(`jobId cannot be empty.`)); | ||
@@ -163,2 +168,8 @@ | ||
}); | ||
it('requests stargate if no api client is provided', async () => { | ||
const jobProgress = getJobProgress('test-queue-name#job-id'); | ||
await jobProgress.getStats(); | ||
expect(createRequestStargateAsApp()).toBeCalledTimes(1); | ||
}); | ||
}); |
@@ -12,13 +12,18 @@ import { | ||
} from '../errors'; | ||
import { getApiClientMock, getApiClientMockWithoutResponseBody, verifyApiClientCalledPushPathWith } from './utils'; | ||
import { getMockFetchMethod, getApiClientMockWithoutResponseBody, verifyApiClientCalledPushPathWith } from './utils'; | ||
import { Queue } from '../queue'; | ||
import { JobProgress } from '../jobProgress'; | ||
import { createRequestStargateAsApp } from '@forge/api'; | ||
const getQueue = (apiClientMock: any, queueName: string) => new Queue({ key: queueName }, apiClientMock); | ||
jest.mock('@forge/api', () => ({ | ||
createRequestStargateAsApp: jest.fn().mockReturnValue(getMockFetchMethod('done', 201)) | ||
})); | ||
const getQueue = (queueName: string, apiClientMock?: any) => new Queue({ key: queueName }, apiClientMock); | ||
describe('Queue methods', () => { | ||
describe('constructor', () => { | ||
it('should throw InvalidQueueNameError', async () => { | ||
const apiClientMock = getApiClientMock(); | ||
expect(() => getQueue(apiClientMock, 'invalid name')).toThrowError( | ||
const apiClientMock = getMockFetchMethod(); | ||
expect(() => getQueue('invalid name', apiClientMock)).toThrowError( | ||
new InvalidQueueNameError('Queue names can only contain alphanumeric characters, dashes and underscores.') | ||
@@ -31,4 +36,4 @@ ); | ||
it('should call the queue/publish endpoint', async () => { | ||
const apiClientMock = getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod(); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = { | ||
@@ -42,4 +47,4 @@ page: 1 | ||
it('should throw InvalidPushSettingsError for delay', async () => { | ||
const apiClientMock = getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push(1, { delayInSeconds: 901 })).rejects.toThrow( | ||
@@ -52,4 +57,4 @@ new InvalidPushSettingsError(`The delayInSeconds setting must be between 0 and 900.`) | ||
it('should throw NoEventsToPushError', async () => { | ||
const apiClientMock = getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push([])).rejects.toThrow(new NoEventsToPushError(`No events pushed.`)); | ||
@@ -60,4 +65,4 @@ expect(apiClientMock).toHaveBeenCalledTimes(0); | ||
it('should throw TooManyEventsError', async () => { | ||
const apiClientMock = getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push([...Array(51).keys()])).rejects.toThrow( | ||
@@ -70,4 +75,4 @@ new TooManyEventsError(`This push contains more than the 50 events allowed.`) | ||
it('should throw PayloadTooBigError', async () => { | ||
const apiClientMock = getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod(); | ||
const queue = getQueue('name', apiClientMock); | ||
await expect(queue.push('x'.repeat(201 * 1024))).rejects.toThrow( | ||
@@ -80,4 +85,4 @@ new PayloadTooBigError(`The maximum payload size is 200KB.`) | ||
it('should throw RateLimitError', async () => { | ||
const apiClientMock = getApiClientMock({}, 429); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod({}, 429); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [...Array(10).keys()]; | ||
@@ -89,4 +94,4 @@ await expect(queue.push(payload)).rejects.toThrow(new RateLimitError(`Too many requests.`)); | ||
it('should throw InvocationLimitReachedError', async () => { | ||
const apiClientMock = getApiClientMock({}, 405); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod({}, 405); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [...Array(5).keys()]; | ||
@@ -100,3 +105,3 @@ await expect(queue.push(payload)).rejects.toThrow( | ||
it('should throw PartialSuccessError when there are failed events', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -116,3 +121,3 @@ failedEvents: [ | ||
); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [ | ||
@@ -149,3 +154,3 @@ { | ||
it('should throw PartialSuccessError when backend failed to create job stats', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -156,3 +161,3 @@ errorMessage: 'Failed to create stats for job name#12345' | ||
); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [ | ||
@@ -176,3 +181,3 @@ { | ||
it('should throw PartialSuccessError when there are failed events and backend failed to create job stats', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -189,3 +194,3 @@ errorMessage: 'Failed to create stats for job name#12345', | ||
); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [ | ||
@@ -213,3 +218,3 @@ { | ||
it('should throw InternalServerError', async () => { | ||
const apiClientMock = getApiClientMock( | ||
const apiClientMock = getMockFetchMethod( | ||
{ | ||
@@ -222,3 +227,3 @@ message: 'AWS SQS timed out', | ||
); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [...Array(9).keys()]; | ||
@@ -237,3 +242,3 @@ await expect(queue.push(payload)).rejects.toThrow( | ||
const apiClientMock = getApiClientMockWithoutResponseBody(504, 'Gateway Timeout'); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const queue = getQueue('name', apiClientMock); | ||
const payload = [1]; | ||
@@ -243,2 +248,8 @@ await expect(queue.push(1)).rejects.toThrow(new InternalServerError(`504 Gateway Timeout`, 504)); | ||
}); | ||
it('requests stargate if no api client is provided', async () => { | ||
const queue = getQueue('queue'); | ||
await queue.push({ test: 'stargate' }); | ||
expect(createRequestStargateAsApp()).toBeCalledTimes(1); | ||
}); | ||
}); | ||
@@ -248,4 +259,4 @@ | ||
it('should create a JobProgress by jobId', async () => { | ||
const apiClientMock = getApiClientMock(); | ||
const queue = getQueue(apiClientMock, 'name'); | ||
const apiClientMock = getMockFetchMethod(); | ||
const queue = getQueue('name', apiClientMock); | ||
const jobProgress = queue.getJob('test-job-id'); | ||
@@ -252,0 +263,0 @@ expect(jobProgress).toEqual(new JobProgress('test-job-id', apiClientMock)); |
import { Payload } from '../types'; | ||
export const getApiClientMock = (response?: any, statusCode = 201) => { | ||
export const getMockFetchMethod = (response?: any, statusCode = 201) => { | ||
return jest.fn().mockReturnValue({ | ||
@@ -5,0 +5,0 @@ created: statusCode === 201, |
@@ -16,1 +16,4 @@ export { Queue } from './queue'; | ||
export { QueueResponse } from './queueResponse'; | ||
export { InvocationError } from './invocationError'; | ||
export { InvocationErrorCode } from './invocationErrorCode'; | ||
export { RetryOptions } from './retryOptions'; |
@@ -9,2 +9,3 @@ import { CANCEL_JOB_PATH, GET_STATS_PATH, post } from './queries'; | ||
import { APIResponse, CancelJobRequest, FetchMethod, GetStatsRequest } from './types'; | ||
import { createRequestStargateAsApp } from '@forge/api'; | ||
@@ -17,3 +18,3 @@ export class JobProgress { | ||
this.id = id; | ||
this.apiClient = apiClient || (global as any).api.asApp().__requestAtlassian; | ||
this.apiClient = apiClient || createRequestStargateAsApp(); | ||
} | ||
@@ -20,0 +21,0 @@ |
@@ -6,2 +6,3 @@ import { PUSH_PATH, post } from './queries'; | ||
import { JobProgress } from './jobProgress'; | ||
import { createRequestStargateAsApp } from '@forge/api'; | ||
@@ -15,3 +16,3 @@ export class Queue { | ||
this.queueParams = queueParams; | ||
this.apiClient = apiClient || (global as any).api.asApp().__requestAtlassian; | ||
this.apiClient = apiClient || createRequestStargateAsApp(); | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -1,4 +0,10 @@ | ||
export class QueueResponse { | ||
protected _retry = false; | ||
export class Response { | ||
constructor(protected _retry: boolean) {} | ||
} | ||
export class QueueResponse extends Response { | ||
constructor() { | ||
super(false); | ||
} | ||
retry(): void { | ||
@@ -5,0 +11,0 @@ this._retry = true; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
73
1797
0
111988