Git Product home page Git Product logo

endpoints's Introduction

Summary

Simple REST endpoint and action routing.

  • .body to rawBody and .body should be castable
  • object to wrap around head, querystring, path parameters and authorizer that provide 'getAs*' functions on values.

Setup

Your handler or entrypoint file and function should look something similar to what is outlined below. First, any customizations of the EndpointRouter. Then, you create an instance of the classes containing your @endpoint mappings, and their dependencies, as appropriate. Lastly, you expose EndpointRouter.handle as the entrypoint so it can take ownership over properly routing your requests.

We recommend you use a .rest postfix on your export in case you want to handle different event trigger types for this lambda function. For example, if you wanted to also listen to an SNS topic you would create another exported fucntion module.export.sns and wouldnt have to worry about remapping module.export.rest.

import { EndpointRouter } from '@mu-ts/endpoints';

/**
 * To modify how the event will be serialized, you can specify your own
 * implementation of the `EventSerailizer`.
 */
EndpointRouter.serializer = new CustomSerializer();

/**
 * Define the default headers for each response.
 */
EndpointRouter.setDefaultHeaders({
  'Server': 'Super awesome',
  'X-Powered-By': 'Super awesome',
  'Set-Cookie': 'Secure',
  'X-Frame-Options': 'DENY',
  'Content-Type': 'application/json',
  'Cache-Control': 'max-age=86400'
});

const usersService: UsersService = new UsersService();

/**
 * Once an instance is created it registers its endpoints with
 * the EndpointRouter. Only do it once.
 */
new GetUsersEndpoint(usersService);

module.export.rest = EndpointRouter.handle;

Request Routing

For each function that will be handling a specific 'route' you will need to use the @endpoint() decorator. At minimum you must provide the resource and action. The resource is the non unique url pattern, such as /users/{user-id} not the unique value which might be /users/1234. The action should is the HTTPAction or string action (upper case).

Conditions allow you to specify limitations for when a specific piece of logic will be invoked. It takes a function with the signature (body: HTTPBody | undefined, event: HTTPEvent): boolean;. This means you can do quick logic checks on the body or event to determine if the 'current' state eliminates or includes this endpoint from being invoked.

Priority allows you to determine the order of execution for your endpoints. A higher value grants it higher priority in the sort order, placing it first in the list for execution.

If multiple endpoints qualify, the first endpoint that returns a value will terminate the execution for other endpoints.

Examples:

import { endpoint, endpoints, EndpointResponse, EndpointEvent } from '@authvia/endpoints';

@endpoints('/v3')
public class GetUsersEndpoint {

  constructor(usersService: UsersService){
    this.usersService = usersService;
  }

  @endpoint('PATCH','/users/{user-id}')
  updateUser(event: EndpointEvent, context: Context):Promise<HTTPResponse> {
    return HTTPResponse.body('the-body');
  }

  /**
   * Only call this function for the /users/{user-id} POST operation if the body
   * contains an attribute action with the value 'promote'.
   *
   * This could be more complex, and
   */
  @endpoint(HTTPAction.POST, '/users/{user-id}', (body:HTTPBody) => body['action'] === 'promote' || false, 50)
  updateUser(event: EndpointEvent, context: Context):Promise<HTTPResponse> {
    return HTTPResponse
      .body('the-body');
  }

  /**
   * This function will be the 'default'. So if the body does not contain an attribute
   * called action, with the value 'promote' then this function will be exeecuted.
   */
  @endpoint(HTTPAction.POST,'/users/{user-id}')
  updateUser(event: EndpointEvent, context: Context):Promise<HTTPResponse> {
    return HTTPResponse.body('the-body');
  }
}

CORS

For any endpoint you can modify the CORS values that will be returned for a specific request.

Note: This must be done in concert with the cors:true within the serverless.yaml file that enables the OPTIONS response in API Gateways.

Example:


import { Response } from './endpoints';
import Response as EndpointResponse from './.endpoints';

@endpoint('GET','/users')
@cors(allowedOrigin,allowedActions,allowedHeaders,allowCredentials)
handle(event: EndpointEvent, context: Context):Promise<HTTPResponse> {

  if(hasError){
   return HTTPResponse
     .status(400)
     .body({message: 'I\'m verbose!'},Error);
  }

  if(updated) {
    return HTTPResponse
      .body(user,User)
      .addHeader('eTag',user.eTag);
  }

  return HTTPResponse
    .status(201)
    .body(user,User)
    .addHeader('eTag',user.eTag);

}

Redaction

You can mark attributes to be removed from the response when they are being serialized via the @redacted decorator.

public class User {
  private id: string;
  private name: string;
  @redacted
  private role: string; //Will not be returned when serialized.
  private createdBy: string;
}

endpoints's People

Contributors

strandedmusician avatar matt-filion avatar jeff812 avatar phil-authvia avatar dependabot[bot] avatar

Watchers

James Cloos 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.