Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@qrvey/function-gateway

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@qrvey/function-gateway

![install size](https://packagephobia.com/badge?p=%40qrvey%2Ffunction-gateway) ![coverage](https://img.shields.io/badge/unit_test_coverage-100%25-brightgreen)

  • 1.0.8
  • latest
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

@qrvey/function-gateway

install size coverage

The @qrvey/function-gateway package provides a unified interface for invoking lambda and REST functions based on the specific configurations of the service.

Installation

You can install the package using npm or yarn:

npm install @qrvey/function-gateway

Or with yarn:

yarn add @qrvey/function-gateway

Note: If the application does not have @aws-sdk/client-lambda installed, it must be installed manually. @qrvey/function-gateway requires it to make Lambda invoke calls.

Conditions for Lambda or REST call

The function gateway in the @qrvey/function-gateway package can invoke functions either as a Lambda function or as a REST API call, depending on certain conditions. These conditions are determined by:

  1. REST Invocation:

    • If the environment variable PLATFORM_TYPE is set to 'CONTAINER' and the function mapping contains a REST configuration.
    • If the options.mappingType parameter is explicitly set to 'rest'.
  2. Lambda Invocation:

    • If the options.mappingType parameter is explicitly set to 'lambda'.
    • In the absence of options.mappingType being specified, the function gateway will default to Lambda invocation unless there is a REST configuration and no Lambda configuration.

Usage Example

const { FunctionGatewayService } = require("@qrvey/function-gateway");
const mapper = require("./fnGwMapping");

class MyFunctionGateway extends FunctionGatewayService {
  getInfoById(id) {
    const headers: {
        'x-api-key': 'my-api-key'
    }
    const params = { headers, dataId: id };
    return MyFunctionGateway.invoke("getData", params, mapper);
  }
}

const myFunctionGw = new MyFunctionGateway();
myFunctionGw
  .getInfoById("my_id")
  .then((res) => {
    // Handle successful result
    console.log(JSON.parse(res.Payload));
  })
  .catch((error) => {
    // Handle errors
    console.log(error);
  });

API

Class

FunctionGatewayService

Function

static invoke(functionName: string, params: unknown, functionMapping: IFunctionMapping, options: IFunctionOptions): Promise<any>

  • functionName: The name of the function to invoke.
  • params: Parameters to be provided to the corresponding property in the functionMapping definition.
  • functionMapping: An object that maps function names to function implementations.
  • options: An object containing additional options for invoking the function.
  • Returns: A promise that resolves with the result of the invoked function or rejects with an error if the invocation fails.
functionMapping param

Defines the mapping of function names to their corresponding implementations.

Example

This example demonstrates how to configure both a Lambda function and a REST API call:

const mapper = {
    // Define a mapping for a function named 'getData'
    getData: (param) => {
        return {
            // Configuration for a Lambda invocation
            lambda: {
                // [required] The name of the Lambda function
                name: process.env.SERVER_PREFIX + '_getDataFunction',
                // [required] The payload to send to the Lambda function
                payload: {
                    headers: param.headers,
                    method: 'get', // HTTP method for the request
                    body: {
                        pathParameters: {
                            dataID: param.dataID, // Parameters to pass to the Lambda function
                        },
                    },
                },
                // [optional]
                options: {
                    asHttp: false,
                    //invocationParams defines the additional params to be passed in lambda.invoke call
                    invocationParams: {
                        Qualifier: 'my_lambda_alias' //should be the name of the property used in lambda.invoke of @aws-sdk/client-lambda library
                    },
                },
                // A callback function to handle the Lambda response
                callback: (res) => {
                    const responseObj = JSON.parse(res.Payload);
                    return responseObj.data; // Extracting and returning the data from the response
                },
            },
            // Configuration for REST API invocation
            rest: {
                // The URL for the REST API call
                endpoint: `/api/service-data/${param.dataID}`,
                // The HTTP method for the request
                method: 'get',
                // Additional options for the REST API call
                options: {
                    baseDomain: process.env.API_DOMAIN,
                    headers: param.headers, // Headers to include in the request
                },
            },
        };
    }
};

Keywords

FAQs

Package last updated on 02 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc