parent
2542eb4968
commit
3add883c25
|
|
@ -1,46 +1,80 @@
|
||||||
import { RequestInitProvider} from "@libreccm/ccm-apiclient-commons";
|
import {
|
||||||
|
ApiResponse,
|
||||||
|
LibreCcmApiClient,
|
||||||
|
} from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
import { Category } from "./entities/categorization";
|
import { Category, buildCategoryFromRecord } from "./entities/categorization";
|
||||||
|
import * as Constants from "./constants";
|
||||||
// interface RequestInitProvider {
|
|
||||||
// buildRequestInit(): RequestInit;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// export class CcmCoreAdminApiClient {
|
|
||||||
// libreCcmHost: string;
|
|
||||||
// jwt: string;
|
|
||||||
|
|
||||||
// constructor(libreCcmHost: string, jwt: string) {
|
|
||||||
// this.libreCcmHost = libreCcmHost;
|
|
||||||
// this.jwt = jwt;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// jwtRequired(): boolean {
|
|
||||||
// const documentUrl = new URL(document.documentURI);
|
|
||||||
// return documentUrl.host != this.libreCcmHost;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
export class CategorizationApiClient {
|
export class CategorizationApiClient {
|
||||||
#requestInitProvider: RequestInitProvider;
|
#apiClient: LibreCcmApiClient;
|
||||||
|
|
||||||
constructor(requestInitProvider: RequestInitProvider) {
|
readonly #CATEGORIES_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/categories`
|
||||||
this.#requestInitProvider = requestInitProvider;
|
|
||||||
|
constructor(apiClient: LibreCcmApiClient) {
|
||||||
|
this.#apiClient = apiClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCategory(domain: string, path: string): Promise<Category> {
|
|
||||||
const fetchInit = this.#requestInitProvider.buildRequestInit();
|
|
||||||
fetchInit.method = "GET";
|
async getCategoryByDomainAndPath(domain: string, path: string): Promise<Category> {
|
||||||
try {
|
try {
|
||||||
const result = await fetch("localhost:8080", fetchInit);
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
if (result.ok) {
|
`${this.#CATEGORIES_API_PREFIX}/${domain}/${path}`
|
||||||
const category: Category = await result.json();
|
);
|
||||||
return category;
|
|
||||||
|
if (response.ok) {
|
||||||
|
return buildCategoryFromRecord(
|
||||||
|
await response.json()
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Failed to get category ${path} of domain ${domain}: ${result.status} ${result.statusText}`);
|
throw `Failed to get category ${path} of domain ${domain}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
}
|
}
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
throw new Error(`Failed to get category ${path} of domain ${domain}: ${err}`);
|
throw `Failed to get category ${path} of domain ${domain}: ${err}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCategoryById(categoryId: number): Promise<Category> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#CATEGORIES_API_PREFIX}/categories/ID-${categoryId}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return buildCategoryFromRecord(
|
||||||
|
await response.json()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw `Failed to get category with ID ${categoryId}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch(err) {
|
||||||
|
throw `Failed to get category with Id ${categoryId}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getCategoryByUuid(uuid: string): Promise<Category> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#CATEGORIES_API_PREFIX}/categories/UUID-${uuid}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return buildCategoryFromRecord(
|
||||||
|
await response.json()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw `Failed to get category with UUID ${uuid}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch(err) {
|
||||||
|
throw `Failed to get category with UUID ${uuid}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
const ADMIN_API_PREFIX = "/api/admin";
|
||||||
|
|
||||||
|
export {
|
||||||
|
ADMIN_API_PREFIX
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
import { LocalizedString } from "@libreccm/ccm-apiclient-commons";
|
import {
|
||||||
|
LocalizedString,
|
||||||
|
assertProperties,
|
||||||
|
} from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
export interface AssociatedCategory {
|
export interface AssociatedCategory {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
@ -15,6 +18,40 @@ export interface Category {
|
||||||
description: LocalizedString;
|
description: LocalizedString;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
abstractCategory: boolean;
|
abstractCategory: boolean;
|
||||||
parentCategory: AssociatedCategory;
|
parentCategory: AssociatedCategory | null;
|
||||||
categoryOrder: number;
|
categoryOrder: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function buildCategoryFromRecord(
|
||||||
|
record: Record<string, unknown>
|
||||||
|
): Category {
|
||||||
|
assertProperties(record, [
|
||||||
|
"categoryId",
|
||||||
|
"uuid",
|
||||||
|
"uniqueId",
|
||||||
|
"name",
|
||||||
|
"title",
|
||||||
|
"description",
|
||||||
|
"enabled",
|
||||||
|
"abstractCategory",
|
||||||
|
"parentCategory",
|
||||||
|
"categoryOrder",
|
||||||
|
]);
|
||||||
|
|
||||||
|
const parentCategory: AssociatedCategory | null = record.parentCategory
|
||||||
|
? (record.parentCategory as AssociatedCategory)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
categoryId: record.categoryId as number,
|
||||||
|
uuid: record.uuid as string,
|
||||||
|
uniqueId: record.uniqueId as string,
|
||||||
|
name: record.name as string,
|
||||||
|
title: record.title as LocalizedString,
|
||||||
|
description: record.description as LocalizedString,
|
||||||
|
enabled: record.enable as boolean,
|
||||||
|
abstractCategory: record.abstractCategory as boolean,
|
||||||
|
parentCategory,
|
||||||
|
categoryOrder: record.categoryOrder as number,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue