1 | const fetch = require('node-fetch')
|
2 |
|
3 | const headers = token => ({
|
4 | Authorization: `Bearer ${token}`,
|
5 | 'User-Agent': 'google-api-nodejs-client/0.10.0',
|
6 | host: 'www.googleapis.com',
|
7 | accept: 'application/json'
|
8 | })
|
9 |
|
10 | function handleParams ({
|
11 | base = 'https://www.googleapis.com',
|
12 | params = null,
|
13 | endpoint = '/'
|
14 | }) {
|
15 | let url = base + endpoint
|
16 | if (!params) return url
|
17 | url += '?'
|
18 | return url + Object.keys(params)
|
19 | .map(key => `${key}=${encodeURIComponent(params[key])}`)
|
20 | .join('&')
|
21 | }
|
22 |
|
23 | function createFetch (params) {
|
24 | const { accessToken } = params
|
25 | const url = handleParams(params)
|
26 | return fetch(url, {
|
27 | method: 'get',
|
28 | headers: headers(accessToken)
|
29 | })
|
30 | .then(resp => resp.json())
|
31 | }
|
32 |
|
33 | module.exports = createFetch
|