You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
npm i --save @lomray/microservice-remote-middleware
Usage
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';constmicroservice=Microservice.create({name: 'configuration'});// Add this endpoint for testing remote middlewaremicroservice.addEndpoint('example-method',({ hello })=>({ hello }));constresult=async()=>{try{// In this case we are use TYPEORM constconnection=awaitcreateConnection();// Get Middleware repository for store middlewares dataconstrepository=connection.getRepository(Middleware)// Enable remote middlewareconstremoteMiddleware=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-methodmicroservice.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.awaitremoteMiddleware.add('configuration','middleware-method','gateway','configuration.*',);awaitmicroservice.start();}catch(e){console.info('\x1b[31m%s\x1b[0m',`Failed to start microservice: ${e.messageasstring}`);}};exportdefaultresult();
Create client remote middleware instance. It might be any microservice.
import{Gateway}from'@lomray/microservice-remote-middleware';import{RemoteMiddlewareClient}from'@lomray/microservice-remote-middleware';constmicroservice=Gateway.create({name: 'gateway'});constresult=async()=>{try{// Enable remote middlewareawaitRemoteMiddlewareClient.create(microservice).addRegisterEndpoint().obtainMiddlewares();awaitmicroservice.start();}catch(e){console.info('\x1b[31m%s\x1b[0m',`Failed to start microservice: ${e.messageasstring}`);}};exportdefaultresult();
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)awaitremoteMiddleware.add('configuration','middleware-method','gateway','*',);// Add remote middleware on specific microservice method (note: this is gateway microservice)awaitremoteMiddleware.add('configuration','middleware-method','gateway','users.list',);// Also you can add remote middleware directly to microserviceawaitremoteMiddleware.add('configuration','middleware-method','users','users.create',);// Just one more example :) Add authentication for requests (you should have authentication microservice)awaitremoteMiddleware.add('authentication','middleware-authenticate','gateway','*',);// Or you can chose another middleware response strategy and convert input/output dataawaitremoteMiddleware.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'}});