-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathbackendRequests.ts
64 lines (56 loc) · 1.88 KB
/
backendRequests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { ErrorCode } from "./constants";
import { logError } from "./loggingService";
export async function fetchJsonDocAsync<T = any>(url: string): Promise<T | undefined> {
try {
const response = await fetch(url, {
cache: "no-cache",
});
if (!response.ok) {
throw new Error("Unable to fetch the json file");
} else {
const json = await response.json();
return json;
}
} catch (e) {
logError(ErrorCode.fetchJsonDocAsync, e);
}
return undefined;
}
export async function getProjectTextAsync(projectId: string): Promise<pxt.Cloud.JsonText | undefined> {
try {
const projectTextUrl = `${pxt.Cloud.apiRoot}/${projectId}/text`;
const response = await fetch(projectTextUrl);
if (!response.ok) {
throw new Error("Unable to fetch the project details");
} else {
const projectText = await response.json();
return projectText;
}
} catch (e) {
logError(ErrorCode.getProjectTextAsync, e);
}
return undefined;
}
export async function getProjectMetaAsync(projectId: string): Promise<pxt.Cloud.JsonScript | undefined> {
try {
const projectMetaUrl = `${pxt.Cloud.apiRoot}/${projectId}`;
const response = await fetch(projectMetaUrl);
if (!response.ok) {
throw new Error("Unable to fetch the project meta information");
} else {
const projectMeta = await response.json();
return projectMeta;
}
} catch (e) {
logError(ErrorCode.getProjectMetaAsync, e);
}
return undefined;
}
export async function downloadTargetConfigAsync(): Promise<pxt.TargetConfig | undefined> {
try {
return await pxt.targetConfigAsync();
} catch (e) {
logError(ErrorCode.downloadTargetConfigAsync, e);
}
return undefined;
}