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

gulp-swag

v0.0.3

Published

A Gulp plugin for developing and deploy APIs on Amazon Web Services

Downloads

2

Readme

gulp-swag Build Status

Gives the ability to develop and test your API locally and deploy it on AWS. It uses API Gateway and Lambda at its core.

Install

$ npm install -D gulp-swag

In your gulpfile.js:

var swag       = require('gulp-swag'),
    db         = swag.db
    lambda     = swag.lambda,
    apigateway = swag.apigateway,
    i          = swag.utils.interpolate

gulp.task('db:migrate', function(){
  return gulp.src('./db/definitions/*.json')
    .pipe(db.migrate({
      config: i('./env/{NODE_ENV}/config.json')
    }));
});

gulp.task('lambda:server', function() {
  gulp.src('./handlers/*')
    .pipe(lambda.server({
      config : i('./env/{NODE_ENV}/config'),
      routes : i('./env/{NODE_ENV}/routes'),
      port   : 5000
    }));
});

gulp.task('lambda:deploy', function(){
  gulp.src('./handlers/*')
    .pipe(lambda.deploy({
      config : i('./env/{NODE_ENV}/config'),
      routes : i('./env/{NODE_ENV}/routes')
  }));
});

gulp.task('apigateway:deploy', function(){
  gulp.src(i('./env/{NODE_ENV}/routes.json'))
    .pipe(apigateway.deploy({
      handlers: './handlers',
      version : i('./env/{NODE_ENV}/version'),
      config  : i('./env/{NODE_ENV}/config')
    }));
});

Sample project file structure using 'gulp-swag'

  db
    definitions
      createTable.json     
  env
    development
      config.json
      routes.json
      version.json
  handlers
    lambdaFN1
      index.js
      intergation
        request
          application_json.vm
      node_modules    
    lambdaFN2
      index.js
      intergation
        request
          application_json.vm
      node_modules

Integration templates

Request template

Use to define the 'event' object passed to the lambda function. More information on mapping template. Find out about Api Gateway Stage Variables.

NB: $stageVariables are defined in the config.json file

{
  "db": {
    "tableNamespace": "$stageVariables.tableNamespace",
    "config": {
      "endpoint"  : "$stageVariables.dynamoDbEndpoint",
      "apiVersion": "$stageVariables.dynamoDbApiVersion",
      "region"    : "$stageVariables.dynamoDbRegion"
    }
  },
  "article": $input.json('$.article')
}

Examples configuration files of routes.json

config.json

This file store AWS settings for the services used (region, api version, db endpoint).

{
  "AWS":{
    "region"    : "us-west-2",
    "DynamoDB"  : {
      "apiVersion" : "2012-08-10",
      "endpoint"   : "http://localhost:8000"
    },
    "Lambda"    : { "apiVersion" : "2015-03-31" },
    "APIGateway": { "apiVersion" : "2015-07-09" }
  },

  "deployment": {
    "api"        : { "name": "MyNewApi" },
    "credentials": "arn:aws:iam::11111111111:role/DeployApiGateway",
    "stage"      : {
      "name"       : "dev",
      "description": "Development stage (Edge)",
      "variables"  : {
        "tableNamespace": "dev_",
        "dynamoDbEndpoint"    : "http://localhost:8000",
        "dynamoDbApiVersion"  : "2012-08-10",
        "dynamoDbRegion"      : "us-west-2"
      }
    }
  }
}

The property deployment.credentials is set to the IAM Role ARN with at least the following permissions:

  • lambda:InvokeFunction
  • iam:PassRole

and needs to have the Trust Relationships set like below:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": "apigateway.amazonaws.com"    
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

routes.json

This file defines the associatons between path and lambda functions.

{
  "/": {
    "methods": {
      "POST": "lambdaFn1",
      "GET" : {
        "enableCORS": false,
        "role": "arn:aws:iam::1111111111:role/APIGatewayLambdaExecRole",
        "name": "lambdaFn2"
      }
    },
    "enableCORS": true,
    "role": "arn:aws:iam::1111111111:role/APIGatewayLambdaExecRole"
  }
}

The role property is set to ARN role defined with sufficent permission depending on what services are used. More information on how to create it here.

version.json

Specifies which lambda function version to use for the deployment.

{
  "Lambda":{
    "lambdaRootPost": "$LATEST",
    "lambdaRootGet": 2
  }
}

NB: At the moment version.json is only used for deployed API. the local server uses the latest version of lambda handlers.