nodester 0.2.5 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +13 -0
- package/Readme.md +12 -4
- package/lib/application/index.js +61 -53
- package/lib/body/extract.js +10 -10
- package/lib/controllers/methods/index.js +29 -14
- package/lib/controllers/mixins/index.js +13 -13
- package/lib/database/connection.js +78 -17
- package/lib/database/migration.js +9 -3
- package/lib/errors/CustomError.js +7 -2
- package/lib/errors/NodesterError.js +10 -2
- package/lib/errors/NodesterQueryError.js +9 -2
- package/lib/errors/index.js +2 -2
- package/lib/facades/methods/index.js +15 -15
- package/lib/facades/mixins/index.js +12 -12
- package/lib/factories/responses/html.js +20 -9
- package/lib/factories/responses/rest.js +21 -19
- package/lib/http/codes/descriptions.js +4 -3
- package/lib/http/codes/index.js +3 -2
- package/lib/http/codes/symbols.js +3 -2
- package/lib/http/request/index.js +20 -250
- package/lib/http/request/utils.js +4 -4
- package/lib/http/response/headers.js +16 -19
- package/lib/http/response/index.js +25 -28
- package/lib/http/response/utils.js +7 -6
- package/lib/loggers/console.js +3 -4
- package/lib/loggers/dev.js +3 -4
- package/lib/middlewares/404/index.js +38 -0
- package/lib/middlewares/SearchParams/index.js +3 -3
- package/lib/middlewares/cookies/index.js +2 -2
- package/lib/middlewares/etag/index.js +10 -8
- package/lib/middlewares/formidable/index.js +2 -2
- package/lib/middlewares/ql/sequelize/index.js +2 -2
- package/lib/middlewares/ql/sequelize/interpreter/ModelsTree.js +25 -4
- package/lib/middlewares/ql/sequelize/interpreter/QueryLexer.js +30 -18
- package/lib/middlewares/render/index.js +3 -3
- package/lib/models/associate.js +3 -2
- package/lib/models/define.js +37 -18
- package/lib/models/mixins.js +9 -9
- package/lib/query/traverse.js +40 -32
- package/lib/router/handlers.util.js +5 -5
- package/lib/router/index.js +84 -70
- package/lib/router/markers.js +5 -5
- package/lib/router/route.js +18 -19
- package/lib/router/routes.util.js +4 -4
- package/lib/router/utils.js +2 -2
- package/lib/stacks/MarkersStack.js +11 -9
- package/lib/stacks/MiddlewaresStack.js +25 -21
- package/lib/structures/Enum.js +8 -2
- package/lib/structures/Filter.js +31 -29
- package/lib/structures/Params.js +3 -3
- package/lib/tools/nql.tool.js +10 -2
- package/lib/tools/sql.tool.js +19 -2
- package/lib/utils/json.util.js +28 -27
- package/lib/utils/models.js +3 -2
- package/lib/utils/objects.util.js +10 -11
- package/lib/utils/path.util.js +9 -3
- package/lib/utils/sanitizations.util.js +3 -2
- package/lib/utils/types.util.js +11 -4
- package/lib/validators/arguments.js +28 -9
- package/lib/validators/dates.js +4 -5
- package/lib/validators/numbers.js +4 -4
- package/package.json +43 -39
- package/tests/nql.test.js +20 -7
- /package/lib/constants/{Operations.js → Operators.js} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
|
|
8
8
|
const { IncomingMessage } = require('http');
|
|
9
9
|
|
|
10
|
-
const accepts = require('accepts');
|
|
11
10
|
const isIP = require('net').isIP;
|
|
12
11
|
const typeis = require('type-is');
|
|
13
12
|
const fresh = require('fresh');
|
|
@@ -28,8 +27,7 @@ module.exports = req;
|
|
|
28
27
|
* The `Referrer` header field is special-cased,
|
|
29
28
|
* both `Referrer` and `Referer` are interchangeable.
|
|
30
29
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
30
|
+
* @example
|
|
33
31
|
* req.get('Content-Type');
|
|
34
32
|
* // => "text/plain"
|
|
35
33
|
*
|
|
@@ -39,13 +37,12 @@ module.exports = req;
|
|
|
39
37
|
* req.get('Something');
|
|
40
38
|
* // => undefined
|
|
41
39
|
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @param {String} name
|
|
40
|
+
* @param {string} name
|
|
45
41
|
*
|
|
46
|
-
* @return {
|
|
47
|
-
*
|
|
48
|
-
* @
|
|
42
|
+
* @return {string}
|
|
43
|
+
*
|
|
44
|
+
* @alias req.header
|
|
45
|
+
* @access public
|
|
49
46
|
*/
|
|
50
47
|
req.get =
|
|
51
48
|
req.header = function header(name) {
|
|
@@ -69,178 +66,6 @@ req.header = function header(name) {
|
|
|
69
66
|
};
|
|
70
67
|
|
|
71
68
|
|
|
72
|
-
/**
|
|
73
|
-
* To do: update docs.
|
|
74
|
-
*
|
|
75
|
-
* Check if the given `type(s)` is acceptable, returning
|
|
76
|
-
* the best match when true, otherwise `undefined`, in which
|
|
77
|
-
* case you should respond with 406 "Not Acceptable".
|
|
78
|
-
*
|
|
79
|
-
* The `type` value may be a single MIME type string
|
|
80
|
-
* such as "application/json", an extension name
|
|
81
|
-
* such as "json", a comma-delimited list such as "json, html, text/plain",
|
|
82
|
-
* an argument list such as `"json", "html", "text/plain"`,
|
|
83
|
-
* or an array `["json", "html", "text/plain"]`. When a list
|
|
84
|
-
* or array is given, the _best_ match, if any is returned.
|
|
85
|
-
*
|
|
86
|
-
* Examples:
|
|
87
|
-
*
|
|
88
|
-
* // Accept: text/html
|
|
89
|
-
* req.accepts('html');
|
|
90
|
-
* // => "html"
|
|
91
|
-
*
|
|
92
|
-
* // Accept: text/*, application/json
|
|
93
|
-
* req.accepts('html');
|
|
94
|
-
* // => "html"
|
|
95
|
-
* req.accepts('text/html');
|
|
96
|
-
* // => "text/html"
|
|
97
|
-
* req.accepts('json, text');
|
|
98
|
-
* // => "json"
|
|
99
|
-
* req.accepts('application/json');
|
|
100
|
-
* // => "application/json"
|
|
101
|
-
*
|
|
102
|
-
* // Accept: text/*, application/json
|
|
103
|
-
* req.accepts('image/png');
|
|
104
|
-
* req.accepts('png');
|
|
105
|
-
* // => undefined
|
|
106
|
-
*
|
|
107
|
-
* // Accept: text/*;q=.5, application/json
|
|
108
|
-
* req.accepts(['html', 'json']);
|
|
109
|
-
* req.accepts('html', 'json');
|
|
110
|
-
* req.accepts('html, json');
|
|
111
|
-
* // => "json"
|
|
112
|
-
*
|
|
113
|
-
* @param {String|Array} type(s)
|
|
114
|
-
* @return {String|Array|Boolean}
|
|
115
|
-
*
|
|
116
|
-
* @api public
|
|
117
|
-
*/
|
|
118
|
-
req.accepts = function() {
|
|
119
|
-
const accept = accepts(this);
|
|
120
|
-
return accept.types.apply(accept, arguments);
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Check if the given `encoding`s are accepted.
|
|
126
|
-
*
|
|
127
|
-
* @param {String} ...encoding
|
|
128
|
-
* @return {String|Array}
|
|
129
|
-
*
|
|
130
|
-
* @api public
|
|
131
|
-
*/
|
|
132
|
-
req.acceptsEncodings = function() {
|
|
133
|
-
const accept = accepts(this);
|
|
134
|
-
return accept.encodings.apply(accept, arguments);
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Check if the given `charset`s are acceptable,
|
|
140
|
-
* otherwise you should respond with 406 "Not Acceptable".
|
|
141
|
-
*
|
|
142
|
-
* @param {String} ...charset
|
|
143
|
-
* @return {String|Array}
|
|
144
|
-
*
|
|
145
|
-
* @api public
|
|
146
|
-
*/
|
|
147
|
-
req.acceptsCharsets = function() {
|
|
148
|
-
const accept = accepts(this);
|
|
149
|
-
return accept.charsets.apply(accept, arguments);
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Check if the given `lang`s are acceptable,
|
|
155
|
-
* otherwise you should respond with 406 "Not Acceptable".
|
|
156
|
-
*
|
|
157
|
-
* @param {String} ...lang
|
|
158
|
-
* @return {String|Array}
|
|
159
|
-
*
|
|
160
|
-
* @api public
|
|
161
|
-
*/
|
|
162
|
-
req.acceptsLanguages = function() {
|
|
163
|
-
const accept = accepts(this);
|
|
164
|
-
return accept.languages.apply(accept, arguments);
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Parse Range header field, capping to the given `size`.
|
|
170
|
-
*
|
|
171
|
-
* Unspecified ranges such as "0-" require knowledge of your resource length. In
|
|
172
|
-
* the case of a byte range this is of course the total number of bytes. If the
|
|
173
|
-
* Range header field is not given `undefined` is returned, `-1` when unsatisfiable,
|
|
174
|
-
* and `-2` when syntactically invalid.
|
|
175
|
-
*
|
|
176
|
-
* When ranges are returned, the array has a "type" property which is the type of
|
|
177
|
-
* range that is required (most commonly, "bytes"). Each array element is an object
|
|
178
|
-
* with a "start" and "end" property for the portion of the range.
|
|
179
|
-
*
|
|
180
|
-
* The "combine" option can be set to `true` and overlapping & adjacent ranges
|
|
181
|
-
* will be combined into a single range.
|
|
182
|
-
*
|
|
183
|
-
* NOTE: remember that ranges are inclusive, so for example "Range: users=0-3"
|
|
184
|
-
* should respond with 4 users when available, not 3.
|
|
185
|
-
*
|
|
186
|
-
* @param {number} size
|
|
187
|
-
* @param {object} [options]
|
|
188
|
-
* @param {boolean} [options.combine=false]
|
|
189
|
-
*
|
|
190
|
-
* @return {number|array}
|
|
191
|
-
*
|
|
192
|
-
* @api public
|
|
193
|
-
*/
|
|
194
|
-
req.range = function range(size, options) {
|
|
195
|
-
const range = this.get('Range');
|
|
196
|
-
if (!range)
|
|
197
|
-
return;
|
|
198
|
-
|
|
199
|
-
return parseRange(size, range, options);
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Check if the incoming request contains the "Content-Type"
|
|
205
|
-
* header field, and it contains the given mime `type`.
|
|
206
|
-
*
|
|
207
|
-
* Examples:
|
|
208
|
-
*
|
|
209
|
-
* // With Content-Type: text/html; charset=utf-8
|
|
210
|
-
* req.is('html');
|
|
211
|
-
* req.is('text/html');
|
|
212
|
-
* req.is('text/*');
|
|
213
|
-
* // => true
|
|
214
|
-
*
|
|
215
|
-
* // When Content-Type is application/json
|
|
216
|
-
* req.is('json');
|
|
217
|
-
* req.is('application/json');
|
|
218
|
-
* req.is('application/*');
|
|
219
|
-
* // => true
|
|
220
|
-
*
|
|
221
|
-
* req.is('html');
|
|
222
|
-
* // => false
|
|
223
|
-
*
|
|
224
|
-
* @param {String|Array} types...
|
|
225
|
-
* @return {String|false|null}
|
|
226
|
-
*
|
|
227
|
-
* @api public
|
|
228
|
-
*/
|
|
229
|
-
req.is = function is(types) {
|
|
230
|
-
let arr = types;
|
|
231
|
-
|
|
232
|
-
// support flattened arguments
|
|
233
|
-
if (!Array.isArray(types)) {
|
|
234
|
-
arr = new Array(arguments.length);
|
|
235
|
-
for (let i = 0; i < arr.length; i++) {
|
|
236
|
-
arr[i] = arguments[i];
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return typeis(this, arr);
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
|
|
244
69
|
/**
|
|
245
70
|
* Return the protocol string "http" or "https"
|
|
246
71
|
* when requested with TLS. When the "trust proxy"
|
|
@@ -251,11 +76,10 @@ req.is = function is(types) {
|
|
|
251
76
|
* If you're running behind a reverse proxy that
|
|
252
77
|
* supplies https for you this may be enabled.
|
|
253
78
|
*
|
|
254
|
-
* @return {
|
|
79
|
+
* @return {string}
|
|
255
80
|
*
|
|
256
|
-
* @
|
|
81
|
+
* @access public
|
|
257
82
|
*/
|
|
258
|
-
|
|
259
83
|
defineGetter(req, 'protocol', function protocol() {
|
|
260
84
|
const proto = this.connection.encrypted
|
|
261
85
|
? 'https'
|
|
@@ -278,66 +102,12 @@ defineGetter(req, 'protocol', function protocol() {
|
|
|
278
102
|
});
|
|
279
103
|
|
|
280
104
|
|
|
281
|
-
/**
|
|
282
|
-
* Short-hand for:
|
|
283
|
-
*
|
|
284
|
-
* req.protocol === 'https'
|
|
285
|
-
*
|
|
286
|
-
* @return {Boolean}
|
|
287
|
-
*
|
|
288
|
-
* @api public
|
|
289
|
-
*/
|
|
290
|
-
defineGetter(req, 'secure', function secure() {
|
|
291
|
-
return this.protocol === 'https';
|
|
292
|
-
});
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Return the remote address from the trusted proxy.
|
|
297
|
-
*
|
|
298
|
-
* The is the remote address on the socket unless
|
|
299
|
-
* "trust proxy" is set.
|
|
300
|
-
*
|
|
301
|
-
* @return {String}
|
|
302
|
-
*
|
|
303
|
-
* @api public
|
|
304
|
-
*/
|
|
305
|
-
defineGetter(req, 'ip', function ip() {
|
|
306
|
-
const trust = this.app.get('trust proxy fn');
|
|
307
|
-
return proxyaddr(this, trust);
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* When "trust proxy" is set, trusted proxy addresses + client.
|
|
313
|
-
*
|
|
314
|
-
* For example if the value were "client, proxy1, proxy2"
|
|
315
|
-
* you would receive the array `["client", "proxy1", "proxy2"]`
|
|
316
|
-
* where "proxy2" is the furthest down-stream and "proxy1" and
|
|
317
|
-
* "proxy2" were trusted.
|
|
318
|
-
*
|
|
319
|
-
* @return {Array} addresses
|
|
320
|
-
*
|
|
321
|
-
* @api public
|
|
322
|
-
*/
|
|
323
|
-
defineGetter(req, 'ips', function ips() {
|
|
324
|
-
const trust = this.app.get('trust proxy fn');
|
|
325
|
-
const addrs = proxyaddr.all(this, trust);
|
|
326
|
-
|
|
327
|
-
// reverse the order (to farthest -> closest)
|
|
328
|
-
// and remove socket address
|
|
329
|
-
addresses.reverse().pop()
|
|
330
|
-
|
|
331
|
-
return addresses;
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
|
|
335
105
|
/**
|
|
336
106
|
* Short-hand for `url.parse(req.url).pathname`.
|
|
337
107
|
*
|
|
338
|
-
* @return {
|
|
108
|
+
* @return {string}
|
|
339
109
|
*
|
|
340
|
-
* @
|
|
110
|
+
* @access public
|
|
341
111
|
*/
|
|
342
112
|
defineGetter(req, 'path', function path() {
|
|
343
113
|
return parse(this).pathname;
|
|
@@ -349,9 +119,9 @@ defineGetter(req, 'path', function path() {
|
|
|
349
119
|
* Will return "X-Forwarded-Host" if set,
|
|
350
120
|
* or "Host" as a fallback.
|
|
351
121
|
*
|
|
352
|
-
* @return {
|
|
122
|
+
* @return {string}
|
|
353
123
|
*
|
|
354
|
-
* @
|
|
124
|
+
* @access public
|
|
355
125
|
*/
|
|
356
126
|
defineGetter(req, 'hostname', function hostname() {
|
|
357
127
|
const host = this.get('X-Forwarded-Host') ?? this.get('Host');
|
|
@@ -364,9 +134,9 @@ defineGetter(req, 'hostname', function hostname() {
|
|
|
364
134
|
* Last-Modified and/or the ETag
|
|
365
135
|
* still match.
|
|
366
136
|
*
|
|
367
|
-
* @return {
|
|
137
|
+
* @return {boolean}
|
|
368
138
|
*
|
|
369
|
-
* @
|
|
139
|
+
* @access public
|
|
370
140
|
*/
|
|
371
141
|
defineGetter(req, 'fresh', function() {
|
|
372
142
|
const method = this.method;
|
|
@@ -394,9 +164,9 @@ defineGetter(req, 'fresh', function() {
|
|
|
394
164
|
* "Last-Modified" and / or the "ETag" for the
|
|
395
165
|
* resource has changed.
|
|
396
166
|
*
|
|
397
|
-
* @return {
|
|
167
|
+
* @return {boolean}
|
|
398
168
|
*
|
|
399
|
-
* @
|
|
169
|
+
* @access public
|
|
400
170
|
*/
|
|
401
171
|
defineGetter(req, 'stale', function stale() {
|
|
402
172
|
return !this.fresh;
|
|
@@ -406,9 +176,9 @@ defineGetter(req, 'stale', function stale() {
|
|
|
406
176
|
/**
|
|
407
177
|
* Check if the request was an _XMLHttpRequest_.
|
|
408
178
|
*
|
|
409
|
-
* @return {
|
|
179
|
+
* @return {boolean}
|
|
410
180
|
*
|
|
411
|
-
* @
|
|
181
|
+
* @access public
|
|
412
182
|
*/
|
|
413
183
|
defineGetter(req, 'xhr', function xhr() {
|
|
414
184
|
const val = this.get('X-Requested-With') || '';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -13,10 +13,10 @@ module.exports = {
|
|
|
13
13
|
* Helper function for creating a getter on an object.
|
|
14
14
|
*
|
|
15
15
|
* @param {Object} obj
|
|
16
|
-
* @param {
|
|
16
|
+
* @param {string} name
|
|
17
17
|
* @param {Function} getter
|
|
18
18
|
*
|
|
19
|
-
* @
|
|
19
|
+
* @access private
|
|
20
20
|
*/
|
|
21
21
|
function _defineGetter(obj, name, getter) {
|
|
22
22
|
Object.defineProperty(obj, name, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -23,21 +23,19 @@ module.exports = {
|
|
|
23
23
|
* Set header `field` to `value`, or pass
|
|
24
24
|
* an object of header fields.
|
|
25
25
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
26
|
+
* @example
|
|
28
27
|
* res.set('Foo', ['bar', 'baz']);
|
|
29
28
|
* res.set('Accept', 'application/json');
|
|
30
29
|
* res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
31
30
|
*
|
|
32
|
-
* Aliased as `res.header()`.
|
|
33
31
|
*
|
|
34
|
-
* @param {
|
|
35
|
-
* @param {
|
|
32
|
+
* @param {string|Object} keyOrObject
|
|
33
|
+
* @param {string|Array} value
|
|
36
34
|
*
|
|
37
35
|
* @return {ServerResponse} for chaining
|
|
38
36
|
*
|
|
39
37
|
* @alias setHeader
|
|
40
|
-
* @
|
|
38
|
+
* @access public
|
|
41
39
|
*/
|
|
42
40
|
function _setHeader(keyOrObject, value) {
|
|
43
41
|
// As a pair:
|
|
@@ -81,18 +79,17 @@ function _setHeader(keyOrObject, value) {
|
|
|
81
79
|
/**
|
|
82
80
|
* Append additional header `field` with value `val`.
|
|
83
81
|
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
82
|
+
* @example
|
|
86
83
|
* res.append.header('Link', ['<http://localhost/>', '<http://localhost:3000/>']);
|
|
87
84
|
* res.append.header('Set-Cookie', 'foo=bar; Path=/; HttpOnly');
|
|
88
85
|
* res.append.header('Warning', '199 Miscellaneous warning');
|
|
89
86
|
*
|
|
90
|
-
* @param {
|
|
91
|
-
* @param {
|
|
87
|
+
* @param {string} field
|
|
88
|
+
* @param {string|Array} value
|
|
92
89
|
* @return {ServerResponse} for chaining
|
|
93
90
|
*
|
|
94
91
|
* @alias appendHeader
|
|
95
|
-
* @
|
|
92
|
+
* @access public
|
|
96
93
|
*/
|
|
97
94
|
function _appendHeader(field, value) {
|
|
98
95
|
const prev = this.get(field);
|
|
@@ -112,12 +109,12 @@ function _appendHeader(field, value) {
|
|
|
112
109
|
/**
|
|
113
110
|
* Get value for header `key`.
|
|
114
111
|
*
|
|
115
|
-
* @param {
|
|
112
|
+
* @param {string} key
|
|
116
113
|
*
|
|
117
|
-
* @return {
|
|
114
|
+
* @return {string}
|
|
118
115
|
*
|
|
119
116
|
* @alias getHeader
|
|
120
|
-
* @
|
|
117
|
+
* @access public
|
|
121
118
|
*/
|
|
122
119
|
function _getHeader(key) {
|
|
123
120
|
return this.getHeader(key);
|
|
@@ -126,12 +123,12 @@ function _getHeader(key) {
|
|
|
126
123
|
/**
|
|
127
124
|
* Remove value for header `key`.
|
|
128
125
|
*
|
|
129
|
-
* @param {
|
|
126
|
+
* @param {string} key
|
|
130
127
|
*
|
|
131
|
-
* @return {
|
|
128
|
+
* @return {string}
|
|
132
129
|
*
|
|
133
130
|
* @alias removeHeader
|
|
134
|
-
* @
|
|
131
|
+
* @access public
|
|
135
132
|
*/
|
|
136
133
|
function _removeHeader(key) {
|
|
137
134
|
return this.removeHeader(key);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -49,12 +49,12 @@ module.exports = response;
|
|
|
49
49
|
/**
|
|
50
50
|
* Set status `code`.
|
|
51
51
|
*
|
|
52
|
-
* @param {
|
|
52
|
+
* @param {Int|string} code
|
|
53
53
|
*
|
|
54
54
|
* @return {ServerResponse}
|
|
55
55
|
*
|
|
56
56
|
* @alias status
|
|
57
|
-
* @
|
|
57
|
+
* @access public
|
|
58
58
|
*/
|
|
59
59
|
function _status(code) {
|
|
60
60
|
this.statusCode = code;
|
|
@@ -69,14 +69,13 @@ function _status(code) {
|
|
|
69
69
|
* response to the standard description from node's http.STATUS_CODES
|
|
70
70
|
* or the statusCode number if no description.
|
|
71
71
|
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* res.sendStatus(200);
|
|
72
|
+
* @example
|
|
73
|
+
* res.sendStatus(200);
|
|
75
74
|
*
|
|
76
75
|
* @param {number} statusCode
|
|
77
76
|
*
|
|
78
77
|
* @alias _sendStatus
|
|
79
|
-
* @
|
|
78
|
+
* @access public
|
|
80
79
|
*/
|
|
81
80
|
function _sendStatus(statusCode) {
|
|
82
81
|
const body = statuses.message[statusCode] || String(statusCode);
|
|
@@ -90,16 +89,15 @@ function _sendStatus(statusCode) {
|
|
|
90
89
|
/**
|
|
91
90
|
* Sends a response.
|
|
92
91
|
*
|
|
93
|
-
*
|
|
92
|
+
* @example
|
|
93
|
+
* res.send(Buffer.from('wahoo'));
|
|
94
|
+
* res.send({ some: 'json' });
|
|
95
|
+
* res.send('<p>some html</p>');
|
|
94
96
|
*
|
|
95
|
-
*
|
|
96
|
-
* res.send({ some: 'json' });
|
|
97
|
-
* res.send('<p>some html</p>');
|
|
98
|
-
*
|
|
99
|
-
* @param {string|number|boolean|object|Buffer} body
|
|
97
|
+
* @param {string|number|boolean|Object|Buffer} body
|
|
100
98
|
*
|
|
101
99
|
* @alias send
|
|
102
|
-
* @
|
|
100
|
+
* @access public
|
|
103
101
|
*/
|
|
104
102
|
function _send(body) {
|
|
105
103
|
const req = this.req;
|
|
@@ -190,10 +188,10 @@ function _send(body) {
|
|
|
190
188
|
/**
|
|
191
189
|
* Send JSON response.
|
|
192
190
|
*
|
|
193
|
-
* @param {string|number|boolean|
|
|
191
|
+
* @param {string|number|boolean|Object} obj
|
|
194
192
|
*
|
|
195
193
|
* @alias json
|
|
196
|
-
* @
|
|
194
|
+
* @access public
|
|
197
195
|
*/
|
|
198
196
|
function _json(obj) {
|
|
199
197
|
|
|
@@ -210,20 +208,19 @@ function _json(obj) {
|
|
|
210
208
|
* Set _Content-Type_ response header with `type` through `mime.getType()`
|
|
211
209
|
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
212
210
|
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
*
|
|
218
|
-
*
|
|
219
|
-
* res.type('png');
|
|
211
|
+
* @example
|
|
212
|
+
* res.setContentType('.html');
|
|
213
|
+
* res.setContentType('html');
|
|
214
|
+
* res.setContentType('json');
|
|
215
|
+
* res.setContentType('application/json');
|
|
216
|
+
* res.setContentType('png');
|
|
220
217
|
*
|
|
221
|
-
* @param {
|
|
218
|
+
* @param {string} type
|
|
222
219
|
*
|
|
223
220
|
* @return {ServerResponse} for chaining
|
|
224
221
|
*
|
|
225
222
|
* @alias setContentType
|
|
226
|
-
* @
|
|
223
|
+
* @access public
|
|
227
224
|
*/
|
|
228
225
|
function _setContentType(type) {
|
|
229
226
|
const contentType = type.indexOf('/') === -1 ?
|
|
@@ -238,10 +235,10 @@ function _setContentType(type) {
|
|
|
238
235
|
/**
|
|
239
236
|
* Returns _Content-Type_ header.
|
|
240
237
|
*
|
|
241
|
-
* @return {
|
|
238
|
+
* @return {string} type
|
|
242
239
|
*
|
|
243
240
|
* @alias getContentType
|
|
244
|
-
* @
|
|
241
|
+
* @access public
|
|
245
242
|
*/
|
|
246
243
|
function _getContentType() {
|
|
247
244
|
return this.getHeader('Content-Type');
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
// Utils:
|
|
9
9
|
const contentType = require('content-type');
|
|
10
10
|
|
|
11
|
+
|
|
11
12
|
module.exports = {
|
|
12
13
|
setCharset: _setCharset
|
|
13
14
|
}
|
|
@@ -15,12 +16,12 @@ module.exports = {
|
|
|
15
16
|
/**
|
|
16
17
|
* Set the charset in a given Content-Type string.
|
|
17
18
|
*
|
|
18
|
-
* @param {
|
|
19
|
-
* @param {
|
|
19
|
+
* @param {string} type
|
|
20
|
+
* @param {string} charset
|
|
20
21
|
*
|
|
21
|
-
* @return {
|
|
22
|
+
* @return {string}
|
|
22
23
|
*
|
|
23
|
-
* @
|
|
24
|
+
* @access private
|
|
24
25
|
*/
|
|
25
26
|
function _setCharset(type, charset) {
|
|
26
27
|
if (!type || !charset) {
|
package/lib/loggers/console.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -16,9 +16,8 @@ exports = module.exports = {
|
|
|
16
16
|
* @param {Error} err
|
|
17
17
|
*
|
|
18
18
|
* @alias error
|
|
19
|
-
* @public
|
|
19
|
+
* @access public
|
|
20
20
|
*/
|
|
21
|
-
|
|
22
21
|
function _error(err) {
|
|
23
22
|
console.error(err.stack || err.toString());
|
|
24
23
|
}
|
package/lib/loggers/dev.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
3
|
* MIT Licensed
|
|
4
4
|
*/
|
|
5
5
|
|
|
@@ -16,9 +16,8 @@ exports = module.exports = {
|
|
|
16
16
|
* @param {Array} args
|
|
17
17
|
*
|
|
18
18
|
* @alias error
|
|
19
|
-
* @public
|
|
19
|
+
* @access public
|
|
20
20
|
*/
|
|
21
|
-
|
|
22
21
|
function _error(...args) {
|
|
23
22
|
const activeEnv = process.env.NODE_ENV
|
|
24
23
|
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* nodester
|
|
3
|
+
* MIT Licensed
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
HTTP_CODE_NOT_FOUND
|
|
10
|
+
} = require('nodester/http/codes');
|
|
11
|
+
|
|
12
|
+
const { createErrorResponse } = require('nodester/factories/responses/rest');
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Initialize `404` middleware.
|
|
17
|
+
*
|
|
18
|
+
* @param {Object} [options]
|
|
19
|
+
*
|
|
20
|
+
* @return {Function}
|
|
21
|
+
*
|
|
22
|
+
* @access public
|
|
23
|
+
*/
|
|
24
|
+
module.exports = function init404Middleware(options={}) {
|
|
25
|
+
const context = {
|
|
26
|
+
options
|
|
27
|
+
}
|
|
28
|
+
return handle.bind(context);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function handle(req, res, next) {
|
|
32
|
+
const err = new Error('Route not found');
|
|
33
|
+
|
|
34
|
+
return createErrorResponse(res, {
|
|
35
|
+
error: err,
|
|
36
|
+
status: HTTP_CODE_NOT_FOUND
|
|
37
|
+
});
|
|
38
|
+
}
|