-
Notifications
You must be signed in to change notification settings - Fork 52
/
configuration.ts
87 lines (77 loc) · 2.17 KB
/
configuration.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { APIError, fetchApiJson, fetchApiText, streamLogs } from ".";
import { SupportedPlatforms } from "../const";
export interface CreateConfigParams {
name: string;
ssid: string;
psk: string;
board: string;
platform: SupportedPlatforms;
}
export interface Configuration {
storage_version: number;
name: string;
comment: string | null;
esphome_version: string;
src_version: number;
arduino_version: string;
address: string;
esp_platform: SupportedPlatforms;
board: string;
build_path: string;
firmware_bin_path: string;
loaded_integrations: string[];
}
export const createConfiguration = (params: CreateConfigParams) =>
fetchApiText("./wizard", {
method: "post",
body: JSON.stringify(params),
});
export const getConfiguration = (configuration: string) =>
fetchApiJson<Configuration>(`./info?configuration=${configuration}`);
export const deleteConfiguration = (configuration: string) =>
fetchApiText(`./delete?configuration=${configuration}`, {
method: "post",
});
export const compileConfiguration = (
configuration: string,
abortController?: AbortController
) => streamLogs("compile", { configuration }, undefined, abortController);
export const compileConfigurationMetadata = (
configuration: string,
abortController?: AbortController
) =>
streamLogs(
"compile",
{ configuration, only_generate: true },
undefined,
abortController
);
export const getDownloadUrl = (
configuration: string,
factoryFirmware: boolean
) => {
let url = `./download.bin?configuration=${encodeURIComponent(configuration)}`;
if (factoryFirmware) {
url += "&type=firmware-factory.bin";
}
return url;
};
// null if file not found.
export const getJsonConfig = async (
filename: string
): Promise<Record<string, any> | null> => {
try {
return fetchApiJson(`./json-config?configuration=${filename}`);
} catch (err) {
if (err instanceof APIError && err.status === 404) {
return null;
}
throw err;
}
};
export const getConfigurationApiKey = async (
configuration: string
): Promise<string | undefined> => {
const config = await getJsonConfig(configuration);
return config?.api?.encryption?.key;
};