UNPKG

3.91 kBJavaScriptView Raw
1'use strict';
2
3const typeIs = require('type-is');
4const accepts = require('accepts');
5
6module.exports = (Grown, util) => {
7 return Grown('Conn.Request', {
8 $mixins() {
9 function _accepts(req) {
10 /* istanbul ignore else */
11 if (!_accepts.fn) {
12 _accepts.fn = accepts(req);
13 }
14
15 return _accepts.fn;
16 }
17
18 return {
19 props: {
20 req_headers() {
21 return this.req.headers;
22 },
23
24 // ajax
25 is_xhr() {
26 return this.req.headers['x-requested-with'] === 'XMLHttpRequest';
27 },
28
29 is_json() {
30 return typeIs.is(this.req.headers['content-type'], ['json']) === 'json';
31 },
32
33 // current connection
34 host() {
35 return (this.req.headers.host && this.req.headers.host.split(':')[0]) || process.env.HOST || '0.0.0.0';
36 },
37
38 port() {
39 return (this.req.headers.host && this.req.headers.host.split(':')[1]) || process.env.PORT || '80';
40 },
41
42 remote_ip() {
43 // FIXME:
44 return '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 this.req.url.split('?')[1] || '';
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};