Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added --lambdaVersion params #39

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
25 changes: 5 additions & 20 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [3.0.4] - 2020-05-19
### Changed
- Added `error.message` into logged error
- Added integration test to confirm `application/x-www-form-urlencoded` requests work

## [3.0.3] - 2020-05-19
### Changed
- Incorporated user `mattdelsordo` to support binary response and abbreviate logging

## [3.0.2] - 2020-05-19
## [1.0.1] - 2021-11-17
### Changed
- Updated CircleCI and Greenkeeper badges in README.md

## [3.0.1] - 2020-05-19
## [1.0.0] - 2021-11-17
### Added
- Added `.codacy.yml` for Codacy analysis
- Added `.mocharc.yml`

### Changed
- Migrated to using `nyc` from `istanbul`
- Migrated `husky` pre-push hooks
- Migrated to CircleCI v2 pipelines
- Updated `devDependencies` to latest versions
- Updated `package.json` to new Node.js and NPM version compatibility
- Forked from https://www.npmjs.com/package/claudia-local-api
- Added reuqestUUID forcing
- Added --stage argument
27 changes: 9 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
# claudia-local-api
# ts-claudia-local-api

[![CircleCI](https://circleci.com/gh/suddi/claudia-local-api.svg?style=svg)](https://circleci.com/gh/suddi/claudia-local-api)
[![codecov](https://codecov.io/gh/suddi/claudia-local-api/branch/master/graph/badge.svg)](https://codecov.io/gh/suddi/claudia-local-api)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/4aaafdcb86574c709f856f2e00d3a809)](https://www.codacy.com/app/Suddi/claudia-local-api)
[![npm](https://img.shields.io/npm/v/claudia-local-api.svg)](https://www.npmjs.com/package/claudia-local-api)
[![npm](https://img.shields.io/npm/dt/claudia-local-api.svg)](https://www.npmjs.com/package/claudia-local-api)
[![David](https://img.shields.io/david/suddi/claudia-local-api.svg)](https://david-dm.org/suddi/claudia-local-api)
[![David](https://img.shields.io/david/dev/suddi/claudia-local-api.svg)](https://david-dm.org/suddi/claudia-local-api?type=dev)
[![license](https://img.shields.io/github/license/suddi/claudia-local-api.svg)](https://github.com/suddi/claudia-local-api/blob/master/LICENSE)
Command line utility to launch Express local API for claudia-api-builder. Test drive your lambda functions before deployment (Based on Sudharshan Ravindran claudia-local-api) with the aws lambda function stage parameter. BE AWARE NOT INSTALL WITH claudia-local-api package!
Extends claudia-local-api with suport for AWS requestUUID and stages using --stage {stagename} provided as commandline argument

[![codecov](https://codecov.io/gh/suddi/claudia-local-api/branch/master/graphs/commits.svg)](https://codecov.io/gh/suddi/claudia-local-api)

Command line utility to launch Express local API for claudia-api-builder. Test drive your lambda functions before deployment

````
npm install --save-dev claudia-local-api
npm install --save-dev ts-claudia-local-api
````

To install globally:

````
npm install --global claudia-local-api
npm install --global ts-claudia-local-api
````

## Usage
Expand Down Expand Up @@ -73,24 +64,24 @@ function bootstrap() {
module.exports = bootstrap()
````

You can install `claudia-local-api` and run the command line Express API to test out the lambda function locally:
You can install `ts-claudia-local-api` and run the command line Express API to test out the lambda function locally:

````
claudia-local-api --api-module lib/app.js
claudia-local-api --api-module app.js
````

Or add into your `package.json`:

````json
"server": "claudia-local-api --api-module lib/app.js"
"server": "claudia-local-api --api-module app.js --stage develop"
````

This will start up a local Express server on port 3000 to proxy requests to your [`claudia-api-builder`](https://www.npmjs.com/package/claudia-api-builder) app.

You can also pipe it into [`bunyan`](https://www.npmjs.com/package/bunyan) to pretty print the log:

````
claudia-local-api --api-module lib/app.js | bunyan --output short
claudia-local-api --api-module app.js --stage develop | bunyan --output short
````

---
Expand Down
4 changes: 2 additions & 2 deletions bin/claudia-local-api
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');

function execute() {
const potentialPaths = [
path.join(process.cwd(), 'node_modules/claudia-local-api/lib'),
path.join(process.cwd(), 'node_modules/claudia-local-api-with-lambda-version/lib'),
path.join(__dirname, '../lib')
];

Expand All @@ -16,7 +16,7 @@ function execute() {
if (!existingPaths.length) {
throw new Error('claudia-local-api not found, is it installed correctly?');
}

const program = require(existingPaths[0]);
program.run();
}
Expand Down
34 changes: 25 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ const program = require('commander');
const path = require('path');
const pathParser = require('path-parser');
const packageJson = require('../package');
const { v4: uuidv4 } = require('uuid');

function getDefaultConfig() {
return {
port: 3000
port: 3000,
lambdaVersion: 'develop'
};
}

Expand Down Expand Up @@ -82,19 +84,29 @@ function getPathParams(req, routes) {
};
}

function getParams(req, routes) {
function getParams(req, routes, lambdaVersion = null) {
const uuid = uuidv4();
const pathParams = getPathParams(req, routes);

return {
let paramsObject = {
requestContext: {
resourcePath: pathParams.resourcePath,
httpMethod: req.method
httpMethod: req.method,
resourceId: uuid
},
headers: req.headers,
queryStringParameters: req.query,
body: req.body,
pathParameters: pathParams.pathParameters
};

if(lambdaVersion){
paramsObject['stageVariables'] = {
lambdaVersion: lambdaVersion
}
}

return paramsObject;
}

// Determine whether to enable binary content handling
Expand Down Expand Up @@ -133,17 +145,16 @@ function makeHandleResponse(logger, res, convertToBinary) {
.status(response.statusCode || 200)
.send(response.body ? Buffer.from(response.body, 'base64') : undefined);
}

return res
.set(response.headers || {})
.status(response.statusCode || 200)
.send(response.body || {});
};
}

function makeHandleRequest(logger, app, routes) {
function makeHandleRequest(logger, app, routes, lambdaVersion) {
return function (req, res) {
const params = getParams(req, routes);
const params = getParams(req, routes, lambdaVersion);
logJson(logger, params);

const convertToBinary = parseContentHandling(params, app);
Expand All @@ -157,7 +168,11 @@ function getRoutes(routesObj) {
const routePaths = Object.keys(routesObj);

return routePaths.map(function (routePath) {
const supportedMethods = Object.keys(routesObj[routePath] || {});
let supportedMethods = Object.keys(routesObj[routePath] || {});
if(supportedMethods == 'ANY'){
supportedMethods = `GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS`;
}

const route = `/${routePath}`;
return {
resourcePath: route,
Expand All @@ -168,7 +183,7 @@ function getRoutes(routesObj) {
}

function bootstrap(server, logger, claudiaApp, routes, options) {
const handleRequest = makeHandleRequest(logger, claudiaApp, routes);
const handleRequest = makeHandleRequest(logger, claudiaApp, routes, options.stage);

server.all('*', handleRequest);
const instance = server.listen(options.port);
Expand All @@ -183,6 +198,7 @@ function runCmd(bootstrapFn) {
.option('-a --api-module <apiModule>', 'Specify claudia api path from project root')
.option('-p --port [port]', `Specify port to use [${config.port}]`, config.port)
.option('--abbrev [body length]', 'Specify the maximum logged length of a response/request body [no abbreviation]', -1)
.option('--stage', 'Specify the AWS lambda stage [develop, latest]', 'develop')
.parse(process.argv);

const apiPath = path.join(process.cwd(), program.apiModule);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "claudia-local-api",
"description": "Command line utility to launch Express local API for claudia-api-builder. Test drive your lambda functions before deployment",
"version": "3.0.5",
"homepage": "https://github.com/suddi/claudia-local-api",
"name": "ts-claudia-local-api",
"description": "Command line utility to launch Express local API for claudia-api-builder. Test drive your lambda functions before deployment (BASED on Sudharshan Ravindran claudia-local-api )",
"version": "1.0.2",
"homepage": "https://github.com/TantosvagoRepos/ts-claudia-local-api",
"author": {
"name": "Sudharshan Ravindran",
"email": "[email protected]",
"url": "https://suddi.io"
"name": "TantosvagoTeam",
"email": "[email protected]",
"url": "https://github.com/TantosvagoRepos"
},
"repository": {
"type": "git",
"url": "https://github.com/suddi/claudia-local-api"
"url": "https://github.com/TantosvagoRepos/ts-claudia-local-api"
},
"bugs": {
"url": "https://github.com/suddi/claudia-local-api/issues"
"url": "https://github.com/TantosvagoRepos/ts-claudia-local-api/issues"
},
"files": [
"bin",
Expand Down
Loading