parent
910b61cd81
commit
62faaf6384
|
|
@ -8,6 +8,7 @@ import {
|
||||||
ThemeInfo,
|
ThemeInfo,
|
||||||
ThemeManifest,
|
ThemeManifest,
|
||||||
ThemeFileInfo,
|
ThemeFileInfo,
|
||||||
|
buildThemeFileInfoFromRecord,
|
||||||
buildThemeInfoFromRecord,
|
buildThemeInfoFromRecord,
|
||||||
} from "../entities/themes";
|
} from "../entities/themes";
|
||||||
|
|
||||||
|
|
@ -159,19 +160,96 @@ export class ThemesApiClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// async getThemeFile(
|
async getThemeFileInfo(
|
||||||
// themeName: string,
|
themeName: string,
|
||||||
// path: string | null = null
|
path = ""
|
||||||
// ): Promise<ThemeFileInfo[]> {
|
): Promise<ThemeFileInfo> {
|
||||||
// try {
|
try {
|
||||||
// const response: ApiResponse =
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
// } catch (err) {
|
`${this.#THEMES_API_PREFIX}/${themeName}/files/${path}/@info`
|
||||||
// if (path) {
|
);
|
||||||
// throw `Failed to get file ${path} of theme ${themeName}: ${err}`;
|
|
||||||
// } else {
|
if (response.ok) {
|
||||||
// throw `Failed to get root directory of
|
const result: Record<
|
||||||
// theme ${themeName}: ${err}`;
|
string,
|
||||||
// }
|
unknown
|
||||||
// }
|
> = (await response.json()) as Record<string, unknown>;
|
||||||
// }
|
|
||||||
|
return buildThemeFileInfoFromRecord(result);
|
||||||
|
} else {
|
||||||
|
throw `Failed to get theme file info for file ${path}
|
||||||
|
of theme ${themeName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to get theme file info for file ${path}
|
||||||
|
of theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async listThemesFiles(
|
||||||
|
themeName: string,
|
||||||
|
path = ""
|
||||||
|
): Promise<ThemeFileInfo[]> {
|
||||||
|
try {
|
||||||
|
const isDirectory = await this.getThemeFileInfo(themeName, path);
|
||||||
|
if (!isDirectory) {
|
||||||
|
throw `Failed to list files in directory ${path}
|
||||||
|
of theme ${themeName}: Is not a directory.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#THEMES_API_PREFIX}/${themeName}/files/${path}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return (await response.json()) as ThemeFileInfo[];
|
||||||
|
} else {
|
||||||
|
throw `Failed to list files in directory ${path}
|
||||||
|
of theme ${themeName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to list files in directory ${path}
|
||||||
|
of theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getThemeFile(themeName: string, path = ""): Promise<ArrayBuffer> {
|
||||||
|
try {
|
||||||
|
const isDirectory = await this.getThemeFileInfo(themeName, path);
|
||||||
|
if (!isDirectory) {
|
||||||
|
throw `Failed to get file ${path}
|
||||||
|
from theme ${themeName}: Is a directory.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#THEMES_API_PREFIX}/${themeName}/files/${path}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return response.arrayBuffer();
|
||||||
|
} else {
|
||||||
|
throw `Failed to get file ${path} of theme ${themeName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to get file ${path} of theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async createOrUpdateThemeFile(
|
||||||
|
themeName: string,
|
||||||
|
path: string,
|
||||||
|
data: unknown
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.put(
|
||||||
|
`${this.#THEMES_API_PREFIX}/${themeName}/files/${path}`,
|
||||||
|
data
|
||||||
|
);
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to create or update file ${path} in theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,3 +73,23 @@ export function buildThemeManifestFromRecord(
|
||||||
defaultTemplate: record.defaultTemplate as string,
|
defaultTemplate: record.defaultTemplate as string,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function buildThemeFileInfoFromRecord(
|
||||||
|
record: Record<string, unknown>
|
||||||
|
): ThemeFileInfo {
|
||||||
|
assertProperties(record, [
|
||||||
|
"name",
|
||||||
|
"directory",
|
||||||
|
"mimeType",
|
||||||
|
"size",
|
||||||
|
"writable",
|
||||||
|
]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: record.name as string,
|
||||||
|
directory: record.directory as boolean,
|
||||||
|
mimeType: record.mimeType as string,
|
||||||
|
size: record.size as number,
|
||||||
|
writable: record.writeable as boolean
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue