@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
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:
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'
.
- If the environment variable
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.
- If the
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
},
},
};
}
};