Skip to content

feat: Enrich put and publish method results with initial command payload #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@typescript-eslint/eslint-plugin": "^5.11.0",
"@typescript-eslint/parser": "^5.11.0",
"aws-lambda-mock-context": "^3.2.1",
"aws-sdk-client-mock": "^0.5.6",
"aws-sdk-client-mock": "^2.0.0",
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-jest": "^26.1.0",
Expand Down
74 changes: 71 additions & 3 deletions src/Bus.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { chunkEntries, computeEventSize } from './Bus';
import { Bus, chunkEntries, computeEventSize } from './Bus';
import { mockClient } from 'aws-sdk-client-mock';
import {
EventBridgeClient,
PutEventsCommand,
} from '@aws-sdk/client-eventbridge';

const smallEntry = {
Detail: 'small'.repeat(Math.round(25000 / 5)), // ~ 25Kb
};
const bigEntry = {
Detail: 'big'.repeat(Math.round(100000 / 3)), // ~ 100Kb
};
const failEntry = {
Detail: 'fail',
};
const successEntry = {
Detail: 'success',
};

const cases = [
{
Expand Down Expand Up @@ -60,14 +71,71 @@ const cases = [
];

describe('Bus', () => {
describe('#put', () => {
const eventBridgeClientMock = mockClient(EventBridgeClient);
const bus = new Bus({
name: 'testBus',
// @ts-expect-error Mocking library mocked client is not type compatible with actual client
EventBridge: eventBridgeClientMock,
});
beforeEach(() => {
eventBridgeClientMock.reset();
});
it('should return events command payload with results', () => {
const testCase = [
failEntry,
successEntry,
successEntry,
failEntry,
successEntry,
failEntry,
successEntry,
];
eventBridgeClientMock.on(PutEventsCommand).resolves({
FailedEntryCount: 3,
Entries: [
{ ErrorCode: '12', ErrorMessage: 'There was an error' },
{ EventId: '97a200f1-6919-4619-ac2f-e0026ebb15b7' },
{ EventId: 'e4c40e6a-1fc0-4f5a-82b3-c6c284d772ca' },
{ ErrorCode: '13', ErrorMessage: 'There was an error' },
{ EventId: '1cdf5682-6f5c-4c21-b922-f83d06447952' },
{ ErrorCode: '14', ErrorMessage: 'There was an error' },
{ EventId: '9039f372-d810-474b-9741-b369394cefef' },
],
});
const failEntryResultBuilder = (code: string) => ({
ErrorCode: code,
ErrorMessage: 'There was an error',
Detail: 'fail',
EventBusName: 'testBus',
});
const successEntryResultBuilder = (id: string) => ({
EventId: id,
Detail: 'success',
EventBusName: 'testBus',
});
expect(bus.put(testCase)).resolves.toEqual({
FailedEntryCount: 3,
Entries: [
failEntryResultBuilder('12'),
successEntryResultBuilder('97a200f1-6919-4619-ac2f-e0026ebb15b7'),
successEntryResultBuilder('e4c40e6a-1fc0-4f5a-82b3-c6c284d772ca'),
failEntryResultBuilder('13'),
successEntryResultBuilder('1cdf5682-6f5c-4c21-b922-f83d06447952'),
failEntryResultBuilder('14'),
successEntryResultBuilder('9039f372-d810-474b-9741-b369394cefef'),
],
});
});
});
describe('#computeEventSize', () => {
it('should compute event size', () => {
expect(
computeEventSize({
DetailType: 'myType',
Detail: JSON.stringify({ property: 'vaalue' }),
Detail: JSON.stringify({ property: 'value' }),
}),
).toBe(27);
).toBe(26);
});
});

Expand Down
15 changes: 10 additions & 5 deletions src/Bus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type {
EventBridgeClient,
PutEventsResponse,
PutEventsRequestEntry,
PutEventsResultEntry,
} from '@aws-sdk/client-eventbridge';
Expand Down Expand Up @@ -29,7 +28,10 @@ export class Bus {
this._eventBridge = EventBridge;
}

async put(events: PutEventsRequestEntry[]): Promise<PutEventsResponse> {
async put(events: PutEventsRequestEntry[]): Promise<{
FailedEntryCount?: number;
Entries?: (PutEventsRequestEntry & PutEventsResultEntry)[];
}> {
const entries = events.map((entry) =>
Object.assign({}, { ...entry }, { EventBusName: this._name }),
);
Expand All @@ -43,15 +45,18 @@ export class Bus {
);

return results.reduce<{
Entries: PutEventsResultEntry[];
Entries: (PutEventsRequestEntry & PutEventsResultEntry)[];
FailedEntryCount: number;
}>(
(returnValue, result) => {
(returnValue, result, index) => {
if (result.FailedEntryCount) {
returnValue.FailedEntryCount += result.FailedEntryCount;
}
if (result.Entries) {
returnValue.Entries.push(...result.Entries);
const enrichedResult = result.Entries.map((entry) =>
Object.assign({}, { ...entry }, { ...entries[index] }),
);
returnValue.Entries.push(...enrichedResult);
}

return returnValue;
Expand Down
19 changes: 13 additions & 6 deletions src/Event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,19 @@ describe('Event', () => {
});

it('should allow publishing an event', async () => {
expect(
await myEvent.publish({
attribute: 'hello',
numberAttribute: 12,
}),
).toHaveProperty('Entries', [{ EventId: '123456' }]);
const eventPayload = {
attribute: 'hello',
numberAttribute: 12,
};
expect(await myEvent.publish(eventPayload)).toHaveProperty('Entries', [
{
EventId: '123456',
DetailType: 'myEvent',
Detail: JSON.stringify(eventPayload),
EventBusName: 'test',
Source: 'source',
},
]);
});

it('should fail with the use of validationMiddleware on wrong event', () => {
Expand Down
7 changes: 5 additions & 2 deletions src/Event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ajv from 'ajv';
import type {
PutEventsRequestEntry,
PutEventsResponse,
PutEventsResultEntry,
} from '@aws-sdk/client-eventbridge';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import type { EventBridgeEvent } from 'aws-lambda';
Expand Down Expand Up @@ -91,7 +91,10 @@ export class Event<N extends string, S extends JSONSchema> {
};
}

async publish(event: FromSchema<S>): Promise<PutEventsResponse> {
async publish(event: FromSchema<S>): Promise<{
FailedEntryCount?: number;
Entries?: (PutEventsRequestEntry & PutEventsResultEntry)[];
}> {
return this._bus.put([this.create(event)]);
}
}
Expand Down