@learnpack/learnpack 2.1.33 → 2.1.35

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -21,7 +21,7 @@ $ npm install -g @learnpack/learnpack
21
21
  $ learnpack COMMAND
22
22
  running command...
23
23
  $ learnpack (-v|--version|version)
24
- @learnpack/learnpack/2.1.33 darwin-arm64 node-v16.20.0
24
+ @learnpack/learnpack/2.1.35 darwin-arm64 node-v16.20.0
25
25
  $ learnpack --help [COMMAND]
26
26
  USAGE
27
27
  $ learnpack COMMAND
@@ -74,7 +74,7 @@ DESCRIPTION
74
74
  12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)
75
75
  ```
76
76
 
77
- _See code: [src/commands/audit.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/audit.ts)_
77
+ _See code: [src/commands/audit.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/audit.ts)_
78
78
 
79
79
  ## `learnpack clean`
80
80
 
@@ -89,7 +89,7 @@ DESCRIPTION
89
89
  Extra documentation goes here
90
90
  ```
91
91
 
92
- _See code: [src/commands/clean.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/clean.ts)_
92
+ _See code: [src/commands/clean.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/clean.ts)_
93
93
 
94
94
  ## `learnpack download [PACKAGE]`
95
95
 
@@ -107,7 +107,7 @@ DESCRIPTION
107
107
  Extra documentation goes here
108
108
  ```
109
109
 
110
- _See code: [src/commands/download.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/download.ts)_
110
+ _See code: [src/commands/download.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/download.ts)_
111
111
 
112
112
  ## `learnpack help [COMMAND]`
113
113
 
@@ -138,7 +138,7 @@ OPTIONS
138
138
  -h, --grading show CLI help
139
139
  ```
140
140
 
141
- _See code: [src/commands/init.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/init.ts)_
141
+ _See code: [src/commands/init.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/init.ts)_
142
142
 
143
143
  ## `learnpack login [PACKAGE]`
144
144
 
@@ -156,7 +156,7 @@ DESCRIPTION
156
156
  Extra documentation goes here
157
157
  ```
158
158
 
159
- _See code: [src/commands/login.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/login.ts)_
159
+ _See code: [src/commands/login.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/login.ts)_
160
160
 
161
161
  ## `learnpack logout [PACKAGE]`
162
162
 
@@ -174,7 +174,7 @@ DESCRIPTION
174
174
  Extra documentation goes here
175
175
  ```
176
176
 
177
- _See code: [src/commands/logout.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/logout.ts)_
177
+ _See code: [src/commands/logout.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/logout.ts)_
178
178
 
179
179
  ## `learnpack plugins`
180
180
 
@@ -309,7 +309,7 @@ DESCRIPTION
309
309
  Extra documentation goes here
310
310
  ```
311
311
 
312
- _See code: [src/commands/publish.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/publish.ts)_
312
+ _See code: [src/commands/publish.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/publish.ts)_
313
313
 
314
314
  ## `learnpack start`
315
315
 
@@ -330,7 +330,7 @@ OPTIONS
330
330
  -w, --watch Watch for file changes
331
331
  ```
332
332
 
333
- _See code: [src/commands/start.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/start.ts)_
333
+ _See code: [src/commands/start.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/start.ts)_
334
334
 
335
335
  ## `learnpack test [EXERCISESLUG]`
336
336
 
@@ -344,7 +344,7 @@ ARGUMENTS
344
344
  EXERCISESLUG The name of the exercise to test
345
345
  ```
346
346
 
347
- _See code: [src/commands/test.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.33/src/commands/test.ts)_
347
+ _See code: [src/commands/test.ts](https://github.com/learnpack/learnpack-cli/blob/v2.1.35/src/commands/test.ts)_
348
348
  <!-- commandsstop -->
349
349
 
350
350
  > > > > > > > 0cb3e56d84c197f9d008836bb573eade212b7e57
@@ -18,7 +18,7 @@ class StartCommand extends SessionCommand_1.default {
18
18
  await this.initSession(flags);
19
19
  }
20
20
  async run() {
21
- var _a, _b, _c, _d, _e;
21
+ var _a, _b, _c, _d, _e, _f;
22
22
  // get configuration object
23
23
  const configObject = (_a = this.configManager) === null || _a === void 0 ? void 0 : _a.get();
24
24
  const config = configObject === null || configObject === void 0 ? void 0 : configObject.config;
@@ -45,6 +45,8 @@ class StartCommand extends SessionCommand_1.default {
45
45
  if (configObject.exercises) {
46
46
  const agent = ((_d = configObject.config) === null || _d === void 0 ? void 0 : _d.editor.agent) || "";
47
47
  const path = ((_e = configObject.config) === null || _e === void 0 ? void 0 : _e.dirPath) || "";
48
+ const tutorialSlug = ((_f = configObject.config) === null || _f === void 0 ? void 0 : _f.slug) || "";
49
+ console_1.default.info("Starting Telemetry with slug...", tutorialSlug);
48
50
  const steps = configObject.exercises.map((e, index) => ({
49
51
  slug: e.slug,
50
52
  position: e.position || index,
@@ -55,7 +57,7 @@ class StartCommand extends SessionCommand_1.default {
55
57
  is_testeable: e.graded || false,
56
58
  }));
57
59
  if (path && steps.length > 0) {
58
- telemetry_1.default.start(agent, steps, path);
60
+ telemetry_1.default.start(agent, steps, path, tutorialSlug);
59
61
  }
60
62
  if (config.telemetry) {
61
63
  telemetry_1.default.urls = config.telemetry;
@@ -43,6 +43,7 @@ declare type TStudent = {
43
43
  export interface ITelemetryJSONSchema {
44
44
  telemetry_id?: string;
45
45
  student?: TStudent;
46
+ slug: string;
46
47
  agent?: string;
47
48
  tutorial_started_at?: number;
48
49
  last_interaction_at?: number;
@@ -59,7 +60,7 @@ interface ITelemetryManager {
59
60
  configPath: string | null;
60
61
  urls: TTelemetryUrls;
61
62
  salute: (message: string) => void;
62
- start: (agent: string, steps: TStep[], path: string) => void;
63
+ start: (agent: string, steps: TStep[], path: string, tutorialSlug: string) => void;
63
64
  prevStep?: number;
64
65
  registerStepEvent: (stepPosition: number, event: TStepEvent, data: any) => void;
65
66
  streamEvent: (stepPosition: number, event: string, data: any) => void;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ const api_1 = require("../utils/api");
3
4
  const fs = require("fs");
4
5
  function createUUID() {
5
6
  return (Math.random().toString(36).slice(2, 10) +
@@ -15,7 +16,7 @@ const TelemetryManager = {
15
16
  salute: message => {
16
17
  console.log(message);
17
18
  },
18
- start: function (agent, steps, path) {
19
+ start: function (agent, steps, path, tutorialSlug) {
19
20
  this.configPath = path;
20
21
  if (!this.current) {
21
22
  this.retrieve(agent, steps)
@@ -28,6 +29,7 @@ const TelemetryManager = {
28
29
  else {
29
30
  this.current = {
30
31
  telemetry_id: createUUID(),
32
+ slug: tutorialSlug,
31
33
  agent,
32
34
  tutorial_started_at: Date.now(),
33
35
  steps,
@@ -159,22 +161,9 @@ const TelemetryManager = {
159
161
  const url = this.urls.batch;
160
162
  if (!url) {
161
163
  return;
162
- // throw new Error("Batch URL not specified");
163
164
  }
164
165
  const body = this.current;
165
- fetch(url, {
166
- method: "POST",
167
- headers: {
168
- "Content-Type": "application/json",
169
- },
170
- body: JSON.stringify(body),
171
- })
172
- .then(response => {
173
- return response.text();
174
- })
175
- .catch(error => {
176
- console.log("Error", error);
177
- });
166
+ api_1.default.sendBatchTelemetry(url, body);
178
167
  },
179
168
  save: function () {
180
169
  fs.writeFile(`${this.configPath}/telemetry.json`, JSON.stringify(this.current), (err) => {
@@ -200,20 +189,7 @@ const TelemetryManager = {
200
189
  event,
201
190
  data,
202
191
  };
203
- try {
204
- const response = await fetch(url, {
205
- method: "POST",
206
- headers: {
207
- "Content-Type": "application/json",
208
- },
209
- body: JSON.stringify(body),
210
- });
211
- const responseText = await response.text();
212
- }
213
- catch (error) {
214
- error;
215
- // Console.error(error);
216
- }
192
+ api_1.default.sendStreamTelemetry(url, body);
217
193
  },
218
194
  };
219
195
  exports.default = TelemetryManager;
@@ -8,5 +8,7 @@ declare const _default: {
8
8
  lang?: string | undefined;
9
9
  slug?: string | undefined;
10
10
  }) => Promise<any>;
11
+ sendBatchTelemetry: (url: string, body: object) => Promise<void>;
12
+ sendStreamTelemetry: (url: string, body: object) => Promise<void>;
11
13
  };
12
14
  export default _default;
package/lib/utils/api.js CHANGED
@@ -7,9 +7,9 @@ const HOST = "https://breathecode.herokuapp.com";
7
7
  const RIGOBOT_HOST = "https://rigobot.herokuapp.com";
8
8
  // eslint-disable-next-line
9
9
  const _fetch = require("node-fetch");
10
- const fetch = async (url, options = {}) => {
10
+ const fetch = async (url, options = {}, returnAsJson = true) => {
11
11
  const headers = { "Content-Type": "application/json" };
12
- console_1.default.log(`Fetching ${url}`);
12
+ console_1.default.debug(`Fetching ${url}`);
13
13
  let session = null;
14
14
  try {
15
15
  session = await storage.getItem("bc-payload");
@@ -19,10 +19,12 @@ const fetch = async (url, options = {}) => {
19
19
  catch (_a) { }
20
20
  try {
21
21
  const resp = await _fetch(url, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, headers), options.headers) }));
22
- if (resp.status >= 200 && resp.status < 300)
23
- return await resp.json();
22
+ if (resp.status >= 200 && resp.status < 300) {
23
+ return returnAsJson ? await resp.json() : await resp.text();
24
+ }
24
25
  if (resp.status === 401)
25
- throw APIError("Invalid authentication credentials", 401);
26
+ console_1.default.debug("Invalid authentication credentials", `Code: 401`);
27
+ // throw APIError("Invalid authentication credentials", 401)
26
28
  else if (resp.status === 404)
27
29
  throw APIError("Package not found", 404);
28
30
  else if (resp.status >= 500)
@@ -196,6 +198,50 @@ const APIError = (error, code) => {
196
198
  _err.status = code || 400;
197
199
  return _err;
198
200
  };
201
+ const sendBatchTelemetry = async function (url, body) {
202
+ if (!url) {
203
+ return;
204
+ }
205
+ const session = await storage.getItem("bc-payload");
206
+ if (!session ||
207
+ !Object.prototype.hasOwnProperty.call(session, "token") ||
208
+ session.token === "") {
209
+ console_1.default.debug("No token found, skipping stream telemetry delivery");
210
+ return;
211
+ }
212
+ fetch(url, {
213
+ method: "POST",
214
+ body: JSON.stringify(body),
215
+ }, false)
216
+ .then(response => {
217
+ return response.text();
218
+ })
219
+ .catch(error => {
220
+ console_1.default.debug("Error while sending batch Telemetry", error);
221
+ });
222
+ };
223
+ const sendStreamTelemetry = async function (url, body) {
224
+ if (!url) {
225
+ return;
226
+ }
227
+ const session = await storage.getItem("bc-payload");
228
+ if (!session ||
229
+ !Object.prototype.hasOwnProperty.call(session, "token") ||
230
+ session.token === "") {
231
+ console_1.default.debug("No token found, skipping stream telemetry delivery");
232
+ return;
233
+ }
234
+ fetch(url, {
235
+ method: "POST",
236
+ body: JSON.stringify(body),
237
+ }, false)
238
+ .then(response => {
239
+ return response;
240
+ })
241
+ .catch(error => {
242
+ console_1.default.debug("Error while sending stream Telemetry", error);
243
+ });
244
+ };
199
245
  exports.default = {
200
246
  login,
201
247
  publish,
@@ -203,4 +249,6 @@ exports.default = {
203
249
  getPackage,
204
250
  getLangs,
205
251
  getAllPackages,
252
+ sendBatchTelemetry,
253
+ sendStreamTelemetry,
206
254
  };
@@ -1 +1 @@
1
- {"version":"2.1.33","commands":{"audit":{"id":"audit","description":"learnpack audit is the command in charge of creating an auditory of the repository\n...\nlearnpack audit checks for the following information in a repository:\n 1. The configuration object has slug, repository and description. (Error)\n 2. The command learnpack clean has been run. (Error)\n 3. If a markdown or test file doesn't have any content. (Error)\n 4. The links are accessing to valid servers. (Error)\n 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)\n 6. The external images are working (If they are pointing to a valid server). (Error)\n 7. The exercises directory names are valid. (Error)\n 8. If an exercise doesn't have a README file. (Error)\n 9. The exercises array (Of the config file) has content. (Error)\n 10. The exercses have the same translations. (Warning)\n 11. The .gitignore file exists. (Warning)\n 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"clean":{"id":"clean","description":"Clean the configuration object\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"download":{"id":"download","description":"Describe the command here\n...\nExtra documentation goes here\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"init":{"id":"init","description":"Create a new learning package: Book, Tutorial or Exercise","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"grading":{"name":"grading","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"login":{"id":"login","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"logout":{"id":"logout","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"publish":{"id":"publish","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"start":{"id":"start","description":"Runs a small server with all the exercise instructions","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"port":{"name":"port","type":"option","char":"p","description":"server port"},"host":{"name":"host","type":"option","char":"h","description":"server host"},"disableGrading":{"name":"disableGrading","type":"boolean","char":"D","description":"disble grading functionality","allowNo":false},"watch":{"name":"watch","type":"boolean","char":"w","description":"Watch for file changes","allowNo":false},"editor":{"name":"editor","type":"option","char":"e","description":"[standalone, gitpod]","options":["standalone","gitpod"]},"version":{"name":"version","type":"option","char":"v","description":"E.g: 1.0.1"},"grading":{"name":"grading","type":"option","char":"g","description":"[isolated, incremental]","options":["isolated","incremental"]},"debug":{"name":"debug","type":"boolean","char":"d","description":"debugger mode for more verbage","allowNo":false}},"args":[]},"test":{"id":"test","description":"Test exercises","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"exerciseSlug","description":"The name of the exercise to test","required":false,"hidden":false}]}}}
1
+ {"version":"2.1.35","commands":{"audit":{"id":"audit","description":"learnpack audit is the command in charge of creating an auditory of the repository\n...\nlearnpack audit checks for the following information in a repository:\n 1. The configuration object has slug, repository and description. (Error)\n 2. The command learnpack clean has been run. (Error)\n 3. If a markdown or test file doesn't have any content. (Error)\n 4. The links are accessing to valid servers. (Error)\n 5. The relative images are working (If they have the shortest path to the image or if the images exists in the assets). (Error)\n 6. The external images are working (If they are pointing to a valid server). (Error)\n 7. The exercises directory names are valid. (Error)\n 8. If an exercise doesn't have a README file. (Error)\n 9. The exercises array (Of the config file) has content. (Error)\n 10. The exercses have the same translations. (Warning)\n 11. The .gitignore file exists. (Warning)\n 12. If there is a file within the exercises folder but not inside of any particular exercise's folder. (Warning)\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"clean":{"id":"clean","description":"Clean the configuration object\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[]},"download":{"id":"download","description":"Describe the command here\n...\nExtra documentation goes here\n","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"init":{"id":"init","description":"Create a new learning package: Book, Tutorial or Exercise","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"grading":{"name":"grading","type":"boolean","char":"h","description":"show CLI help","allowNo":false}},"args":[]},"login":{"id":"login","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"logout":{"id":"logout","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"publish":{"id":"publish","description":"Describe the command here\n ...\n Extra documentation goes here\n ","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"package","description":"The unique string that identifies this package on learnpack","required":false,"hidden":false}]},"start":{"id":"start","description":"Runs a small server with all the exercise instructions","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{"port":{"name":"port","type":"option","char":"p","description":"server port"},"host":{"name":"host","type":"option","char":"h","description":"server host"},"disableGrading":{"name":"disableGrading","type":"boolean","char":"D","description":"disble grading functionality","allowNo":false},"watch":{"name":"watch","type":"boolean","char":"w","description":"Watch for file changes","allowNo":false},"editor":{"name":"editor","type":"option","char":"e","description":"[standalone, gitpod]","options":["standalone","gitpod"]},"version":{"name":"version","type":"option","char":"v","description":"E.g: 1.0.1"},"grading":{"name":"grading","type":"option","char":"g","description":"[isolated, incremental]","options":["isolated","incremental"]},"debug":{"name":"debug","type":"boolean","char":"d","description":"debugger mode for more verbage","allowNo":false}},"args":[]},"test":{"id":"test","description":"Test exercises","pluginName":"@learnpack/learnpack","pluginType":"core","aliases":[],"flags":{},"args":[{"name":"exerciseSlug","description":"The name of the exercise to test","required":false,"hidden":false}]}}}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@learnpack/learnpack",
3
3
  "description": "Create, sell or download and take learning amazing learning packages",
4
- "version": "2.1.33",
4
+ "version": "2.1.35",
5
5
  "author": "Alejandro Sanchez @alesanchezr",
6
6
  "bin": {
7
7
  "learnpack": "bin/run"
@@ -119,7 +119,9 @@ export default class StartCommand extends SessionCommand {
119
119
  if (configObject.exercises) {
120
120
  const agent = configObject.config?.editor.agent || ""
121
121
  const path = configObject.config?.dirPath || ""
122
+ const tutorialSlug = configObject.config?.slug || ""
122
123
 
124
+ Console.info("Starting Telemetry with slug...", tutorialSlug)
123
125
  const steps = configObject.exercises.map(
124
126
  (e: IExercise, index): TStep => ({
125
127
  slug: e.slug,
@@ -132,7 +134,7 @@ export default class StartCommand extends SessionCommand {
132
134
  })
133
135
  )
134
136
  if (path && steps.length > 0) {
135
- TelemetryManager.start(agent, steps, path)
137
+ TelemetryManager.start(agent, steps, path, tutorialSlug)
136
138
  }
137
139
 
138
140
  if (config.telemetry) {
@@ -1,5 +1,5 @@
1
1
  import { IFile } from "../models/file"
2
-
2
+ import API from "../utils/api"
3
3
  const fs = require("fs")
4
4
 
5
5
  function createUUID(): string {
@@ -63,6 +63,7 @@ type TStudent = {
63
63
  export interface ITelemetryJSONSchema {
64
64
  telemetry_id?: string;
65
65
  student?: TStudent;
66
+ slug: string;
66
67
  agent?: string;
67
68
  tutorial_started_at?: number;
68
69
  last_interaction_at?: number;
@@ -83,7 +84,12 @@ interface ITelemetryManager {
83
84
  configPath: string | null;
84
85
  urls: TTelemetryUrls;
85
86
  salute: (message: string) => void;
86
- start: (agent: string, steps: TStep[], path: string) => void;
87
+ start: (
88
+ agent: string,
89
+ steps: TStep[],
90
+ path: string,
91
+ tutorialSlug: string
92
+ ) => void;
87
93
  prevStep?: number;
88
94
  registerStepEvent: (
89
95
  stepPosition: number,
@@ -109,7 +115,7 @@ const TelemetryManager: ITelemetryManager = {
109
115
  console.log(message)
110
116
  },
111
117
 
112
- start: function (agent, steps, path) {
118
+ start: function (agent, steps, path, tutorialSlug) {
113
119
  this.configPath = path
114
120
  if (!this.current) {
115
121
  this.retrieve(agent, steps)
@@ -121,6 +127,7 @@ const TelemetryManager: ITelemetryManager = {
121
127
  } else {
122
128
  this.current = {
123
129
  telemetry_id: createUUID(),
130
+ slug: tutorialSlug,
124
131
  agent,
125
132
  tutorial_started_at: Date.now(),
126
133
  steps,
@@ -277,23 +284,11 @@ return Promise.resolve()
277
284
  const url = this.urls.batch
278
285
  if (!url) {
279
286
  return
280
- // throw new Error("Batch URL not specified");
281
287
  }
282
288
 
283
289
  const body = this.current
284
- fetch(url, {
285
- method: "POST",
286
- headers: {
287
- "Content-Type": "application/json",
288
- },
289
- body: JSON.stringify(body),
290
- })
291
- .then(response => {
292
- return response.text()
293
- })
294
- .catch(error => {
295
- console.log("Error", error)
296
- })
290
+
291
+ API.sendBatchTelemetry(url, body)
297
292
  },
298
293
  save: function () {
299
294
  fs.writeFile(
@@ -327,19 +322,7 @@ return
327
322
  data,
328
323
  }
329
324
 
330
- try {
331
- const response = await fetch(url, {
332
- method: "POST",
333
- headers: {
334
- "Content-Type": "application/json",
335
- },
336
- body: JSON.stringify(body),
337
- })
338
- const responseText = await response.text()
339
- } catch (error) {
340
- error
341
- // Console.error(error);
342
- }
325
+ API.sendStreamTelemetry(url, body)
343
326
  },
344
327
  }
345
328
 
package/src/utils/api.ts CHANGED
@@ -18,9 +18,13 @@ interface IOptions {
18
18
  body?: string;
19
19
  }
20
20
 
21
- const fetch = async (url: string, options: IOptions = {}) => {
21
+ const fetch = async (
22
+ url: string,
23
+ options: IOptions = {},
24
+ returnAsJson = true
25
+ ) => {
22
26
  const headers: IHeaders = { "Content-Type": "application/json" }
23
- Console.log(`Fetching ${url}`)
27
+ Console.debug(`Fetching ${url}`)
24
28
  let session = null
25
29
  try {
26
30
  session = await storage.getItem("bc-payload")
@@ -34,10 +38,13 @@ const fetch = async (url: string, options: IOptions = {}) => {
34
38
  headers: { ...headers, ...options.headers },
35
39
  } as any)
36
40
 
37
- if (resp.status >= 200 && resp.status < 300)
38
- return await resp.json()
41
+ if (resp.status >= 200 && resp.status < 300) {
42
+ return returnAsJson ? await resp.json() : await resp.text()
43
+ }
44
+
39
45
  if (resp.status === 401)
40
- throw APIError("Invalid authentication credentials", 401)
46
+ Console.debug("Invalid authentication credentials", `Code: 401`)
47
+ // throw APIError("Invalid authentication credentials", 401)
41
48
  else if (resp.status === 404)
42
49
  throw APIError("Package not found", 404)
43
50
  else if (resp.status >= 500)
@@ -221,6 +228,68 @@ const APIError = (error: TypeError | string, code?: number) => {
221
228
  return _err
222
229
  }
223
230
 
231
+ const sendBatchTelemetry = async function (url: string, body: object) {
232
+ if (!url) {
233
+ return
234
+ }
235
+
236
+ const session = await storage.getItem("bc-payload")
237
+ if (
238
+ !session ||
239
+ !Object.prototype.hasOwnProperty.call(session, "token") ||
240
+ session.token === ""
241
+ ) {
242
+ Console.debug("No token found, skipping stream telemetry delivery")
243
+ return
244
+ }
245
+
246
+ fetch(
247
+ url,
248
+ {
249
+ method: "POST",
250
+ body: JSON.stringify(body),
251
+ },
252
+ false
253
+ )
254
+ .then(response => {
255
+ return response.text()
256
+ })
257
+ .catch(error => {
258
+ Console.debug("Error while sending batch Telemetry", error)
259
+ })
260
+ }
261
+
262
+ const sendStreamTelemetry = async function (url: string, body: object) {
263
+ if (!url) {
264
+ return
265
+ }
266
+
267
+ const session = await storage.getItem("bc-payload")
268
+ if (
269
+ !session ||
270
+ !Object.prototype.hasOwnProperty.call(session, "token") ||
271
+ session.token === ""
272
+ ) {
273
+ Console.debug("No token found, skipping stream telemetry delivery")
274
+ return
275
+ }
276
+
277
+ fetch(
278
+ url,
279
+ {
280
+ method: "POST",
281
+ body: JSON.stringify(body),
282
+ },
283
+ false
284
+ )
285
+ .then(response => {
286
+ return response
287
+ })
288
+ .catch(error => {
289
+ Console.debug("Error while sending stream Telemetry", error)
290
+ })
291
+ }
292
+
224
293
  export default {
225
294
  login,
226
295
  publish,
@@ -228,4 +297,6 @@ export default {
228
297
  getPackage,
229
298
  getLangs,
230
299
  getAllPackages,
300
+ sendBatchTelemetry,
301
+ sendStreamTelemetry,
231
302
  }