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

@mu-ts/endpoints

v3.2.0

Published

Simple REST endpoint and action routing.

Downloads

460

Readme

Objective

Create a lightweight solution for routing endpoint requests received by a lambda function to different handlers, written in TypeScript. Targeted to work for AWS http/rest endpoints and own all the normal middleware that might exist, like facilitating validation, serialization and cors.

Start

Your handler for lambda needs to create and map the handling function to the router.

/**
 * It is recommended that you export an instance, and not just the class. This will help your system maintain
 * better control over how the class is constructed, as its not uncommon for contructors to contain dependencies
 * and if you have an IoC solution handling this, lets this framework stay un involved.
 *
 * _This has not yet been tested with a stand alon function outside of a class._
 */
export * from './path/to/class/with/endpoints';

const router: Router = HttpHandler.instance().router();
module.exports.rest = async (event: any, context: LambdaContext) => router.handle(event, context);

Above this, you would export any of the classes (or functions) that are decorated with endpoint decorators.

Endpoints

To map an endpoint, use the appropriate decorator for the action you want. Then add the resource path as the second argument.

These mappings must be done in addition to the definition of the pathing within your API gateway definition, that routes the requests to the Lambda function.

  • @any('/my-path', validation?: ajvValidationSchema, deserialize?: jsonSchema, serialize?: jsonSchema) [beta] This has not gotten a lot of testing, but intended to be a 'catch all' for any action used on this path.
  • @get('/my-path', deserialize?: jsonSchema)
  • @put('/my-path', validation?: ajvValidationSchema, deserialize?: jsonSchema, serialize?: jsonSchema)
  • @post('/my-path', validation?: ajvValidationSchema, deserialize?: jsonSchema, serialize?: jsonSchema)
  • @put('/my-path', validation?: ajvValidationSchema, deserialize?: jsonSchema, serialize?: jsonSchema)
  • @xdelete('/my-path')
  • @options('/my-path')

The validation schema depends on the implementation of your Validator. The above presumes you are using AJV, which also provides the ability to transform an object to an alternative format. Generally this would be useful for the outbound request where you want to ensure that a contract is always respected, regardless of how the underlying object being serialized is changed. For example, adding a properly to the object would not result in the property being returned in a response, if you had a deserialization schema defined that did not include that additional property.

Sugar

These helper methods can help reduce the amount of code you need to write.

  • response(statusCode, objectToSerialize) can be used to return a quick HttpResponse and not having to format a whole object.
  • json('jsonFile.json') to load a JSON file. Useful for externalizing JSON schema files for validation. @post('/some/path', json('myschema.validation.json'))

Object Factory

If your instances need complex instantiation, you can implement the ObjectFactory interface to populate the instances. The default implementation just uses 'new' on the class being decorated.

Decorated functions recieve the registered 'instance' as the basis for execution of the function, function.apply(instance, [request, context]).

This is useful if you are using IoC.

import { ObjectFactory, Constructable } = '@mu-ts/endpoints';

class MyObjectFactory implements ObjectFactory {
  
  public instantiate<T>(constructable: Constructable<T>): T {
    const name: string = constructable.name;
    const instance: T = superSpecialStuff(name);
    return instance
  }
} 

Validation

/** AJV is used by default **/
const router: Router = HttpHandler.instance().validation('ajv').router();

/** Supply your own **/
const myValidator: Validator = {
  validate(request: HttpRequest<object>, schema: object): T[] | undefined { 
    // it does a validation.
  }
  /**
   * Optional, but used to format the response based on the validation errors identified.
   */
  format(errors: T[], request: HttpRequest<object>): HttpResponse { 
    // it does formatting.
  }
}
const router: Router = HttpHandler.instance().validation(myValidator).router();

Serialization

Both application/json and application/x-www-form-urlencoded are supported out of the box.

/** AJV is used by default **/
const xmlSerializer: HttpSerializer = {
  request?(body: string, schema?: object): object { 
    // do a de-serialize
  }
  response?(body: string | Buffer | object, schema?: object): string {
    // do a serialize
  }
}

/**
 * You can add as many serializers to the main handler as you want by chainining more .serializer calls.
 */
const router: Router = HttpHandler.instance().serializer('application/xml', xmlSerializer).router();

Cors

Use the @cors decorator, which is pretty self-explanatory. If you are using http endpoints instead of rest endpoints then cors should be a cloud formation setting and not something your code needs to handle.

Example

handler.ts

import { HttpHandler, LambdaContext, Router } from '@mu-ts/endpoints';

/*
 * A class per endpoint can help keep your code easier to test and manage.
 */
export * from './memories/CreateUser';  // @post
export * from './memories/GetUser';     // @get
export * from './memories/UpdateUser';  // @patch
export * from './memories/DeleteUser';  // @_delete
export * from './memories/ListUsers';   // @get

/**
 * Creating a router instance. Execute the handle event on the router
 * when the endpoint is invoked.
 */
const router: Router = HttpHandler.instance().router();
module.exports.rest = async (event: any, context: LambdaContext) => router.handle(event, context);

endpoint implementations

class CreateUser {

  /**
   * If userPersistence is provided (for unit testing) then a new instance is not
   * created. 
   */
  constructor(private userPersistence: UserPersistence = new UserPersistence()){}

  @post('/v1/users', {
    type: "object",
    properties: {
      phone: { type: "string", pattern: "(^\\+[1-9]\\d{1,14}$)" },
      name: { type: "string" },
    },
    required: ["phone", "name"],
    additionalProperties: false
  })
  public async create({ body }: HttpRequest<Pick<User, 'to' | 'name'>>): Promise<HttpResponse> {
    console.log('create()', { body });
    const user: User = await this.userPersistence.create(body!);
    return response(201, user);
  }
}

export const createUser: CreateUser = new CreateUser();

Common Problems

Don't map invocation directly to router.handle, it loses hold of 'this' and has trouble executing functions. So while it's a bit cleaner, it doesn't work.

module.export.rest = HttpHandler.instance().router().handle;