Skip to content

zigante/serverless-api-manager

Repository files navigation

Serverless Api Manager

Optimized web framework for serverless environment

NPM Version NPM Downloads


To prevent the cold-start of serverless functions from getting too long, a good alternative is not to use Express as it simulates the creation of an entire server and configures all routes and all middlewares of each of the routes, with only 1 being used.


Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 0.10 or higher is required.

If this is a brand new project, make sure to create a package.json first with the npm init command. Installation is done using the npm install command:

$ npm install --save serverless-api-manager

Usage

// index.ts

import type { APIGatewayEvent } from 'aws-lambda';
import AnyLoggerLibrary from 'any-logger-library';
import { ServerlessApiManager, Services } from 'serverless-api-manager';
import { MyAction } from './actions/my-action';
import { IContext } from './interfaces/IContext';

export const handler = async (event: APIGatewayEvent) => {
  const manager = new ServerlessApiManager<APIGatewayEvent, IContext>()
    .withEvent(event)
    .withService(Services.API_GATEWAY)
    .withContextId('13245-12345-13245-12345')
    .withLogger(new AnyLoggerLibrary())
    .withContext({ appName: 'my-example' })
    .withAction(new MyAction());

  return manager.run();
};
// interfaces/IContext.ts

export interface IContext {
  appName: string;
}
// actions/my-action.ts

import { IContext } from '../interfaces/IContext';

export class MyAction implements IAction<IContext> {
  execute: ExecutorHandler<IContext> = (request, response) => {
    const { body, context, headers, params, query } = request;
    context.logger.debug(`Initializing my action: ${context.appName}`);

    // ALL YOUR CODE GOES HERE

    context.logger.debug('Sending my response');
    return response
      .headers({ ...headers, 'content-type': 'application/json' })
      .status(200)
      .json({ foo: 'bar', body, params, query });
  };
}

About

Optmized web framework for serverless environment

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published