|
1 | | -import Promise from 'pinkie'; |
2 | | -import request from 'request-promise'; |
| 1 | +import fetch from 'node-fetch'; |
| 2 | +import { HttpsProxyAgent } from 'https-proxy-agent'; |
3 | 3 | import * as ERROR_MESSAGES from '../templates/error-messages'; |
4 | 4 |
|
5 | | -const apiRequestPromise = Promise.resolve(null); |
6 | | - |
7 | | -export default function (apiPath, params = {}) { |
| 5 | +export default async function (apiPath, params = {}) { |
8 | 6 | if (!process.env['BROWSERSTACK_USERNAME'] || !process.env['BROWSERSTACK_ACCESS_KEY']) |
9 | 7 | throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED()); |
10 | 8 |
|
11 | | - var { body, executeImmediately, ...queryParams } = params; |
| 9 | + const { body, ...queryParams } = params; |
12 | 10 |
|
13 | | - var opts = { |
14 | | - url: apiPath.url, |
15 | | - auth: { |
16 | | - user: process.env['BROWSERSTACK_USERNAME'], |
17 | | - pass: process.env['BROWSERSTACK_ACCESS_KEY'], |
18 | | - }, |
| 11 | + const urlObj = new URL(apiPath.url); |
19 | 12 |
|
20 | | - headers: { |
21 | | - 'user-agent': 'testcafe-browserstack', |
22 | | - }, |
| 13 | + for (const [key, value] of Object.entries(queryParams)) |
| 14 | + urlObj.searchParams.append(key, value); |
| 15 | + |
| 16 | + const url = urlObj.toString(); |
23 | 17 |
|
24 | | - qs: { ...queryParams }, |
| 18 | + const user = process.env['BROWSERSTACK_USERNAME']; |
| 19 | + const pass = process.env['BROWSERSTACK_ACCESS_KEY']; |
25 | 20 |
|
26 | | - method: apiPath.method || 'GET', |
27 | | - json: apiPath.encoding === void 0 |
| 21 | + const options = { |
| 22 | + method: apiPath.method || 'GET', |
| 23 | + headers: { |
| 24 | + 'Authorization': `Basic ${Buffer.from(user + ':' + pass).toString('base64')}`, |
| 25 | + 'User-Agent': 'testcafe-browserstack', |
| 26 | + }, |
28 | 27 | }; |
29 | 28 |
|
30 | 29 | const proxy = process.env['BROWSERSTACK_PROXY']; |
31 | 30 |
|
32 | 31 | if (proxy) |
33 | | - opts.proxy = `http://${proxy}`; |
| 32 | + options.agent = new HttpsProxyAgent(`http://${proxy}`); |
34 | 33 |
|
35 | | - if (body) |
36 | | - opts.body = body; |
| 34 | + if (body) { |
| 35 | + options.body = JSON.stringify(body); |
| 36 | + options.headers['Content-Type'] = 'application/json'; |
| 37 | + } |
37 | 38 |
|
38 | | - if (apiPath.encoding !== void 0) |
39 | | - opts.encoding = apiPath.encoding; |
| 39 | + const res = await fetch(url, options); |
40 | 40 |
|
41 | | - const chainPromise = executeImmediately ? Promise.resolve(null) : apiRequestPromise; |
42 | | - |
43 | | - const currentRequestPromise = chainPromise |
44 | | - .then(() => request(opts)) |
45 | | - .catch(error => { |
46 | | - if (error.statusCode === 401) |
47 | | - throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED()); |
| 41 | + if (res.status === 401) |
| 42 | + throw new Error(ERROR_MESSAGES.BROWSERSTACK_AUTHENTICATION_FAILED()); |
48 | 43 |
|
49 | | - throw error; |
50 | | - }); |
| 44 | + if (apiPath.encoding === null) |
| 45 | + return Buffer.from(await res.arrayBuffer()); |
51 | 46 |
|
52 | | - return currentRequestPromise; |
| 47 | + return res.json(); |
53 | 48 | } |
0 commit comments