UNPKG

1.06 kBJavaScriptView Raw
1/* @flow */
2'use strict'
3
4/* ::
5import type {
6 API,
7 BlinkMRCServer
8} from '../types.js'
9*/
10
11const request = require('request')
12
13const scope = require('./scope.js')
14
15module.exports = async function getAPIInstance (
16 config /* : BlinkMRCServer */,
17 accessToken /* : string | void */
18) /* : Promise<API> */ {
19 const serverCLIServiceConfig = scope.serverCLIServiceConfig(config)
20 return new Promise((resolve, reject) => {
21 if (!config.project) {
22 return reject(new Error('Please run the "scope" command to set the project scope.'))
23 }
24 request(
25 `${serverCLIServiceConfig.origin}/apis/${config.project}`,
26 {
27 auth: {
28 bearer: accessToken
29 },
30 json: true
31 },
32 (err, response, body) => {
33 if (err) {
34 return reject(err)
35 }
36 if (response.statusCode !== 200) {
37 return reject(new Error(body && body.message ? body.message : 'Unknown error, please try again and contact support if the problem persists'))
38 }
39 return resolve(body)
40 })
41 })
42}