More functions for the themes API

Former-commit-id: 37b2512ef2
restapi
Jens Pelzetter 2020-08-11 15:43:09 +02:00
parent 910b61cd81
commit 62faaf6384
2 changed files with 113 additions and 15 deletions

View File

@ -8,6 +8,7 @@ import {
ThemeInfo,
ThemeManifest,
ThemeFileInfo,
buildThemeFileInfoFromRecord,
buildThemeInfoFromRecord,
} from "../entities/themes";
@ -159,19 +160,96 @@ export class ThemesApiClient {
}
}
// async getThemeFile(
// themeName: string,
// path: string | null = null
// ): Promise<ThemeFileInfo[]> {
// try {
// const response: ApiResponse =
// } catch (err) {
// if (path) {
// throw `Failed to get file ${path} of theme ${themeName}: ${err}`;
// } else {
// throw `Failed to get root directory of
// theme ${themeName}: ${err}`;
// }
// }
// }
async getThemeFileInfo(
themeName: string,
path = ""
): Promise<ThemeFileInfo> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#THEMES_API_PREFIX}/${themeName}/files/${path}/@info`
);
if (response.ok) {
const result: Record<
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}`;
}
}
}

View File

@ -73,3 +73,23 @@ export function buildThemeManifestFromRecord(
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
};
}