├── .eslintrc ├── .gitignore ├── .jscsrc ├── .jshintignore ├── .jshintrc ├── .travis.yml ├── CONTRIBUTION.md ├── LICENSE ├── README.md ├── adapt.js ├── adapters.js ├── adapters ├── probing.js ├── statsd-measure.js ├── statsd-report-request-made.js ├── statsd-report-status-code.js ├── statsd.js └── validating.js ├── docs.jsig ├── errors.js ├── index.js ├── make-typed-request.js ├── package.json ├── result.js ├── test ├── index.js ├── integration.js ├── invalid-client-args.js ├── make-typed-request.js ├── result.js ├── typed-request-client.js ├── validate-shape.js └── write-stats.js ├── validate-shape.js └── write-stats.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "node": false, 5 | "amd": false, 6 | "mocha": false, 7 | "jasmine": false 8 | }, 9 | 10 | "globals": { 11 | "__dirname": false, 12 | "__filename": false, 13 | "require": false, 14 | "module": false 15 | }, 16 | 17 | "rules": { 18 | "no-alert": 2, 19 | "no-array-constructor": 2, 20 | "no-bitwise": 0, 21 | "no-caller": 2, 22 | "no-catch-shadow": 0, 23 | "no-comma-dangle": 2, 24 | "no-cond-assign": 2, 25 | "no-console": 1, 26 | "no-constant-condition": 2, 27 | "no-control-regex": 2, 28 | "no-debugger": 2, 29 | "no-delete-var": 2, 30 | "no-div-regex": 0, 31 | "no-dupe-keys": 2, 32 | "no-else-return": 0, 33 | "no-empty": 2, 34 | "no-empty-class": 2, 35 | "no-empty-label": 2, 36 | "no-eq-null": 0, 37 | "no-eval": 2, 38 | "no-ex-assign": 2, 39 | "no-extend-native": 2, 40 | "no-extra-bind": 2, 41 | "no-extra-boolean-cast": 2, 42 | "no-extra-parens": 0, 43 | "no-extra-semi": 2, 44 | "no-extra-strict": 2, 45 | "no-fallthrough": 2, 46 | "no-floating-decimal": 2, 47 | "no-func-assign": 2, 48 | "no-implied-eval": 2, 49 | "no-inline-comments": 0, 50 | "no-inner-declarations": [2, "functions"], 51 | "no-invalid-regexp": 2, 52 | "no-irregular-whitespace": 2, 53 | "no-iterator": 2, 54 | "no-label-var": 2, 55 | "no-labels": 2, 56 | "no-lone-blocks": 2, 57 | "no-lonely-if": 1, 58 | "no-loop-func": 2, 59 | "no-mixed-requires": [0, false], 60 | "no-mixed-spaces-and-tabs": [2, false], 61 | "no-multi-spaces": 2, 62 | "no-multi-str": 2, 63 | "no-multiple-empty-lines": [2, {"max": 1}], 64 | "no-native-reassign": 2, 65 | "no-negated-in-lhs": 2, 66 | "no-nested-ternary": 0, 67 | "no-new": 2, 68 | "no-new-func": 2, 69 | "no-new-object": 2, 70 | "no-new-require": 2, 71 | "no-new-wrappers": 2, 72 | "no-obj-calls": 2, 73 | "no-octal": 2, 74 | "no-octal-escape": 2, 75 | "no-path-concat": 2, 76 | "no-plusplus": 0, 77 | "no-process-env": 2, 78 | "no-process-exit": 2, 79 | "no-proto": 2, 80 | "no-redeclare": 2, 81 | "no-regex-spaces": 2, 82 | "no-reserved-keys": 0, 83 | "no-restricted-modules": 0, 84 | "no-return-assign": 2, 85 | "no-script-url": 2, 86 | "no-self-compare": 2, 87 | "no-sequences": 2, 88 | "no-shadow": 2, 89 | "no-shadow-restricted-names": 2, 90 | "no-space-before-semi": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-sync": 0, 94 | "no-ternary": 0, 95 | "no-trailing-spaces": 2, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-undefined": 0, 99 | "no-underscore-dangle": 0, 100 | "no-unreachable": 2, 101 | "no-unused-expressions": 2, 102 | "no-unused-vars": [2, {"vars": "all", "args": "none"}], 103 | "no-use-before-define": [2, "nofunc"], 104 | "no-void": 0, 105 | "no-warning-comments": [0, { "terms": ["todo", "fixme", "xxx"], "location": "start" }], 106 | "no-with": 2, 107 | "no-wrap-func": 2, 108 | "block-scoped-var": 0, 109 | "brace-style": [2, "1tbs"], 110 | "camelcase": 2, 111 | "comma-spacing": [2, {"before": false, "after": true}], 112 | "comma-style": [2, "last"], 113 | "complexity": [1, 11], 114 | "consistent-return": 2, 115 | "consistent-this": [2, "self"], 116 | "curly": [2, "all"], 117 | "default-case": 2, 118 | "dot-notation": 2, 119 | "eol-last": 2, 120 | "eqeqeq": 2, 121 | "func-names": 2, 122 | "func-style": [0, "declaration"], 123 | "global-strict": [2, "always"], 124 | "guard-for-in": 2, 125 | "handle-callback-err": [2, "^(err|error|anySpecificError)$" ], 126 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 127 | "max-depth": [2, 4], 128 | "max-len": [2, 80, 4], 129 | "max-nested-callbacks": [2, 3], 130 | "max-params": [2, 4], 131 | "max-statements": [1, 15], 132 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 133 | "new-parens": 2, 134 | "one-var": 0, 135 | "operator-assignment": [0, "always"], 136 | "padded-blocks": 0, 137 | "quote-props": 0, 138 | "quotes": [2, "single"], 139 | "radix": 2, 140 | "semi": 2, 141 | "sort-vars": 0, 142 | "space-after-function-name": [2, "never"], 143 | "space-after-keywords": [1, "always"], 144 | "space-before-blocks": [1, "always"], 145 | "space-in-brackets": [1, "never"], 146 | "space-in-parens": [2, "never"], 147 | "space-infix-ops": 2, 148 | "space-return-throw-case": 2, 149 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 150 | "spaced-line-comment": [2, "always"], 151 | "strict": 2, 152 | "use-isnan": 2, 153 | "valid-jsdoc": [2, { "requireReturn": false, "requireParamDescription": false }], 154 | "valid-typeof": 2, 155 | "vars-on-top": 0, 156 | "wrap-iife": 2, 157 | "wrap-regex": 0, 158 | "yoda": [2, "never"] 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.a 8 | *.o 9 | *.so 10 | *.node 11 | 12 | # Node Waf Byproducts # 13 | ####################### 14 | .lock-wscript 15 | build/ 16 | autom4te.cache/ 17 | 18 | # Node Modules # 19 | ################ 20 | # Better to let npm install these from the package.json defintion 21 | # rather than maintain this manually 22 | node_modules/ 23 | 24 | # Packages # 25 | ############ 26 | # it's better to unpack these files and commit the raw source 27 | # git has its own built in compression methods 28 | *.7z 29 | *.dmg 30 | *.gz 31 | *.iso 32 | *.jar 33 | *.rar 34 | *.tar 35 | *.zip 36 | 37 | # Logs and databases # 38 | ###################### 39 | *.log 40 | dump.rdb 41 | *.tap 42 | *.xml 43 | 44 | # OS generated files # 45 | ###################### 46 | .DS_Store? 47 | .DS_Store 48 | ehthumbs.db 49 | Icon? 50 | Thumbs.db 51 | coverage 52 | 53 | # Text Editor Byproducts # 54 | ########################## 55 | *.sw? 56 | .idea/ 57 | 58 | # Python object code 59 | ########################## 60 | *.py[oc] 61 | 62 | # All translation files # 63 | ######################### 64 | static/translations-s3/ 65 | scratchlol/ 66 | TODO.md 67 | -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "validateIndentation": 4, 3 | "maximumLineLength": { 4 | "value": 120, 5 | "allowUrlComments": true, 6 | "allowComments": false, 7 | "allowRegex": false 8 | }, 9 | "requireSpaceAfterKeywords": [ 10 | "if", 11 | "else", 12 | "for", 13 | "while", 14 | "do", 15 | "switch", 16 | "return", 17 | "try", 18 | "catch" 19 | ], 20 | "disallowSpacesInFunctionDeclaration": { 21 | "beforeOpeningRoundBrace": true 22 | }, 23 | "disallowSpacesInNamedFunctionExpression": { 24 | "beforeOpeningRoundBrace": true 25 | }, 26 | "requireSpacesInFunctionDeclaration": { 27 | "beforeOpeningCurlyBrace": true 28 | }, 29 | "requireSpacesInNamedFunctionExpression": { 30 | "beforeOpeningCurlyBrace": true 31 | }, 32 | "disallowTrailingComma": true, 33 | "requireBlocksOnNewline": true, 34 | "excludeFiles": [], 35 | "requireCurlyBraces": [ 36 | "if", 37 | "else", 38 | "for", 39 | "while", 40 | "do", 41 | "try", 42 | "catch" 43 | ], 44 | "disallowMultipleVarDecl": true, 45 | "disallowEmptyBlocks": true, 46 | "disallowSpaceAfterObjectKeys": true, 47 | "requireCommaBeforeLineBreak": true, 48 | "requireSpaceBeforeBinaryOperators": [ 49 | "+", 50 | "-", 51 | "/", 52 | "*", 53 | "=", 54 | "==", 55 | "===", 56 | "!=", 57 | "!==" 58 | ], 59 | "requireSpacesInConditionalExpression": true, 60 | "requireSpaceAfterBinaryOperators": [ 61 | "+", 62 | "-", 63 | "/", 64 | "*", 65 | "=", 66 | "==", 67 | "===", 68 | "!=", 69 | "!==", 70 | "," 71 | ], 72 | "disallowSpaceAfterPrefixUnaryOperators": [ 73 | "++", 74 | "--", 75 | "+", 76 | "-", 77 | "~", 78 | "!" 79 | ], 80 | "disallowKeywords": [ 81 | "with", 82 | "try", 83 | "catch", 84 | "finally" 85 | ], 86 | "disallowMultipleLineBreaks": true, 87 | "validateLineBreaks": "LF", 88 | "validateQuoteMarks": { 89 | "mark": "'", 90 | "escape": true 91 | }, 92 | "disallowMixedSpacesAndTabs": true, 93 | "disallowTrailingWhitespace": true, 94 | "disallowKeywordsOnNewLine": [ 95 | "else" 96 | ], 97 | "requireLineFeedAtFileEnd": true, 98 | "requireDotNotation": true 99 | } 100 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | /coverage 2 | /node_modules 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "globalstrict": true, 3 | "indent": 4, 4 | "undef": true, 5 | "validthis": true, 6 | "newcap": false, 7 | "globals": { 8 | "__dirname": false, 9 | "__filename": false, 10 | "require": false, 11 | "module": false, 12 | "console": true, 13 | "exports": true, 14 | "global": true, 15 | "process": true, 16 | "Buffer": true, 17 | "setTimeout": true, 18 | "clearTimeout": true, 19 | "setInterval": true, 20 | "clearInterval": true 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | - "0.11" 5 | script: npm run travis 6 | -------------------------------------------------------------------------------- /CONTRIBUTION.md: -------------------------------------------------------------------------------- 1 | # Want to contribute? 2 | 3 | Great! That's why this is an open source project. We use this project in our infrastructure at Uber, and we hope that it's useful to others as well. 4 | 5 | Before you get started, here are some suggestions: 6 | 7 | - Check open issues for what you want. 8 | - If there is an open issue, comment on it. Otherwise open an issue describing your bug or feature with use cases. 9 | - Before undertaking a major change, please discuss this on the issue. We'd hate to see you spend a lot of time working on something that conflicts with other goals or requirements that might not be obvious. 10 | - Write code to fix the problem, then open a pull request with tests and documentation. 11 | - The pull requests gets reviewed and then merged assuming there are no problems. 12 | - A new release version gets cut. 13 | 14 | ## Licencing 15 | 16 | - Every file must have a licence block at the top. This is enforced using `uber-licence` 17 | - If you contribute to a file in this project and are not an Uber employee, then you should 18 | add your name to the copyright section of the licence file. 19 | - Work that you contribute must be your own. 20 | 21 | ## Releases 22 | 23 | Declaring formal releases requires peer review. 24 | 25 | - A reviewer of a pull request should recommend a new version number (patch, minor or major). 26 | - Once your change is merged feel free to bump the version as recommended by the reviewer. 27 | - A new version number should not be cut without peer review unless done by the project maintainer. 28 | 29 | ### Cutting a new version 30 | 31 | - Get your branch merged on master 32 | - Run `npm version major` or `npm version minor` or `npm version patch` 33 | - `git push origin master --tags` 34 | - If you are a project owner, then `npm publish` 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Uber Technologies, Inc. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # typed-request-client 2 | 3 | Make HTTP requests using TypedRequest and TypedResponse objects. 4 | 5 | ## Example 6 | 7 | ```js 8 | var TypedRequestClient = require('typed-request-client'); 9 | var Statsd = require('lynx'); 10 | 11 | var statsd = Statsd({ 12 | host: 'localhost', 13 | port: 6379 14 | }); 15 | 16 | var request = TypedRequestClient({ 17 | clientName: 'my-client', 18 | statsd: statsd 19 | }); 20 | 21 | var typedRequest = { 22 | url: 'http://localhost:9000/', 23 | method: 'GET', 24 | headers: {}, 25 | body: { userId: '42' } 26 | }; 27 | 28 | request(typedRequest, { 29 | requestSchema: someJSONSchemaForRequest, 30 | responseSchema: someJSONSchemaForResponse, 31 | resource: '.read' 32 | }, function (err, typedResponse) { 33 | // if an IO err happened then err 34 | 35 | // typedResponse is a plain object with 36 | // - statusCode (number) 37 | // - httpVersion (string) 38 | // - headers (object) 39 | // - body (object) 40 | }); 41 | ``` 42 | 43 | ## Scope 44 | 45 | The `typed-request-client` module will do the following for you: 46 | 47 | - Make HTTP client requests using `TypedRequest` and 48 | `TypedResponse` interfaces. 49 | - Wrap your HTTP client request in a `Prober` using the 50 | `airlock` module. 51 | - Validate the `TypedRequest` and `TypedResponse` as per the 52 | supplied `requestSchema` and `responseSchema`. 53 | - Add statsd integration to your service, it will write four 54 | different keys, `increment:request`, `timing:request-time`, 55 | `increment:statusCode`, `timing:total-time` 56 | 57 | ## Docs 58 | 59 | ### `var makeReq = TypedRequestClient(opts)` 60 | 61 | To create a TypedRequestClient you must pass a number of options. 62 | 63 | When you create one it will return a `makeReq` function you can call. 64 | 65 | #### `opts.clientName` 66 | 67 | You must pass a `clientName` into the `TypedRequestClient`. This 68 | will be a name used the statsd events being emitted. 69 | 70 | This means you should pick a name you want to use for statsd. 71 | 72 | #### `opts.statsd` 73 | 74 | You must pass in a working `statsd` client. A statsd client is 75 | required since the `TypedRequestClient` must output statsd. 76 | 77 | A valid statsd client has at least two methods: 78 | 79 | - `statsd.increment(listOfKeys)` 80 | - `statsd.timing(listsOfKeys, numericTimeDelta)` 81 | 82 | #### `opts.request` 83 | 84 | You can optionally pass in a different `request` function. This 85 | will default to `mikeal/request` from npm if you do not pass 86 | one in. 87 | 88 | #### `opts.now` 89 | 90 | You can optionally pass in a different `now` function. This 91 | will default to `Date.now()` from ES5 if you do not pass one 92 | in. 93 | 94 | ### `makeReq(typedRequest, options, callback)` 95 | 96 | The function returned from `TypedRequestClient` allows you to 97 | make typed requests to a server. 98 | 99 | The `makeReq` interface is purposefully low level and kept 100 | simple. You must supply all information. 101 | 102 | #### `typedRequest` 103 | 104 | The first argument is the `typedRequest` you want to make. 105 | 106 | ```jsig 107 | type TypedRequest : { 108 | url: String, 109 | method?: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | 110 | "DELETE" | "TRACE" | "PATCH", 111 | query?: Object, 112 | headers?: Object, 113 | body?: Any 114 | } 115 | 116 | ``` 117 | 118 | A `TypedRequest` is a plain javascript object that looks similar 119 | to a `HttpRequest` from node core, however it is not a stream. 120 | 121 | The `url` property must be a valid full URI including host & port 122 | 123 | The `method` property must be a valid HTTP method. It will 124 | default to the `"GET"` HTTP method. 125 | 126 | The `query` property is an optional object you can pass. It will 127 | be serialized to a string using the `querystring` module 128 | and correctly appended to the url you passed. 129 | 130 | If you pass a `query` ensure that there are no querystring 131 | parameters on the `url`. 132 | 133 | The `headers` property is an optional object of headers. If you 134 | pass any headers then they will be used as part of the 135 | outgoing HTTP request. 136 | 137 | The `body` property is an optional javascript object to send 138 | as part of the HTTP request. If set to valid JSON then the 139 | `makeReq` function will send your value as a JSON encoded 140 | string as part of the outgoing HTTP request. 141 | 142 | #### `options` 143 | 144 | The second argument is `options` and it is required. 145 | 146 | ```jsig 147 | type HandlerOptions : { 148 | requestSchema: JSONSchema, 149 | responseSchema: JSONSchema, 150 | resource: String 151 | } 152 | ``` 153 | 154 | You must specifiy a `requestSchema` which must be a valid 155 | JSONSchema object. 156 | 157 | This will be used to validate the `typedRequest` argument. 158 | 159 | Feel free to look at [integration tests](test/integration.js) 160 | for an example of a valid requestSchema. 161 | 162 | You must specify a `responseSchema` which must be a valid 163 | JSONSchema object. 164 | 165 | This will be used to validate the `typedResponse` argument 166 | coming out of the callback from the outgoing HTTP request. 167 | 168 | You must specify a `resource` name which must be a string and 169 | will be used when emitting stats events. 170 | 171 | #### `callback(error, typedResponse)` 172 | 173 | The `callback` to `makeReq` is the third and final argument. It 174 | will get called with an `Error` or a `TypedResponse`. 175 | 176 | If you get an `Error` then that's either an IO error or a 177 | validation error. 178 | 179 | If you get a `typedResponse` then that will look like: 180 | 181 | ```jsig 182 | type TypedResponse : { 183 | httpVersion: String, 184 | statusCode: Number, 185 | headers: Object 186 | body?: Any 187 | } 188 | ``` 189 | 190 | The `typedResponse` will have a `httpVersion` field that is the 191 | version of HTTP used. 192 | 193 | The `typedResponse` will have a `statusCode` field that is the 194 | statusCode of response to the outgoing HTTP request. 195 | 196 | The `typedResponse` will have a `headers` field that is an 197 | object of heeaders returned by the outgoing HTTP request. 198 | 199 | The `body` will be the HTTP body of the HTTP response. 200 | 201 | ## Extending 202 | 203 | The typed request client as exported by `typed-request-client` uses a default 204 | stack of configurable adapters. 205 | These can be customized. 206 | Each of these layers is exported by various modules under `adapters` and can be 207 | coposed as a pipeline as exported by `make-typed-request/adapt`. 208 | 209 | ```js 210 | var adapt = require('make-typed-request/adapt'); 211 | function MyTypedRequestClient(options) { 212 | return adapt() 213 | .probing(options) 214 | .statsdMeasure(options, 'requestTime') 215 | .statsdReportStatusCode(options) 216 | .validating(options) 217 | .statsdReportRequestMade(options) 218 | .statsdMeasure(options, 'totalTime') 219 | .statsd(options) 220 | .valueOf(); 221 | } 222 | ``` 223 | 224 | We use the `enchain` module to create fluent interfaces based on a collection 225 | of adapter methods. 226 | This layer can be bypassed. 227 | 228 | ```js 229 | var TypedRequestClient = require('make-typed-request/make-typed-request'); 230 | var Validating = require('make-typed-request/adapters/validating'); 231 | var MyTypedRequestClient = Validating(TypedRequestClient, {}); 232 | ``` 233 | 234 | Additional client adapters can be made as functions that accept a client as 235 | their first argument and return a decorated client. 236 | By convention we pass a shared `options` object through every adapter, but 237 | further arguments may be adapter instance specific. 238 | 239 | ```js 240 | function MyAdapter(client, options, myArgument) { 241 | return myClient; 242 | function myClient(request, shared, respond) { 243 | // Intercept request 244 | client(request, shared, onResponse); 245 | function onResponse(error, response) { 246 | // Observe progress 247 | if (error) { respond(error); } 248 | // Intercept response 249 | respond(null, response); 250 | } 251 | } 252 | } 253 | ``` 254 | 255 | You can then create your own adapter chain vocabulary with `enchain`. 256 | All of the adapters that this package provides are exported as a single object 257 | from `typed-request-client/adapters`, that you may mix into your own chains. 258 | 259 | ```js 260 | var enchain = require('enchain'); 261 | var adapt = enchain({ 262 | validating: require('make-typed-request/adapters').validating, 263 | // etc 264 | myAdapter: require('./my-adapter') 265 | }); 266 | var TypedRequestClient = require('make-typed-request/make-typed-request'); 267 | var options = { 268 | requestSchema: someJSONSchemaForRequest, 269 | responseSchema: someJSONSchemaForResponse 270 | }; 271 | var MyTypedRequestClient = adapt(TypedRequestClient) 272 | .myAdapter(options, myArgument) 273 | .validating(options) 274 | .valueOf() 275 | ``` 276 | 277 | ## Installation 278 | 279 | `npm install typed-request-client` 280 | 281 | ## Tests 282 | 283 | `npm test` 284 | 285 | ## Contributors 286 | 287 | - Raynos 288 | - Kris Kowal 289 | 290 | ## MIT Licenced 291 | 292 | -------------------------------------------------------------------------------- /adapt.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var enchain = require('enchain'); 4 | var makeTypedRequest = require('./make-typed-request.js'); 5 | var adapters = require('./adapters.js'); 6 | 7 | var chain = enchain(adapters); 8 | 9 | module.exports = adapt; 10 | function adapt(myMakeTypedRequest) { 11 | return chain({request: myMakeTypedRequest || makeTypedRequest}); 12 | } 13 | -------------------------------------------------------------------------------- /adapters.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | statsd: require('./adapters/statsd.js'), 5 | statsdMeasure: require('./adapters/statsd-measure.js'), 6 | statsdReportStatusCode: require('./adapters/statsd-report-status-code.js'), 7 | statsdReportRequestMade: 8 | require('./adapters/statsd-report-request-made.js'), 9 | validating: require('./adapters/validating.js'), 10 | probing: require('./adapters/probing.js') 11 | }; 12 | -------------------------------------------------------------------------------- /adapters/probing.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var globalRequest = require('request'); 4 | var Prober = require('airlock'); 5 | var xtend = require('xtend'); 6 | var errors = require('../errors.js'); 7 | 8 | module.exports = ProbingRequestHandler; 9 | function ProbingRequestHandler(requestHandler, options) { 10 | if (!(this instanceof ProbingRequestHandler)) { 11 | return new ProbingRequestHandler(requestHandler, options); 12 | } 13 | 14 | if (typeof options.clientName !== 'string') { 15 | throw errors.MissingClientName({ 16 | optionsStr: JSON.stringify(options) 17 | }); 18 | } 19 | 20 | this.prober = options.prober = Prober({ 21 | enabled: options.proberEnabled === false ? false : true, 22 | title: 'typed-request-client.' + options.clientName, 23 | statsd: options.statsd 24 | }); 25 | // TODO consider moving this one line into a separate layer. 26 | options.request = options.request || globalRequest; 27 | this.requestHandler = requestHandler; 28 | this.options = options; 29 | } 30 | 31 | ProbingRequestHandler.prototype.request = 32 | function handleProbingRequest(request, opts, cb) { 33 | opts = xtend(opts, {request: this.options.request}); 34 | var thunk = this.requestHandler.request.bind( 35 | this.requestHandler, 36 | request, 37 | opts 38 | ); 39 | this.prober.probe(thunk, cb); 40 | }; 41 | -------------------------------------------------------------------------------- /adapters/statsd-measure.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = StatsdMeasureClient; 4 | function StatsdMeasureClient(requestHandler, options, metricName) { 5 | if (!(this instanceof StatsdMeasureClient)) { 6 | return new StatsdMeasureClient(requestHandler, options, metricName); 7 | } 8 | this.requestHandler = requestHandler; 9 | this.metricName = metricName; 10 | this.now = options.now || Date.now; 11 | // To lazy-bind statsEmitter, not necessarily available at time of 12 | // construction since it is introduced by another layer. 13 | this.options = options; 14 | } 15 | 16 | StatsdMeasureClient.prototype.request = 17 | function handleMeasuringRequest( 18 | request, 19 | requestOptions, 20 | handleMeasuredResponse 21 | ) { 22 | var self = this; 23 | var resource = requestOptions.resource; 24 | var begin = self.now(); 25 | self.requestHandler.request(request, requestOptions, handleResponse); 26 | function handleResponse(error, response) { 27 | // TODO Note that this measures the response time regardless of 28 | // whether there was an error. 29 | // Error times should probably not be measured. 30 | var end = self.now(); 31 | var duration = end - begin; 32 | self.options.statsEmitter.emit(self.metricName, resource, duration); 33 | 34 | if (error) { 35 | return handleMeasuredResponse(error); 36 | } 37 | handleMeasuredResponse(null, response); 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /adapters/statsd-report-request-made.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = StatsdReportRequestMadeHandler; 4 | function StatsdReportRequestMadeHandler(requestHandler, options) { 5 | if (!(this instanceof StatsdReportRequestMadeHandler)) { 6 | return new StatsdReportRequestMadeHandler(requestHandler, options); 7 | } 8 | this.requestHandler = requestHandler; 9 | this.options = options; 10 | } 11 | 12 | StatsdReportRequestMadeHandler.prototype.request = 13 | function handleStatsdRequestMadeReportingRequest( 14 | request, 15 | requestOptions, 16 | handleResponse 17 | ) { 18 | var resource = requestOptions.resource; 19 | var statsEmitter = this.options.statsEmitter; 20 | statsEmitter.emit('makeRequest', resource); 21 | this.requestHandler.request(request, requestOptions, handleResponse); 22 | }; 23 | -------------------------------------------------------------------------------- /adapters/statsd-report-status-code.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // Variation on what exists in the typed-request package 4 | module.exports = StatsdReportStatusCodeHandler; 5 | function StatsdReportStatusCodeHandler(requestHandler, options) { 6 | if (!(this instanceof StatsdReportStatusCodeHandler)) { 7 | return new StatsdReportStatusCodeHandler(requestHandler, options); 8 | } 9 | this.requestHandler = requestHandler; 10 | this.options = options; 11 | } 12 | 13 | StatsdReportStatusCodeHandler.prototype.request = 14 | function handleReportingRequest(request, requestOptions, handleResponse) { 15 | var self = this; 16 | self.requestHandler.request(request, requestOptions, onResponse); 17 | function onResponse(error, response) { 18 | var resource = requestOptions.resource; 19 | var statsEmitter = self.options.statsEmitter; 20 | statsEmitter.emit('requestResult', resource, 'request-all'); 21 | if (error) { 22 | statsEmitter.emit('requestResult', resource, 23 | 'request-failed.client-error', error.code || 'unknown'); 24 | return handleResponse(error); 25 | } 26 | statsEmitter.emit('statusCode', resource, response.statusCode); 27 | if (response.statusCode >= 400 && response.statusCode <= 599) { 28 | statsEmitter.emit('requestResult', resource, 29 | 'request-failed.server-error', response.statusCode); 30 | } 31 | handleResponse(null, response); 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /adapters/statsd.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var EventEmitter = require('events').EventEmitter; 4 | var errors = require('../errors.js'); 5 | var writeStats = require('../write-stats.js'); 6 | 7 | module.exports = StatsdRequestHandler; 8 | function StatsdRequestHandler(requestHandler, options) { 9 | /* istanbul ignore if */ 10 | if (typeof options.clientName !== 'string') { 11 | throw errors.MissingClientName({ 12 | optionsStr: JSON.stringify(options) 13 | }); 14 | } 15 | if (typeof options.statsd !== 'object') { 16 | throw errors.MissingStatsd({ 17 | optionsStr: JSON.stringify(options) 18 | }); 19 | } 20 | 21 | var statsEmitter = new EventEmitter(); 22 | options.statsEmitter = statsEmitter; 23 | writeStats(statsEmitter, { 24 | clientName: options.clientName, 25 | statsd: options.statsd 26 | }); 27 | return requestHandler; 28 | } 29 | -------------------------------------------------------------------------------- /adapters/validating.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var ValidateShape = require('../validate-shape.js'); 4 | var filter = require('uber-json-schema-filter'); 5 | 6 | module.exports = ValidatingRequestHandler; 7 | function ValidatingRequestHandler(requestHandler) { 8 | if (!(this instanceof ValidatingRequestHandler)) { 9 | return new ValidatingRequestHandler(requestHandler); 10 | } 11 | this.requestHandler = requestHandler; 12 | this.shape = new ValidateShape(); 13 | } 14 | 15 | ValidatingRequestHandler.prototype.request = handleValidatingRequest; 16 | ValidatingRequestHandler.prototype.response = handleValidatingResponse; 17 | ValidatingRequestHandler.prototype.validateRequest = validateRequest; 18 | ValidatingRequestHandler.prototype.validateResponse = validateResponse; 19 | 20 | function handleValidatingRequest(treq, requestOptions, handleResponse) { 21 | var self = this; 22 | var filterEnabled = requestOptions.filterRequest; 23 | var validationEnabled = requestOptions.validateRequest; 24 | var requestSchema = requestOptions.requestSchema; 25 | 26 | if (filterEnabled) { 27 | treq = filter(requestSchema, treq); 28 | } 29 | 30 | if (validationEnabled) { 31 | var error = self.validateRequest(treq, requestSchema); 32 | if (error) { 33 | return handleResponse(error); 34 | } 35 | } 36 | 37 | self.requestHandler.request( 38 | treq, 39 | requestOptions, 40 | validatingRequestCallback 41 | ); 42 | 43 | function validatingRequestCallback(err, tres) { 44 | self.response(err, tres, requestOptions, handleResponse); 45 | } 46 | } 47 | 48 | function handleValidatingResponse(err, tres, requestOptions, handleResponse) { 49 | if (err) { 50 | return handleResponse(err); 51 | } 52 | 53 | var statusCode = tres.statusCode; 54 | var validate = statusCode < 400 || statusCode >= 600; 55 | var filterEnabled = validate && requestOptions.filterResponse; 56 | var validationEnabled = validate && requestOptions.validateResponse; 57 | var responseSchema = requestOptions.responseSchema; 58 | 59 | if (filterEnabled) { 60 | tres = filter(responseSchema, tres); 61 | } 62 | 63 | if (validationEnabled) { 64 | var error = this.validateResponse(tres, responseSchema); 65 | 66 | if (error) { 67 | return handleResponse(error); 68 | } 69 | } 70 | 71 | handleResponse(null, tres); 72 | } 73 | 74 | function validateRequest(treq, schema) { 75 | var error = this.shape.validate(treq, schema); 76 | 77 | if (error) { 78 | error.treq = treq; 79 | error.schema = schema; 80 | } 81 | 82 | return error; 83 | } 84 | 85 | function validateResponse(tres, schema) { 86 | var error = this.shape.validate(tres, schema); 87 | 88 | if (error) { 89 | error.tres = tres; 90 | error.schema = schema; 91 | } 92 | 93 | return error; 94 | } 95 | -------------------------------------------------------------------------------- /docs.jsig: -------------------------------------------------------------------------------- 1 | import { HttpResponse } from "node.http.jsig" 2 | 3 | type JSONSchema : Object 4 | type Statsd : { 5 | increment: (Array) => void, 6 | timing: (Array, Number) => void 7 | } 8 | type MikealRequest : (opts: { 9 | url: String, 10 | method: String, 11 | headers: Object, 12 | timeout: Number, 13 | json: true | Any 14 | }, cb: (Error, HttpResponse & { 15 | body: Any 16 | }) => void) => void 17 | 18 | type HandlerOptions : { 19 | requestSchema: JSONSchema, 20 | responseSchema: JSONSchema, 21 | resource: String 22 | } 23 | 24 | type TypedRequest : { 25 | url: String, 26 | method?: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | 27 | "DELETE" | "TRACE" | "PATCH", 28 | query?: Object, 29 | headers?: Object, 30 | body?: Any 31 | } 32 | 33 | type TypedResponse : { 34 | httpVersion: String, 35 | statusCode: Number, 36 | headers: Object 37 | body?: Any 38 | } 39 | 40 | typed-request-client : ({ 41 | clientName: String, 42 | statsd: Statsd, 43 | request?: MikealRequest, 44 | now?: () => Number 45 | }) => ( 46 | typedRequest: TypedRequest, 47 | opts: HandlerOptions, 48 | cb: (Error, TypedResponse) => void 49 | ) => void 50 | 51 | type ValidationError : Error & { 52 | message: String, 53 | type: 'ValidationError', 54 | errors: Array 55 | } 56 | 57 | type Result => { 58 | type: 'ok', 59 | ok: O, 60 | error: null 61 | } | { 62 | type: 'error', 63 | ok: null, 64 | error: E 65 | } 66 | 67 | typed-request-client/validate-shape : ( 68 | shape: T, schema: JSONSchema 69 | ) => Result 70 | -------------------------------------------------------------------------------- /errors.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var TypedError = require('error/typed'); 4 | 5 | var MissingOptions = TypedError({ 6 | type: 'typed-request-client.missing-options', 7 | message: 'Expected options to be defined.\n' + 8 | 'When calling `TypedRequestClient(...)` you need to ' + 9 | 'pass an options object.\n' + 10 | 'SUGGESTED FIX: Update the `TypedRequestClient()` ' + 11 | 'callsite and add an options argument.\n' 12 | }); 13 | 14 | var MissingClientName = TypedError({ 15 | type: 'typed-request-client.missing-clientName', 16 | message: 'Expected `options.clientName` to be defined.\n' + 17 | 'Expected to see a `clientName` property but instead ' + 18 | 'found {optionsStr}.\n' + 19 | 'SUGGESTED FIX: Update the `TypedRequestClient({})` ' + 20 | 'callsite and add `options.clientName`.\n', 21 | optionsStr: null 22 | }); 23 | 24 | var MissingStatsd = TypedError({ 25 | type: 'typed-request-client.missing-statsd', 26 | message: 'Expected `options.statsd` to be defined.\n' + 27 | 'Expected to see a `statsd` property but instead ' + 28 | 'found {optionsStr}.\n' + 29 | 'SUGGESTED FIX: Update the `TypedRequestClient({})` ' + 30 | 'callsite and add `options.statsd`.\n', 31 | optionsStr: null 32 | }); 33 | 34 | module.exports = { 35 | MissingOptions: MissingOptions, 36 | MissingClientName: MissingClientName, 37 | MissingStatsd: MissingStatsd 38 | }; 39 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var errors = require('./errors.js'); 4 | var adapt = require('./adapt.js'); 5 | 6 | module.exports = TypedRequestClient; 7 | 8 | function TypedRequestClient(options) { 9 | if (!options) { 10 | throw errors.MissingOptions(); 11 | } 12 | 13 | var requestHandler = adapt() 14 | .probing(options) 15 | .statsdMeasure(options, 'requestTime') 16 | .statsdReportStatusCode(options) 17 | .validating(options) 18 | .statsdReportRequestMade(options) 19 | .statsdMeasure(options, 'totalTime') 20 | .statsd(options) 21 | .valueOf(); 22 | 23 | var request = requestHandler.request.bind(requestHandler); 24 | 25 | request.setProberEnabled = function setProberEnabled(enabled) { 26 | requestHandler.options.prober.setEnabled(enabled); 27 | }; 28 | 29 | return request; 30 | } 31 | -------------------------------------------------------------------------------- /make-typed-request.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var querystring = require('querystring'); 4 | var xtend = require('xtend'); 5 | 6 | var DEFUALT_TIMEOUT = 30 * 1000; 7 | 8 | module.exports = makeTypedRequest; 9 | 10 | function makeTypedRequest(treq, opts, cb) { 11 | var request = opts.request; 12 | 13 | var reqOpts = { 14 | url: treq.url, 15 | method: treq.method || 'GET', 16 | headers: treq.headers || {}, 17 | timeout: opts.timeout || DEFUALT_TIMEOUT, 18 | transformUrlFn: opts.transformUrlFn || undefined 19 | }; 20 | 21 | if (opts.requestOpts) { 22 | reqOpts = xtend(reqOpts, opts.requestOpts); 23 | } 24 | 25 | if (treq.body !== undefined) { 26 | reqOpts.json = treq.body; 27 | } else { 28 | reqOpts.json = true; 29 | } 30 | 31 | if (treq.query) { 32 | var query = querystring.stringify(treq.query); 33 | if (query !== '') { 34 | reqOpts.url = reqOpts.url + '?' + query; 35 | } 36 | } 37 | 38 | if (typeof reqOpts.transformUrlFn === 'function') { 39 | reqOpts.url = reqOpts.transformUrlFn(reqOpts.url); 40 | } 41 | 42 | request(reqOpts, onResponse); 43 | 44 | function onResponse(err, resp) { 45 | if (err) { 46 | return cb(err); 47 | } 48 | 49 | var tres = { 50 | httpVersion: resp.httpVersion, 51 | headers: resp.headers, 52 | statusCode: resp.statusCode, 53 | body: resp.body 54 | }; 55 | 56 | cb(null, tres); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typed-request-client", 3 | "version": "6.2.4", 4 | "description": "Make HTTP requests using TypedRequest and TypedResponse objects.", 5 | "keywords": [], 6 | "author": "Raynos ", 7 | "repository": "git://github.com/uber/typed-request-client.git", 8 | "main": "index", 9 | "homepage": "https://github.com/uber/typed-request-client", 10 | "bugs": { 11 | "url": "https://github.com/uber/typed-request-client/issues", 12 | "email": "raynos2@gmail.com" 13 | }, 14 | "dependencies": { 15 | "airlock": "^2.1.2", 16 | "enchain": "0.0.1", 17 | "error": "^4.1.1", 18 | "glob": "^4.0.5", 19 | "jayschema": "^0.3.1", 20 | "jayschema-error-messages": "^1.0.2", 21 | "request": "^2.44.0", 22 | "uber-json-schema-filter": "^2.0.3", 23 | "xtend": "^4.0.0" 24 | }, 25 | "devDependencies": { 26 | "body": "^4.5.0", 27 | "coveralls": "^2.10.0", 28 | "format-stack": "^2.0.0", 29 | "istanbul": "^0.3.13", 30 | "jscs": "^1.6.2", 31 | "jshint": "^2.5.0", 32 | "opn": "^0.1.2", 33 | "pre-commit": "0.0.5", 34 | "send-data": "^3.3.3", 35 | "tap-spec": "^0.1.8", 36 | "tape": "^3.0.0", 37 | "lint-trap": "^1.0.1" 38 | }, 39 | "licenses": [ 40 | { 41 | "type": "MIT", 42 | "url": "http://github.com/uber/typed-request-client/raw/master/LICENSE" 43 | } 44 | ], 45 | "scripts": { 46 | "test": "npm run jshint -s && npm run cover -s", 47 | "fast-test": "npm run cover -s", 48 | "jshint-pre-commit": "jshint --verbose $(git diff --cached --name-only --diff-filter=ACMRTUXB | grep '\\.js$')", 49 | "jshint": "lint-trap", 50 | "cover": "istanbul cover --report html --print detail test/index.js && istanbul check-coverage --lines=100 --branches=100", 51 | "view-cover": "istanbul report html && opn ./coverage/index.html", 52 | "travis": "npm run cover -s && istanbul report lcov && ((cat coverage/lcov.info | coveralls) || exit 0)" 53 | }, 54 | "engine": { 55 | "node": ">= 0.8.x" 56 | }, 57 | "pre-commit": [ 58 | "jshint-pre-commit", 59 | "fast-test" 60 | ], 61 | "playdoh-version": "2.5.0" 62 | } 63 | -------------------------------------------------------------------------------- /result.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = Result; 4 | 5 | function Result(type, error, ok) { 6 | if (!(this instanceof Result)) { 7 | return new Result(type, error, ok); 8 | } 9 | 10 | this.type = type; 11 | this.error = error; 12 | this.ok = ok; 13 | } 14 | 15 | Result.Ok = function Ok(ok) { 16 | return new Result('ok', null, ok); 17 | }; 18 | 19 | Result.Error = function Error(error) { 20 | return new Result('error', error, null); 21 | }; 22 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('./make-typed-request.js'); 4 | require('./result.js'); 5 | require('./validate-shape.js'); 6 | require('./typed-request-client.js'); 7 | require('./integration.js'); 8 | require('./invalid-client-args.js'); 9 | require('./write-stats.js'); 10 | -------------------------------------------------------------------------------- /test/integration.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | var http = require('http'); 5 | var sendJson = require('send-data/json'); 6 | var setTimeout = require('timers').setTimeout; 7 | 8 | var TypedRequestClient = require('../index.js'); 9 | 10 | function createFakeStatsd(assert) { 11 | return { 12 | stats: [], 13 | increment: function increment(key) { 14 | this.stats.push({ 15 | type: 'increment', 16 | key: key 17 | }); 18 | }, 19 | timing: function timing(key, delta) { 20 | this.stats.push({ 21 | type: 'timing', 22 | key: key, 23 | delta: delta 24 | }); 25 | }, 26 | assertStat: function assertStat(stat, times) { 27 | var foundTimes = 0; 28 | for (var i = 0; i < this.stats.length; i++) { 29 | if (this.stats[i].type === stat.type && 30 | this.stats[i].key === stat.key && 31 | this.stats[i].delta === stat.delta) { 32 | foundTimes++; 33 | } 34 | } 35 | assert.equal(times, foundTimes, 36 | 'cannot find stat of expected times'); 37 | } 38 | }; 39 | } 40 | 41 | var requestSchema = { 42 | type: 'object', 43 | properties: { 44 | 'url': {type: 'string'}, 45 | 'method': {type: 'string'}, 46 | 'headers': {type: 'object'}, 47 | 'body': { 48 | type: 'object', 49 | properties: { 50 | 'foo': {type: 'string'} 51 | }, 52 | required: ['foo'] 53 | } 54 | }, 55 | required: ['url', 'method', 'headers', 'body'] 56 | }; 57 | 58 | var responseSchema = { 59 | type: 'object', 60 | properties: { 61 | 'statusCode': {type: 'number'}, 62 | 'httpVersion': {type: 'string'}, 63 | 'headers': {type: 'object'}, 64 | 'body': {type: 'object'} 65 | }, 66 | required: ['statusCode', 'httpVersion', 'headers', 'body'] 67 | }; 68 | 69 | test('can make http request', function t(assert) { 70 | var server = http.createServer(function onReq(req, res) { 71 | sendJson(req, res, { 72 | statusCode: 200, 73 | body: {} 74 | }); 75 | }); 76 | server.listen(0, function onPort() { 77 | var port = server.address().port; 78 | 79 | var statsd = createFakeStatsd(assert); 80 | var request = TypedRequestClient({ 81 | clientName: 'demo', 82 | statsd: statsd 83 | }); 84 | 85 | var treq = { 86 | url: 'http://localhost:' + port + '/', 87 | method: 'GET', 88 | headers: {}, 89 | body: { 90 | 'foo': 'bar' 91 | } 92 | }; 93 | 94 | request(treq, { 95 | timeout: 100, 96 | requestSchema: requestSchema, 97 | responseSchema: responseSchema, 98 | resource: 'read', 99 | filterRequest: true, 100 | filterResponse: true, 101 | validateRequest: true, 102 | validateResponse: true 103 | }, onResponse); 104 | 105 | function onResponse(err, tres) { 106 | assert.ifError(err); 107 | 108 | assert.equal(tres.statusCode, 200); 109 | assert.equal(tres.httpVersion, '1.1'); 110 | // assert.deepEqual(tres.headers, {}); 111 | assert.deepEqual(tres.body, {}); 112 | assert.deepEqual(Object.keys(tres), [ 113 | 'statusCode', 'httpVersion', 'headers', 'body' 114 | ]); 115 | 116 | statsd.assertStat({ 117 | type: 'increment', 118 | key: 'typed-request-client.demo.read.statusCode.200' 119 | }, 1); 120 | statsd.assertStat({ 121 | type: 'increment', 122 | key: 'typed-request-client.demo.read.request-all' 123 | }, 1); 124 | 125 | server.close(); 126 | assert.end(); 127 | } 128 | }); 129 | }); 130 | 131 | test('passes 500 right through', function t(assert) { 132 | var server = http.createServer(function onReq(req, res) { 133 | sendJson(req, res, { 134 | statusCode: 500, 135 | body: {message: 'sad'} 136 | }); 137 | }); 138 | server.listen(0, function onPort() { 139 | var port = server.address().port; 140 | 141 | var statsd = createFakeStatsd(assert); 142 | var request = TypedRequestClient({ 143 | clientName: 'demo', 144 | statsd: statsd 145 | }); 146 | 147 | var treq = { 148 | url: 'http://localhost:' + port + '/', 149 | method: 'GET', 150 | headers: {}, 151 | body: { 152 | 'foo': 'bar' 153 | } 154 | }; 155 | 156 | request(treq, { 157 | timeout: 100, 158 | requestSchema: requestSchema, 159 | responseSchema: responseSchema, 160 | resource: 'read' 161 | }, onResponse); 162 | 163 | function onResponse(err, tres) { 164 | assert.ifError(err); 165 | 166 | assert.equal(tres.statusCode, 500); 167 | assert.equal(tres.httpVersion, '1.1'); 168 | // assert.deepEqual(tres.headers, {}); 169 | assert.deepEqual(tres.body, {message: 'sad'}); 170 | assert.deepEqual(Object.keys(tres), [ 171 | 'httpVersion', 'headers', 'statusCode', 'body' 172 | ]); 173 | 174 | statsd.assertStat({ 175 | type: 'increment', 176 | key: 'typed-request-client.demo.read.statusCode.500' 177 | }, 1); 178 | statsd.assertStat({ 179 | type: 'increment', 180 | key: 'typed-request-client.demo.read' + 181 | '.request-failed.server-error.500' 182 | }, 1); 183 | statsd.assertStat({ 184 | type: 'increment', 185 | key: 'typed-request-client.demo.read.request-all' 186 | }, 1); 187 | 188 | server.close(); 189 | assert.end(); 190 | } 191 | }); 192 | }); 193 | 194 | test('respects timeout', function t(assert) { 195 | var server = http.createServer(function onReq(req, res) { 196 | var timeout = setTimeout(function onTimeout() { 197 | sendJson(req, res, { 198 | statusCode: 200, 199 | body: {} 200 | }); 201 | }, 300); 202 | timeout.unref(); 203 | }); 204 | server.listen(0, function onPort() { 205 | var port = server.address().port; 206 | 207 | var statsd = createFakeStatsd(assert); 208 | var request = TypedRequestClient({ 209 | clientName: 'demo', 210 | statsd: statsd 211 | }); 212 | 213 | var treq = { 214 | url: 'http://localhost:' + port + '/', 215 | method: 'GET', 216 | headers: {}, 217 | body: { 218 | 'foo': 'bar' 219 | } 220 | }; 221 | 222 | request(treq, { 223 | timeout: 100, 224 | requestSchema: requestSchema, 225 | responseSchema: responseSchema, 226 | resource: 'read' 227 | }, onResponse); 228 | 229 | function onResponse(err) { 230 | assert.ok( 231 | err.code === 'ETIMEDOUT' || 232 | err.code === 'ESOCKETTIMEDOUT' 233 | ); 234 | server.close(); 235 | statsd.assertStat({ 236 | type: 'increment', 237 | key: 'typed-request-client.demo.read.request-failed.client-error.ETIMEDOUT' 238 | }, 1); 239 | statsd.assertStat({ 240 | type: 'increment', 241 | key: 'typed-request-client.demo.read.request-all' 242 | }, 1); 243 | assert.end(); 244 | } 245 | }); 246 | }); 247 | -------------------------------------------------------------------------------- /test/invalid-client-args.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | 5 | var TypedRequestClient = require('../index.js'); 6 | 7 | test('throws without options', function t(assert) { 8 | assert.throws(function throwIt() { 9 | TypedRequestClient(); 10 | }, /Expected options to be defined/); 11 | 12 | assert.end(); 13 | }); 14 | 15 | test('throws without options.clientName', function t(assert) { 16 | assert.throws(function throwIt() { 17 | TypedRequestClient({}); 18 | }, /Expected `options.clientName` to be defined/); 19 | 20 | assert.end(); 21 | }); 22 | 23 | test('throws without options.statsd', function t(assert) { 24 | assert.throws(function throwIt() { 25 | TypedRequestClient({clientName: 'test'}); 26 | }, /Expected `options.statsd` to be defined/); 27 | 28 | assert.end(); 29 | }); 30 | -------------------------------------------------------------------------------- /test/make-typed-request.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | var request = require('request'); 5 | var http = require('http'); 6 | var parseJSON = require('body/json'); 7 | var sendError = require('send-data/error'); 8 | var sendJSON = require('send-data/json'); 9 | 10 | var makeTypedRequest = require('../make-typed-request.js'); 11 | 12 | var reqOpts = { 13 | request: request 14 | }; 15 | 16 | function createServer(onPort) { 17 | var server = http.createServer(onRequest); 18 | 19 | server.listen(0, onListening); 20 | 21 | return server; 22 | 23 | function onRequest(req, res) { 24 | if (req.url === '/redirect') { 25 | res.writeHead(301, {location: '/'}); 26 | return res.end(); 27 | } 28 | parseJSON(req, res, onJSON); 29 | 30 | function onJSON(err, json) { 31 | if (err) { 32 | return sendError(req, res, err); 33 | } 34 | 35 | sendJSON(req, res, { 36 | headers: {}, 37 | body: { 38 | req: json, 39 | url: req.url, 40 | method: req.method 41 | } 42 | }); 43 | } 44 | } 45 | 46 | function onListening() { 47 | var address = server.address(); 48 | 49 | onPort(address.port); 50 | } 51 | } 52 | 53 | test('can make request', function t(assert) { 54 | var server = createServer(function onPort(port) { 55 | var treq = { 56 | method: 'GET', 57 | url: 'http://localhost:' + port + '/', 58 | body: {'hello': 'world'} 59 | }; 60 | 61 | makeTypedRequest(treq, reqOpts, onResponse); 62 | 63 | function onResponse(err, resp) { 64 | assert.ifError(err); 65 | 66 | assert.equal(resp.httpVersion, '1.1'); 67 | assert.ok(resp.headers); 68 | assert.equal(resp.statusCode, 200); 69 | assert.equal(resp.headers['content-type'], 70 | 'application/json'); 71 | assert.deepEqual(resp.body, { 72 | method: 'GET', 73 | url: '/', 74 | req: { 75 | 'hello': 'world' 76 | } 77 | }); 78 | 79 | server.close(); 80 | assert.end(); 81 | } 82 | }); 83 | }); 84 | 85 | test('request with POST', function t(assert) { 86 | var server = createServer(function onPort(port) { 87 | var treq = { 88 | method: 'POST', 89 | url: 'http://localhost:' + port + '/', 90 | body: {'hello': 'world'} 91 | }; 92 | 93 | makeTypedRequest(treq, reqOpts, onResponse); 94 | 95 | function onResponse(err, resp) { 96 | assert.ifError(err); 97 | 98 | assert.equal(resp.httpVersion, '1.1'); 99 | assert.ok(resp.headers); 100 | assert.equal(resp.headers['content-type'], 101 | 'application/json'); 102 | assert.deepEqual(resp.body, { 103 | method: 'POST', 104 | url: '/', 105 | req: { 106 | 'hello': 'world' 107 | } 108 | }); 109 | 110 | server.close(); 111 | assert.end(); 112 | } 113 | }); 114 | }); 115 | 116 | test('request with GET w/ query', function t(assert) { 117 | var server = createServer(function onPort(port) { 118 | var treq = { 119 | method: 'GET', 120 | url: 'http://localhost:' + port + '/', 121 | query: {'hello': 'world'}, 122 | body: {} 123 | }; 124 | 125 | makeTypedRequest(treq, reqOpts, onResponse); 126 | 127 | function onResponse(err, resp) { 128 | assert.ifError(err); 129 | 130 | assert.equal(resp.httpVersion, '1.1'); 131 | assert.ok(resp.headers); 132 | assert.equal(resp.headers['content-type'], 133 | 'application/json'); 134 | assert.deepEqual(resp.body, { 135 | method: 'GET', 136 | url: '/?hello=world', 137 | req: {} 138 | }); 139 | 140 | server.close(); 141 | assert.end(); 142 | } 143 | }); 144 | }); 145 | 146 | test('request with GET w/o query', function t(assert) { 147 | var server = createServer(function onPort(port) { 148 | var treq = { 149 | method: 'GET', 150 | url: 'http://localhost:' + port + '/', 151 | query: {}, 152 | body: {} 153 | }; 154 | 155 | makeTypedRequest(treq, reqOpts, onResponse); 156 | 157 | function onResponse(err, resp) { 158 | assert.ifError(err); 159 | 160 | assert.equal(resp.httpVersion, '1.1'); 161 | assert.ok(resp.headers); 162 | assert.equal(resp.headers['content-type'], 163 | 'application/json'); 164 | assert.deepEqual(resp.body, { 165 | method: 'GET', 166 | url: '/', 167 | req: {} 168 | }); 169 | 170 | server.close(); 171 | assert.end(); 172 | } 173 | }); 174 | }); 175 | 176 | test('request with default method', function t(assert) { 177 | var server = createServer(function onPort(port) { 178 | var treq = { 179 | url: 'http://localhost:' + port + '/', 180 | body: {'hello': 'world'} 181 | }; 182 | 183 | makeTypedRequest(treq, reqOpts, onResponse); 184 | 185 | function onResponse(err, resp) { 186 | assert.ifError(err); 187 | 188 | assert.equal(resp.httpVersion, '1.1'); 189 | assert.ok(resp.headers); 190 | assert.equal(resp.headers['content-type'], 191 | 'application/json'); 192 | assert.deepEqual(resp.body, { 193 | method: 'GET', 194 | url: '/', 195 | req: { 196 | 'hello': 'world' 197 | } 198 | }); 199 | 200 | server.close(); 201 | assert.end(); 202 | } 203 | }); 204 | }); 205 | 206 | test('request without body', function t(assert) { 207 | var server = http.createServer(function onReq(req, res) { 208 | res.end('"hello world"'); 209 | }); 210 | server.listen(0, function onPort() { 211 | var port = server.address().port; 212 | 213 | var treq = { 214 | method: 'GET', 215 | url: 'http://localhost:' + port + '/' 216 | }; 217 | 218 | makeTypedRequest(treq, reqOpts, onResponse); 219 | 220 | function onResponse(err, resp) { 221 | assert.ifError(err); 222 | 223 | assert.equal(resp.httpVersion, '1.1'); 224 | assert.ok(resp.headers); 225 | assert.deepEqual(resp.body, 'hello world'); 226 | 227 | server.close(); 228 | assert.end(); 229 | } 230 | }); 231 | }); 232 | 233 | test('request that has network error', function t(assert) { 234 | var server = http.createServer(function onReq(req, res) { 235 | res.destroy(); 236 | }); 237 | server.listen(0, function onPort() { 238 | var port = server.address().port; 239 | 240 | var treq = { 241 | method: 'GET', 242 | url: 'http://localhost:' + port + '/' 243 | }; 244 | 245 | makeTypedRequest(treq, reqOpts, onResponse); 246 | 247 | function onResponse(err, resp) { 248 | assert.ok(err); 249 | assert.equal(err.code, 'ECONNRESET'); 250 | 251 | assert.equal(resp, undefined); 252 | 253 | server.close(); 254 | assert.end(); 255 | } 256 | }); 257 | }); 258 | 259 | test('request that has 500', function t(assert) { 260 | var server = http.createServer(function onReq(req, res) { 261 | res.statusCode = 500; 262 | res.end('"OOPS"'); 263 | }); 264 | server.listen(0, function onPort() { 265 | var port = server.address().port; 266 | 267 | var treq = { 268 | method: 'GET', 269 | url: 'http://localhost:' + port + '/' 270 | }; 271 | 272 | makeTypedRequest(treq, reqOpts, onResponse); 273 | 274 | function onResponse(err, resp) { 275 | assert.ifError(err); 276 | 277 | assert.equal(resp.httpVersion, '1.1'); 278 | assert.equal(resp.statusCode, 500); 279 | assert.ok(resp.headers); 280 | assert.deepEqual(resp.body, 'OOPS'); 281 | 282 | server.close(); 283 | assert.end(); 284 | } 285 | }); 286 | }); 287 | 288 | test('request that has transformUrlFn', function t(assert) { 289 | var server = createServer(function onPort(port) { 290 | var treq = { 291 | method: 'GET', 292 | url: 'http://localhost:' + port + '/', 293 | query: {prop: ['a,b', 'c,d']}, 294 | body: {'hello': 'world'} 295 | }; 296 | 297 | var transformReqOpts = { 298 | // special request to return the modified query 299 | request: function r(opts, cb) { 300 | assert.equal(opts.url, treq.url + '?prop=a,b&prop=c,d'); 301 | 302 | cb(null, { 303 | statusCode: 200, 304 | httpVersion: '1.1', 305 | headers: {}, 306 | body: {} 307 | }); 308 | } 309 | }; 310 | 311 | function transformUrlFn(url) { 312 | // Restore all the ',' from being stringify'ed 313 | return url.replace(/%2C/g, ','); 314 | } 315 | 316 | transformReqOpts.transformUrlFn = transformUrlFn; 317 | 318 | makeTypedRequest(treq, transformReqOpts, onResponse); 319 | 320 | function onResponse(err, resp) { 321 | assert.ifError(err); 322 | 323 | server.close(); 324 | assert.end(); 325 | } 326 | }); 327 | }); 328 | 329 | test('can make request with custom request options', function t(assert) { 330 | var server = createServer(function onPort(port) { 331 | var treq = { 332 | method: 'GET', 333 | url: 'http://localhost:' + port + '/redirect', 334 | body: {'hello': 'world'} 335 | }; 336 | 337 | var extendedReqOpts = { 338 | request: request, 339 | requestOpts: { 340 | followRedirect: false 341 | } 342 | }; 343 | 344 | makeTypedRequest(treq, extendedReqOpts, onResponse); 345 | 346 | function onResponse(err, resp) { 347 | assert.ifError(err); 348 | 349 | assert.equal(resp.httpVersion, '1.1'); 350 | assert.ok(resp.headers); 351 | assert.equal(resp.statusCode, 301); 352 | 353 | server.close(); 354 | assert.end(); 355 | } 356 | }); 357 | }); 358 | -------------------------------------------------------------------------------- /test/result.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | 5 | var Result = require('../result.js'); 6 | 7 | test('create Result', function t(assert) { 8 | var r = Result('ok', null, 42); 9 | 10 | assert.equal(r.type, 'ok'); 11 | assert.equal(r.ok, 42); 12 | assert.equal(r.error, null); 13 | 14 | assert.end(); 15 | }); 16 | 17 | test('Result.Error()', function t(assert) { 18 | var r = Result.Error(new Error('foo')); 19 | 20 | assert.equal(r.type, 'error'); 21 | assert.equal(r.ok, null); 22 | assert.equal(r.error.message, 'foo'); 23 | 24 | assert.end(); 25 | }); 26 | 27 | test('Result.Ok()', function t(assert) { 28 | var r = Result.Ok(42); 29 | 30 | assert.equal(r.type, 'ok'); 31 | assert.equal(r.ok, 42); 32 | assert.equal(r.error, null); 33 | 34 | assert.end(); 35 | }); 36 | -------------------------------------------------------------------------------- /test/typed-request-client.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | 5 | var TypedRequestClient = require('../index.js'); 6 | 7 | var fakeStatsd = { 8 | stats: [], 9 | increment: function increment(key) { 10 | this.stats.push({ 11 | type: 'increment', 12 | key: key 13 | }); 14 | }, 15 | timing: function timing(key, delta) { 16 | this.stats.push({ 17 | type: 'timing', 18 | key: key, 19 | delta: delta 20 | }); 21 | } 22 | }; 23 | 24 | var requestSchema = { 25 | type: 'object', 26 | properties: { 27 | 'url': {type: 'string'}, 28 | 'method': {type: 'string'}, 29 | 'headers': {type: 'object'}, 30 | 'query': {type: 'object'}, 31 | 'body': { 32 | type: 'object', 33 | properties: { 34 | 'foo': {type: 'string'} 35 | }, 36 | required: ['foo'] 37 | } 38 | }, 39 | required: ['url', 'method', 'headers', 'body'], 40 | additionalProperties: false 41 | }; 42 | 43 | var responseSchema = { 44 | type: 'object', 45 | properties: { 46 | 'statusCode': {type: 'number'}, 47 | 'httpVersion': {type: 'string'}, 48 | 'headers': {type: 'object'}, 49 | 'body': { 50 | type: 'object', 51 | properties: { 52 | 'test': {'type': 'string'} 53 | }, 54 | additionalProperties: false 55 | } 56 | }, 57 | required: ['statusCode', 'httpVersion', 'headers', 'body'] 58 | }; 59 | 60 | test('can make request', function t(assert) { 61 | var request = TypedRequestClient({ 62 | clientName: 'demo', 63 | statsd: fakeStatsd, 64 | request: function r(opts, cb) { 65 | assert.equal(opts.url, 'http://localhost:8000/'); 66 | assert.equal(opts.method, 'GET'); 67 | assert.deepEqual(opts.headers, {}); 68 | assert.equal(opts.timeout, 30000); 69 | assert.deepEqual(opts.json, { 70 | 'foo': 'bar' 71 | }); 72 | 73 | assert.deepEqual(Object.keys(opts).sort(), [ 74 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 75 | ]); 76 | 77 | cb(null, { 78 | statusCode: 200, 79 | httpVersion: '1.1', 80 | headers: {}, 81 | body: {} 82 | }); 83 | } 84 | }); 85 | 86 | var treq = { 87 | url: 'http://localhost:8000/', 88 | method: 'GET', 89 | headers: {}, 90 | body: { 91 | 'foo': 'bar' 92 | } 93 | }; 94 | 95 | request(treq, { 96 | requestSchema: requestSchema, 97 | responseSchema: responseSchema, 98 | resource: '.read', 99 | filterRequest: true, 100 | filterResponse: true, 101 | validateRequest: true, 102 | validateResponse: true 103 | }, onResponse); 104 | 105 | function onResponse(err, tres) { 106 | assert.ifError(err); 107 | 108 | assert.equal(tres.statusCode, 200); 109 | assert.equal(tres.httpVersion, '1.1'); 110 | assert.deepEqual(tres.headers, {}); 111 | assert.deepEqual(tres.body, {}); 112 | assert.deepEqual(Object.keys(tres).sort(), [ 113 | 'body', 'headers', 'httpVersion', 'statusCode' 114 | ]); 115 | 116 | assert.end(); 117 | } 118 | }); 119 | 120 | test('request error', function t(assert) { 121 | var request = TypedRequestClient({ 122 | clientName: 'demo', 123 | statsd: fakeStatsd, 124 | request: function r(opts, cb) { 125 | assert.fail('request called'); 126 | } 127 | }); 128 | 129 | var treq = { 130 | url: 'http://localhost:8000/', 131 | method: 'GET', 132 | headers: {} 133 | }; 134 | 135 | request(treq, { 136 | requestSchema: requestSchema, 137 | responseSchema: responseSchema, 138 | resource: '.read', 139 | filterRequest: true, 140 | filterResponse: true, 141 | validateRequest: true, 142 | validateResponse: true 143 | }, onResponse); 144 | 145 | function onResponse(err, tres) { 146 | assert.ok(err); 147 | 148 | assert.equal(err.message, 'Required'); 149 | assert.equal(err.attribute, 'body'); 150 | 151 | assert.end(); 152 | } 153 | }); 154 | 155 | test('io error', function t(assert) { 156 | var request = TypedRequestClient({ 157 | clientName: 'demo', 158 | statsd: fakeStatsd, 159 | request: function r(opts, cb) { 160 | cb(new Error('ECONNRESET')); 161 | } 162 | }); 163 | 164 | var treq = { 165 | url: 'http://localhost:8000/', 166 | method: 'GET', 167 | headers: {}, 168 | body: { 169 | 'foo': 'bar' 170 | } 171 | }; 172 | 173 | request(treq, { 174 | requestSchema: requestSchema, 175 | responseSchema: responseSchema, 176 | resource: '.read', 177 | filterRequest: true, 178 | filterResponse: true, 179 | validateRequest: true, 180 | validateResponse: true 181 | }, onResponse); 182 | 183 | function onResponse(err, tres) { 184 | assert.ok(err); 185 | assert.equal(err.message, 'ECONNRESET'); 186 | 187 | assert.end(); 188 | } 189 | }); 190 | 191 | test('response error', function t(assert) { 192 | var request = TypedRequestClient({ 193 | clientName: 'demo', 194 | statsd: fakeStatsd, 195 | request: function r(opts, cb) { 196 | cb(null, { 197 | statusCode: 200, 198 | httpVersion: '1.1', 199 | headers: {} 200 | }); 201 | } 202 | }); 203 | 204 | var treq = { 205 | url: 'http://localhost:8000/', 206 | method: 'GET', 207 | headers: {}, 208 | body: { 209 | 'foo': 'bar' 210 | } 211 | }; 212 | 213 | request(treq, { 214 | requestSchema: requestSchema, 215 | responseSchema: responseSchema, 216 | resource: '.read', 217 | filterRequest: true, 218 | filterResponse: true, 219 | validateRequest: true, 220 | validateResponse: true 221 | }, onResponse); 222 | 223 | function onResponse(err, tres) { 224 | assert.ok(err); 225 | 226 | assert.equal(err.message, 'Required'); 227 | assert.equal(err.attribute, 'body'); 228 | 229 | assert.end(); 230 | } 231 | }); 232 | 233 | test('response with nonstringified query', function t(assert) { 234 | var treq = { 235 | url: 'http://localhost:8000/', 236 | method: 'GET', 237 | headers: {}, 238 | query: {prop: ['a,b', 'c,d']}, 239 | body: { 240 | 'foo': 'bar' 241 | } 242 | }; 243 | 244 | var request = TypedRequestClient({ 245 | clientName: 'demo', 246 | statsd: fakeStatsd, 247 | request: function r(opts, cb) { 248 | assert.equal(opts.url, treq.url + '?prop=a,b&prop=c,d'); 249 | cb(null, { 250 | statusCode: 200, 251 | httpVersion: '1.1', 252 | headers: {}, 253 | body: {} 254 | }); 255 | } 256 | }); 257 | 258 | request(treq, { 259 | requestSchema: requestSchema, 260 | responseSchema: responseSchema, 261 | resource: '.read', 262 | filterRequest: true, 263 | filterResponse: true, 264 | validateRequest: true, 265 | validateResponse: true, 266 | transformUrlFn: function transform(url) { 267 | return url.replace(/%2C/g, ','); 268 | } 269 | }, onResponse); 270 | 271 | function onResponse(err, tres) { 272 | assert.ifError(err); 273 | assert.equal(tres.httpVersion, '1.1'); 274 | 275 | assert.end(); 276 | } 277 | }); 278 | 279 | test('can make request without request validation', function t(assert) { 280 | var request = TypedRequestClient({ 281 | clientName: 'demo', 282 | statsd: fakeStatsd, 283 | request: function r(opts, cb) { 284 | assert.equal(opts.url, 'http://localhost:8000/'); 285 | assert.equal(opts.method, 'GET'); 286 | assert.deepEqual(opts.headers, {}); 287 | assert.equal(opts.timeout, 30000); 288 | assert.deepEqual(opts.json, {}); 289 | 290 | assert.deepEqual(Object.keys(opts).sort(), [ 291 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 292 | ]); 293 | 294 | cb(null, { 295 | statusCode: 200, 296 | httpVersion: '1.1', 297 | headers: {}, 298 | body: { 299 | 'baz': 'filter-this' 300 | } 301 | }); 302 | } 303 | }); 304 | 305 | var treq = { 306 | url: 'http://localhost:8000/', 307 | method: 'GET', 308 | headers: {}, 309 | body: { 310 | 'moo': 'filter-this' 311 | } 312 | }; 313 | 314 | request(treq, { 315 | requestSchema: requestSchema, 316 | responseSchema: responseSchema, 317 | resource: '.read', 318 | filterRequest: true, 319 | filterResponse: true, 320 | validateRequest: false, 321 | validateResponse: true 322 | }, onResponse); 323 | 324 | function onResponse(err, tres) { 325 | assert.ifError(err); 326 | 327 | assert.equal(tres.statusCode, 200); 328 | assert.equal(tres.httpVersion, '1.1'); 329 | assert.deepEqual(tres.headers, {}); 330 | assert.deepEqual(tres.body, {}); 331 | assert.deepEqual(Object.keys(tres).sort(), [ 332 | 'body', 'headers', 'httpVersion', 'statusCode' 333 | ]); 334 | 335 | assert.end(); 336 | } 337 | }); 338 | 339 | test('can make request without filtering request', function t(assert) { 340 | var request = TypedRequestClient({ 341 | clientName: 'demo', 342 | statsd: fakeStatsd, 343 | request: function r(opts, cb) { 344 | assert.equal(opts.url, 'http://localhost:8000/'); 345 | assert.equal(opts.method, 'GET'); 346 | assert.deepEqual(opts.headers, {}); 347 | assert.equal(opts.timeout, 30000); 348 | assert.deepEqual(opts.json, { 349 | 'foo': 'bar', 350 | 'moo': 'dont-filter-this' 351 | }); 352 | 353 | assert.deepEqual(Object.keys(opts).sort(), [ 354 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 355 | ]); 356 | 357 | cb(null, { 358 | statusCode: 200, 359 | httpVersion: '1.1', 360 | headers: {}, 361 | body: {} 362 | }); 363 | } 364 | }); 365 | 366 | var treq = { 367 | url: 'http://localhost:8000/', 368 | method: 'GET', 369 | headers: {}, 370 | body: { 371 | 'foo': 'bar', 372 | 'moo': 'dont-filter-this' 373 | } 374 | }; 375 | 376 | request(treq, { 377 | requestSchema: requestSchema, 378 | responseSchema: responseSchema, 379 | resource: '.read', 380 | filterRequest: false, 381 | filterResponse: true, 382 | validateRequest: false, 383 | validateResponse: true 384 | }, onResponse); 385 | 386 | function onResponse(err, tres) { 387 | assert.ifError(err); 388 | 389 | assert.equal(tres.statusCode, 200); 390 | assert.equal(tres.httpVersion, '1.1'); 391 | assert.deepEqual(tres.headers, {}); 392 | assert.deepEqual(tres.body, {}); 393 | assert.deepEqual(Object.keys(tres), [ 394 | 'statusCode', 'httpVersion', 'headers', 'body' 395 | ]); 396 | 397 | assert.end(); 398 | } 399 | }); 400 | 401 | test('can make request without validating response', function t(assert) { 402 | var request = TypedRequestClient({ 403 | clientName: 'demo', 404 | statsd: fakeStatsd, 405 | request: function r(opts, cb) { 406 | assert.equal(opts.url, 'http://localhost:8000/'); 407 | assert.equal(opts.method, 'GET'); 408 | assert.deepEqual(opts.headers, {}); 409 | assert.equal(opts.timeout, 30000); 410 | assert.deepEqual(opts.json, { 411 | 'foo': 'bar' 412 | }); 413 | 414 | assert.deepEqual(Object.keys(opts).sort(), [ 415 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 416 | ]); 417 | 418 | cb(null, { 419 | statusCode: 200, 420 | httpVersion: '1.1', 421 | headers: {}, 422 | body: { 423 | 'test': {'message': 'test should be a string'}, 424 | 'moo': 'filter-this' 425 | } 426 | }); 427 | } 428 | }); 429 | 430 | var treq = { 431 | url: 'http://localhost:8000/', 432 | method: 'GET', 433 | headers: {}, 434 | body: { 435 | 'foo': 'bar' 436 | } 437 | }; 438 | 439 | request(treq, { 440 | requestSchema: requestSchema, 441 | responseSchema: responseSchema, 442 | resource: '.read', 443 | filterRequest: true, 444 | filterResponse: true, 445 | validateRequest: true, 446 | validateResponse: false 447 | }, onResponse); 448 | 449 | function onResponse(err, tres) { 450 | assert.ifError(err); 451 | 452 | assert.equal(tres.statusCode, 200); 453 | assert.equal(tres.httpVersion, '1.1'); 454 | assert.deepEqual(tres.headers, {}); 455 | assert.deepEqual(tres.body, { 456 | 'test': { 457 | 'message': 'test should be a string' 458 | } 459 | }); 460 | assert.deepEqual(Object.keys(tres).sort(), [ 461 | 'body', 'headers', 'httpVersion', 'statusCode' 462 | ]); 463 | 464 | assert.end(); 465 | } 466 | }); 467 | 468 | test('can make request without filtering response', function t(assert) { 469 | var request = TypedRequestClient({ 470 | clientName: 'demo', 471 | statsd: fakeStatsd, 472 | request: function r(opts, cb) { 473 | assert.equal(opts.url, 'http://localhost:8000/'); 474 | assert.equal(opts.method, 'GET'); 475 | assert.deepEqual(opts.headers, {}); 476 | assert.equal(opts.timeout, 30000); 477 | assert.deepEqual(opts.json, { 478 | 'foo': 'bar' 479 | }); 480 | 481 | assert.deepEqual(Object.keys(opts).sort(), [ 482 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 483 | ]); 484 | 485 | cb(null, { 486 | statusCode: 200, 487 | httpVersion: '1.1', 488 | headers: {}, 489 | body: { 490 | 'moo': 'invalid-property' 491 | } 492 | }); 493 | } 494 | }); 495 | 496 | var treq = { 497 | url: 'http://localhost:8000/', 498 | method: 'GET', 499 | headers: {}, 500 | body: { 501 | 'foo': 'bar' 502 | } 503 | }; 504 | 505 | request(treq, { 506 | requestSchema: requestSchema, 507 | responseSchema: responseSchema, 508 | resource: '.read', 509 | filterRequest: true, 510 | filterResponse: false, 511 | validateRequest: true, 512 | validateResponse: false 513 | }, onResponse); 514 | 515 | function onResponse(err, tres) { 516 | assert.ifError(err); 517 | 518 | assert.equal(tres.statusCode, 200); 519 | assert.equal(tres.httpVersion, '1.1'); 520 | assert.deepEqual(tres.headers, {}); 521 | assert.deepEqual(tres.body, { 522 | 'moo': 'invalid-property' 523 | }); 524 | assert.deepEqual(Object.keys(tres).sort(), [ 525 | 'body', 'headers', 'httpVersion', 'statusCode' 526 | ]); 527 | 528 | assert.end(); 529 | } 530 | }); 531 | 532 | test('can make request with prober enabled', function t(assert) { 533 | fakeStatsd.stats = []; 534 | var request = TypedRequestClient({ 535 | clientName: 'demo', 536 | statsd: fakeStatsd, 537 | // Prober should be enabled 538 | request: function r(opts, cb) { 539 | assert.equal(opts.url, 'http://localhost:8000/'); 540 | assert.equal(opts.method, 'GET'); 541 | assert.deepEqual(opts.headers, {}); 542 | assert.equal(opts.timeout, 30000); 543 | assert.deepEqual(opts.json, { 544 | 'foo': 'bar' 545 | }); 546 | 547 | assert.deepEqual(Object.keys(opts).sort(), [ 548 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 549 | ]); 550 | 551 | cb(null, { 552 | statusCode: 200, 553 | httpVersion: '1.1', 554 | headers: {}, 555 | body: { 556 | 'moo': 'invalid-property' 557 | } 558 | }); 559 | } 560 | }); 561 | 562 | var treq = { 563 | url: 'http://localhost:8000/', 564 | method: 'GET', 565 | headers: {}, 566 | body: { 567 | 'foo': 'bar' 568 | } 569 | }; 570 | 571 | request(treq, { 572 | requestSchema: requestSchema, 573 | responseSchema: responseSchema, 574 | resource: '.read', 575 | filterRequest: true, 576 | filterResponse: false, 577 | validateRequest: true, 578 | validateResponse: false 579 | }, onResponse); 580 | 581 | function onResponse(err, tres) { 582 | assert.ifError(err); 583 | 584 | assert.equal(tres.statusCode, 200); 585 | assert.equal(tres.httpVersion, '1.1'); 586 | assert.deepEqual(tres.headers, {}); 587 | assert.deepEqual(tres.body, { 588 | 'moo': 'invalid-property' 589 | }); 590 | assert.deepEqual(Object.keys(tres).sort(), [ 591 | 'body', 'headers', 'httpVersion', 'statusCode' 592 | ]); 593 | var statsed = false; 594 | fakeStatsd.stats.forEach(function f(stat) { 595 | if (stat.key.indexOf('prober') !== -1) { 596 | statsed = true; 597 | } 598 | }); 599 | assert.ok(statsed, 'Prober fired'); 600 | 601 | assert.end(); 602 | } 603 | }); 604 | 605 | test('can make request without prober enabled', function t(assert) { 606 | fakeStatsd.stats = []; 607 | var request = TypedRequestClient({ 608 | clientName: 'demo', 609 | statsd: fakeStatsd, 610 | proberEnabled: false, 611 | request: function r(opts, cb) { 612 | assert.equal(opts.url, 'http://localhost:8000/'); 613 | assert.equal(opts.method, 'GET'); 614 | assert.deepEqual(opts.headers, {}); 615 | assert.equal(opts.timeout, 30000); 616 | assert.deepEqual(opts.json, { 617 | 'foo': 'bar' 618 | }); 619 | 620 | assert.deepEqual(Object.keys(opts).sort(), [ 621 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 622 | ]); 623 | 624 | cb(null, { 625 | statusCode: 200, 626 | httpVersion: '1.1', 627 | headers: {}, 628 | body: { 629 | 'moo': 'invalid-property' 630 | } 631 | }); 632 | } 633 | }); 634 | 635 | var treq = { 636 | url: 'http://localhost:8000/', 637 | method: 'GET', 638 | headers: {}, 639 | body: { 640 | 'foo': 'bar' 641 | } 642 | }; 643 | 644 | request(treq, { 645 | requestSchema: requestSchema, 646 | responseSchema: responseSchema, 647 | resource: '.read', 648 | filterRequest: true, 649 | filterResponse: false, 650 | validateRequest: true, 651 | validateResponse: false 652 | }, onResponse); 653 | 654 | function onResponse(err, tres) { 655 | assert.ifError(err); 656 | 657 | assert.equal(tres.statusCode, 200); 658 | assert.equal(tres.httpVersion, '1.1'); 659 | assert.deepEqual(tres.headers, {}); 660 | assert.deepEqual(tres.body, { 661 | 'moo': 'invalid-property' 662 | }); 663 | assert.deepEqual(Object.keys(tres).sort(), [ 664 | 'body', 'headers', 'httpVersion', 'statusCode' 665 | ]); 666 | 667 | fakeStatsd.stats.forEach(function f(stat) { 668 | if (stat.key.indexOf('prober') !== -1) { 669 | assert.fail('Prober fired a statsd when it should be disabled'); 670 | } 671 | }); 672 | 673 | assert.end(); 674 | } 675 | }); 676 | 677 | test('can disable and enable prober after instantiation', function t(assert) { 678 | fakeStatsd.stats = []; 679 | var request = TypedRequestClient({ 680 | clientName: 'demo', 681 | statsd: fakeStatsd, 682 | proberEnabled: true, 683 | request: function r(opts, cb) { 684 | assert.equal(opts.url, 'http://localhost:8000/'); 685 | assert.equal(opts.method, 'GET'); 686 | assert.deepEqual(opts.headers, {}); 687 | assert.equal(opts.timeout, 30000); 688 | assert.deepEqual(opts.json, { 689 | 'foo': 'bar' 690 | }); 691 | 692 | assert.deepEqual(Object.keys(opts).sort(), [ 693 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 694 | ]); 695 | 696 | cb(null, { 697 | statusCode: 200, 698 | httpVersion: '1.1', 699 | headers: {}, 700 | body: { 701 | 'moo': 'invalid-property' 702 | } 703 | }); 704 | } 705 | }); 706 | 707 | var treq = { 708 | url: 'http://localhost:8000/', 709 | method: 'GET', 710 | headers: {}, 711 | body: { 712 | 'foo': 'bar' 713 | } 714 | }; 715 | 716 | [false, true].forEach(function proberEnabled(enabled) { 717 | request.setProberEnabled(enabled); 718 | request(treq, { 719 | requestSchema: requestSchema, 720 | responseSchema: responseSchema, 721 | resource: '.read', 722 | filterRequest: true, 723 | filterResponse: false, 724 | validateRequest: true, 725 | validateResponse: false 726 | }, onResponse); 727 | 728 | function onResponse(err, tres) { 729 | assert.ifError(err); 730 | 731 | assert.equal(tres.statusCode, 200); 732 | assert.equal(tres.httpVersion, '1.1'); 733 | assert.deepEqual(tres.headers, {}); 734 | assert.deepEqual(tres.body, { 735 | 'moo': 'invalid-property' 736 | }); 737 | assert.deepEqual(Object.keys(tres).sort(), [ 738 | 'body', 'headers', 'httpVersion', 'statusCode' 739 | ]); 740 | 741 | var statsed = false; 742 | fakeStatsd.stats.forEach(function f(stat) { 743 | if (stat.key.indexOf('prober') !== -1) { 744 | statsed = true; 745 | if (!enabled) { 746 | assert.fail( 747 | 'Prober fired a statsd when it should be disabled'); 748 | } 749 | } 750 | }); 751 | if (enabled) { 752 | assert.ok(statsed, 'Prober fired'); 753 | } 754 | } 755 | }); 756 | 757 | assert.end(); 758 | }); 759 | 760 | test('can return error validating response', function t(assert) { 761 | var request = TypedRequestClient({ 762 | clientName: 'demo', 763 | statsd: fakeStatsd, 764 | request: function r(opts, cb) { 765 | assert.equal(opts.url, 'http://localhost:8000/'); 766 | assert.equal(opts.method, 'GET'); 767 | assert.deepEqual(opts.headers, {}); 768 | assert.equal(opts.timeout, 30000); 769 | assert.deepEqual(opts.json, { 770 | 'foo': 'bar' 771 | }); 772 | 773 | assert.deepEqual(Object.keys(opts).sort(), [ 774 | 'headers', 'json', 'method', 'timeout', 'transformUrlFn', 'url' 775 | ]); 776 | 777 | cb(null, { 778 | statusCode: 500, 779 | httpVersion: '1.1', 780 | headers: {}, 781 | body: { 782 | 'message': 'Internal service error' 783 | } 784 | }); 785 | } 786 | }); 787 | 788 | var treq = { 789 | url: 'http://localhost:8000/', 790 | method: 'GET', 791 | headers: {}, 792 | body: { 793 | 'foo': 'bar' 794 | } 795 | }; 796 | 797 | request(treq, { 798 | requestSchema: requestSchema, 799 | responseSchema: responseSchema, 800 | resource: '.read', 801 | filterRequest: true, 802 | filterResponse: true, 803 | validateRequest: true, 804 | validateResponse: false 805 | }, onResponse); 806 | 807 | function onResponse(err, tres) { 808 | assert.ifError(err); 809 | 810 | assert.equal(tres.statusCode, 500); 811 | assert.equal(tres.httpVersion, '1.1'); 812 | assert.deepEqual(tres.headers, {}); 813 | assert.deepEqual(tres.body, { 814 | 'message': 'Internal service error' 815 | }); 816 | assert.deepEqual(Object.keys(tres).sort(), [ 817 | 'body', 'headers', 'httpVersion', 'statusCode' 818 | ]); 819 | 820 | assert.end(); 821 | } 822 | }); 823 | 824 | test('request error', function t(assert) { 825 | var request = TypedRequestClient({ 826 | clientName: 'demo', 827 | statsd: fakeStatsd, 828 | request: function r(opts, cb) { 829 | assert.fail('request called'); 830 | } 831 | }); 832 | 833 | var treq = { 834 | url: 'http://localhost:8000/', 835 | method: 'GET', 836 | headers: {} 837 | }; 838 | 839 | request(treq, { 840 | requestSchema: requestSchema, 841 | responseSchema: responseSchema, 842 | resource: '.read', 843 | filterRequest: true, 844 | filterResponse: true, 845 | validateRequest: true, 846 | validateResponse: true 847 | }, onResponse); 848 | 849 | function onResponse(err, tres) { 850 | assert.ok(err); 851 | 852 | assert.equal(err.message, 'Required'); 853 | assert.equal(err.attribute, 'body'); 854 | 855 | assert.end(); 856 | } 857 | }); 858 | -------------------------------------------------------------------------------- /test/validate-shape.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var test = require('tape'); 4 | 5 | var ValidateShape = require('../validate-shape.js'); 6 | 7 | test('can validate object', function t(assert) { 8 | var shape = new ValidateShape(); 9 | var obj = { 10 | foo: 'bar' 11 | }; 12 | var result = shape.validate(obj, { 13 | type: 'object', 14 | properties: { 15 | foo: {type: 'string'} 16 | }, 17 | required: ['foo'] 18 | }); 19 | 20 | assert.strictEqual(result, null); 21 | 22 | assert.end(); 23 | }); 24 | 25 | test('validation error', function t(assert) { 26 | var shape = new ValidateShape(); 27 | var obj = {}; 28 | var err = shape.validate(obj, { 29 | type: 'object', 30 | properties: { 31 | foo: {type: 'string'} 32 | }, 33 | required: ['foo'] 34 | }); 35 | 36 | assert.equal(err.message, 'Required'); 37 | assert.equal(err.attribute, 'foo'); 38 | assert.equal(err.errors[0].message, 'Required'); 39 | assert.equal(err.errors[0].attribute, 'foo'); 40 | 41 | assert.end(); 42 | }); 43 | -------------------------------------------------------------------------------- /test/write-stats.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var EventEmitter = require('events').EventEmitter; 4 | var test = require('tape'); 5 | var writeStats = require('../write-stats.js'); 6 | 7 | function assertIncrement(assert, testData) { 8 | emitStat(testData, { 9 | increment: function incrementIt(actual) { 10 | assert.equal(actual, testData.statKey); 11 | } 12 | }); 13 | } 14 | 15 | function assertTiming(assert, testData) { 16 | emitStat(testData, { 17 | timing: function timeIt(actual) { 18 | assert.equal(actual, testData.statKey); 19 | } 20 | }); 21 | } 22 | 23 | function createTestData(resource, statKeyPart) { 24 | return { 25 | makeRequest: { 26 | args: [resource], 27 | eventName: 'makeRequest', 28 | statKey: 'typed-request-client.myclient.' + 29 | (statKeyPart || resource) + '.request' 30 | }, 31 | requestTime: { 32 | args: [resource, 1], 33 | eventName: 'requestTime', 34 | statKey: 'typed-request-client.myclient.' + 35 | (statKeyPart || resource) + '.request-time' 36 | }, 37 | statusCode: { 38 | args: [resource, 200], 39 | eventName: 'statusCode', 40 | statKey: 'typed-request-client.myclient.' + 41 | (statKeyPart || resource) + '.statusCode.200' 42 | }, 43 | totalTime: { 44 | args: [resource], 45 | eventName: 'totalTime', 46 | statKey: 'typed-request-client.myclient.' + 47 | (statKeyPart || resource) + '.total-time' 48 | } 49 | }; 50 | } 51 | 52 | function emitStat(testData, statsd) { 53 | var emitter = new EventEmitter(); 54 | 55 | writeStats(emitter, { 56 | clientName: 'myclient', 57 | statsd: statsd 58 | }); 59 | 60 | testData.args.unshift(testData.eventName); 61 | 62 | emitter.emit.apply(emitter, testData.args); 63 | } 64 | 65 | test('writes stats with dots', function t(assert) { 66 | assert.plan(4); 67 | 68 | var testData = createTestData('myresource'); 69 | assertIncrement(assert, testData.makeRequest); 70 | assertIncrement(assert, testData.statusCode); 71 | assertTiming(assert, testData.requestTime); 72 | assertTiming(assert, testData.totalTime); 73 | assert.end(); 74 | }); 75 | 76 | test('writes stats without curlies', function t(assert) { 77 | assert.plan(4); 78 | 79 | var testData = createTestData('{myresource}', 'myresource'); 80 | assertIncrement(assert, testData.makeRequest); 81 | assertIncrement(assert, testData.statusCode); 82 | assertTiming(assert, testData.requestTime); 83 | assertTiming(assert, testData.totalTime); 84 | assert.end(); 85 | }); 86 | -------------------------------------------------------------------------------- /validate-shape.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var JaySchema = require('jayschema'); 4 | var normalize = require('jayschema-error-messages'); 5 | var ValidationError = require('error/validation'); 6 | 7 | module.exports = ValidateShape; 8 | 9 | function ValidateShape() { 10 | this.js = new JaySchema(); 11 | } 12 | 13 | ValidateShape.prototype.validate = validateShape; 14 | 15 | function validateShape(shape, schema) { 16 | var validationErrors = this.js.validate(shape, schema); 17 | 18 | if (validationErrors.length > 0) { 19 | var fields = normalize(validationErrors).fields; 20 | var errors = buildErrors(fields); 21 | var err = ValidationError(errors); 22 | err.attribute = errors[0].attribute; 23 | err.original = validationErrors; 24 | return err; 25 | } 26 | 27 | return null; 28 | } 29 | 30 | /* ```jsig 31 | type FieldName : String 32 | type Message : String 33 | 34 | buildErrors : (Object) => 35 | Array 36 | ``` */ 37 | function buildErrors(fields) { 38 | var fieldNames = Object.keys(fields); 39 | 40 | return flatten(fieldNames.map(function toErrors(fieldName) { 41 | var messages = fields[fieldName]; 42 | 43 | return messages.map(function toError(message) { 44 | var err = new Error(message); 45 | err.attribute = fieldName; 46 | return err; 47 | }); 48 | })); 49 | } 50 | 51 | function flatten(arr) { 52 | var list = []; 53 | return list.concat.apply(list, arr); 54 | } 55 | -------------------------------------------------------------------------------- /write-stats.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = writeStats; 4 | 5 | function writeStats(emitter, options) { 6 | var clientName = options.clientName; 7 | var statsd = options.statsd; 8 | 9 | emitter.on('makeRequest', onMakeRequest); 10 | emitter.on('requestTime', onRequestTime); 11 | emitter.on('statusCode', onStatusCode); 12 | emitter.on('requestResult', onRequestResult); 13 | emitter.on('totalTime', onTotalTime); 14 | 15 | function sanitize(statStr) { 16 | return statStr.replace(/{|}/g, ''); 17 | } 18 | 19 | function onMakeRequest(resource) { 20 | statsd.increment(sanitize('typed-request-client.' + clientName + 21 | '.' + resource + '.request')); 22 | } 23 | 24 | function onRequestTime(resource, delta) { 25 | statsd.timing(sanitize('typed-request-client.' + clientName + 26 | '.' + resource + '.request-time'), delta); 27 | } 28 | 29 | function onStatusCode(resource, statusCode) { 30 | statsd.increment(sanitize('typed-request-client.' + clientName + 31 | '.' + resource + '.statusCode.' + statusCode)); 32 | } 33 | 34 | function onRequestResult(resource, requestResultType, errorCode) { 35 | var stat = 'typed-request-client.' + clientName + 36 | '.' + resource + '.' + requestResultType; 37 | if (errorCode) { 38 | stat += '.' + errorCode; 39 | } 40 | statsd.increment(sanitize(stat)); 41 | } 42 | 43 | function onTotalTime(resource, delta) { 44 | statsd.timing(sanitize('typed-request-client.' + clientName + 45 | '.' + resource + '.total-time'), delta); 46 | } 47 | } 48 | --------------------------------------------------------------------------------