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

@lomray/microservice-remote-middleware

v1.8.2

Published

Microservice Remote Middleware lib

Downloads

508

Readme

NodeJS Microservice Remote Middleware lib for Microservices lib

npm GitHub GitHub package.json dependency version (dev dep on branch) semantic-release

Quality Gate Status Reliability Rating Security Rating Vulnerabilities Lines of Code Coverage

Install

npm i --save @lomray/microservice-remote-middleware

Usage

  1. Create server remote middleware instance. It might be any microservice but only one.
import { Microservice } from '@lomray/microservice-remote-middleware';
import { RemoteMiddlewareServer } from '@lomray/microservice-remote-middleware';
import { createConnection } from 'typeorm';

const microservice = Microservice.create({ name: 'configuration' });

// Add this endpoint for testing remote middleware
microservice.addEndpoint('example-method', ({ hello }) => ({ hello }));

const result = async () => {
  try {
    // In this case we are use TYPEORM  
    const connection = await createConnection();
    // Get Middleware repository for store middlewares data
    const repository = connection.getRepository(Middleware)

    // Enable remote middleware
    const remoteMiddleware = RemoteMiddlewareServer.create(microservice, repository)
      .addRegisterEndpoint()
      .addObtainEndpoint();

    /**
     * Add microservice method like remote middleware.
     * In future, you can create CRUD for this actions.
     * For example:
     */
    // 1. Create method for register like middleware: configuration.middleware-method
    microservice.addEndpoint('middleware-method', () => ({ hello: 'middleware world' }), {
      isDisableMiddlewares: true,
      isPrivate: true,
    });
    // 2. Register remote middleware on gateway microservice. This middleware will be triggered only for requests to any configuration methods.
    await remoteMiddleware.add(
      'configuration',
      'middleware-method',
      'gateway',
      'configuration.*',
    );

    await microservice.start();
  } catch (e) {
    console.info('\x1b[31m%s\x1b[0m', `Failed to start microservice: ${e.message as string}`);
  }
};

export default result();
  1. Create client remote middleware instance. It might be any microservice.
import { Gateway } from '@lomray/microservice-remote-middleware';
import { RemoteMiddlewareClient } from '@lomray/microservice-remote-middleware';

const microservice = Gateway.create({ name: 'gateway' });

const result = async () => {
  try {
    // Enable remote middleware
    await RemoteMiddlewareClient.create(microservice)
      .addRegisterEndpoint()
      .obtainMiddlewares();

    await microservice.start();
  } catch (e) {
    console.info('\x1b[31m%s\x1b[0m', `Failed to start microservice: ${e.message as string}`);
  }
};

export default result();
  1. That is all. Check it:
curl -X POST http://127.0.0.1:3000
   -H 'Content-Type: application/json'
   -d '{"id":"unique-id-1","method":"configuration.example-method","params":{"hello":"world"}}'

You can run any microservice and create remote middleware client. Check out the examples below to get the desired result:

// Add remote middleware on any microservices methods (note: this is gateway microservice)
await remoteMiddleware.add(
  'configuration',
  'middleware-method',
  'gateway',
  '*',
);

// Add remote middleware on specific microservice method (note: this is gateway microservice)
await remoteMiddleware.add(
  'configuration',
  'middleware-method',
  'gateway',
  'users.list',
);

// Also you can add remote middleware directly to microservice
await remoteMiddleware.add(
  'configuration',
  'middleware-method',
  'users',
  'users.create',
);

// Just one more example :) Add authentication for requests (you should have authentication microservice)
await remoteMiddleware.add(
  'authentication',
  'middleware-authenticate',
  'gateway',
  '*',
);

// Or you can chose another middleware response strategy and convert input/output data
await remoteMiddleware.add(
  'authentication',
  'get-jwt',
  'gateway',
  '*',
  {
    type: MiddlewareType.response,
    strategy: MiddlewareStrategy.same,
    isRequired: true,
    convertResult: {
        // here we get user id from users microservice response and pass like param to 'get-jwt' method
        'user.id': 'userId'
    }
  }
);