Implemented client functions for some of the API endpoints for theme management
Former-commit-id: 324574f314
restapi
parent
34303af9fc
commit
2e6c97b775
|
|
@ -0,0 +1,177 @@
|
||||||
|
import {
|
||||||
|
ApiResponse,
|
||||||
|
LibreCcmApiClient,
|
||||||
|
ListView,
|
||||||
|
} from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ThemeInfo,
|
||||||
|
ThemeManifest,
|
||||||
|
ThemeFileInfo,
|
||||||
|
buildThemeInfoFromRecord,
|
||||||
|
} from "../entities/themes";
|
||||||
|
|
||||||
|
export class ThemesApiClient {
|
||||||
|
#apiClient: LibreCcmApiClient;
|
||||||
|
|
||||||
|
readonly #THEMES_API_PREFIX = "/api/themes";
|
||||||
|
|
||||||
|
constructor(apiClient: LibreCcmApiClient) {
|
||||||
|
this.#apiClient = apiClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getThemeProviders(): Promise<string[]> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#THEMES_API_PREFIX}/providers`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const result: unknown = await response.json();
|
||||||
|
return result as string[];
|
||||||
|
} else {
|
||||||
|
throw `Failed to get theme providers:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to get theme providers: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAvailableThemes(): Promise<ThemeInfo[]> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#THEMES_API_PREFIX}/themes`
|
||||||
|
);
|
||||||
|
if (response.ok) {
|
||||||
|
const list: Record<
|
||||||
|
string,
|
||||||
|
unknown
|
||||||
|
>[] = (await response.json()) as Record<string, unknown>[];
|
||||||
|
|
||||||
|
return list.map((record) => buildThemeInfoFromRecord(record));
|
||||||
|
} else {
|
||||||
|
throw `Failed to get avaiable themes:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to get avaiable themes: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getTheme(themeName: string): Promise<ThemeInfo> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#THEMES_API_PREFIX}/themes/${themeName}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const record: Record<
|
||||||
|
string,
|
||||||
|
unknown
|
||||||
|
> = (await response.json()) as Record<string, unknown>;
|
||||||
|
return buildThemeInfoFromRecord(record);
|
||||||
|
} else {
|
||||||
|
throw `Failed to get theme ${themeName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to get theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async createTheme(
|
||||||
|
themeName: string,
|
||||||
|
providerName: string
|
||||||
|
): Promise<ThemeInfo> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.put(
|
||||||
|
`${this.#THEMES_API_PREFIX}/themes/${themeName}`,
|
||||||
|
{
|
||||||
|
provider: providerName,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const record: Record<
|
||||||
|
string,
|
||||||
|
unknown
|
||||||
|
> = (await response.json()) as Record<string, unknown>;
|
||||||
|
return buildThemeInfoFromRecord(record);
|
||||||
|
} else {
|
||||||
|
throw `Failed to create theme ${themeName}
|
||||||
|
using provider ${providerName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to create theme ${themeName}
|
||||||
|
using provider ${providerName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async deleteTheme(themeName: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.delete(
|
||||||
|
`${this.#THEMES_API_PREFIX}/themes/${themeName}`
|
||||||
|
);
|
||||||
|
if (response.ok) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
throw `Failed to delete theme ${themeName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to delete theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async publishTheme(themeName: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.post(
|
||||||
|
`${this.#THEMES_API_PREFIX}/themes/${themeName}/live`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
throw `Failed to publish theme ${themeName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to publish theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async unpublishTheme(themeName: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.delete(
|
||||||
|
`${this.#THEMES_API_PREFIX}/themes/${themeName}/live`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
throw `Failed to unpublish theme ${themeName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to unpublish theme ${themeName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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}`;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue