/
functions.js
36 lines (26 loc) · 965 Bytes
/
functions.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
import { call } from 'redux-saga/effects'
export function getFunctionURL(functionName, parameters = {}) {
// If the function name is already a URL, just return it.
const baseUrl = /^https?:\/\//.test(functionName)
? functionName
: `https://${this.region}-${this.projectId()}.cloudfunctions.net/${functionName}`
const query = Object.keys(parameters)
.map(key => `${key}=${parameters[key]}`)
.join('&')
if (query) return `${baseUrl}?${query}`
else return baseUrl
}
function* _call(functionName, queryParams = {}, init = {}) {
const url = getFunctionURL.call(this, functionName, queryParams)
const response = yield call(fetch, url, init)
if (!response.ok) throw response
const contentType = response.headers.get('Content-Type')
const parser = contentType.startsWith('application/json')
? response.json
: response.text
const data = yield call([response, parser])
return data
}
export default {
call: _call,
}