Git Product home page Git Product logo

bs-aws-lambda's Introduction

bs-aws-lambda

bs-aws-lambda is a set of types to use when creating AWS lambda handlers.

Those types are inspired by the @types/aws-lambda package with typescript types.

Installation

yarn add @ahrefs/bs-aws-lambda

or to follow the master version:

yarn add https://github.com/ahrefs/bs-aws-lambda.git

Usage

Add @ahrefs/bs-aws-lambda to the bs-dependencies of bsconfig.json.

Then when you create a function handler for a lambda, you can annotate it with one of AwsLambda.Scheduled.handler, AwsLambda.Dynamodb.streamHandler, AwsLambda.Sns.handler, etc. This way you are sure to expose a function which respect the signature expected by Lambda and you get types information for all the parameters this function will receive.

Example

A full example is documented here. It is using the sources from the example directory.

Simple echo function for the API Gateway. This might seem a little verbose compare to a typescript version. But using this package, you are sure to handle all the possible cases where a value is actually null or base64 encoded. Plus the returned object given to the callback will always have the expected fields and types.

let handler: AwsLambda.APIGatewayProxy.handler =
  (event, _context, cb) => {
    open AwsLambda.APIGatewayProxy;
    let parameter =
      event
      |. Event.queryStringParameters
      |> Js.Option.andThen((. params) => Js.Dict.get(params, "userid"));
    switch (parameter) {
    | Some(userid) => Js.log2("executing lambda for", userid)
    | None => Js.log("executing lambda for anonymous user")
    };
    let result =
      switch (event |. Event.body) {
      | None =>
        Js.log("error: no body available in the request");
        result(
          ~body=`Plain({|{"status": "no body available in the request"}|}),
          ~statusCode=400,
          (),
        );
      | Some(body) =>
        Result.make(~statusCode=200, ~body, ~isBase64Encoded=event |. Event.isBase64Encoded, ())
      };
    cb(Js.null, result);
    Js.Promise.resolve();
  };

The typescript equivalent of the type annotation is actually more verbose:

export const handle: Lambda.APIGatewayProxyHandler = async (
    event: Lambda.APIGatewayEvent,
    context: Lambda.Context,
    cb: Lambda.APIGatewayProxyCallback) => {
}

bs-aws-lambda's People

Contributors

catlion avatar johnhaley81 avatar khady avatar thomasthiebaud avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.