testdriverai 5.7.31 → 5.7.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/sdk.js +41 -29
- package/package.json +1 -1
package/lib/sdk.js
CHANGED
|
@@ -5,14 +5,32 @@ const session = require("./session");
|
|
|
5
5
|
// get the version from package.json
|
|
6
6
|
const { version } = require("../package.json");
|
|
7
7
|
const root = config["TD_API_ROOT"];
|
|
8
|
-
const axios = require(
|
|
8
|
+
const axios = require("axios");
|
|
9
9
|
|
|
10
|
-
const { logger } = require(
|
|
10
|
+
const { logger } = require("./logger");
|
|
11
11
|
|
|
12
12
|
let token = null;
|
|
13
13
|
|
|
14
|
-
const outputError =
|
|
15
|
-
logger.
|
|
14
|
+
const outputError = (error) => {
|
|
15
|
+
logger.error(
|
|
16
|
+
"API Error: %s (%s)",
|
|
17
|
+
chalk.red(
|
|
18
|
+
// HTTP status code from Axios
|
|
19
|
+
error.status ||
|
|
20
|
+
// ...or an explicit error `reason` set by Sails
|
|
21
|
+
error.reason ||
|
|
22
|
+
// ...or default to the error message
|
|
23
|
+
error.message,
|
|
24
|
+
),
|
|
25
|
+
chalk.red(
|
|
26
|
+
// e.g. "teamNotExist" from Sails' exits
|
|
27
|
+
error.response?.data?.raw ||
|
|
28
|
+
// ...or the status text
|
|
29
|
+
error.statusText ||
|
|
30
|
+
// ...or the HTTP status code
|
|
31
|
+
error.code,
|
|
32
|
+
),
|
|
33
|
+
);
|
|
16
34
|
};
|
|
17
35
|
|
|
18
36
|
const parseBody = async (response, body) => {
|
|
@@ -62,9 +80,7 @@ const parseBody = async (response, body) => {
|
|
|
62
80
|
};
|
|
63
81
|
|
|
64
82
|
let auth = async () => {
|
|
65
|
-
|
|
66
83
|
if (config["TD_API_KEY"]) {
|
|
67
|
-
|
|
68
84
|
const url = [root, "auth/exchange-api-key"].join("/");
|
|
69
85
|
const c = {
|
|
70
86
|
method: "post",
|
|
@@ -73,21 +89,20 @@ let auth = async () => {
|
|
|
73
89
|
},
|
|
74
90
|
data: {
|
|
75
91
|
apiKey: config["TD_API_KEY"],
|
|
76
|
-
version
|
|
77
|
-
}
|
|
92
|
+
version,
|
|
93
|
+
},
|
|
78
94
|
};
|
|
79
|
-
|
|
95
|
+
|
|
80
96
|
try {
|
|
81
97
|
let res = await axios(url, c);
|
|
82
|
-
|
|
98
|
+
|
|
83
99
|
token = res.data.token;
|
|
84
100
|
return token;
|
|
85
101
|
} catch (error) {
|
|
86
|
-
|
|
102
|
+
outputError(error);
|
|
87
103
|
process.exit(1);
|
|
88
104
|
}
|
|
89
105
|
}
|
|
90
|
-
|
|
91
106
|
};
|
|
92
107
|
|
|
93
108
|
const req = async (path, data, onChunk) => {
|
|
@@ -104,15 +119,15 @@ const req = async (path, data, onChunk) => {
|
|
|
104
119
|
|
|
105
120
|
const c = {
|
|
106
121
|
method: "post",
|
|
107
|
-
headers: {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
},
|
|
122
|
+
headers: {
|
|
123
|
+
"Content-Type": "application/json",
|
|
124
|
+
...(token && { Authorization: `Bearer ${token}` }), // Add the authorization bearer token only if token is set
|
|
125
|
+
},
|
|
111
126
|
responseType: typeof onChunk === "function" ? "stream" : "json",
|
|
112
127
|
data: {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
128
|
+
...data,
|
|
129
|
+
session: session.get(),
|
|
130
|
+
stream: typeof onChunk === "function",
|
|
116
131
|
},
|
|
117
132
|
};
|
|
118
133
|
|
|
@@ -130,19 +145,17 @@ const req = async (path, data, onChunk) => {
|
|
|
130
145
|
let lastLineIndex = -1;
|
|
131
146
|
|
|
132
147
|
await new Promise((resolve, reject) => {
|
|
133
|
-
|
|
134
148
|
// theres some kind of race condition here that makes things resolve
|
|
135
149
|
// before the stream is done
|
|
136
|
-
|
|
137
|
-
response.data.on(
|
|
138
|
-
|
|
150
|
+
|
|
151
|
+
response.data.on("data", (chunk) => {
|
|
139
152
|
result += chunk.toString();
|
|
140
153
|
const lines = result.split("\n");
|
|
141
154
|
|
|
142
155
|
const events = lines
|
|
143
156
|
.slice(lastLineIndex + 1, lines.length - 1)
|
|
144
157
|
.filter((line) => line.length)
|
|
145
|
-
.map((line) => JSON.parse(line));
|
|
158
|
+
.map((line) => JSON.parse(line));
|
|
146
159
|
|
|
147
160
|
for (const event of events) {
|
|
148
161
|
onChunk(event);
|
|
@@ -151,8 +164,7 @@ const req = async (path, data, onChunk) => {
|
|
|
151
164
|
lastLineIndex = lines.length - 2;
|
|
152
165
|
});
|
|
153
166
|
|
|
154
|
-
response.data.on(
|
|
155
|
-
|
|
167
|
+
response.data.on("end", () => {
|
|
156
168
|
if (isJsonl) {
|
|
157
169
|
const events = result
|
|
158
170
|
.split("\n")
|
|
@@ -164,11 +176,11 @@ const req = async (path, data, onChunk) => {
|
|
|
164
176
|
onChunk(event);
|
|
165
177
|
}
|
|
166
178
|
}
|
|
167
|
-
|
|
179
|
+
|
|
168
180
|
resolve();
|
|
169
181
|
});
|
|
170
182
|
|
|
171
|
-
response.data.on(
|
|
183
|
+
response.data.on("error", (error) => {
|
|
172
184
|
reject(error);
|
|
173
185
|
});
|
|
174
186
|
});
|
|
@@ -178,7 +190,7 @@ const req = async (path, data, onChunk) => {
|
|
|
178
190
|
|
|
179
191
|
return value;
|
|
180
192
|
} catch (error) {
|
|
181
|
-
|
|
193
|
+
outputError(error);
|
|
182
194
|
}
|
|
183
195
|
};
|
|
184
196
|
|