UNPKG

3.97 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 return this.req.connection.remoteAddress || '0.0.0.0';
45 },
46
47 method() {
48 return this.req.method;
49 },
50
51 params() {
52 return util.extendValues({}, this.path_params, this.query_params, this.body_params);
53 },
54
55 path_info() {
56 return this.req.url.split('?')[0].split('/').filter(x => x);
57 },
58
59 path_params() {
60 return util.extendValues({}, this.req.params || {});
61 },
62
63 body_params() {
64 return util.extendValues({}, this.req.body || {});
65 },
66
67 request_path() {
68 return this.req.url.split('?')[0];
69 },
70
71 query_string() {
72 return qs.stringify(this.req.query).replace(/=$/, '');
73 },
74
75 query_params() {
76 return this.req.query || {};
77 },
78
79 accept_charset(charsets) {
80 return _accepts(this.req).charset(charsets);
81 },
82 accept_charsets() {
83 return _accepts(this.req).charsets();
84 },
85
86 accept_encoding(encodings) {
87 return _accepts(this.req).encoding(encodings);
88 },
89 accept_encodings() {
90 return _accepts(this.req).encodings();
91 },
92
93 accept_language(languages) {
94 return _accepts(this.req).language(languages);
95 },
96 accept_languages() {
97 return _accepts(this.req).languages();
98 },
99
100 accept_type(types) {
101 return _accepts(this.req).type(types);
102 },
103 accept_types() {
104 return _accepts(this.req).types();
105 },
106 },
107 methods: {
108 has_type() {
109 return typeIs(this.req, Array.prototype.slice.call(arguments));
110 },
111
112 get_req_header(name, defvalue) {
113 /* istanbul ignore else */
114 if (!(name && typeof name === 'string')) {
115 throw new Error(`Invalid req_header: '${name}'`);
116 }
117
118 /* istanbul ignore else */
119 if (typeof this.req.headers[name] === 'undefined') {
120 return defvalue;
121 }
122
123 return this.req.headers[name];
124 },
125
126 put_req_header(name, value) {
127 /* istanbul ignore else */
128 if (!name || typeof name !== 'string') {
129 throw new Error(`Invalid req_header: '${name}' => '${value}'`);
130 }
131
132 this.req.headers[name] = value;
133
134 return this;
135 },
136
137 delete_req_header(name) {
138 /* istanbul ignore else */
139 if (!(name && typeof name === 'string')) {
140 throw new Error(`Invalid req_header: '${name}'`);
141 }
142
143 delete this.req.headers[name];
144
145 return this;
146 },
147 },
148 };
149 },
150 });
151};