Configuration API, some general improvments for API client
parent
4dc4e9855d
commit
0b621bf7b4
|
|
@ -99,7 +99,7 @@ export interface ApiResponse {
|
|||
/**
|
||||
* Gets the Response Body as JSON.
|
||||
*/
|
||||
json(): Promise<Record<string, unknown>>;
|
||||
json(): Promise<unknown>;
|
||||
/**
|
||||
* Gets the Response Body as ArrayBuffer.
|
||||
*/
|
||||
|
|
@ -194,7 +194,7 @@ class FetchResponse implements ApiResponse {
|
|||
this.#response = response;
|
||||
}
|
||||
|
||||
json(): Promise<Record<string, unknown>> {
|
||||
json(): Promise<unknown> {
|
||||
return this.#response.json();
|
||||
}
|
||||
|
||||
|
|
@ -449,7 +449,7 @@ class NodeResponse implements ApiResponse {
|
|||
this.#data = data;
|
||||
}
|
||||
|
||||
json(): Promise<Record<string, unknown>> {
|
||||
json(): Promise<unknown> {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
if (this.#data) {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import {
|
|||
buildCategoryFromRecord,
|
||||
buildCategorizationFromRecord,
|
||||
} from "../entities/categorization";
|
||||
|
||||
import * as Constants from "../constants";
|
||||
|
||||
export class CategorizationApiClient {
|
||||
|
|
@ -31,7 +32,9 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
return buildCategoryFromRecord(
|
||||
(await response.json()) as Record<string, unknown>
|
||||
);
|
||||
} else {
|
||||
throw `Failed to get category ${path} of domain ${domain}:
|
||||
${response.status} ${response.statusText}`;
|
||||
|
|
@ -48,7 +51,9 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
return buildCategoryFromRecord(
|
||||
(await response.json()) as Record<string, unknown>
|
||||
);
|
||||
} else {
|
||||
throw `Failed to get category with ID ${categoryId}:
|
||||
${response.status} ${response.statusText}`;
|
||||
|
|
@ -65,7 +70,9 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
if (response.ok) {
|
||||
return buildCategoryFromRecord(await response.json());
|
||||
return buildCategoryFromRecord(
|
||||
(await response.json()) as Record<string, unknown>
|
||||
);
|
||||
} else {
|
||||
throw `Failed to get category with UUID ${uuid}:
|
||||
${response.status} ${response.statusText}`;
|
||||
|
|
@ -204,7 +211,10 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
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<
|
||||
string,
|
||||
unknown
|
||||
|
|
@ -242,7 +252,10 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
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<
|
||||
string,
|
||||
unknown
|
||||
|
|
@ -280,7 +293,10 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
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<
|
||||
string,
|
||||
unknown
|
||||
|
|
@ -399,7 +415,10 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
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<
|
||||
string,
|
||||
unknown
|
||||
|
|
@ -437,7 +456,10 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
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<
|
||||
string,
|
||||
unknown
|
||||
|
|
@ -475,7 +497,10 @@ export class CategorizationApiClient {
|
|||
);
|
||||
|
||||
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<
|
||||
string,
|
||||
unknown
|
||||
|
|
|
|||
|
|
@ -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}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +1,74 @@
|
|||
import { assertProperties } from "@libreccm/ccm-apiclient-commons";
|
||||
|
||||
export interface ConfigurationInfo {
|
||||
name: string,
|
||||
descBundle: string,
|
||||
titleKey: string,
|
||||
settings: Record<string, SettingInfo>
|
||||
name: string;
|
||||
descBundle: string;
|
||||
titleKey: string;
|
||||
descKey: string;
|
||||
settings: Record<string, SettingInfo>;
|
||||
}
|
||||
|
||||
export interface SettingInfo {
|
||||
name: string,
|
||||
valueType: string,
|
||||
defaultValue: string,
|
||||
confClass: string,
|
||||
descBundle: string,
|
||||
labelKey: string,
|
||||
descKey: string
|
||||
name: string;
|
||||
valueType: string;
|
||||
defaultValue: string;
|
||||
confClass: string;
|
||||
descBundle: string;
|
||||
labelKey: 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,
|
||||
};
|
||||
}
|
||||
Loading…
Reference in New Issue