-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rui Marinho
committed
Jul 24, 2015
0 parents
commit 438d4e0
Showing
9 changed files
with
613 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
{ | ||
"ecmaFeatures": { | ||
"modules": true | ||
}, | ||
"env": { | ||
"es6": true, | ||
"mocha": true, | ||
"node": true | ||
}, | ||
"parser": "babel-eslint", | ||
"plugins": [ | ||
"babel" | ||
], | ||
"rules": { | ||
"accessor-pairs": [2, true], | ||
"array-bracket-spacing": 2, | ||
"babel/block-scoped-var": 2, | ||
"babel/generator-star-spacing": 2, | ||
"babel/new-cap": 2, | ||
"babel/object-shorthand": [2, "always"], | ||
"brace-style": 2, | ||
"camelcase": 2, | ||
"comma-dangle": [2, "never"], | ||
"comma-spacing": 2, | ||
"comma-style": 2, | ||
"computed-property-spacing": 2, | ||
"consistent-return": 2, | ||
"consistent-this": 2, | ||
"constructor-super": 2, | ||
"curly": [2, "all"], | ||
"default-case": 2, | ||
"dot-location": [2, "property"], | ||
"dot-notation": [2, {"allowKeywords": true, "allowPattern": ""}], | ||
"eol-last": 2, | ||
"eqeqeq": [2, "smart"], | ||
"func-names": 0, | ||
"func-style": 2, | ||
"guard-for-in": 2, | ||
"indent": [2, 2], | ||
"key-spacing": 2, | ||
"linebreak-style": [2, "unix"], | ||
"lines-around-comment": 2, | ||
"max-nested-callbacks": 0, | ||
"new-parens": 2, | ||
"newline-after-var": 2, | ||
"no-alert": 2, | ||
"no-caller": 2, | ||
"no-catch-shadow": 2, | ||
"no-cond-assign": [2, "always"], | ||
"no-console": 2, | ||
"no-constant-condition": 2, | ||
"no-control-regex": 2, | ||
"no-debugger": 2, | ||
"no-delete-var": 2, | ||
"no-div-regex": 2, | ||
"no-dupe-args": 2, | ||
"no-dupe-keys": 2, | ||
"no-duplicate-case": 2, | ||
"no-else-return": 2, | ||
"no-empty": 2, | ||
"no-empty-character-class": 2, | ||
"no-empty-label": 2, | ||
"no-eq-null": 2, | ||
"no-eval": 2, | ||
"no-ex-assign": 2, | ||
"no-extend-native": 2, | ||
"no-extra-bind": 2, | ||
"no-extra-boolean-cast": 2, | ||
"no-extra-parens": 2, | ||
"no-extra-semi": 2, | ||
"no-fallthrough": 2, | ||
"no-floating-decimal": 2, | ||
"no-func-assign": 2, | ||
"no-implied-eval": 2, | ||
"no-inline-comments": 2, | ||
"no-inner-declarations": 2, | ||
"no-invalid-regexp": 2, | ||
"no-irregular-whitespace": 2, | ||
"no-iterator": 2, | ||
"no-label-var": 2, | ||
"no-labels": 2, | ||
"no-lone-blocks": 2, | ||
"no-lonely-if": 2, | ||
"no-loop-func": 0, | ||
"no-mixed-spaces-and-tabs": 2, | ||
"no-multi-spaces": 2, | ||
"no-multi-str": 2, | ||
"no-multiple-empty-lines": 2, | ||
"no-native-reassign": 2, | ||
"no-negated-in-lhs": 2, | ||
"no-nested-ternary": 2, | ||
"no-new": 2, | ||
"no-new-func": 2, | ||
"no-new-object": 2, | ||
"no-new-require": 2, | ||
"no-new-wrappers": 2, | ||
"no-obj-calls": 2, | ||
"no-octal": 2, | ||
"no-octal-escape": 2, | ||
"no-path-concat": 2, | ||
"no-process-env": 2, | ||
"no-process-exit": 2, | ||
"no-proto": 2, | ||
"no-redeclare": 2, | ||
"no-regex-spaces": 2, | ||
"no-reserved-keys": 2, | ||
"no-restricted-modules": 2, | ||
"no-return-assign": 2, | ||
"no-script-url": 2, | ||
"no-self-compare": 2, | ||
"no-sequences": 2, | ||
"no-shadow": 2, | ||
"no-shadow-restricted-names": 2, | ||
"no-spaced-func": 2, | ||
"no-sparse-arrays": 2, | ||
"no-sync": 2, | ||
"no-ternary": 2, | ||
"no-this-before-super": 2, | ||
"no-throw-literal": 2, | ||
"no-trailing-spaces": 2, | ||
"no-undef": 2, | ||
"no-undef-init": 2, | ||
"no-undefined": 2, | ||
"no-underscore-dangle": 2, | ||
"no-unexpected-multiline": 2, | ||
"no-unneeded-ternary": 2, | ||
"no-unreachable": 2, | ||
"no-unused-expressions": 2, | ||
"no-unused-vars": 2, | ||
"no-use-before-define": 2, | ||
"no-var": 2, | ||
"no-void": 2, | ||
"no-warning-comments": 2, | ||
"no-with": 2, | ||
"object-curly-spacing": [2, "always"], | ||
"one-var": 2, | ||
"operator-assignment": 2, | ||
"operator-linebreak": 2, | ||
"padded-blocks": [2, "never"], | ||
"prefer-const": 2, | ||
"quote-props": [2, "as-needed"], | ||
"quotes": [2, "single"], | ||
"radix": 2, | ||
"semi": [2, "always"], | ||
"semi-spacing": 2, | ||
"sort-vars": 2, | ||
"space-after-keywords": 2, | ||
"space-before-blocks": 2, | ||
"space-before-function-paren": [2, "never"], | ||
"space-in-parens": 2, | ||
"space-infix-ops": 2, | ||
"space-return-throw-case": 2, | ||
"space-unary-ops": 2, | ||
"spaced-comment": [2, "always"], | ||
"strict": [2, "global"], | ||
"use-isnan": 2, | ||
"valid-jsdoc": 2, | ||
"valid-typeof": 2, | ||
"vars-on-top": 2, | ||
"wrap-iife": 2, | ||
"wrap-regex": 2, | ||
"yoda": [2, "never"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
coverage | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"esnext": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- iojs | ||
- "0.12" | ||
|
||
after_success: | ||
- npm run coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# koa-requestid | ||
|
||
## Status | ||
|
||
[![npm version][npm-image]][npm-url] | ||
[![build status][travis-image]][travis-url] | ||
|
||
## Installation | ||
|
||
Install the package via `npm`: | ||
|
||
```bash | ||
$ npm install koa-requestid --save | ||
``` | ||
|
||
## Usage | ||
|
||
Use `koa-requestid` as a middleware for a `koa` app. By default, it generates a unique uuid (v4) and exposes it on the response via the `Request-Id` header. The id is also saved as part of the request *state*, which is usually acessible in the `context.state` object on recent versions of `koa`. | ||
On older versions, it is directly set on `context.id`. | ||
|
||
In the following example, the generated uuid is manually exposed on the body for debugging purposes: | ||
|
||
```js | ||
var koa = require('koa'); | ||
var requestId = require('koa-requestid'); | ||
var app = koa(); | ||
|
||
app.use(requestId()); | ||
app.use(function *() { | ||
this.body = this.state.id; | ||
}); | ||
|
||
app.listen(3000); | ||
``` | ||
|
||
Execute a request to the running app: | ||
|
||
```bash | ||
❯ curl -v http://localhost:3000 | ||
|
||
< HTTP/1.1 200 OK | ||
< Request-Id: cc0f12c7-f3b6-4c86-94c2-8c4ce7751651 | ||
|
||
cc0f12c7-f3b6-4c86-94c2-8c4ce7751651 | ||
``` | ||
|
||
Sometimes it is also useful to pass a custom id via a request header or query string, specifically in debugging sessions. Please note that the input id is not sanitized, so the usual precautions apply. | ||
|
||
Using the above snippet to send a custom via the default `Request-Id` header: | ||
|
||
```bash | ||
❯ curl -v -H 'Request-Id: foobar' http://localhost:3000 | ||
|
||
< HTTP/1.1 200 OK | ||
< Request-Id: foobar | ||
|
||
foobar | ||
``` | ||
|
||
or using a query string parameter (default is `requestId`): | ||
|
||
```bash | ||
❯ curl -v http://localhost:3000?requestId=foobar | ||
|
||
< HTTP/1.1 200 OK | ||
< Request-Id: foobar | ||
|
||
foobar | ||
``` | ||
|
||
## Configuration | ||
|
||
#### Arguments | ||
1. `options` *(object)*: A dictionary of options. | ||
|
||
#### Options | ||
1. `expose` *(boolean|string)*: The name of the header to expose the id on the response, or `false` to disable. | ||
2. `header` *(boolean|string)*: The name of the header to read the id on the request, or `false` to disable. | ||
3. `query` *(boolean|string)*: The name of the header to read the id on the query string, or `false` to disable. | ||
|
||
Example: | ||
|
||
```js | ||
var koa = require('koa'); | ||
var requestId = require('koa-requestid'); | ||
var app = koa(); | ||
|
||
app.use(requestId({ | ||
expose: 'X-Request-Id', | ||
header: 'X-Req-Id', | ||
query: 'request-id' | ||
})); | ||
``` | ||
|
||
This configuration would expose every generated request id via the `X-Request-Id` response header and accept a custom id via the `X-Req-Id` header or `request-id` query string parameter. | ||
|
||
## Tests | ||
|
||
``` | ||
$ npm test | ||
``` | ||
|
||
## License | ||
|
||
MIT | ||
|
||
[npm-image]: https://img.shields.io/npm/v/koa-requestid.svg | ||
[npm-url]: https://www.npmjs.com/package/koa-requestid | ||
[travis-image]: https://travis-ci.org/seegno/koa-requestid.svg | ||
[travis-url]: https://travis-ci.org/seegno/koa-requestid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
exports.__esModule = true; | ||
|
||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
|
||
var _nodeUuid = require('node-uuid'); | ||
|
||
/** | ||
* Request id middleware. | ||
*/ | ||
|
||
exports['default'] = function (options) { | ||
options = _extends({ | ||
expose: 'Request-Id', | ||
header: 'Request-Id', | ||
query: 'requestId' | ||
}, options); | ||
|
||
var keys = Object.keys(options); | ||
|
||
for (var i = 0; i < keys.length; i++) { | ||
if (typeof options[keys[i]] !== 'boolean' && typeof options[keys[i]] !== 'string') { | ||
throw new Error('Option `' + keys[i] + '` requires a boolean or a string'); | ||
} | ||
} | ||
|
||
return function* requestId(next) { | ||
var id = undefined; | ||
|
||
if (options.query) { | ||
id = this.query[options.query]; | ||
} | ||
|
||
if (!id && options.header) { | ||
id = this.get(options.header); | ||
} | ||
|
||
if (!id) { | ||
id = _nodeUuid.v4(); | ||
} | ||
|
||
if (options.expose) { | ||
this.set(options.expose, id); | ||
} | ||
|
||
if (this.state) { | ||
this.state.id = id; | ||
} else { | ||
this.id = id; | ||
} | ||
|
||
yield next; | ||
}; | ||
}; | ||
|
||
module.exports = exports['default']; |
Oops, something went wrong.