Skip to content

Commit

Permalink
Merge branch 'release/v0.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
rudijs committed Aug 24, 2014
2 parents 2b244ae + 6485cfb commit a7df612
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

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

# Coverage directory used by tools like istanbul
coverage

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

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

# Dependency directory
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

.idea/
44 changes: 44 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"node": true,
"browser": true,
"esnext": true,

"indent": 2,
"laxcomma": true,

"strict": true,
"eqeqeq": true,
"immed": true,
"latedef": true,
"newcap": true,
"curly": true,
"noarg": true,
"plusplus": true,
"quotmark": "single",
"undef": true,
"unused": true,
"trailing": true,
"expr": true,

"globals": {
"angular": false,
"_": true,
"$": true,
"describe": false,
"inject": false,
"it": false,
"beforeEach": false,
"afterEach": false,
"expect": false,
"db": false,
"element": false,
"by": false,
"browser": true,
"spyOn": true,
"jasmine": true,
"google": true,
"sinon": true

}

}
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.git/
.gitignore
.idea/
.jshintrc
node_modules/
test/
log/
examples/
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: node_js
node_js:
- '0.11'
env:
- NODE_ENV=test
before_script:
- npm install
script:
- npm run coveralls
- ./node_modules/jshint/bin/jshint lib/*.js
- ./node_modules/jshint/bin/jshint test/*.js
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog
All notable changes to this project will be documented in this file.

## 0.0.1 - 2014-08-25

### Added
- Initial Commit

### Deprecated
- Nothing.

### Removed
- Nothing.

### Fixed
- Nothing.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 Rudi Starcevic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
koa-jsonapi-headers
===================

KoaJS Validate JSON-API Request Headers Middleware

[![Build Status](https://travis-ci.org/rudijs/koa-jsonapi-headers.svg?branch=master)](https://travis-ci.org/rudijs/koa-jsonapi-headers)
[![Coverage Status](https://coveralls.io/repos/rudijs/koa-jsonapi-headers/badge.png?branch=master)](https://coveralls.io/r/rudijs/koa-jsonapi-headers?branch=master)
[![NPM version](https://badge.fury.io/js/koa-jsonapi-headers.svg)](http://badge.fury.io/js/koa-jsonapi-headers)

## Overview

KoaJS middleware to validate required Request headers for [JSON API](http://jsonapi.org/format/) spec.

Content-type: application/vnd.api+json

Accept: application/vnd.api+json

Validation failure will return HTTP `400 Bad Request`

Code review, suggestions and pull requests very much welcome - thanks!

## Install

`npm install koa-jsonapi-headers`

## Usage

This middleware will throw a nested object in the application error like so:

this.throw(400, {
message: {
status: 400,
title: 'Bad Request',
detail: 'API requires header "Content-type application/vnd.api+json" for exchanging data.'
}
});

It's designed this way so that the application logging will log the entire JSON response and then rethrow the JSON error message.

Therefore you need to use some application logging like [koa-json-logger](https://github.com/rudijs/koa-json-logger) or catch and rethrow the error yourself.

Here's an example using koa-json-logger:

var koaJsonLogger = require('koa-json-logger');
var koaJsonApiHeaders = require('koa-jsonapi-headers')

app.use(koaJsonLogger({jsonapi: true}));
app.use(koaJsonApiHeaders());

Here's an example of manual catch and re-throw:

var koaJsonApiHeaders = require('koa-jsonapi-headers')

app.use(function *catchJsonApiErrors(next) {
try {
yield next;
}
catch (err) {

// Response properties
this.status = err.status || 500;
this.body = err.message;
}
this.response.type = 'application/vnd.api+json';
});

app.use(koaJsonApiHeaders());

## Tests with code coverage report in `test/coverage`

Note: Requires nodes at least v0.11.13 (earlier v0.11 versions may work, have not checked for this).

git clone the full repo: `git clone git@github.com:rudijs/koa-jsonapi-headers.git`

`cd koa-jsonapi-headers`

`npm install`

`npm test`

## Code Linting

`./node_modules/jshint/bin/jshint lib/*.js`

`./node_modules/jshint/bin/jshint test/*.spec.js`
33 changes: 33 additions & 0 deletions lib/koa-jsonapi-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

module.exports = function koaJsonApiHeaders() {

return function *jsonApiHeaders(next) {

// Content-type: application/vnd.api+json
if (!this.header['content-type'] || !/application\/vnd\.api\+json/.test(this.header['content-type'])) {
this.throw(400, {
message: {
status: 400,
title: 'Bad Request',
detail: 'API requires header "Content-type application/vnd.api+json" for exchanging data.'
}
});
}

// Accept: application/vnd.api+json
if (!this.header.accept || !/application\/vnd\.api\+json/.test(this.header.accept)) {
this.throw(400, {
message: {
status: 400,
title: 'Bad Request',
detail: 'API requires header "Accept application/vnd.api+json" for exchanging data.'
}
});
}

yield next;

};

};
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "koa-jsonapi-headers",
"version": "0.0.1",
"description": "KoaJS Validate JSON-API Request Headers Middleware",
"repository": {
"type": "git",
"url": "https://github.com/rudijs/koa-jsonapi-headers.git"
},
"main": "./lib/koa-jsonapi-headers.js",
"scripts": {
"test": "NODE_ENV=test node --harmony node_modules/istanbul-harmony/lib/cli.js cover node_modules/mocha/bin/_mocha --root lib/ --dir test/coverage -- -u exports -R spec",
"coveralls": "NODE_ENV=test node --harmony node_modules/istanbul-harmony/lib/cli.js cover node_modules/mocha/bin/_mocha --root lib/ --dir test/coverage -- -u exports -R spec && cat ./test/coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"keywords": [
"koa",
"koajs",
"jsonapi"
],
"author": {
"name": "Rudi Starcevic",
"email": "ooly.me@gmail.com"
},
"license": "MIT",
"engines": {
"node": ">=0.11.13"
},
"devDependencies": {
"chai": "^1.9.1",
"coveralls": "^2.11.1",
"istanbul-harmony": "^0.3.0",
"jshint": "^2.5.4",
"koa": "^0.10.0",
"mocha": "^1.21.4",
"supertest": "^0.13.0"
}
}

0 comments on commit a7df612

Please sign in to comment.