UNPKG

telefunc

Version:

Remote functions. Instead of API.

104 lines (103 loc) 5.05 kB
export { configUser as config }; export { getServerConfig }; import { assertUsage, hasProp, toPosixPath, isTelefuncFilePath, pathIsAbsolute, isObject } from '../utils.js'; const configUser = new Proxy({}, { set: validateUserConfig }); function getServerConfig() { var _a, _b, _c, _d, _e, _f; return { disableEtag: (_a = configUser.disableEtag) !== null && _a !== void 0 ? _a : false, disableNamingConvention: (_b = configUser.disableNamingConvention) !== null && _b !== void 0 ? _b : false, shield: typeof configUser.shield === 'boolean' ? { dev: configUser.shield, prod: configUser.shield } : { dev: (_d = (_c = configUser.shield) === null || _c === void 0 ? void 0 : _c.dev) !== null && _d !== void 0 ? _d : false, prod: (_f = (_e = configUser.shield) === null || _e === void 0 ? void 0 : _e.prod) !== null && _f !== void 0 ? _f : true }, log: { shieldErrors: (() => { var _a, _b, _c, _d; const shieldErrors = (_b = (_a = configUser.log) === null || _a === void 0 ? void 0 : _a.shieldErrors) !== null && _b !== void 0 ? _b : {}; if (typeof shieldErrors === 'boolean') return { dev: true, prod: true }; return { dev: (_c = shieldErrors.dev) !== null && _c !== void 0 ? _c : true, prod: (_d = shieldErrors.prod) !== null && _d !== void 0 ? _d : false, }; })(), }, telefuncUrl: configUser.telefuncUrl || '/_telefunc', telefuncFiles: (() => { if (configUser.telefuncFiles) { return configUser.telefuncFiles.map(toPosixPath); } return null; })(), root: (() => { if (configUser.root) { return toPosixPath(configUser.root); } if (typeof process == 'undefined' || !hasProp(process, 'cwd')) return null; return toPosixPath(process.cwd()); })(), }; } function validateUserConfig(configUserUnwrapped, prop, val) { if (prop === 'root') { assertUsage(typeof val === 'string', 'config.root should be a string'); assertUsage(pathIsAbsolute(val), 'config.root should be an absolute path'); configUserUnwrapped[prop] = val; } else if (prop === 'telefuncUrl') { assertUsage(typeof val === 'string', 'config.telefuncUrl should be a string'); assertUsage(val.startsWith('/'), `config.telefuncUrl (server-side) is '${val}' but it should start with '/' (it should be a URL pathname such as '/_telefunc'), see https://telefunc.com/telefuncUrl`); configUserUnwrapped[prop] = val; } else if (prop === 'telefuncFiles') { const wrongType = 'config.telefuncFiles should be a list of paths'; assertUsage(Array.isArray(val), wrongType); val.forEach((val) => { assertUsage(typeof val === 'string', wrongType); assertUsage(pathIsAbsolute(val), `[config.telefuncFiles] ${val} should be an absolute path`); assertUsage(isTelefuncFilePath(toPosixPath(val)), `[config.telefuncFiles] ${val} doesn't contain \`.telefunc.\``); }); configUserUnwrapped[prop] = val; } else if (prop === 'disableEtag') { assertUsage(typeof val === 'boolean', 'config.disableEtag should be a boolean'); configUserUnwrapped[prop] = val; } else if (prop === 'disableNamingConvention') { assertUsage(typeof val === 'boolean', 'config.disableNamingConvention should be a boolean'); configUserUnwrapped[prop] = val; } else if (prop === 'shield') { assertUsage(typeof val === 'object' && val !== null, 'config.shield should be a object'); if ('dev' in val) { assertUsage(typeof val.dev === 'boolean', 'config.shield.dev should be a boolean'); } configUserUnwrapped[prop] = val; } else if (prop === 'log') { assertUsage(typeof val === 'object' && val !== null, 'config.log should be an object'); if ('shieldErrors' in val) { const shieldErrors = val.shieldErrors; if (typeof shieldErrors === 'boolean') { // Boolean is valid } else if (isObject(shieldErrors)) { if ('dev' in shieldErrors) { assertUsage(typeof shieldErrors.dev === 'boolean', 'config.log.shieldErrors.dev should be a boolean'); } if ('prod' in shieldErrors) { assertUsage(typeof shieldErrors.prod === 'boolean', 'config.log.shieldErrors.prod should be a boolean'); } } else { assertUsage(false, 'config.log.shieldErrors should be either a boolean or an object with dev and prod boolean properties'); } } configUserUnwrapped[prop] = val; } else { assertUsage(false, `Unknown config.${prop}`); } return true; }