diff --git a/ccm-core-apiclient/src/main/typescript/clients/themes.ts b/ccm-core-apiclient/src/main/typescript/clients/themes.ts new file mode 100644 index 000000000..b119ae5bb --- /dev/null +++ b/ccm-core-apiclient/src/main/typescript/clients/themes.ts @@ -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 { + 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 { + 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[]; + + 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 { + 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; + 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 { + 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; + 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 { + 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 { + 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 { + 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 { + // 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}`; + // } + // } + // } +}