@vivekthangam/my-node-library
v1.1.1
Published
A simple annotation-based library for Node.js
Readme
My Node Library
A simple TypeScript library demonstrating the use of decorators.
Description
This project provides a @Log() decorator that can be used to log method calls, their arguments, and their return values. It's a great example of how to use TypeScript decorators for aspect-oriented programming, such as logging or monitoring.
Installation
To use this library in your project, you would typically install it via npm. If it were published, you would run:
npm install @vivekthangam/my-node-libraryFor local development, you can clone the repository and install dependencies:
git clone <your-repo-url>
cd my-test-app
npm installUsage
Here's how to use the @Log decorator in your TypeScript project.
First, you need to enable decorators in your tsconfig.json:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}Then, you can import and use the decorator on your methods:
import { Log } from "@vivekthangam/my-node-library";
class Greeter {
@Log()
sayHello(name: string): string {
console.log(`Executing the actual method for ${name}...`);
return `Hello, ${name}!`;
}
}
const greeter = new Greeter();
const message = greeter.sayHello('World');
console.log(`Final message: ${message}`);Running the example
To run the example file (index.ts), you can use ts-node:
npx ts-node index.tsYou should see output showing the method call being logged by the decorator, followed by the output from the method itself.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
MIT
