Stanley The Template For Typescript By Dragons Gamers
Template Stanley for Typescript projects and packages !
Table of Contents
Benefits
- Event Pattern for your project
- Over 800 rules for pattern, possible errors and errors in Linter
- Code quality guaranteed
- Ioc Container
- 100% Test Coverage
Libraries
Dependencies
- Node.js 16 or later
- Yarn Optional/Recommended
- ODG TsConfig Last Version
Get Started
Use Event Interface
Install in your project using this command
yarn add @odg/events
Usage
Implementation
interface EventType {
"event1": number, // receive number
"userRegister": {
name: string,
age: number
} // receive object,
}
const eventBus = new EventEmitterBus<EventType>();
const callback = (eventMessage: string) => {
console.log(eventMessage);
};
eventBus.subscribe("event1", callback); // Subscribe before dispatch
eventBus.dispatch("event1", 1); // Send 1 to event1
eventBus.dispatch("userRegister", { name: "ODGodinho", age: 18 }); // Send object to userRegister
Ioc
class Example {
public constructor(
private eventBus: EventEmitterBus<EventType>
) {
}
public example(){
// code ...
this.eventBus.dispatch("event1", 1);
// code ...
}
}
Use Event Provider
Enum Events Types
enum EventName {
Example = "Example",
}
import { type EventObjectType } from "@odg/events";
import { type EventName } from "../app/Enums";
export interface EventExampleParameters {
// anything params
}
export interface EventBaseInterface extends EventObjectType {
[EventName.Example]: EventExampleParameters;
}
// IF the above interface does not have all existing laws in the enum it will report an error
export type EventTypes<T extends Record<EventName, unknown> = EventBaseInterface> = T;
Make a provider
import {
EventBusInterface,
EventServiceProvider as EventServiceProviderBase,
type EventListener,
} from "@odg/events";
import {
Container,
// inject, prefer inversify strongly typed Container
injectable,
} from "inversify";
// if use inversify Strongly Typed Container
import { $inject } from "#app/ContainerInject";
import { type EventTypes } from "../../Interfaces/EventsInterface";
import { ContainerName, EventName } from "../Enums";
import { type HomeEventListeners } from "../Listeners/HomeEventListeners";
@injectable()
export class EventServiceProvider extends EventServiceProviderBase<EventTypes> {
public constructor(
@$inject(ContainerName.EventBus) public readonly bus: EventBusInterface<Events>,
) {
}
/**
* Listeners for events in the application.
*
* @protected
* @type {EventListener<EventTypes>}
* @memberof EventServiceProvider
*/
protected listeners: EventListener<EventTypes> = {
[EventName.Example]: [
{
listener: new HomeEventListeners(), // You can use inversify
options: {},
},
],
};
public async boot(): Promise<void> {
await super.boot();
}
public async shutdown(): Promise<void> {
await super.shutdown();
}
}
Use Event Listeners
import type { EventListenerInterface } from "@odg/events";
import type { LoggerInterface } from "@odg/log";
import { injectable } from "inversify"; // if used inversify
import { type EventExampleType, type EventTypes } from "../../Interfaces/EventsInterface";
import { ContainerName, type EventName } from "../Enums"; // If use inversify
import { PageFactoryType } from "../Factory/PageFactory";
import { $inject } from "#app/ContainerInject"; // If use inversify Strongly Typed Container
@injectable()
export class HomeEventListeners implements EventListenerInterface<EventTypes, EventName.Example> {
public constructor(
@$inject(ContainerName.Logger) public readonly logger: LoggerInterface,
) {
}
public async handler({ page }: EventExampleType): Promise<void> {
await this.logger.debug("HomeEventListeners is sended");
}
}
Prepare To Develop
Copy .env.example to .env and add the values according to your needs.
Start Project
First install dependencies with the following command
bun install
# or
npm install
Build and Run
To build the project, you can use the following command
if you change files, you need to run
bun run buildandbun run startagain
bun run build && bun run start
# or
bun run dev
Teste Code
To Test execute this command
bun run test
# or
bun run test:watch