npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

microframe-ts

v1.0.3

Published

Typescript framework for creating microservices.

Downloads

24

Readme

Api

ApiManager

The ApiManager handles the Express Application and all of the registered controllers.

BaseController

The BaseController provides base functionality for an Api Controller, and should be extended when creating a controller. Controller routes can be registered using the BaseController's RegisterRoutes method.

Events

NewApiRequestEvent

The NewApiRequestEvent is published to the EventAggregator when a Api request is received by a controller.

Event

EventAggregator

The EventAggregator aggregates published events to subscribed handlers. The EventAggregator singleton can be accessed via the UseEventAggregator function.

Usage

The EventAggregator works best when used with Dependecy Injection. Below is an example of getting the EventAggregator singleton, then registering the singleton with a typedi container.

const eventAggregator = UseEventAggregator();
Container.set(EventAggregator, eventAggregator);

When registered in a typedi container, the EventAggregator can be resolved anywhere with access to the container, or via constructor injection (example below).

@Service()
export default class ArticleService {
    constructor (eventAggregator: EventAggregator) {
        eventAggregator.GetEvent(NewApiRequestEvent).Subscribe((args: NewApiRequestEvent) => {console.log(args.Request)});
    }
}

Creating a Microservice

Setup

First thing to do is setup the Depenecy Injection for the service, typedi works well with microframe-ts but any Dependecy Injection package should work (setup for typedi can be found here. Start by registering any packages outside your project with the typedi container, example below shows the microframe-ts ApiManager being registered with the container.

Container.set(ApiManager, apiManager);

Any classes you wish to be automatically registered with the container as a singleton should have the Service decorator.

@Service()
export default class ExampleService {
}

Web Api

A Web Api allows for communication via HTTP requests. microframe-ts uses controllers to handle the Api requests recieved. Start by creating a controller, a controller should be a class that extends the BaseController class; controllers should also be marked with the Service typedi decorator or manually registered with the DI container. To initialise routes, the controller needs a method named InitRoutes which will be called by the BaseController when it is ready to initialise; this is where routes can be registered (see example below).

@Service()
export default class ExampleController extends BaseController {
    public constructor() {
        super('/example');
    }

    public InitRoutes(): void {
        // Routes go here.
        console.log(`Init routes - ${this.path}`);
        this.RegisterRoutes([
            { method: HttpMethod.GET, path: `${this.path}/helloworld`, handler: async (_, response: Express.Response) => await this.helloWorld(response) },
        ]);
        console.log(`Routes registered - ${this.routes}`);
    }

    private async helloWorld(response: Express.Response): Promise<void> {
        response.send('Hello World');
    }
}

Next step is to create an instance of ApiManager, the ApiManager expects a port number and a list of controllers, this list of controllers can be resolved from the container as seen below.

 const apiManager = new ApiManager(5000,[Container.get(ArticleController)]);
 Container.set(ApiManager, apiManager);

Finally call the Listen method on the ApiManager.

 const apiManager = new ApiManager(5000,[Container.get(ArticleController)]);
 Container.set(ApiManager, apiManager);
 apiManager.Listen();

Logger

microframe-ts uses the package tslog for logging, more information on tslog can be found here. In order to use the Logger throughout the microservice, register the Logger with the DI container using UseLogger to get the Logger singleton; as shown below.

const logger = UseLogger();
Container.set(Logger, logger);

The Logger can then be injected in services as so.

@Service()
class ExampleService {
    private readonly logger: Logger;
    
    constructor(logger: Logger) {
        this.logger = logger;
        this.logger.info('Hello World');
    }
}

Information on Microservice architecture can be found here.