@cheep/transport
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "@cheep/transport", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "src/index.js", | ||
"typings": "src/index.d.ts" | ||
} |
## Transport | ||
[@cheep/transport](https://www.npmjs.com/package/@cheep/transport) will help you to build Event Driven applications with 💯 type safety. | ||
[@cheep/transport](https://www.npmjs.com/package/@cheep/transport) will help you to build Realtime, Event Driven application. Keep in mind that there is difference between Event Driven and Event Sourcing and they aren't same, [read more about it](https://pablo-iorio.medium.com/event-driven-architectures-vs-event-sourcing-patterns-23d328289bf9). | ||
1. [Naming](docs/naming.md) | ||
2. [Microservices - Big Picture](docs/microservices.md) | ||
Basic (dynamic) example: | ||
## Example | ||
```ts | ||
@@ -20,3 +17,3 @@ import { MemoryTransport } from '@cheep/transport' | ||
// register listeners for specific routes | ||
transport.on('PING', () => 'PONG') | ||
transport.on('PING', async () => 'PONG') | ||
@@ -33,2 +30,68 @@ // start listening messages | ||
// result will be 'PONG' | ||
expect(result).toBe('PONG') | ||
// stop listening messages | ||
await transport.stop() | ||
// dispose will call stop as well if necessary | ||
await transport.dispose() | ||
``` | ||
Basic (Type Safed) Example: | ||
```ts | ||
import { | ||
MemoryTransport, | ||
createTransportHandler, | ||
createTransportApi, | ||
ApiWithExecutableKeys, | ||
} from '@cheep/transport' | ||
/** | ||
* Define api type | ||
*/ | ||
type Api = { | ||
Command: { | ||
User: { | ||
login: (props: { | ||
username: string | ||
password: string | ||
}): Promise<boolean> | ||
} | ||
} | ||
} | ||
type UserApi = ApiWithExecutableKeys<Api, 'Command'> | ||
/** | ||
* Use UserApi for type safety | ||
*/ | ||
const transport = new MemoryTransport() | ||
const handler = createTransportHandler<UserApi>(transport) | ||
const api = createTransportApi<UserApi>(transport) | ||
await transport.init() | ||
// register listeners | ||
handler.on( | ||
x => x.Command.User.login, | ||
(_, payload) => { | ||
return payload.username === payload.password | ||
}, | ||
) | ||
await transport.start() | ||
// RPC call on the PING route | ||
const result = await api.execute.Command.User.login({ | ||
username: 'Me', | ||
password: 'Me', | ||
}) | ||
expect(result).toBe('PONG') | ||
await transport.dispose() | ||
``` |
@@ -11,2 +11,5 @@ import { SendMessageProps, SendReplyMessageProps, TransportBase, TransportOptions, TransportUtils } from './transport.base'; | ||
}, utils?: TransportUtils); | ||
/** | ||
* Initializes Memory Transport | ||
*/ | ||
init(): Promise<void>; | ||
@@ -13,0 +16,0 @@ protected sendMessage(props: SendMessageProps): Promise<void>; |
@@ -17,5 +17,9 @@ "use strict"; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
/** | ||
* Initializes Memory Transport | ||
*/ | ||
init() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { }); | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
/**/ | ||
}); | ||
} | ||
@@ -22,0 +26,0 @@ sendMessage(props) { |
@@ -26,3 +26,9 @@ import { NormalizedError } from './domain/normalizeError'; | ||
abstract init(): Promise<void>; | ||
/** | ||
* Starts listening to the transport | ||
*/ | ||
start(): Promise<void>; | ||
/** | ||
* Stops listening to the transport | ||
*/ | ||
stop(): Promise<void>; | ||
@@ -32,5 +38,24 @@ protected abstract sendMessage(props: SendMessageProps): Promise<void>; | ||
protected newRpcCallRegistered(activeRpcCallsCount: number): void; | ||
/** | ||
* Registers handler for the specific route | ||
* @param route you want to listen | ||
* @param action registered handler | ||
* @returns | ||
*/ | ||
on(route: string, action: RouteHandler): () => void; | ||
/** | ||
* Unregisteres all handlers for the specific route | ||
* @param route | ||
*/ | ||
off(route: string): void; | ||
/** | ||
* Publishes message to the specific route | ||
* @returns Promise and it's recommended to wait it, to make sure that message was received by the broker | ||
*/ | ||
publish(props: PublishProps<MessageMetadata>): Promise<void>; | ||
/** | ||
* Executes route and returns resolved value from the remote route handler | ||
* | ||
* throws RPC Timeout Error | ||
*/ | ||
execute(props: ExecuteProps<MessageMetadata>): Promise<unknown>; | ||
@@ -41,2 +66,5 @@ /** provide a fire-and-forget handler for an array of prefixes*/ | ||
onEvery(prefix: string, action: RawHandler, isRawHandler: true): void; | ||
/** | ||
* Clears all allocated resources and stops the transport | ||
*/ | ||
dispose(): Promise<void>; | ||
@@ -43,0 +71,0 @@ protected processMessage(msg: TransportMessage): Promise<void>; |
@@ -24,2 +24,5 @@ "use strict"; | ||
} | ||
/** | ||
* Starts listening to the transport | ||
*/ | ||
start() { | ||
@@ -30,2 +33,5 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* Stops listening to the transport | ||
*/ | ||
stop() { | ||
@@ -38,2 +44,8 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
newRpcCallRegistered(activeRpcCallsCount) { } | ||
/** | ||
* Registers handler for the specific route | ||
* @param route you want to listen | ||
* @param action registered handler | ||
* @returns | ||
*/ | ||
on(route, action) { | ||
@@ -63,2 +75,6 @@ const handlers = this.routeHandlers.get(route); | ||
} | ||
/** | ||
* Unregisteres all handlers for the specific route | ||
* @param route | ||
*/ | ||
off(route) { | ||
@@ -69,2 +85,6 @@ if (this.routeHandlers.has(route)) { | ||
} | ||
/** | ||
* Publishes message to the specific route | ||
* @returns Promise and it's recommended to wait it, to make sure that message was received by the broker | ||
*/ | ||
publish(props) { | ||
@@ -88,2 +108,7 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* Executes route and returns resolved value from the remote route handler | ||
* | ||
* throws RPC Timeout Error | ||
*/ | ||
execute(props) { | ||
@@ -158,4 +183,8 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
} | ||
/** | ||
* Clears all allocated resources and stops the transport | ||
*/ | ||
dispose() { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
yield this.stop(); | ||
this.routeHandlers.clear(); | ||
@@ -162,0 +191,0 @@ this.prefixHandlers.clear(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
73685
1155
96