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

@qrvey/function-gateway

v1.0.7

Published

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

Downloads

8,320

Readme

@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
                },
            },
        };
    }
};