EZ4: Components to build topic services
npm install @ez4/topicIt uses the power of reflection to provide a contract that determines how to build and connect topic components.
#### Install
``sh`
npm install @ez4/topic @ez4/local-topic @ez4/aws-topic -D
#### Create topic
`ts
// file: topic.ts
import type { Environment, Service } from '@ez4/common';
import type { Topic } from '@ez4/topic';
// MyTopic message
type MyTopicMessage = {
foo: string;
bar: number;
};
// MyTopic declaration
export declare class MyTopic extends Topic.Service
subscriptions: [
Topic.UseSubscription<{
handler: typeof eventHandler;
}>
];
variables: {
myVariable: Environment.Variable<'MY_VARIABLE'>;
};
services: {
otherService: Environment.Service
variables: Environment.ServiceVariables;
};
}
// MyTopic message handler
export function eventHandler(request: Topic.Incoming
const { otherService, variables } = context;
const { message } = request;
// Access message contents
message.foo;
// Access injected services
otherService.call();
// Access injected variables
variables.myVariable;
}
`
#### Use topic
`ts
// file: handler.ts
import type { Service } from '@ez4/common';
import type { MyTopic } from './topic';
// Any other handler that has injected MyTopic service
export async function anyHandler(_request: any, context: Service.Context
const { myTopic } = context;
await myTopic.sendMessage({
foo: 'foo',
bar: 123
});
}
`
#### Service
| Name | Type | Description |
| ------------- | ----------------------- | ----------------------------------------------------- |
| fifoMode | Topic.UseFifoMode<> | Enable and configure the FIFO mode options. |
| subscriptions | Topic.UseSubscription<> | All subscriptions associated to the topic. |
| variables | object | Environment variables associated to all subscription. |
| services | object | Injected services associated to all subscription. |
> Use type helpers for fifoMode and subscriptions` properties.
#### Subscriptions (Function)
| Name | Type | Description |
| ------------ | -------- | ----------------------------------------------------- |
| listener | function | Life-cycle listener function for the subscription. |
| handler | function | Entry-point handler function for the subscription. |
| variables | object | Environment variables associated to the subscription. |
| logRetention | integer | Log retention (in days) for the handler. |
| timeout | integer | Maximum execution time (in seconds) for the handler. |
| memory | integer | Memory available (in megabytes) for the handler. |
#### Subscriptions (Queue)
| Name | Type | Description |
| ------- | --------------------- | ------------------------------- |
| service | Environment.Service<> | Reference to the queue service. |
- Get started with topic
- Importing topic
- Local provider
- AWS provider
MIT License