Skip to content

Commit

Permalink
feat: route wrappers (#1)
Browse files Browse the repository at this point in the history
* feat: route wrappers

* chore: updated package name
  • Loading branch information
DecathectZero authored Dec 12, 2019
1 parent ae4d179 commit 1dd2175
Show file tree
Hide file tree
Showing 18 changed files with 14,423 additions and 0 deletions.
55 changes: 55 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
jobs:
release:
docker:
- image: node:11
steps:
- checkout
- run: npm i -g semantic-release@15
- run: semantic-release
test:
docker:
- image: node:11
working_directory: ~/repo
steps:
- checkout
- run:
name: Add token
command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc

# Download and cache dependencies
- restore_cache:
keys:
- v2-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v2-dependencies-

- run: npm i
- save_cache:
paths:
- node_modules
key: v2-dependencies-{{ checksum "package.json" }}

# run tests!
- run:
name: testing
command: npm run test

- run:
name: Coverage
command: npm run send-coverage

# run lint
- run:
name: lint
command: npm run lint
workflows:
version: 2
test-and-build:
jobs:
- test
- release:
requires:
- test
filters:
branches:
only: master
30 changes: 30 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[vcbuild.bat]
end_of_line = crlf

[Makefile]
indent_size = 8
indent_style = tab

[{deps}/**]
charset = ignore
end_of_line = ignore
indent_size = ignore
indent_style = ignore
trim_trailing_whitespace = ignore

[{test/fixtures,deps,tools/node_modules,tools/gyp,tools/icu,tools/msvs}/**]
insert_final_newline = false

[*.js]
trim_trailing_whitespace = false
max_line_length = 150
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/coverage
/.nyc_output
/.idea
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
extends: '@voiceflow/eslint-config',
rules: {
'no-continue': 'off',
'quotes': ['error', 'single', 'avoid-escape'],
'sonarjs/cognitive-complexity': 'warn',
'promise/always-return': 'off',
}
};
61 changes: 61 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# next.js build output
.next
12 changes: 12 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
coverage
.idea
.nyc_output
scratch.js
/scripts
/.ebextensions/
/app/
/log/
/routes/multimodal/apl_authoring_tool
/database
/node_modules

3 changes: 3 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = { extends: ['@commitlint/config-conventional'] };
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

/* eslint global-require: "off" */
module.exports = require('./lib');
77 changes: 77 additions & 0 deletions lib/exceptionHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

const _ = require('lodash');
const VError = require('@voiceflow/verror');

const log = require('../logger');

/**
* @class
*/
class ExceptionHandler {
/**
* @param {ResponseBuilder} responseBuilder
*/
constructor(responseBuilder) {
if (!_.isObject(responseBuilder)) {
throw new VError('responseBuilder must be an object');
}
this.responseBuilder = responseBuilder;
}

/**
* Express middleware for json body parser errors.
* From here: {@link https://github.com/expressjs/body-parser/issues/122#issuecomment-328190379}
* @param {Error|VError|object} err unhandled error
* @param {Request} req express request
* @param {Response} res express response
* @param {Function} next
* @return {Promise<void>}
*/
async handleJsonParseError(err, req, res, next) {
if (err.type && err.type === 'entity.parse.failed') {
await this.responseBuilder.route(new VError('Could not parse JSON input', VError.HTTP_STATUS.BAD_REQUEST, err.body))(req, res);
return null;
}

next(err);
return null;
}

/**
* Express middleware for catching unhandled errors and returning a 500
* @param {Error|VError|object} err unhandled error
* @param {Request} req express request
* @param {Response} res express response
* @param {Function} next
* @return {Promise<void>}
*/
async handleError(err, req, res, next) {
// If the error object doesn't exist
if (!err) return next();

if (err.stack) {
log.error(`Unhandled error: ${err.stack}`);
} else {
log.error(`Unhandled error without stack: ${JSON.stringify(err)}`);
}

await this.responseBuilder.route(new VError('Unhandled error occurred'))(req, res);
return null;
}

/**
* Express middleware for catching all unhandled requests and returning 404
* @param {Request} req express request
* @param {Response} res express response
* @return {Promise<void>}
*/
async handleNotFound(req, res) {
const url = req.originalUrl;
const { method } = req;

await this.responseBuilder.route(new VError(`URL: ${url} with method: ${method} is not a valid path`, VError.HTTP_STATUS.NOT_FOUND))(req, res);
}
}

module.exports = ExceptionHandler;
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

/* eslint global-require: "off" */
module.exports = {
ExceptionHandler: require('./exceptionHandler'),
ResponseBuilder: require('./responseBuilder'),
};
Loading

0 comments on commit 1dd2175

Please sign in to comment.