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

moleculer-universal-api

v0.0.6-alpha

Published

`moleculer-universal-api` allows you to use any node API app (e.g. `express`, `koa`, `hapi`, `feathers`, `nestjs`) as an API gateway.

Downloads

3

Readme

moleculer-universal-api

moleculer-universal-api allows you to use any node API app (e.g. express, koa, hapi, feathers, nestjs) as an API gateway.

NB: This is an alpha version as it hasn't been tested with multiple frameworks yet. So please use with caution.

Installation

npm i moleculer-universal-api

Usage

Create your app, returning an object with start, and stop methods - these are used to start and stop the server. Below is a simple example using express, you're free to create your app in any way you wish as long as it returns the two methods.

Note that the dependency services are passed in as parameters, so their implemation is decoupled (good for maintenance and testing).

export function createApp({
  logger,
  math,
}: {
  logger: SomeLoggerService;
  math: SomeMathService;
}) {
  const app = express();
  let server: Server | null = null;

  app.get('/add', async (req, res) => {
    const added = await math.add({ a: 1, b: 2 });

    res.json({ added });
  });

  return {
    start,
    stop,
  };

  function start() {
    return new Promise<void>((resolve) => {
      server = app.listen(PORT, () => {
        logger.info(`Listening on port ${PORT}`);
        
        resolve());
    });
  }

  function stop() {
    return new Promise<string>((resolve, reject) => {
      if (server?.listening) {
        server.close((err) => {
          if (err) {
            logger.error(`Server close error: ${err}`)
            reject();
          }

          logger.info('Server stopped successfully.');
          resolve();
        });
      }
    });
  }
}

Then simply create your gateway service using the mua function:

const broker = new ServiceBroker();

broker.createService(mathService);

const { createService, getApp } = mua(createApp, {
  dependencies: ['math'],
  // mixins: ...,
  // settings: ...,
  // methods: ...,
  // etc.
});

createService(broker);

// use `getApp` to retrieve the app instance if you wish to use it later: 
// const app = getApp();
// app.use(someMiddleware);

return broker.start();

If you need access to the broker's internal logger, there are two strategies. You could either wrap it as a separate service, and pass it in as a dependency as above. Or you could simply wrap your createApp and provide it when you create your gateway:

export function withLogger(logger) {
  return function createApp() {
    const app = express();
    ...
  }
}

const broker = new ServiceBroker();

const { createService, getApp } = mua(withLogger(broker.logger));

createService(broker);