|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +var debug = require('debug')('httpsnippet'); |
3 | 4 | var mapper = require('./mapper'); |
| 5 | +var targets = require('./targets'); |
4 | 6 | var url = require('url'); |
| 7 | +var util = require('util'); |
5 | 8 |
|
6 | | -var targets = require('./targets'); |
| 9 | +// constructor |
| 10 | +var HTTPSnippet = function (req, lang) { |
| 11 | + this.source = util._extend({}, req); |
7 | 12 |
|
8 | | -module.exports = function (req, lang, opts) { |
9 | 13 | // construct query string object |
10 | | - req.queryObj = {}; |
11 | | - req.headersObj = {}; |
| 14 | + this.source.queryObj = {}; |
| 15 | + this.source.headersObj = {}; |
| 16 | + |
| 17 | + if (this.source.url === undefined) { |
| 18 | + throw new Error('a request url is required'); |
| 19 | + } |
12 | 20 |
|
13 | 21 | // construct query objects |
14 | | - if (req.queryString && req.queryString.length) { |
15 | | - req.queryString.map(mapper(req.queryObj)); |
| 22 | + if (this.source.queryString && this.source.queryString.length) { |
| 23 | + debug('queryString found, constructing queryString pair map'); |
| 24 | + |
| 25 | + this.source.queryString.map(mapper(this.source.queryObj)); |
16 | 26 | } |
17 | 27 |
|
18 | 28 | // construct headers objects |
19 | | - if (req.headers && req.headers.length) { |
20 | | - req.headers.map(mapper(req.headersObj)); |
| 29 | + if (this.source.headers && this.source.headers.length) { |
| 30 | + debug('headers found, constructing header pair map'); |
| 31 | + |
| 32 | + this.source.headers.map(mapper(this.source.headersObj)); |
21 | 33 | } |
22 | 34 |
|
23 | | - // deconstruct and reset the uri |
24 | | - req.uriObj = url.parse(req.url); |
| 35 | + // deconstruct the uri |
| 36 | + this.source.uriObj = url.parse(this.source.url, true, true); |
| 37 | + |
| 38 | + // search property is evil |
| 39 | + // prevents re-construction with new query values |
| 40 | + this.source.uriObj.search = null; |
| 41 | + |
| 42 | + // merge all possible queryString values |
| 43 | + this.source.queryString = util._extend(this.source.uriObj.query, this.source.queryObj); |
| 44 | + |
| 45 | + // update the query object |
| 46 | + this.source.uriObj.query = this.source.queryString; |
| 47 | + |
| 48 | + // construct a full url |
| 49 | + this.source.fullUrl = url.format(this.source.uriObj); |
| 50 | + |
| 51 | + // reset queryString in url |
| 52 | + this.source.uriObj.query = null; |
| 53 | + |
| 54 | + // keep the base url clean of queryString |
| 55 | + this.source.url = url.format(this.source.uriObj); |
| 56 | +}; |
| 57 | + |
| 58 | +HTTPSnippet.prototype.getSource = function () { |
| 59 | + return this.source; |
| 60 | +}; |
| 61 | + |
| 62 | +// add each target as a prototype |
| 63 | +for (var lang in targets) { |
| 64 | + HTTPSnippet.prototype[lang] = targets[lang]; |
| 65 | +} |
| 66 | + |
| 67 | +// exports |
25 | 68 |
|
26 | | - // reset the query string |
27 | | - req.uriObj.query = req.queryObj; |
28 | | - req.url = url.format(req.uriObj); |
| 69 | +module.exports = HTTPSnippet; |
29 | 70 |
|
30 | | - return targets[lang].call(lang, req, opts); |
| 71 | +module.exports._targets = function () { |
| 72 | + return Object.keys(targets); |
31 | 73 | }; |
0 commit comments