@maur025/core-commands
v1.0.1
Published
utility library to declare and extend the command pattern in Typescript.
Maintainers
Readme
Overview
Utility library to declare and extend Command Pattern, inspired by the java implementation.
Installation
Use the following command to install:
Using pnpm:
pnpm add @maur025/core-commandsUsing npm:
npm install @maur025/core-commandsUsing yarn:
yarn add @maur025/core-commandsUsage
Using thi library is simple. Extend AbstractCommand class to create a command. Override the run method(required) and optionally validate for custom validations.
Example
Command (example.cmd.ts)
import { AbstractCommand } from "@maur025/core-commands";
export default class ExampleCmd extends AbstractCommand<Request, void> {
protected validate(request: Request): void {
console.log("VALIDATING...");
}
protected run(request: Request): void {
console.log("EXECUTING EXAMPLE...");
}
}
interface Request {
paramOne: string;
paramTwo: number;
paramN: string;
}Your can assign any value to the input and output. These can be declarated as interfaces, classes, or primitive types.
Usage in your code (example.controller.ts)
import ExampleCmd from "src/command/example.cmd.ts";
const exampleCmd = new ExampleCmd();
export default class ExampleController {
processData = (): void => {
exampleCmd
.withRequest({
paramOne: "example",
paramTwo: 2,
paramN: "any value",
})
.execute();
};
}Suggestions
To improve code maintainability and reduce verbosity, consider using a Dependency Injection (DI) container to handle command instantation.
For example:
- This avoids manually using
new ExampleCmd()in your classes. - Promote better separation and makes testing easier.
