blob: 0656a561688898992084ca1617f2d8f936fe2e52 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
riot = require \riot
spinner = null
pending = 0
net = riot.observable!
riot.mixin \net do
net: net
log = (riot.mixin \log).log
module.exports = (i, endpoint, data) ->
pending++
if i? and typeof i == \object then i = i.token
body = []
# append user token when signed in
if i? then body.push "i=#i"
for k, v of data
if v != undefined
v = encodeURIComponent v
body.push "#k=#v"
opts =
method: \POST
headers:
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
body: body.join \&
if endpoint == \signin
opts.credentials = \include
ep = if (endpoint.index-of '://') > -1
then endpoint
else "#{CONFIG.api.url}/#{endpoint}"
if pending == 1
spinner := document.create-element \div
..set-attribute \id \wait
document.body.append-child spinner
new Promise (resolve, reject) ->
timer = set-timeout ->
net.trigger \detected-slow-network
, 5000ms
log "API: #{ep}"
fetch ep, opts
.then (res) ->
pending--
clear-timeout timer
if pending == 0
spinner.parent-node.remove-child spinner
if res.status == 200
res.json!.then resolve
else if res.status == 204
resolve!
else
res.json!.then (err) ->
reject err.error
.catch reject
|