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, 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}`;
}
}
} }

View File

@ -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
};
}