telefunc
Version:
Remote functions. Instead of API.
33 lines (32 loc) • 1.63 kB
JavaScript
export { configUser as config };
export { resolveClientConfig };
import { assertUsage } from './utils.js';
const configUser = new Proxy({}, { set: validateUserConfig });
function resolveClientConfig() {
var _a, _b;
return {
httpHeaders: (_a = configUser.httpHeaders) !== null && _a !== void 0 ? _a : null,
telefuncUrl: configUser.telefuncUrl || '/_telefunc',
fetch: (_b = configUser.fetch) !== null && _b !== void 0 ? _b : null,
};
}
function validateUserConfig(configUserUnwrapped, prop, val) {
if (prop === 'telefuncUrl') {
assertUsage(typeof val === 'string', 'config.telefuncUrl should be a string');
const isIpAddress = (value) => /^\d/.test(value);
assertUsage(val.startsWith('/') || val.startsWith('http') || isIpAddress(val), `config.telefuncUrl (client-side) is '${val}' but it should be one of the following: a URL pathname (such as '/_telefunc'), a URL with origin (such as 'https://example.org/_telefunc'), or an IP address (such as '192.158.1.38') — see https://telefunc.com/telefuncUrl`);
configUserUnwrapped[prop] = val;
}
else if (prop === 'httpHeaders') {
assertUsage(typeof val === 'object' && val !== null && Object.values(val).every((v) => typeof v === 'string'), '`config.httpHeaders` should be an object of strings');
configUserUnwrapped[prop] = val;
}
else if (prop === 'fetch') {
assertUsage(typeof val === 'function', '`config.fetch` should be a function');
configUserUnwrapped[prop] = val;
}
else {
assertUsage(false, `Unknown config.${prop}`);
}
return true;
}