@learnpack/learnpack 2.1.39 → 2.1.40
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +370 -35
- package/bin/run +17 -17
- package/bin/run.cmd +3 -3
- package/lib/commands/audit.d.ts +6 -6
- package/lib/commands/audit.js +342 -342
- package/lib/commands/clean.d.ts +8 -8
- package/lib/commands/clean.js +25 -25
- package/lib/commands/download.d.ts +13 -13
- package/lib/commands/download.js +55 -55
- package/lib/commands/init.d.ts +9 -9
- package/lib/commands/init.js +123 -123
- package/lib/commands/login.d.ts +14 -14
- package/lib/commands/login.js +37 -37
- package/lib/commands/logout.d.ts +14 -14
- package/lib/commands/logout.js +37 -37
- package/lib/commands/publish.d.ts +14 -14
- package/lib/commands/publish.js +82 -82
- package/lib/commands/start.d.ts +7 -7
- package/lib/commands/start.js +239 -208
- package/lib/commands/test.d.ts +6 -6
- package/lib/commands/test.js +62 -62
- package/lib/index.d.ts +1 -1
- package/lib/index.js +4 -4
- package/lib/managers/config/allowed_files.d.ts +5 -5
- package/lib/managers/config/allowed_files.js +30 -30
- package/lib/managers/config/defaults.d.ts +41 -41
- package/lib/managers/config/defaults.js +44 -44
- package/lib/managers/config/exercise.d.ts +36 -36
- package/lib/managers/config/exercise.js +236 -236
- package/lib/managers/config/index.d.ts +3 -3
- package/lib/managers/config/index.js +337 -337
- package/lib/managers/file.d.ts +14 -14
- package/lib/managers/file.js +153 -153
- package/lib/managers/gitpod.d.ts +3 -3
- package/lib/managers/gitpod.js +67 -67
- package/lib/managers/server/index.d.ts +6 -6
- package/lib/managers/server/index.js +58 -58
- package/lib/managers/server/routes.d.ts +4 -4
- package/lib/managers/server/routes.js +219 -219
- package/lib/managers/session.d.ts +3 -3
- package/lib/managers/session.js +125 -125
- package/lib/managers/socket.d.ts +3 -3
- package/lib/managers/socket.js +176 -176
- package/lib/managers/telemetry.d.ts +74 -74
- package/lib/managers/telemetry.js +206 -206
- package/lib/managers/test.js +84 -84
- package/lib/models/action.d.ts +2 -2
- package/lib/models/action.js +2 -2
- package/lib/models/audit.d.ts +15 -15
- package/lib/models/audit.js +2 -2
- package/lib/models/config-manager.d.ts +21 -21
- package/lib/models/config-manager.js +2 -2
- package/lib/models/config.d.ts +68 -67
- package/lib/models/config.js +2 -2
- package/lib/models/counter.d.ts +11 -11
- package/lib/models/counter.js +2 -2
- package/lib/models/errors.d.ts +15 -15
- package/lib/models/errors.js +2 -2
- package/lib/models/exercise-obj.d.ts +30 -30
- package/lib/models/exercise-obj.js +2 -2
- package/lib/models/file.d.ts +5 -5
- package/lib/models/file.js +2 -2
- package/lib/models/findings.d.ts +17 -17
- package/lib/models/findings.js +2 -2
- package/lib/models/flags.d.ts +10 -10
- package/lib/models/flags.js +2 -2
- package/lib/models/front-matter.d.ts +11 -11
- package/lib/models/front-matter.js +2 -2
- package/lib/models/gitpod-data.d.ts +16 -16
- package/lib/models/gitpod-data.js +2 -2
- package/lib/models/language.d.ts +4 -4
- package/lib/models/language.js +2 -2
- package/lib/models/package.d.ts +7 -7
- package/lib/models/package.js +2 -2
- package/lib/models/plugin-config.d.ts +16 -16
- package/lib/models/plugin-config.js +2 -2
- package/lib/models/session.d.ts +31 -31
- package/lib/models/session.js +2 -2
- package/lib/models/socket.d.ts +36 -32
- package/lib/models/socket.js +2 -2
- package/lib/models/status.d.ts +1 -1
- package/lib/models/status.js +2 -2
- package/lib/models/success-types.d.ts +1 -1
- package/lib/models/success-types.js +2 -2
- package/lib/plugin/command/compile.d.ts +6 -6
- package/lib/plugin/command/compile.js +18 -18
- package/lib/plugin/command/test.d.ts +6 -6
- package/lib/plugin/command/test.js +25 -25
- package/lib/plugin/index.d.ts +27 -27
- package/lib/plugin/index.js +7 -7
- package/lib/plugin/plugin.d.ts +8 -8
- package/lib/plugin/plugin.js +68 -68
- package/lib/plugin/utils.d.ts +16 -16
- package/lib/plugin/utils.js +58 -58
- package/lib/ui/download.d.ts +5 -5
- package/lib/ui/download.js +61 -61
- package/lib/utils/BaseCommand.d.ts +8 -8
- package/lib/utils/BaseCommand.js +41 -41
- package/lib/utils/SessionCommand.d.ts +10 -10
- package/lib/utils/SessionCommand.js +43 -43
- package/lib/utils/api.d.ts +14 -14
- package/lib/utils/api.js +255 -255
- package/lib/utils/audit.d.ts +16 -16
- package/lib/utils/audit.js +303 -303
- package/lib/utils/checkNotInstalled.d.ts +2 -0
- package/lib/utils/checkNotInstalled.js +36 -0
- package/lib/utils/console.d.ts +12 -12
- package/lib/utils/console.js +19 -19
- package/lib/utils/errors.d.ts +17 -17
- package/lib/utils/errors.js +100 -100
- package/lib/utils/exercisesQueue.d.ts +9 -9
- package/lib/utils/exercisesQueue.js +38 -38
- package/lib/utils/fileQueue.d.ts +40 -40
- package/lib/utils/fileQueue.js +168 -168
- package/lib/utils/misc.d.ts +1 -1
- package/lib/utils/misc.js +23 -23
- package/lib/utils/osOperations.d.ts +5 -5
- package/lib/utils/osOperations.js +72 -72
- package/lib/utils/validators.d.ts +5 -5
- package/lib/utils/validators.js +17 -17
- package/lib/utils/watcher.d.ts +2 -2
- package/lib/utils/watcher.js +25 -25
- package/oclif.manifest.json +1 -1
- package/package.json +139 -139
- package/src/commands/audit.ts +443 -443
- package/src/commands/clean.ts +29 -29
- package/src/commands/download.ts +61 -61
- package/src/commands/init.ts +170 -170
- package/src/commands/login.ts +42 -42
- package/src/commands/logout.ts +43 -43
- package/src/commands/publish.ts +107 -107
- package/src/commands/start.ts +53 -23
- package/src/commands/test.ts +85 -85
- package/src/index.ts +1 -1
- package/src/managers/config/allowed_files.ts +29 -29
- package/src/managers/config/defaults.ts +42 -42
- package/src/managers/config/exercise.ts +311 -311
- package/src/managers/config/index.ts +455 -455
- package/src/managers/file.ts +196 -196
- package/src/managers/gitpod.ts +84 -84
- package/src/managers/server/index.ts +78 -78
- package/src/managers/server/routes.ts +330 -330
- package/src/managers/session.ts +145 -145
- package/src/managers/socket.ts +250 -250
- package/src/managers/telemetry.ts +346 -346
- package/src/managers/test.ts +83 -83
- package/src/models/action.ts +10 -10
- package/src/models/audit.ts +16 -16
- package/src/models/config-manager.ts +23 -23
- package/src/models/config.ts +5 -3
- package/src/models/counter.ts +11 -11
- package/src/models/errors.ts +22 -22
- package/src/models/exercise-obj.ts +29 -29
- package/src/models/file.ts +5 -5
- package/src/models/findings.ts +18 -18
- package/src/models/flags.ts +10 -10
- package/src/models/front-matter.ts +11 -11
- package/src/models/gitpod-data.ts +19 -19
- package/src/models/language.ts +4 -4
- package/src/models/package.ts +7 -7
- package/src/models/plugin-config.ts +17 -17
- package/src/models/session.ts +34 -34
- package/src/models/socket.ts +5 -0
- package/src/models/status.ts +16 -16
- package/src/models/success-types.ts +1 -1
- package/src/plugin/command/compile.ts +17 -17
- package/src/plugin/command/test.ts +30 -30
- package/src/plugin/index.ts +6 -6
- package/src/plugin/plugin.ts +94 -94
- package/src/plugin/utils.ts +87 -87
- package/src/types/node-fetch.d.ts +1 -1
- package/src/ui/download.ts +71 -71
- package/src/utils/BaseCommand.ts +48 -48
- package/src/utils/SessionCommand.ts +43 -43
- package/src/utils/api.ts +303 -303
- package/src/utils/audit.ts +393 -393
- package/src/utils/checkNotInstalled.ts +46 -0
- package/src/utils/console.ts +24 -24
- package/src/utils/errors.ts +117 -117
- package/src/utils/exercisesQueue.ts +51 -51
- package/src/utils/fileQueue.ts +198 -198
- package/src/utils/misc.ts +23 -23
- package/src/utils/osOperations.ts +79 -79
- package/src/utils/templates/gitignore.txt +19 -19
- package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.es.md +24 -24
- package/src/utils/templates/incremental/.learn/exercises/01-hello-world/README.md +24 -24
- package/src/utils/templates/incremental/README.ejs +4 -4
- package/src/utils/templates/incremental/README.es.ejs +4 -4
- package/src/utils/templates/isolated/01-hello-world/README.es.md +26 -26
- package/src/utils/templates/isolated/01-hello-world/README.md +26 -26
- package/src/utils/templates/isolated/README.ejs +4 -4
- package/src/utils/templates/isolated/README.es.ejs +4 -4
- package/src/utils/templates/no-grading/README.ejs +4 -4
- package/src/utils/templates/no-grading/README.es.ejs +4 -4
- package/src/utils/validators.ts +18 -18
- package/src/utils/watcher.ts +27 -27
package/src/utils/api.ts
CHANGED
@@ -1,303 +1,303 @@
|
|
1
|
-
import Console from "../utils/console"
|
2
|
-
import * as storage from "node-persist"
|
3
|
-
import cli from "cli-ux"
|
4
|
-
const HOST = "https://breathecode.herokuapp.com"
|
5
|
-
const RIGOBOT_HOST = "https://rigobot.herokuapp.com"
|
6
|
-
|
7
|
-
// eslint-disable-next-line
|
8
|
-
const _fetch = require("node-fetch");
|
9
|
-
|
10
|
-
interface IHeaders {
|
11
|
-
"Content-Type"?: string;
|
12
|
-
Authorization?: string;
|
13
|
-
}
|
14
|
-
|
15
|
-
interface IOptions {
|
16
|
-
headers?: IHeaders;
|
17
|
-
method?: string;
|
18
|
-
body?: string;
|
19
|
-
}
|
20
|
-
|
21
|
-
const fetch = async (
|
22
|
-
url: string,
|
23
|
-
options: IOptions = {},
|
24
|
-
returnAsJson = true
|
25
|
-
) => {
|
26
|
-
const headers: IHeaders = { "Content-Type": "application/json" }
|
27
|
-
Console.debug(`Fetching ${url}`)
|
28
|
-
let session = null
|
29
|
-
try {
|
30
|
-
session = await storage.getItem("bc-payload")
|
31
|
-
if (session.token && session.token !== "" && !url.includes("/token"))
|
32
|
-
headers.Authorization = "Token " + session.token
|
33
|
-
} catch {}
|
34
|
-
|
35
|
-
try {
|
36
|
-
const resp = await _fetch(url, {
|
37
|
-
...options,
|
38
|
-
headers: { ...headers, ...options.headers },
|
39
|
-
} as any)
|
40
|
-
|
41
|
-
if (resp.status >= 200 && resp.status < 300) {
|
42
|
-
return returnAsJson ? await resp.json() : await resp.text()
|
43
|
-
}
|
44
|
-
|
45
|
-
if (resp.status === 401)
|
46
|
-
Console.debug("Invalid authentication credentials", `Code: 401`)
|
47
|
-
// throw APIError("Invalid authentication credentials", 401)
|
48
|
-
else if (resp.status === 404)
|
49
|
-
throw APIError("Package not found", 404)
|
50
|
-
else if (resp.status >= 500)
|
51
|
-
throw APIError("Impossible to connect with the server", 500)
|
52
|
-
else if (resp.status >= 400) {
|
53
|
-
const error = await resp.json()
|
54
|
-
if (error.detail || error.error) {
|
55
|
-
throw APIError(error.detail || error.error)
|
56
|
-
} else if (error.nonFieldErrors) {
|
57
|
-
throw APIError(error.nonFieldErrors[0], error)
|
58
|
-
} else if (typeof error === "object") {
|
59
|
-
if (Object.keys(error).length > 0) {
|
60
|
-
const key = error[Object.keys(error)[0]]
|
61
|
-
throw APIError(`${key}: ${error[key][0]}`, error)
|
62
|
-
}
|
63
|
-
} else {
|
64
|
-
throw APIError("Uknown error")
|
65
|
-
}
|
66
|
-
} else
|
67
|
-
throw APIError("Uknown error")
|
68
|
-
} catch (error) {
|
69
|
-
Console.error((error as TypeError).message)
|
70
|
-
throw error
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
const login = async (identification: string, password: string) => {
|
75
|
-
try {
|
76
|
-
cli.action.start(`Looking for credentials with ${identification}`)
|
77
|
-
await cli.wait(1000)
|
78
|
-
const url = `${HOST}/v1/auth/login/`
|
79
|
-
|
80
|
-
const data = await fetch(url, {
|
81
|
-
body: JSON.stringify({
|
82
|
-
email: identification,
|
83
|
-
password: password,
|
84
|
-
}),
|
85
|
-
method: "post",
|
86
|
-
})
|
87
|
-
cli.action.stop("ready")
|
88
|
-
let rigoPayload = null
|
89
|
-
try {
|
90
|
-
rigoPayload = await loginRigo(data.token)
|
91
|
-
} catch {
|
92
|
-
return { ...data, rigobot: null }
|
93
|
-
}
|
94
|
-
|
95
|
-
return { ...data, rigobot: rigoPayload }
|
96
|
-
} catch (error) {
|
97
|
-
cli.action.stop("error")
|
98
|
-
Console.error((error as TypeError).message)
|
99
|
-
Console.debug(error)
|
100
|
-
}
|
101
|
-
}
|
102
|
-
|
103
|
-
const loginRigo = async (token: string) => {
|
104
|
-
try {
|
105
|
-
const rigoUrl = `${RIGOBOT_HOST}/v1/auth/me/token?breathecode_token=${token}`
|
106
|
-
const rigoResp = await _fetch(rigoUrl)
|
107
|
-
const rigobotJson = await rigoResp.json()
|
108
|
-
return rigobotJson
|
109
|
-
} catch (error) {
|
110
|
-
// Handle the error as needed, for example log it or return a custom error message
|
111
|
-
Console.error(
|
112
|
-
"Error logging in to Rigo, did you already accepted Rigobot?:",
|
113
|
-
error
|
114
|
-
)
|
115
|
-
throw new Error("Failed to log in to Rigo")
|
116
|
-
}
|
117
|
-
}
|
118
|
-
|
119
|
-
const publish = async (config: any) => {
|
120
|
-
const keys = [
|
121
|
-
"difficulty",
|
122
|
-
"language",
|
123
|
-
"skills",
|
124
|
-
"technologies",
|
125
|
-
"slug",
|
126
|
-
"repository",
|
127
|
-
"author",
|
128
|
-
"title",
|
129
|
-
]
|
130
|
-
|
131
|
-
const payload: { [key: string]: string } = {}
|
132
|
-
for (const k of keys)
|
133
|
-
config[k] ? (payload[k] = config[k]) : null
|
134
|
-
try {
|
135
|
-
console.log("Package to publish:", payload)
|
136
|
-
cli.action.start("Updating package information...")
|
137
|
-
await cli.wait(1000)
|
138
|
-
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
139
|
-
method: "PUT",
|
140
|
-
body: JSON.stringify(payload),
|
141
|
-
})
|
142
|
-
cli.action.stop("ready")
|
143
|
-
return data
|
144
|
-
} catch (error) {
|
145
|
-
Console.error((error as TypeError).message)
|
146
|
-
Console.debug(error)
|
147
|
-
throw error
|
148
|
-
}
|
149
|
-
}
|
150
|
-
|
151
|
-
const update = async (config: any) => {
|
152
|
-
try {
|
153
|
-
cli.action.start("Updating package information...")
|
154
|
-
await cli.wait(1000)
|
155
|
-
const data = await fetch(`${HOST}/v1/package/`, {
|
156
|
-
method: "POST",
|
157
|
-
body: JSON.stringify(config),
|
158
|
-
})
|
159
|
-
cli.action.stop("ready")
|
160
|
-
return data
|
161
|
-
} catch (error) {
|
162
|
-
Console.error((error as any).message)
|
163
|
-
Console.debug(error)
|
164
|
-
throw error
|
165
|
-
}
|
166
|
-
}
|
167
|
-
|
168
|
-
const getPackage = async (slug: string) => {
|
169
|
-
try {
|
170
|
-
cli.action.start("Downloading package information...")
|
171
|
-
await cli.wait(1000)
|
172
|
-
const data = await fetch(`${HOST}/v1/package/${slug}`)
|
173
|
-
cli.action.stop("ready")
|
174
|
-
return data
|
175
|
-
} catch (error) {
|
176
|
-
if ((error as any).status === 404)
|
177
|
-
Console.error(`Package ${slug} does not exist`)
|
178
|
-
else
|
179
|
-
Console.error(`Package ${slug} does not exist`)
|
180
|
-
Console.debug(error)
|
181
|
-
throw error
|
182
|
-
}
|
183
|
-
}
|
184
|
-
|
185
|
-
const getLangs = async () => {
|
186
|
-
try {
|
187
|
-
cli.action.start("Downloading language options...")
|
188
|
-
await cli.wait(1000)
|
189
|
-
const data = await fetch(`${HOST}/v1/package/language`)
|
190
|
-
cli.action.stop("ready")
|
191
|
-
return data
|
192
|
-
} catch (error) {
|
193
|
-
if ((error as any).status === 404)
|
194
|
-
Console.error("Package slug does not exist")
|
195
|
-
else
|
196
|
-
Console.error("Package slug does not exist")
|
197
|
-
Console.debug(error)
|
198
|
-
throw error
|
199
|
-
}
|
200
|
-
}
|
201
|
-
|
202
|
-
const getAllPackages = async ({
|
203
|
-
lang = "",
|
204
|
-
slug = "",
|
205
|
-
}: {
|
206
|
-
lang?: string;
|
207
|
-
slug?: string;
|
208
|
-
}) => {
|
209
|
-
try {
|
210
|
-
cli.action.start("Downloading packages...")
|
211
|
-
await cli.wait(1000)
|
212
|
-
const data = await fetch(
|
213
|
-
`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`
|
214
|
-
)
|
215
|
-
cli.action.stop("ready")
|
216
|
-
return data
|
217
|
-
} catch (error) {
|
218
|
-
Console.error(`Package ${slug} does not exist`)
|
219
|
-
Console.debug(error)
|
220
|
-
throw error
|
221
|
-
}
|
222
|
-
}
|
223
|
-
|
224
|
-
const APIError = (error: TypeError | string, code?: number) => {
|
225
|
-
const message: string = (error as TypeError).message || (error as string)
|
226
|
-
const _err = new Error(message) as any
|
227
|
-
_err.status = code || 400
|
228
|
-
return _err
|
229
|
-
}
|
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
|
-
Console.debug("Telemetry sent successfully")
|
256
|
-
return response.text()
|
257
|
-
})
|
258
|
-
.catch(error => {
|
259
|
-
Console.debug("Error while sending batch Telemetry", error)
|
260
|
-
})
|
261
|
-
}
|
262
|
-
|
263
|
-
const sendStreamTelemetry = async function (url: string, body: object) {
|
264
|
-
if (!url) {
|
265
|
-
return
|
266
|
-
}
|
267
|
-
|
268
|
-
const session = await storage.getItem("bc-payload")
|
269
|
-
if (
|
270
|
-
!session ||
|
271
|
-
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
272
|
-
session.token === ""
|
273
|
-
) {
|
274
|
-
Console.debug("No token found, skipping stream telemetry delivery")
|
275
|
-
return
|
276
|
-
}
|
277
|
-
|
278
|
-
fetch(
|
279
|
-
url,
|
280
|
-
{
|
281
|
-
method: "POST",
|
282
|
-
body: JSON.stringify(body),
|
283
|
-
},
|
284
|
-
false
|
285
|
-
)
|
286
|
-
.then(response => {
|
287
|
-
return response
|
288
|
-
})
|
289
|
-
.catch(error => {
|
290
|
-
Console.debug("Error while sending stream Telemetry", error)
|
291
|
-
})
|
292
|
-
}
|
293
|
-
|
294
|
-
export default {
|
295
|
-
login,
|
296
|
-
publish,
|
297
|
-
update,
|
298
|
-
getPackage,
|
299
|
-
getLangs,
|
300
|
-
getAllPackages,
|
301
|
-
sendBatchTelemetry,
|
302
|
-
sendStreamTelemetry,
|
303
|
-
}
|
1
|
+
import Console from "../utils/console"
|
2
|
+
import * as storage from "node-persist"
|
3
|
+
import cli from "cli-ux"
|
4
|
+
const HOST = "https://breathecode.herokuapp.com"
|
5
|
+
const RIGOBOT_HOST = "https://rigobot.herokuapp.com"
|
6
|
+
|
7
|
+
// eslint-disable-next-line
|
8
|
+
const _fetch = require("node-fetch");
|
9
|
+
|
10
|
+
interface IHeaders {
|
11
|
+
"Content-Type"?: string;
|
12
|
+
Authorization?: string;
|
13
|
+
}
|
14
|
+
|
15
|
+
interface IOptions {
|
16
|
+
headers?: IHeaders;
|
17
|
+
method?: string;
|
18
|
+
body?: string;
|
19
|
+
}
|
20
|
+
|
21
|
+
const fetch = async (
|
22
|
+
url: string,
|
23
|
+
options: IOptions = {},
|
24
|
+
returnAsJson = true
|
25
|
+
) => {
|
26
|
+
const headers: IHeaders = { "Content-Type": "application/json" }
|
27
|
+
Console.debug(`Fetching ${url}`)
|
28
|
+
let session = null
|
29
|
+
try {
|
30
|
+
session = await storage.getItem("bc-payload")
|
31
|
+
if (session.token && session.token !== "" && !url.includes("/token"))
|
32
|
+
headers.Authorization = "Token " + session.token
|
33
|
+
} catch {}
|
34
|
+
|
35
|
+
try {
|
36
|
+
const resp = await _fetch(url, {
|
37
|
+
...options,
|
38
|
+
headers: { ...headers, ...options.headers },
|
39
|
+
} as any)
|
40
|
+
|
41
|
+
if (resp.status >= 200 && resp.status < 300) {
|
42
|
+
return returnAsJson ? await resp.json() : await resp.text()
|
43
|
+
}
|
44
|
+
|
45
|
+
if (resp.status === 401)
|
46
|
+
Console.debug("Invalid authentication credentials", `Code: 401`)
|
47
|
+
// throw APIError("Invalid authentication credentials", 401)
|
48
|
+
else if (resp.status === 404)
|
49
|
+
throw APIError("Package not found", 404)
|
50
|
+
else if (resp.status >= 500)
|
51
|
+
throw APIError("Impossible to connect with the server", 500)
|
52
|
+
else if (resp.status >= 400) {
|
53
|
+
const error = await resp.json()
|
54
|
+
if (error.detail || error.error) {
|
55
|
+
throw APIError(error.detail || error.error)
|
56
|
+
} else if (error.nonFieldErrors) {
|
57
|
+
throw APIError(error.nonFieldErrors[0], error)
|
58
|
+
} else if (typeof error === "object") {
|
59
|
+
if (Object.keys(error).length > 0) {
|
60
|
+
const key = error[Object.keys(error)[0]]
|
61
|
+
throw APIError(`${key}: ${error[key][0]}`, error)
|
62
|
+
}
|
63
|
+
} else {
|
64
|
+
throw APIError("Uknown error")
|
65
|
+
}
|
66
|
+
} else
|
67
|
+
throw APIError("Uknown error")
|
68
|
+
} catch (error) {
|
69
|
+
Console.error((error as TypeError).message)
|
70
|
+
throw error
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
const login = async (identification: string, password: string) => {
|
75
|
+
try {
|
76
|
+
cli.action.start(`Looking for credentials with ${identification}`)
|
77
|
+
await cli.wait(1000)
|
78
|
+
const url = `${HOST}/v1/auth/login/`
|
79
|
+
|
80
|
+
const data = await fetch(url, {
|
81
|
+
body: JSON.stringify({
|
82
|
+
email: identification,
|
83
|
+
password: password,
|
84
|
+
}),
|
85
|
+
method: "post",
|
86
|
+
})
|
87
|
+
cli.action.stop("ready")
|
88
|
+
let rigoPayload = null
|
89
|
+
try {
|
90
|
+
rigoPayload = await loginRigo(data.token)
|
91
|
+
} catch {
|
92
|
+
return { ...data, rigobot: null }
|
93
|
+
}
|
94
|
+
|
95
|
+
return { ...data, rigobot: rigoPayload }
|
96
|
+
} catch (error) {
|
97
|
+
cli.action.stop("error")
|
98
|
+
Console.error((error as TypeError).message)
|
99
|
+
Console.debug(error)
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
const loginRigo = async (token: string) => {
|
104
|
+
try {
|
105
|
+
const rigoUrl = `${RIGOBOT_HOST}/v1/auth/me/token?breathecode_token=${token}`
|
106
|
+
const rigoResp = await _fetch(rigoUrl)
|
107
|
+
const rigobotJson = await rigoResp.json()
|
108
|
+
return rigobotJson
|
109
|
+
} catch (error) {
|
110
|
+
// Handle the error as needed, for example log it or return a custom error message
|
111
|
+
Console.error(
|
112
|
+
"Error logging in to Rigo, did you already accepted Rigobot?:",
|
113
|
+
error
|
114
|
+
)
|
115
|
+
throw new Error("Failed to log in to Rigo")
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
const publish = async (config: any) => {
|
120
|
+
const keys = [
|
121
|
+
"difficulty",
|
122
|
+
"language",
|
123
|
+
"skills",
|
124
|
+
"technologies",
|
125
|
+
"slug",
|
126
|
+
"repository",
|
127
|
+
"author",
|
128
|
+
"title",
|
129
|
+
]
|
130
|
+
|
131
|
+
const payload: { [key: string]: string } = {}
|
132
|
+
for (const k of keys)
|
133
|
+
config[k] ? (payload[k] = config[k]) : null
|
134
|
+
try {
|
135
|
+
console.log("Package to publish:", payload)
|
136
|
+
cli.action.start("Updating package information...")
|
137
|
+
await cli.wait(1000)
|
138
|
+
const data = await fetch(`${HOST}/v1/package/${config.slug}`, {
|
139
|
+
method: "PUT",
|
140
|
+
body: JSON.stringify(payload),
|
141
|
+
})
|
142
|
+
cli.action.stop("ready")
|
143
|
+
return data
|
144
|
+
} catch (error) {
|
145
|
+
Console.error((error as TypeError).message)
|
146
|
+
Console.debug(error)
|
147
|
+
throw error
|
148
|
+
}
|
149
|
+
}
|
150
|
+
|
151
|
+
const update = async (config: any) => {
|
152
|
+
try {
|
153
|
+
cli.action.start("Updating package information...")
|
154
|
+
await cli.wait(1000)
|
155
|
+
const data = await fetch(`${HOST}/v1/package/`, {
|
156
|
+
method: "POST",
|
157
|
+
body: JSON.stringify(config),
|
158
|
+
})
|
159
|
+
cli.action.stop("ready")
|
160
|
+
return data
|
161
|
+
} catch (error) {
|
162
|
+
Console.error((error as any).message)
|
163
|
+
Console.debug(error)
|
164
|
+
throw error
|
165
|
+
}
|
166
|
+
}
|
167
|
+
|
168
|
+
const getPackage = async (slug: string) => {
|
169
|
+
try {
|
170
|
+
cli.action.start("Downloading package information...")
|
171
|
+
await cli.wait(1000)
|
172
|
+
const data = await fetch(`${HOST}/v1/package/${slug}`)
|
173
|
+
cli.action.stop("ready")
|
174
|
+
return data
|
175
|
+
} catch (error) {
|
176
|
+
if ((error as any).status === 404)
|
177
|
+
Console.error(`Package ${slug} does not exist`)
|
178
|
+
else
|
179
|
+
Console.error(`Package ${slug} does not exist`)
|
180
|
+
Console.debug(error)
|
181
|
+
throw error
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
const getLangs = async () => {
|
186
|
+
try {
|
187
|
+
cli.action.start("Downloading language options...")
|
188
|
+
await cli.wait(1000)
|
189
|
+
const data = await fetch(`${HOST}/v1/package/language`)
|
190
|
+
cli.action.stop("ready")
|
191
|
+
return data
|
192
|
+
} catch (error) {
|
193
|
+
if ((error as any).status === 404)
|
194
|
+
Console.error("Package slug does not exist")
|
195
|
+
else
|
196
|
+
Console.error("Package slug does not exist")
|
197
|
+
Console.debug(error)
|
198
|
+
throw error
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
const getAllPackages = async ({
|
203
|
+
lang = "",
|
204
|
+
slug = "",
|
205
|
+
}: {
|
206
|
+
lang?: string;
|
207
|
+
slug?: string;
|
208
|
+
}) => {
|
209
|
+
try {
|
210
|
+
cli.action.start("Downloading packages...")
|
211
|
+
await cli.wait(1000)
|
212
|
+
const data = await fetch(
|
213
|
+
`${HOST}/v1/package/all?limit=100&language=${lang}&slug=${slug}`
|
214
|
+
)
|
215
|
+
cli.action.stop("ready")
|
216
|
+
return data
|
217
|
+
} catch (error) {
|
218
|
+
Console.error(`Package ${slug} does not exist`)
|
219
|
+
Console.debug(error)
|
220
|
+
throw error
|
221
|
+
}
|
222
|
+
}
|
223
|
+
|
224
|
+
const APIError = (error: TypeError | string, code?: number) => {
|
225
|
+
const message: string = (error as TypeError).message || (error as string)
|
226
|
+
const _err = new Error(message) as any
|
227
|
+
_err.status = code || 400
|
228
|
+
return _err
|
229
|
+
}
|
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
|
+
Console.debug("Telemetry sent successfully")
|
256
|
+
return response.text()
|
257
|
+
})
|
258
|
+
.catch(error => {
|
259
|
+
Console.debug("Error while sending batch Telemetry", error)
|
260
|
+
})
|
261
|
+
}
|
262
|
+
|
263
|
+
const sendStreamTelemetry = async function (url: string, body: object) {
|
264
|
+
if (!url) {
|
265
|
+
return
|
266
|
+
}
|
267
|
+
|
268
|
+
const session = await storage.getItem("bc-payload")
|
269
|
+
if (
|
270
|
+
!session ||
|
271
|
+
!Object.prototype.hasOwnProperty.call(session, "token") ||
|
272
|
+
session.token === ""
|
273
|
+
) {
|
274
|
+
Console.debug("No token found, skipping stream telemetry delivery")
|
275
|
+
return
|
276
|
+
}
|
277
|
+
|
278
|
+
fetch(
|
279
|
+
url,
|
280
|
+
{
|
281
|
+
method: "POST",
|
282
|
+
body: JSON.stringify(body),
|
283
|
+
},
|
284
|
+
false
|
285
|
+
)
|
286
|
+
.then(response => {
|
287
|
+
return response
|
288
|
+
})
|
289
|
+
.catch(error => {
|
290
|
+
Console.debug("Error while sending stream Telemetry", error)
|
291
|
+
})
|
292
|
+
}
|
293
|
+
|
294
|
+
export default {
|
295
|
+
login,
|
296
|
+
publish,
|
297
|
+
update,
|
298
|
+
getPackage,
|
299
|
+
getLangs,
|
300
|
+
getAllPackages,
|
301
|
+
sendBatchTelemetry,
|
302
|
+
sendStreamTelemetry,
|
303
|
+
}
|