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

@mecanizou/serverless-tsed-plugin

v0.2.7

Published

This plugin integrates [Ts.ED](https://tsed.io/) with the Serverless Framework, allowing you to automatically generate Serverless functions from your Ts.ED controllers.

Downloads

238

Readme

TsEDPlugin for Serverless Framework

This plugin integrates Ts.ED with the Serverless Framework, allowing you to automatically generate Serverless functions from your Ts.ED controllers.

Installation

First, ensure you have Node.js installed. Then, you can install the plugin via npm:

npm install tsed-plugin-serverless --save-dev

Configuration

To configure the plugin in your Serverless Framework project, you need to add it to your serverless.yml file:

service: my-service

plugins:
  - tsed-plugin-serverless

custom:
  tsedPlugin:
    patterns:
      module: 
        - "src/modules/**/*module.ts"
      controller: 
        - "src/modules/**/*controller.ts"
    environment:
      STAGE: ${opt:stage, 'dev'}
    memorySize: 128
    events:
      http:
        cors: true
    authorizer:
      functionName: myAuthorizerFunction
      HeaderName: Authorization

provider:
  name: aws
  runtime: nodejs18.x

functions:
  # The functions will be auto-generated by the plugin

Usage

The TsEDPlugin will automatically scan your project for Ts.ED controllers and generate corresponding Serverless functions. Here’s an example of how to structure your Ts.ED controllers:

Example Ts.ED Module

import { PlatformServerless } from "@tsed/platform-serverless";
import { ExampleController } from "./example-controller";

export = PlatformServerless.bootstrap({
	lambda: [ExampleController]
}).callbacks();

Example Ts.ED Controller

import { Controller, Get, Post, BodyParams } from "@tsed/common";
import { Summary, Description, Returns } from "@tsed/schema";

@Controller("/example")
export class ExampleController {
  @Get("/")
  @Summary("Get example")
  @Description("Retrieve an example item")
  @Returns(200, TSEDModel)
  getExample(): string {
    return "example";
  }

  @Post("/")
  @Summary("Create example")
  @Description("Create a new example item")
  @Returns(201, { description: "Item created" })
  createExample(@BodyParams() body: any): string {
    return "created";
  }
}

Running Serverless Offline

To run your Serverless project offline, use the following command:

serverless offline

The plugin will hook into the before:offline:start event to ensure all necessary functions are generated before starting the offline server.

How It Works

The plugin scans for Ts.ED controller files matching the patterns specified in your serverless.yml configuration. It extracts route information and generates Serverless functions based on the HTTP methods and paths defined in your controllers.

Parsing Decorators

The plugin parses decorators from your Ts.ED controllers to generate Serverless function definitions. It supports nested and complex decorators, allowing for advanced routing and documentation setups.

Generating Function Names

Function names are generated based on the HTTP method, controller path, and method path. This ensures unique and descriptive function names for your Serverless functions.

Example Generated Function

Given the ExampleController above, the following function might be generated:

functions:
  get-example:
    handler: src/controllers/examplecontroller.getExample
    memorySize: 128
    environment:
      STAGE: dev
    events:
      - http:
          path: example
          method: get
          cors: true
          documentation:
            summary: "Get example"
            description: "Retrieve an example item"
            methodResponses:
              - statusCode: 200
                responseBody:
                  description: "Successful response"

  create-example:
    handler: src/controllers/examplecontroller.createExample
    memorySize: 128
    environment:
      STAGE: dev
    events:
      - http:
          path: example
          method: post
          cors: true
          documentation:
            summary: "Create example"
            description: "Create a new example item"
            methodResponses:
              - statusCode: 201
                responseBody:
                  description: "Item created"
          request:
            schemas:
              "application/json": 
                $ref: "#/definitions/CreateExample"

Additional Configuration

Environment Variables

You can pass environment variables to your functions using the environment configuration:

custom:
  tsedPlugin:
    environment:
      MY_VAR: my-value

Memory Size

Set the memory size for your functions:

custom:
  tsedPlugin:
    memorySize: 256

CORS

Enable CORS for your functions:

custom:
  tsedPlugin:
    events:
      http:
        cors: true

Authorizer

Configure an authorizer for your functions:

custom:
  tsedPlugin:
    authorizer:
      functionName: myAuthorizerFunction
      HeaderName: Authorization

Example

You can see an example at the following URL: https://github.com/mecanizou-eco/serverless-tsed-plugin/tree/master/example.

Additionally, ensure to rename the env-model.yml file to env.yml