forked from joelnet/MojiScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.mjs
28 lines (22 loc) · 763 Bytes
/
api.mjs
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
import pipe from 'mojiscript/core/pipe'
import ifElse from 'mojiscript/logic/ifElse'
const rootUrl = 'https://swapi.co/api/people/'
const STATUS_CODE = {
OK: 200
}
const responseIsOk = ({ status }) => status === STATUS_CODE.OK
const resolveResponse = ({ data }) => Promise.resolve (data.results)
const rejectResponse = ({ status, statusText }) => Promise.reject (`${status}: ${statusText}`)
const parseResponse = ifElse (responseIsOk) (resolveResponse) (rejectResponse)
// searchStringToParams :: String -> AxiosOptions
const searchStringToParams = search => ({
params: {
search
}
})
// peopleSearch :: Axios -> String -> AxiosResponse
export const peopleSearch = axios => pipe ([
searchStringToParams,
axios.get (rootUrl),
parseResponse
])