parent
c8f1e568d8
commit
026576eeed
|
|
@ -27,7 +27,7 @@ export interface LibreCcmApiClient {
|
||||||
*/
|
*/
|
||||||
post(
|
post(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
body: RequestBody,
|
body?: RequestBody,
|
||||||
searchParams?: Record<string, string>
|
searchParams?: Record<string, string>
|
||||||
): Promise<ApiResponse>;
|
): Promise<ApiResponse>;
|
||||||
/**
|
/**
|
||||||
|
|
@ -277,7 +277,7 @@ export class ApiClientFetchImpl implements LibreCcmApiClient {
|
||||||
|
|
||||||
async post(
|
async post(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
body: RequestBody,
|
body?: RequestBody,
|
||||||
searchParams?: Record<string, string>
|
searchParams?: Record<string, string>
|
||||||
): Promise<ApiResponse> {
|
): Promise<ApiResponse> {
|
||||||
const url = buildUrl(this.#baseUrl, endpoint, searchParams);
|
const url = buildUrl(this.#baseUrl, endpoint, searchParams);
|
||||||
|
|
@ -562,7 +562,7 @@ export class ApiClientNodeImpl implements LibreCcmApiClient {
|
||||||
|
|
||||||
post(
|
post(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
body: ArrayBuffer,
|
body?: ArrayBuffer,
|
||||||
searchParams?: Record<string, string>
|
searchParams?: Record<string, string>
|
||||||
): Promise<ApiResponse> {
|
): Promise<ApiResponse> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,10 @@
|
||||||
import {
|
import {
|
||||||
ApiResponse,
|
ApiResponse,
|
||||||
LibreCcmApiClient,
|
LibreCcmApiClient,
|
||||||
ListView,
|
|
||||||
} from "@libreccm/ccm-apiclient-commons";
|
} from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ConfigurationInfo,
|
ConfigurationInfo,
|
||||||
SettingInfo,
|
|
||||||
buildConfigurationInfoFromRecord,
|
buildConfigurationInfoFromRecord,
|
||||||
} from "../entities/configuration";
|
} from "../entities/configuration";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,85 @@
|
||||||
|
import {
|
||||||
|
ApiResponse,
|
||||||
|
LibreCcmApiClient,
|
||||||
|
} from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
|
import {
|
||||||
|
ImportManifest,
|
||||||
|
buildImportManifestFromRecord,
|
||||||
|
} from "../entities/imexport";
|
||||||
|
|
||||||
|
import * as Constants from "../constants";
|
||||||
|
|
||||||
|
export class ImExportClient {
|
||||||
|
#apiClient: LibreCcmApiClient;
|
||||||
|
|
||||||
|
readonly #IMEXPORT_API_PREFIX = `${Constants.ADMIN_API_PREFIX}/imexport`;
|
||||||
|
|
||||||
|
constructor(apiClient: LibreCcmApiClient) {
|
||||||
|
this.#apiClient = apiClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
async listImports(): Promise<ImportManifest[]> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.get(
|
||||||
|
`${this.#IMEXPORT_API_PREFIX}/imports`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
const records: Record<
|
||||||
|
string,
|
||||||
|
unknown
|
||||||
|
>[] = (await response.json()) as Record<string, unknown>[];
|
||||||
|
|
||||||
|
return records.map((record) =>
|
||||||
|
buildImportManifestFromRecord(record)
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw `Failed to list available import archives:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to list available import archives: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async importArchive(importName: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.post(
|
||||||
|
`${this.#IMEXPORT_API_PREFIX}/imports/${importName}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
throw `Failed to import archive ${importName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to import archive ${importName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async exportEntities(
|
||||||
|
exportName: string,
|
||||||
|
entityIds: string[]
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const response: ApiResponse = await this.#apiClient.post(
|
||||||
|
`${this.#IMEXPORT_API_PREFIX}/exports/${exportName}`,
|
||||||
|
JSON.stringify(entityIds)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.ok) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
throw `Failed to export entities
|
||||||
|
${entityIds.join(", ")} as ${exportName}:
|
||||||
|
${response.status} ${response.statusText}`;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw `Failed to export entities
|
||||||
|
${entityIds.join(", ")} as ${exportName}: ${err}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
import { assertProperties } from "@libreccm/ccm-apiclient-commons";
|
||||||
|
|
||||||
|
export interface ImportManifest {
|
||||||
|
created: Date;
|
||||||
|
onServer: string;
|
||||||
|
types: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildImportManifestFromRecord(
|
||||||
|
record: Record<string, unknown>
|
||||||
|
): ImportManifest {
|
||||||
|
assertProperties(record, ["created", "onServer", "types"]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
created: new Date(record.created as string),
|
||||||
|
onServer: record.onServer as string,
|
||||||
|
types: record.types as string[]
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue