diff --git a/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts b/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts index 9344fd886..33bc552a6 100644 --- a/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts +++ b/ccm-apiclient-commons/src/main/typescript/ccm-apiclient-commons.ts @@ -13,10 +13,10 @@ export * from "./entities"; export { LibreCcmApiClient, ApiResponse, ApiError, buildUrl }; /** - * Build an client for the LibreCCM RESTful API suitable for use in + * Build an client for the LibreCCM RESTful API suitable for use in * clientside code served by the Application Server. - * - * The base URL for accessing the LibreCCM RESTful API is automatically + * + * The base URL for accessing the LibreCCM RESTful API is automatically * determined from the URL of the current document. The API client will * use the credentials stored in the browser (cookies) to authenticate itself. */ @@ -39,7 +39,7 @@ export function buildEmbeddedApiClient(): LibreCcmApiClient { /** * Builds a client for the LibreCCM RESTful API suitable for running in a browser. - * + * * @param baseUrl The URL of the LibreCCM installation to access, including the port. * @param jwt The JSON Web Token to use by the client to authenticate itself. */ @@ -61,7 +61,7 @@ export function buildRemoteApiClient( /** * Builds a client for the LibreCCM RESTful API suitable for running inside a node.js * environment. - * + * * @param baseUrl The URL of the LibreCCM installation to access, including the port. * @param jwt The JSON Web Token to use by the client to authenticate itself. */ @@ -76,7 +76,7 @@ export function buildNodeApiClient( * Builds an isomorphic client for the LibreCCM RESTful API which will work in the * browser and in a node.js environment. Use this function to create an API client * for JavaScript applications which use Server-Side-Rendering. - * + * * @param baseUrl The URL of the LibreCCM installation to access, including the port. * @param jwt The JSON Web Token to use by the client to authenticate itself. */ @@ -88,3 +88,54 @@ export function buildIsomorpicApiClient( const nodeClient: LibreCcmApiClient = buildNodeApiClient(baseUrl, jwt); return new IsomorphicClientImpl(fetchClient, nodeClient); } + +/** + * An utility function for checking if an record has specific properties. + * + * @param record The record to check. + * @param properties The required properties. + * + * @return `true` if all properties are found in the record, `false` otherwise. + */ +export function hasProperties( + record: Record, + properties: string[] +): boolean { + const keys = Object.keys(record); + return properties.every((property) => keys.includes(property)); +} + +/** + * An utility function for checking if an record has specific properties. If + * one of the properties is not found, the function will throw an error. + * + * @param record The record to check. + * @param properties The required properties. + */ +export function assertProperties( + record: Record, + properties: string[] +): void { + const missing = findMissingProperties(record, properties); + if (missing.length > 0) { + throw `record is missing the following required properties: + ${missing.join(",")}`; + } +} + +/** + * An utility function which checks which of the given properties the provided + * record is missing. + * + * @param record The record to check. + * @param properties The requird properties. + * + * @return An array with all missing properties. + */ +export function findMissingProperties( + record: Record, + properties: string[] +): string[] { + const keys = Object.keys(record); + return properties.filter((property) => !keys.includes(property)); +}