Utility functions for checking if an record has all required properties
parent
58fb0937d2
commit
af3c2b3da4
|
|
@ -13,10 +13,10 @@ export * from "./entities";
|
||||||
export { LibreCcmApiClient, ApiResponse, ApiError, buildUrl };
|
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.
|
* 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
|
* determined from the URL of the current document. The API client will
|
||||||
* use the credentials stored in the browser (cookies) to authenticate itself.
|
* 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.
|
* 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 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.
|
* @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
|
* Builds a client for the LibreCCM RESTful API suitable for running inside a node.js
|
||||||
* environment.
|
* environment.
|
||||||
*
|
*
|
||||||
* @param baseUrl The URL of the LibreCCM installation to access, including the port.
|
* @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.
|
* @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
|
* 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
|
* browser and in a node.js environment. Use this function to create an API client
|
||||||
* for JavaScript applications which use Server-Side-Rendering.
|
* for JavaScript applications which use Server-Side-Rendering.
|
||||||
*
|
*
|
||||||
* @param baseUrl The URL of the LibreCCM installation to access, including the port.
|
* @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.
|
* @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);
|
const nodeClient: LibreCcmApiClient = buildNodeApiClient(baseUrl, jwt);
|
||||||
return new IsomorphicClientImpl(fetchClient, nodeClient);
|
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<string, unknown>,
|
||||||
|
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<string, unknown>,
|
||||||
|
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<string, unknown>,
|
||||||
|
properties: string[]
|
||||||
|
): string[] {
|
||||||
|
const keys = Object.keys(record);
|
||||||
|
return properties.filter((property) => !keys.includes(property));
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue