-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.js
39 lines (33 loc) · 1.01 KB
/
http.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
const Task = require('data.task')
const { curry } = require('ramda')
const request = require('request')
const { eitherToTask } = require('./utils')
const Either = require('data.either')
// parseJSON :: JSON -> Either JSON String
const parseJSON = Either.try(JSON.parse)
// getJSON :: {} -> String -> Task JSON
const getJSON = curry((headers, url) =>
Http.get(headers, url)
.map(parseJSON)
.chain(eitherToTask))
// postJSON :: {} -> String -> {} -> Task JSON
const postJSON = curry((headers, url, payload) =>
Http.post(headers, url, payload)
.map(parseJSON)
.chain(eitherToTask))
const Http = {
get: (headers, url) =>
new Task((reject, resolve) => {
request({url, headers}, (error, response, body) =>
error ? reject(error) : resolve(body))
}),
post: (headers, url, form) =>
new Task((reject, resolve) => {
request({url, headers, method: 'POST', form}, (error, response, body) =>
error ? reject(error) : resolve(body))
})
}
module.exports = {
getJSON,
postJSON
}