@unito/integration-api
Version:
The Unito Integration API
134 lines (133 loc) • 5.61 kB
JavaScript
import * as Api from './types.js';
/**
* Checks if the input is a record<unknown, unknown>
* @param potentialObject - The value to check.
* @returns True if the value is a record, false otherwise.
*/
export function isObject(potentialObject) {
return typeof potentialObject === 'object' && potentialObject !== null && !Array.isArray(potentialObject);
}
/**
* Checks if the input is a string.
* @param potentialString - The value to check.
* @returns True if the value is a string, false otherwise.
*/
export function isString(potentialString) {
return typeof potentialString === 'string';
}
/**
* Checks if the input is undefined.
* @param potentialUndefined - The value to check.
* @returns True if the value is undefined, false otherwise.
*/
export function isUndefined(potentialUndefined) {
return potentialUndefined === undefined;
}
/**
* Checks if the input is an Api.ItemSummary object.
* @param potentialItemSummary - The value to check.
* @returns True if the value is an ItemSummary, false otherwise.
*/
export function isItemSummary(potentialItemSummary) {
return (isObject(potentialItemSummary) &&
isString(potentialItemSummary['path']) &&
(isUndefined(potentialItemSummary['fields']) || isObject(potentialItemSummary['fields'])) &&
(isUndefined(potentialItemSummary['relations']) ||
(Array.isArray(potentialItemSummary['relations']) && potentialItemSummary['relations'].every(isString))));
}
/**
* Checks if the input is an Api.Item object.
* @param potentialItem - The value to check.
* @returns True if the value is an Item, false otherwise.
*/
export function isItem(potentialItem) {
return (isObject(potentialItem) &&
isObject(potentialItem['fields']) &&
Array.isArray(potentialItem['relations']) &&
potentialItem['relations'].every(isRelation));
}
/**
* Checks if the input is an Api.ReferenceRelation object.
* @param potentialReferenceRelation - The value to check.
* @returns True if the value is a ReferenceRelation, false otherwise.
*/
export function isReferenceRelation(potentialReferenceRelation) {
return (isObject(potentialReferenceRelation) &&
isString(potentialReferenceRelation['path']) &&
isString(potentialReferenceRelation['label']) &&
isRelationSchemaOrSelf(potentialReferenceRelation['schema']));
}
/**
* Checks if the input is an Api.RelationSummary object.
* @param potentialRelationSummary - The value to check.
* @returns True if the value is a RelationSummary, false otherwise.
*/
export function isRelationSummary(potentialRelationSummary) {
return (isObject(potentialRelationSummary) &&
isString(potentialRelationSummary['name']) &&
isString(potentialRelationSummary['label']) &&
isRelationSchemaOrSelf(potentialRelationSummary['schema']));
}
/**
* Checks if the input is an Api.Relation object.
* @param potentialRelation - The value to check.
* @returns True if the value is a Relation, false otherwise.
*/
export function isRelation(potentialRelation) {
return (isObject(potentialRelation) &&
isString(potentialRelation['name']) &&
isString(potentialRelation['path']) &&
isString(potentialRelation['label']) &&
isRelationSchema(potentialRelation['schema']));
}
/**
* Checks if the input is an Api.RelationSchema object.
* @param potentialRelationSchema - The value to check.
* @returns True if the value is a RelationSchema, false otherwise.
*/
export function isRelationSchema(potentialRelationSchema) {
return (isObject(potentialRelationSchema) &&
isString(potentialRelationSchema['label']) &&
Array.isArray(potentialRelationSchema['fields']) &&
potentialRelationSchema['fields'].every(isFieldSchema) &&
(isUndefined(potentialRelationSchema['relations']) ||
(Array.isArray(potentialRelationSchema['relations']) &&
potentialRelationSchema['relations'].every(isRelationSummary))));
}
/**
* Checks if the input is an Api.RelationSchema object or the string '__self'.
* @param potentialRelationSchema - The value to check.
* @returns True if the value is a RelationSchema or '__self', false otherwise.
*/
export function isRelationSchemaOrSelf(potentialRelationSchemaOrSelf) {
return isRelationSchema(potentialRelationSchemaOrSelf) || potentialRelationSchemaOrSelf === '__self';
}
/**
* Checks if the input is an Api.FieldValueType.
* @param potentialFieldValueType - The value to check.
* @returns True if the value is a FieldValueType, false otherwise.
*/
export function isFieldValueType(potentialFieldValueType) {
return Object.values(Api.FieldValueTypes).includes(potentialFieldValueType);
}
/**
* Checks if the input is an Api.Semantic value.
* @param potentialSemantic - The value to check.
* @returns True if the value is a Semantic, false otherwise.
*/
export function isSemantic(potentialSemantic) {
return Object.values(Api.Semantics).includes(potentialSemantic);
}
/**
* Checks if the input is an Api.FieldSchema object.
* @param potentialFieldSchema - The value to check.
* @returns True if the value is a FieldSchema, false otherwise.
*/
export function isFieldSchema(potentialFieldSchema) {
return (isObject(potentialFieldSchema) &&
isString(potentialFieldSchema['name']) &&
isString(potentialFieldSchema['label']) &&
isString(potentialFieldSchema['type']) &&
isFieldValueType(potentialFieldSchema['type']) &&
(isUndefined(potentialFieldSchema['semantic']) || isSemantic(potentialFieldSchema['semantic'])));
}