magicbell
Version:
MagicBell API wrapper
58 lines • 2.1 kB
JavaScript
import { hasOwn, isBoolean, isNumber, isObject, isString, joinAnd, joinOr } from '../lib/utils.js';
const optionValidators = {
host: isString,
maxRetryDelay: isNumber,
timeout: isNumber,
token: isString,
apiKey: isString,
maxRetries: isNumber,
userEmail: isString,
userExternalId: isString,
userHmac: isString,
idempotencyKey: isString,
telemetry: isBoolean,
apiSecret: isString,
appInfo: isObject,
features: isObject,
headers: isObject,
hooks: isObject,
cacheTTL: isNumber,
};
export function isOptionsHash(object) {
if (!isObject(object))
return false;
for (const key of Object.keys(object)) {
if (!optionValidators[key]?.(object[key]))
return false;
}
return true;
}
export function assertHasValidOptions(options) {
const invalidOptions = Object.keys(options).filter((x) => options[x] != null && !optionValidators[x]?.(options[x]));
if (invalidOptions.length) {
throw new Error(`You have provided invalid client options. Please check the options ${joinAnd(...invalidOptions)}.`);
}
}
export function assertHasRequiredOptions(options, required) {
const missingOptions = required.filter((x) => !hasOwn(options, x) || options[x] == null || options[x] === '');
if (missingOptions.length) {
throw new Error(`You have not provided all required client options. Please provide ${joinAnd(...missingOptions)}.`);
}
}
export function assertHasSomeOptions(options, keys) {
const missingOptions = keys.filter((x) => !hasOwn(options, x) || options[x] == null || options[x] === '');
if (missingOptions.length === keys.length) {
throw new Error(`You have not provided any of the required client options. Please provide ${joinOr(...keys)}.`);
}
}
export function mergeHooks(...hooks) {
const result = {};
for (const hook of hooks) {
for (const key of Object.keys(hook || {})) {
result[key] ??= [];
result[key].push(...hook[key]);
}
}
return result;
}
//# sourceMappingURL=options.js.map