UNPKG

1.08 kBPlain TextView Raw
1require! \prelude-ls : {is-type, map}
2
3errors =
4 not_found: 404
5 forbidden: 403
6 'Error on Delete': 500
7
8class Request
9
10 instance: null
11 sent: false
12
13 ([@req, @res, @next]) ->
14 @_Parse!
15
16 Send: !->
17 return if @sent
18
19 if it is null
20 @sent = true
21 return @res.status(404).send "Cannot #{@method.toUpperCase!} #{@url}"
22
23 if is-type 'Array' it
24 it := map (-> if it.ToJSON? => it.ToJSON! else it), it
25
26 if it.ToJSON? => @res.status(200).send it.ToJSON!
27 else => @res.status(200).send it
28
29 @sent = true
30
31 SendError: ->
32 return if @sent
33
34 status = it.code || errors[it.status] || 500
35
36 it = that if it.message?
37
38 @res.status(status).send err: it
39 @sent = true
40
41 SetInstance: !->
42 if it._promise?
43 it
44 .Then ~>
45 @req._instance = it
46 @instance = it
47 @next()
48 .Catch ~> @SendError it
49 else
50 @req._instance = it
51 @instance = it
52 @next()
53
54 _Parse: ->
55 import @req
56 @instance = @_instance if @_instance?
57
58Request.errors = errors
59module.exports = Request