2.1.0 • Published 2 years ago

payment-microservice-client v2.1.0

Weekly downloads
41
License
ISC
Repository
-
Last release
2 years ago

Payment Microservice Client

Description

Application side package for payment-microservice.

This package is meant to be a wrapper for applications using the payment-microservice package with redis.

Usage

Import the package main method:

import pmClient from 'payment-microservice-client';

This method will receive either the express server and the port in which it is running to start a socket server and client on it, or if you already have a socket server you can pass it to the function and it will use it.

If both the socket server and the express server are passed, the socket server will be used and the express server will be ignored.

Returns the socketServer.

Parameters

ParameterTypeDescriptionOptional
httpServerhttp.ServerExpress serverYes. Only needed if no socketServer is passed.
httpPortnumberPor in which the socket client will run. Should be the same as the express server port.No.
socketServerio.ServerSocket server which will be used to set up the connection.Yes. Only needed if no httpServer and httpPort are passed
redisPortnumberPort in which the redis server is running .No
socketPathstringPath in which the socket server will be running. Defaults to /wsYes. Only needed if no socketServer is passed.
redisHoststringPath or IP for the redis serverNo
clientNamestringApplication name for this client. Used for logging.No
connectFnfunction. (socket: io.Socket) => void)Function which will be executed on the on connection event of the socket server.Yes

Example:

const app = express();
  app.use(cors());
  const server = createServer(app);
  server.listen(3005);
  pmClient({
    httpServer: server,
    httpPort: 3005,
    socketPath: '/ws',
    redisHost: '127.0.0.1',
    clientName: 'TestClient',
    connectFn: socketConnect,
  });
  const socketConnect = (socket: Socket): void => {...}

This method will create a socket server (if not passed) with a redisAdapter, and a socket client.

The socket client will connect to its own server, which will push it into a room with name hostName().

This socket client will listen to the alive event emmited by the payment-microservice package. This event will receive an object with a micro service name and a room name, which will be pushed into an array.

Socket interface

This package exposes a socket interface to communicate with the micro services.

To do this we will use the executeToRoom method.

const executeToRoom = <ReturnType>(
  name: string,
  params?: object,
  microName?: string,
): Promise<ReturnType> => {...}

This method will make a synchronous call to a micro service socket event. It receives the name of the event, the params for the event method and the micro service name.

It will take the most recent room in the array with the micro service name passed and will execute the event, which has been set with a methodWrapper from the payment-microservice package. When the micro service method ends its execution it will emit an event with an id supplied by executeToRoom, which will be listening for this event and resolve the promise.

If our application is only communicating with one micro service, we don't need to use the microName param, as it will take the most recent room from the array.

Example:

import Socket from 'payment-microservice-client/Socket';
const keys = await Socket.executeToRoom<string>('getKeys', inData, 'PayPal');
2.1.0

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.26

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.25

3 years ago

1.0.22

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago