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

sls-api-decorators

v0.6.8

Published

serveless plugin to write lambda functions in typescript making use of IoC technics to reduce coding effort

Downloads

36

Readme

Serverless api decorators

serverless Build Status MIT license Conventional Commits

This project is mean to simplify and make more elegant aws lambda development in typescript and automatically handle the serverless configuration file.

This is still under heavy development.

Features:

  • define your service as a class annotating it to provide configuration
  • define lambdas as methods of a class and annotate them to provide configuration
  • lambdas are automatically wrapped into a promise
  • path and query parameters are automatically injected into the lambda
  • support for parameters validation
  • no need to change serverless.yml

At a glance:

@Endpoint(
  // define configuration here
)
export class MyService {

  @Lambda(
    // define configuration
  )
  public sayHello(event) {
    return { message : 'Hello there'}
  }
}

Look into the example folder for a working example

How to use

Create a serverless project

sls create -t aws-nodejs

  • specify your service name and comment out functions section

TBD. instruction on how to set up for ts lambdas

Install sls-api-decorators

npm i -S sls-api-decorators

Edit you plugins section and add typescript output folder.

plugins:
  # this will dynamically set the functions in serverless.yaml
  - sls-api-decorators

custom:
  # this will be your ts output folder
  artifactsFolder: lib

create api/user/user.service.ts

The following will define a service with base path users that will expose two endpoints:

  • users/
  • users/error

The latter being to demostrate error throwing.

// user.service.ts
import {Service, Endpoint} from "sls-api-decorators";


@Endpoint({
  // name of the service (not in the serverless meaning of service)
  // will be used in the future for di purpose
  name: 'userService',
  // this will rapresent the base path for this service
  // i.e.: http://localhost:8000/users
  path: 'users',
  // Allow xOrigin request [TBD]
  xOrigin: true
})
class UserService {
  constructor() { }

  @Lambda({
    // name to reference this method in the serverless ecosystem
    // i.e.: to be used with invoke command
    name: 'hello',
    // sub-path for this endpoint
    // i.e.: http://localhost:8000/users/
    path: '/',
    // method to which this function should listen
    // i.e.: 'get' or ['get', 'post'] [TBD]
    method: 'get',
    // this is just required from serverless-webpack plugin
    integration: 'lambda'
  })
  public welcome(event) {
    debug('Running welcome');

    return { message: 'Go Serverless Webpack (Typescript) v1.0! Your function executed successfully!', event };

  }

  // demostrate use of path params and arguments injection
  // arguments being injected from event.path
  @Lambda({
    name: 'getById',
    path: '/{id}',
    method: 'get',
    integration: 'lambda'
  })
  public getById(id) {
    debug('Running get by id:', id);

    return {
      id: 'abc',
      name: 'dcavaliere',
      email: '[email protected]'
     };

  }

  @Lambda({
    name: 'error',
    path: 'error',
    method: 'get',
    integration: 'lambda'
  })
  public error(event) {
    debug('throwing an error');
    // throwing an error will reject the lambda cb
    throw new Error('something weird just happened');
  }
}

export { UserService };

create api/app.ts

The following will expose the service to be used by serverless

this should be replaced in future versions by a DI system

// api/app.ts
import { Api } from 'sls-api-decorators/lib/application';
import { UserService } from './user/user.service';
import { User } from './user/user.model';

@Api({
  // used for DI purposes
  name : 'app',
  // need to define factories and servises
  factories: [User],
  services: [UserService]
})
export class App {

  public services: any;
  public factories: any;

  constructor() {}

}

run tsc -w & now you can call sls invoke local -f hello

or deploy with sls deploy

Please note if you use the example you need to provide your own service name.