UNPKG

3.95 kBJavaScriptView Raw
1'use strict';
2
3const qs = require('qs');
4const typeIs = require('type-is');
5const accepts = require('accepts');
6
7module.exports = (Grown, util) => {
8 return Grown('Conn.Request', {
9 $mixins() {
10 function _accepts(req) {
11 /* istanbul ignore else */
12 if (!_accepts.fn) {
13 _accepts.fn = accepts(req);
14 }
15
16 return _accepts.fn;
17 }
18
19 return {
20 props: {
21 req_headers() {
22 return this.req.headers;
23 },
24
25 // ajax
26 is_xhr() {
27 return this.req.headers['x-requested-with'] === 'XMLHttpRequest';
28 },
29
30 is_json() {
31 return typeIs.is(this.req.headers['content-type'], ['json']) === 'json';
32 },
33
34 // current connection
35 host() {
36 return (this.req.headers.host && this.req.headers.host.split(':')[0]) || process.env.HOST || '0.0.0.0';
37 },
38
39 port() {
40 return (this.req.headers.host && this.req.headers.host.split(':')[1]) || process.env.PORT || '80';
41 },
42
43 remote_ip() {
44 // FIXME:
45 return '0.0.0.0';
46 },
47
48 method() {
49 return this.req.method;
50 },
51
52 params() {
53 return util.extendValues({}, this.path_params, this.query_params, this.body_params);
54 },
55
56 path_info() {
57 return this.req.url.split('?')[0].split('/').filter(x => x);
58 },
59
60 path_params() {
61 return util.extendValues({}, this.req.params || {});
62 },
63
64 body_params() {
65 return util.extendValues({}, this.req.body || {});
66 },
67
68 request_path() {
69 return this.req.url.split('?')[0];
70 },
71
72 query_string() {
73 return qs.stringify(this.req.query).replace(/=$/, '');
74 },
75
76 query_params() {
77 return this.req.query || {};
78 },
79
80 accept_charset(charsets) {
81 return _accepts(this.req).charset(charsets);
82 },
83 accept_charsets() {
84 return _accepts(this.req).charsets();
85 },
86
87 accept_encoding(encodings) {
88 return _accepts(this.req).encoding(encodings);
89 },
90 accept_encodings() {
91 return _accepts(this.req).encodings();
92 },
93
94 accept_language(languages) {
95 return _accepts(this.req).language(languages);
96 },
97 accept_languages() {
98 return _accepts(this.req).languages();
99 },
100
101 accept_type(types) {
102 return _accepts(this.req).type(types);
103 },
104 accept_types() {
105 return _accepts(this.req).types();
106 },
107 },
108 methods: {
109 has_type() {
110 return typeIs(this.req, Array.prototype.slice.call(arguments));
111 },
112
113 get_req_header(name, defvalue) {
114 /* istanbul ignore else */
115 if (!(name && typeof name === 'string')) {
116 throw new Error(`Invalid req_header: '${name}'`);
117 }
118
119 /* istanbul ignore else */
120 if (typeof this.req.headers[name] === 'undefined') {
121 return defvalue;
122 }
123
124 return this.req.headers[name];
125 },
126
127 put_req_header(name, value) {
128 /* istanbul ignore else */
129 if (!name || typeof name !== 'string') {
130 throw new Error(`Invalid req_header: '${name}' => '${value}'`);
131 }
132
133 this.req.headers[name] = value;
134
135 return this;
136 },
137
138 delete_req_header(name) {
139 /* istanbul ignore else */
140 if (!(name && typeof name === 'string')) {
141 throw new Error(`Invalid req_header: '${name}'`);
142 }
143
144 delete this.req.headers[name];
145
146 return this;
147 },
148 },
149 };
150 },
151 });
152};