-
Notifications
You must be signed in to change notification settings - Fork 2
/
sagas.js
116 lines (108 loc) · 3.15 KB
/
sagas.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { fork, call } from 'redux-saga/effects'
import api from 'services/api'
const req = require.context('.', true, /\.\/.+\/sagas\.js$/)
// Call the backend API
// params:
// method: one of 'GET', 'POST', 'PUT', 'DELETE'
// url: backend URL to call
// data: body of request to send to backend API
// return:
// JSON data of received response
export function* callUrl(method, url, data = {}) {
try {
if (localStorage.hasOwnProperty('token')) {
const email = JSON.parse(localStorage.getItem('email'))
const password = JSON.parse(localStorage.getItem('password'))
const credentials = new Buffer(`${email}:${password}`).toString('base64')
let response
if (method === 'GET' || method === 'DELETE') {
response = yield call(
fetch,
url,
{
method,
headers: {
Authorization: `Basic ${credentials}`,
'Content-Type': 'application/json',
},
}
)
} else if (method === 'POST' || method === 'PUT') {
response = yield call(
fetch,
url,
{
method,
headers: {
Authorization: `Basic ${credentials}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
}
)
} else {
throw Error('callUrl: invalid method, logged in')
}
if (!response.ok) {
console.log(yield response.json())
throw Error(response.statusText)
}
return yield response.json()
} else if (method === 'GET') {
return yield call(api.get, url)
} else if (method === 'POST') {
return yield call(api.post, url, data)
} else if (method === 'PUT') {
return yield call(api.put, url, data)
} else if (method === 'DELETE') {
return yield call(api.delete, url)
}
throw Error('callUrl: Invalid method, not logged in')
} catch (err) {
console.log(err)
throw err
}
}
// Call the backend API with image
// params:
// method: one of 'GET', 'POST', 'PUT', 'DELETE'
// url: backend URL to call
// data: body of request to send to backend API in the format of `FormData`
// return:
// data of received response
export function* callUrlImg(method, url, data = {}) {
try {
if (localStorage.hasOwnProperty('token')) {
const email = JSON.parse(localStorage.getItem('email'))
const password = JSON.parse(localStorage.getItem('password'))
const credentials = new Buffer(`${email}:${password}`).toString('base64')
const response = yield call(
fetch,
url,
{
method,
headers: {
Authorization: `Basic ${credentials}`,
},
body: data,
}
)
if (!response.ok) {
console.log(yield response.json())
throw Error(response.statusText)
}
return yield response.json()
}
throw Error('callUrlImg: not logged in')
} catch (err) {
console.log(err)
throw err
}
}
const sagas = []
req.keys().forEach((key) => {
sagas.push(req(key).default)
})
export default function* () {
yield sagas.map(fork)
}