Skip to content

Commit

Permalink
feat(bus): add HttpCommandDispatcher abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanReznichenko committed Apr 11, 2022
1 parent 5c02ac6 commit 32cc5a0
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
90 changes: 90 additions & 0 deletions packages/bus/src/command-dispatchers/HttpCommand.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { HttpCommand } from './HttpCommand';
import { CommandDispatcher } from '@secbox/core';
import { instance, mock, reset, verify, when } from 'ts-mockito';
import { Method } from 'axios';

class TestCommand extends HttpCommand<string, string | undefined> {
constructor({
payload,
url,
method,
expectReply,
ttl
}: {
payload: string;
url: string;
method: Method;
expectReply?: boolean;
ttl?: number;
}) {
super(payload, url, method, expectReply, ttl);
}
}

describe('HttpCommand', () => {
const mockDispatcher = mock<CommandDispatcher>();

afterEach(() => reset(mockDispatcher));

describe('constructor', () => {
it('should set default values to props', () => {
const payload = 'Test';
const url = '/api/test';
const method = 'GET';

const command = new TestCommand({ payload, url, method });

expect(command).toMatchObject({
payload,
url,
method,
ttl: 10000,
expectReply: true,
type: 'TestCommand',
createdAt: expect.any(Date),
correlationId: expect.any(String)
});
});
});

describe('execute', () => {
it('should dispatch command', async () => {
const command = new TestCommand({
payload: 'Test',
url: '/api/test',
method: 'GET'
});
when(mockDispatcher.execute(command)).thenResolve();

await command.execute(instance(mockDispatcher));

verify(mockDispatcher.execute(command)).once();
});

it('should return a result of execution', async () => {
const command = new TestCommand({
payload: 'Test',
url: '/api/test',
method: 'GET'
});
when(mockDispatcher.execute(command)).thenResolve('result');

const result = await command.execute(instance(mockDispatcher));

expect(result).toEqual('result');
});

it('should rethrow an exception', async () => {
const event = new TestCommand({
payload: 'Test',
url: '/api/test',
method: 'GET'
});
when(mockDispatcher.execute(event)).thenReject();

const result = event.execute(instance(mockDispatcher));

await expect(result).rejects.toThrow();
});
});
});
31 changes: 31 additions & 0 deletions packages/bus/src/command-dispatchers/HttpCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Command } from '@secbox/core';
import { Method } from 'axios';

export abstract class HttpCommand<T, R> extends Command<T, R> {
public readonly method: Method;
public readonly url: string;

protected constructor(
payload: T,
url: string,
method: Method,
expectReply?: boolean,
ttl?: number,
type?: string,
correlationId?: string,
createdAt?: Date
) {
super(payload, expectReply, ttl, type, correlationId, createdAt);

if (typeof url !== 'string') {
throw new Error();
}

if (!method) {
throw new Error();
}

this.url = url;
this.method = method;
}
}
5 changes: 5 additions & 0 deletions packages/bus/src/command-dispatchers/HttpCommandDispatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CommandDispatcher } from '@secbox/core';

export interface HttpCommandDispatcher extends CommandDispatcher {
init?(): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { rateLimitOptions } from 'axios-rate-limit';

export interface HttpCommandDispatcherConfig {
url: string;
axiosLimitOptions?: rateLimitOptions;
credentials?: {
username: string;
password: string;
};
}

export const HttpCommandDispatcherConfig: unique symbol = Symbol(
'HttpCommandDispatcherConfig'
);
3 changes: 3 additions & 0 deletions packages/bus/src/command-dispatchers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './HttpCommand';
export * from './HttpCommandDispatcher';
export * from './HttpCommandDispatcherConfig';

0 comments on commit 32cc5a0

Please sign in to comment.