@teleology/lambda-api

    0.1.6 • Public • Published

    Downloads Version License

    @teleology/lambda-api

    Common utilities for wrapping aws lambda api gateway endpoints

    Installation

    yarn add @teleology/lambda-api
    # or
    npm i --save @teleology/lambda-api

    Documentation

    Wrapper

    The wrapper function is a HOC, and will extract data from the incoming api gateway event. QueryStringParameters, pathParameters, and body data will automatically be collected and exposed in the data object. Header keys will be lowercased.

    import { wrapper } from '@teleology/lambda-api';
    
    const handler = async ({ headers, data }) => {
      // Some implementation
    };
    
    export default wrapper(handler);

    ApiError

    ApiError is inherited from the base Error instance, with some extra features. It defaults with an empty message and a 500 code to reflect a respone error. Additional properties can be added to the error upon creation.

    import { ApiError, wrapper } from '@teleology/lambda-api';
    
    const handler = async ({ headers, data }) => {
      if (!data.username) {
        throw new ApiError('Bad Request', {
          code: 400,
    
          // additional properties
          description: 'Missing username property',
        })
      }
    
      // ...
    };
    
    export default wrapper(handler);

    request

    Consumes an aws api gateway event, enhancing and parsing inputs.

    * Body data will be parsed in the following order:

    1. Buffer
    2. JSON.parse
    3. querystring.parse
    import { request } from '@teleology/lambda-api';
    
    const sampleEvent = {
      headers: {
        'Content-Type': 'application/json',
        'Fake-Header': 'test',
      },
      pathParameters: {
        username: 'foo',
      },
      body: '',
    };
    
    console.log(request(sampleEvent));

    Output:

    {
      headers: { 'content-type': 'application/json', 'fake-header': 'test' },
      data: { username: 'foo' }
    }

    response

    Sanitizes response data into a format api gateway understands.

    import { response } from '@teleology/lambda-api';
    
    const sampleResponse = {
      headers: {
        'Content-Type': 'application/json',
        'Fake-Header': 'test',
      },
      body: {
        foo: 'bar',
      },
    };
    
    console.log(response(sampleResponse));

    Output:

    {
      statusCode: '204',
      body: '{"foo":"bar"}',
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Credentials': true,
        'Content-Type': 'application/json',
        'Fake-Header': 'test'
      }
    }

    Changelog

    0.1.5

    • Exposing elevating auth object to root event

    0.1.2

    • Adding error serialization for clearer reading in wrapper

    0.1.1

    • Allow normal body and header response items, but default to entire response being the body

    0.1.0

    • Fixed body parsing error where strings were being spread operated
    • Added documentation
    • Added full test coverage

    0.0.1

    • Initial upload

    Install

    npm i @teleology/lambda-api

    DownloadsWeekly Downloads

    67

    Version

    0.1.6

    License

    MIT

    Unpacked Size

    10.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • icarus-sullivan