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

aws-lambda-request

v1.0.13

Published

This package allows to create lambda requests quickly as well as allow for the creation of openapi file

Downloads

1

Readme

AWS Lambda Request Package

The AWS Lambda Request package is a powerful tool designed to streamline the creation, validation, and handling of RESTful API requests within an AWS Lambda function. By including classes for request and response validation and encapsulating logic, it offers a reusable and testable framework for RESTful API development in AWS lambda. Moreover, it allows your code to automatically create the openapi spec for your application. Below you will find the installation instructions, features, usage examples, and additional support information.

Source code

You can access the source code via Github

Installation

You can install the aws-lambda-request package via npm with the following command:

npm install aws-lambda-request

Features

  • Encapsulated Logic: The package encapsulates all logic needed for processing and responding to RESTful API requests, providing a cleaner codebase.
  • Reusable and Testable: It offers a well-structured interface, enabling clean, reusable, and testable code.
  • Request and Response Validation: With the Schema class, you can define and validate incoming requests and outgoing responses.
  • Method Type Checking: The package handles HTTP method type checking, error handling, and default response headers, including CORS.
  • OpenAPI Schema Generation: Allows for OpenAPI 3.0 compliant JSON schema generation, representing the REST request.

Usage

Defining Schemas with the Schema Class

The Schema class is used to define validation schemas for both request and response data. Schemas can be constructed with various field types.

import { Schema, ValueField, FieldType } from 'aws-lambda-request';

const userSchema = new Schema({
  name: new ValueField(FieldType.String, true),
  age: new ValueField(FieldType.String, true),
});

Handling Requests with the RestRequest Class

The RestRequest class is the core class used to process HTTP requests and responses, providing flexibility and structure to your API.

import { RestRequest, Schema } from 'aws-lambda-request';

const requestSchema = new Schema({...});
const responseSchema = new Schema({...});

const restRequest = new RestRequest({
    namespace: "The Credit API",
    urlPath: "/some_url",
    methods: ['POST', 'GET'],
    schema: { request: requestSchema, response: responseSchema },
    handler: async (data) => { return processedData; }
});

const response = await restRequest.handle(requestInput);

Generating OpenAPI Compliant Schemas

You can also generate an OpenAPI compliant JSON schema using the jsonSchema method:

const openApiSchema = restRequest.jsonSchema();

Example

An example use-case might be validating user creation in an API:

import {
  RestRequest,
  Schema,
  ValueField,
  FieldType,
  DictField,
} from 'aws-lambda-request';

const requestSchema = new Schema({
  username: new ValueField(FieldType.String, true), // required
  password: new ValueField(FieldType.String, true), // reqired
  user_config: new DictField(
    {
      first_name: new ValueField(FieldType.String, false), // optional
      last_name: new ValueField(FieldType.String, false), // optional
    },
    false,
  ),
});

const responseSchema = new Schema({
  userId: new ValueField(
    FieldType.String,
    true,
    'The user id of the created user',
  ),
});

const createUserRequest = new RestRequest({
  namespace: 'User API',
  urlPath: '/create_user',
  methods: ['POST'],
  schema: { request: requestSchema, response: responseSchema },
  handler: async (data) => {
    // Logic to create user
    return { userId: 'some_user_id' };
  },
});

export async function handler(event: any) {
  return await createUserRequest.handle({
    method: 'POST',
    body: event.body || {},
    headers: event.headers || {},
  });
}

Request handler process

graph TD
    A[Start: handleRequest]
    B[Validate HTTP method]
    C[Validate Request Body against Schema]
    D[Call Handler Function]
    E[Validate Response against Schema]
    F[Construct Response]
    G[End]
    H[Method Not Allowed Error]
    I[Invalid Request Body Error]
    J[Handler Function Error]
    K[Invalid Response Error]

    A --> B
    B -->|Valid| C
    B -->|Invalid| H --> G
    C -->|Valid| D
    C -->|Invalid| I --> G
    D -->|Success| E
    D -->|Error| J --> G
    E -->|Valid| F
    E -->|Invalid| K --> G
    F --> G

Support

For issues, feature requests, or general inquiries, please open an issue on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.