Configuration API, some general improvments for API client

Jens Pelzetter 2020-07-27 21:05:38 +02:00
parent 4dc4e9855d
commit 0b621bf7b4
4 changed files with 154 additions and 24 deletions

View File

@ -99,7 +99,7 @@ export interface ApiResponse {
/** /**
* Gets the Response Body as JSON. * Gets the Response Body as JSON.
*/ */
json(): Promise<Record<string, unknown>>; json(): Promise<unknown>;
/** /**
* Gets the Response Body as ArrayBuffer. * Gets the Response Body as ArrayBuffer.
*/ */
@ -194,7 +194,7 @@ class FetchResponse implements ApiResponse {
this.#response = response; this.#response = response;
} }
json(): Promise<Record<string, unknown>> { json(): Promise<unknown> {
return this.#response.json(); return this.#response.json();
} }
@ -449,7 +449,7 @@ class NodeResponse implements ApiResponse {
this.#data = data; this.#data = data;
} }
json(): Promise<Record<string, unknown>> { json(): Promise<unknown> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
if (this.#data) { if (this.#data) {

View File

@ -10,6 +10,7 @@ import {
buildCategoryFromRecord, buildCategoryFromRecord,
buildCategorizationFromRecord, buildCategorizationFromRecord,
} from "../entities/categorization"; } from "../entities/categorization";
import * as Constants from "../constants"; import * as Constants from "../constants";
export class CategorizationApiClient { export class CategorizationApiClient {
@ -31,7 +32,9 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord(await response.json()); return buildCategoryFromRecord(
(await response.json()) as Record<string, unknown>
);
} else { } else {
throw `Failed to get category ${path} of domain ${domain}: throw `Failed to get category ${path} of domain ${domain}:
${response.status} ${response.statusText}`; ${response.status} ${response.statusText}`;
@ -48,7 +51,9 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord(await response.json()); return buildCategoryFromRecord(
(await response.json()) as Record<string, unknown>
);
} else { } else {
throw `Failed to get category with ID ${categoryId}: throw `Failed to get category with ID ${categoryId}:
${response.status} ${response.statusText}`; ${response.status} ${response.statusText}`;
@ -65,7 +70,9 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
return buildCategoryFromRecord(await response.json()); return buildCategoryFromRecord(
(await response.json()) as Record<string, unknown>
);
} else { } else {
throw `Failed to get category with UUID ${uuid}: throw `Failed to get category with UUID ${uuid}:
${response.status} ${response.statusText}`; ${response.status} ${response.statusText}`;
@ -204,7 +211,10 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
const result: Record<string, unknown> = await response.json(); const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record< const list: Record<string, unknown>[] = result.list as Record<
string, string,
unknown unknown
@ -242,7 +252,10 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
const result: Record<string, unknown> = await response.json(); const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record< const list: Record<string, unknown>[] = result.list as Record<
string, string,
unknown unknown
@ -280,7 +293,10 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
const result: Record<string, unknown> = await response.json(); const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record< const list: Record<string, unknown>[] = result.list as Record<
string, string,
unknown unknown
@ -399,7 +415,10 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
const result: Record<string, unknown> = await response.json(); const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record< const list: Record<string, unknown>[] = result.list as Record<
string, string,
unknown unknown
@ -437,7 +456,10 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
const result: Record<string, unknown> = await response.json(); const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record< const list: Record<string, unknown>[] = result.list as Record<
string, string,
unknown unknown
@ -475,7 +497,10 @@ export class CategorizationApiClient {
); );
if (response.ok) { if (response.ok) {
const result: Record<string, unknown> = await response.json(); const result: Record<
string,
unknown
> = (await response.json()) as Record<string, unknown>;
const list: Record<string, unknown>[] = result.list as Record< const list: Record<string, unknown>[] = result.list as Record<
string, string,
unknown unknown

View File

@ -0,0 +1,47 @@
import {
ApiResponse,
LibreCcmApiClient,
ListView,
} from "@libreccm/ccm-apiclient-commons";
import {
ConfigurationInfo,
SettingInfo,
buildConfigurationInfoFromRecord,
} from "../entities/configuration";
import * as Constants from "../constants";
export class ConfigurationApiClient {
#apiClient: LibreCcmApiClient;
readonly #CONFIGURATION_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/configurations`;
constructor(apiClient: LibreCcmApiClient) {
this.#apiClient = apiClient;
}
async getConfigurations(): Promise<ConfigurationInfo[]> {
try {
const response: ApiResponse = await this.#apiClient.get(
`${this.#CONFIGURATION_API_PREFIX}/`
);
if (response.ok) {
const result: Record<
string,
unknown
>[] = (await response.json()) as Record<string, unknown>[];
return result.map((record) =>
buildConfigurationInfoFromRecord(record)
);
} else {
throw `Failed to retrieve configuration info:
${response.status} ${response.statusText}`;
}
} catch (err) {
throw `Failed to retrieve configuration info: ${err}`;
}
}
}

View File

@ -1,16 +1,74 @@
import { assertProperties } from "@libreccm/ccm-apiclient-commons";
export interface ConfigurationInfo { export interface ConfigurationInfo {
name: string, name: string;
descBundle: string, descBundle: string;
titleKey: string, titleKey: string;
settings: Record<string, SettingInfo> descKey: string;
settings: Record<string, SettingInfo>;
} }
export interface SettingInfo { export interface SettingInfo {
name: string, name: string;
valueType: string, valueType: string;
defaultValue: string, defaultValue: string;
confClass: string, confClass: string;
descBundle: string, descBundle: string;
labelKey: string, labelKey: string;
descKey: string descKey: string;
}
export function buildConfigurationInfoFromRecord(
record: Record<string, unknown>
): ConfigurationInfo {
assertProperties(record, [
"name",
"descBundle",
"titleKey",
"descKey",
"settings",
]);
const settingRecords: Record<
string,
Record<string, unknown>
>[] = record.settings as Record<string, Record<string, unknown>>[];
const settings: Record<string, SettingInfo> = {};
for (const key in settingRecords) {
settings[key] = buildSettingInfoFromRecord(settingRecords[key]);
}
return {
name: record.name as string,
descBundle: record.descBundle as string,
titleKey: record.titleKey as string,
descKey: record.descKey as string,
settings,
};
}
export function buildSettingInfoFromRecord(
record: Record<string, unknown>
): SettingInfo {
assertProperties(record, [
"name",
"valueType",
"defaultValue",
"confClass",
"descBundle",
"labelKey",
"descKey",
]);
return {
name: record.name as string,
valueType: record.valueType as string,
defaultValue: record.defaultValue as string,
confClass: record.confClass as string,
descBundle: record.descBundle as string,
labelKey: record.labelKey as string,
descKey: record.descKey as string,
};
} }