-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bus): add HttpCommandDispatcher abstractions
- Loading branch information
1 parent
5c02ac6
commit 32cc5a0
Showing
5 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
5
packages/bus/src/command-dispatchers/HttpCommandDispatcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/bus/src/command-dispatchers/HttpCommandDispatcherConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './HttpCommand'; | ||
export * from './HttpCommandDispatcher'; | ||
export * from './HttpCommandDispatcherConfig'; |