UNPKG

@tanstack/db

Version:

A reactive client store for building super fast apps on sync

78 lines (77 loc) • 4.14 kB
const require_errors = require("../errors.cjs"); const require_error_message = require("../error-message.cjs"); const require_config_errors = require("./config-errors.cjs"); var KNOWN_CONFIG_KEYS = /* @__PURE__ */ new Set([ `id`, `schema`, `getKey`, `sync`, `gcTime`, `startSync`, `autoIndex`, `defaultIndexType`, `compare`, `syncMode`, `defaultStringCollation`, `onInsert`, `onUpdate`, `onDelete`, `utils`, `singleResult`, `persistence` ]); function findLikelyTypo(key, config) { for (const knownKey of KNOWN_CONFIG_KEYS) { if (knownKey in config) continue; if (key.toLowerCase() === knownKey.toLowerCase()) return knownKey; if (key.length < 5 || key.length !== knownKey.length) continue; for (let i = 0; i < key.length - 1; i++) if (key.slice(0, i) + key[i + 1] + key[i] + key.slice(i + 2) === knownKey) return knownKey; } } function describeType(value) { if (value === null) return `null`; if (value === void 0) return `undefined`; if (Array.isArray(value)) return `an array`; return typeof value; } function validateCollectionConfig(config) { if (!config || typeof config !== `object` || Array.isArray(config)) throw new require_errors.CollectionRequiresConfigError(); const configObj = config; if (/* @__PURE__ */ require_error_message.devBuild() && process.env.NODE_ENV !== `production`) for (const key of Object.keys(configObj)) { if (KNOWN_CONFIG_KEYS.has(key) || key.startsWith(`_`)) continue; const suggestion = findLikelyTypo(key, configObj); if (suggestion) console.warn(`Possible misspelling in collection config: "${key}". Did you mean "${suggestion}"?`); } if (!(`getKey` in configObj) || configObj.getKey === void 0) throw new require_config_errors.CollectionRequiresGetKeyError(); if (typeof configObj.getKey !== `function`) throw new require_config_errors.InvalidGetKeyError(describeType(configObj.getKey)); if (!configObj.sync) throw new require_errors.CollectionRequiresSyncConfigError(); if (typeof configObj.sync !== `object` || Array.isArray(configObj.sync)) throw new require_config_errors.InvalidSyncConfigError(describeType(configObj.sync)); const syncObj = configObj.sync; if (typeof syncObj.sync !== `function`) throw new require_config_errors.InvalidSyncFunctionError(describeType(syncObj.sync)); for (const optionName of [ `onInsert`, `onUpdate`, `onDelete`, `compare` ]) if (optionName in configObj && configObj[optionName] !== void 0 && typeof configObj[optionName] !== `function`) throw new require_config_errors.InvalidCallbackOptionError(optionName, describeType(configObj[optionName])); if (`id` in configObj && configObj.id !== void 0) { if (typeof configObj.id !== `string`) throw new require_config_errors.InvalidOptionTypeError(`id`, `a string`, describeType(configObj.id)); } if (`gcTime` in configObj && configObj.gcTime !== void 0) { if (typeof configObj.gcTime !== `number`) throw new require_config_errors.InvalidOptionTypeError(`gcTime`, `a number`, describeType(configObj.gcTime)); } if (`startSync` in configObj && configObj.startSync !== void 0) { if (typeof configObj.startSync !== `boolean`) throw new require_config_errors.InvalidOptionTypeError(`startSync`, `a boolean`, describeType(configObj.startSync)); } if (`autoIndex` in configObj && configObj.autoIndex !== void 0) { if (configObj.autoIndex !== `off` && configObj.autoIndex !== `eager`) throw new require_config_errors.InvalidOptionTypeError(`autoIndex`, `"off" or "eager"`, String(configObj.autoIndex)); } if (`syncMode` in configObj && configObj.syncMode !== void 0) { if (configObj.syncMode !== `eager` && configObj.syncMode !== `on-demand`) throw new require_config_errors.InvalidOptionTypeError(`syncMode`, `"eager" or "on-demand"`, String(configObj.syncMode)); } if (`utils` in configObj && configObj.utils !== void 0) { if (typeof configObj.utils !== `object` || configObj.utils === null || Array.isArray(configObj.utils)) throw new require_config_errors.InvalidOptionTypeError(`utils`, `an object`, describeType(configObj.utils)); } } exports.validateCollectionConfig = validateCollectionConfig; //# sourceMappingURL=validate-config.cjs.map