telefunc
Version:
Remote functions. Instead of API.
28 lines (27 loc) • 1.34 kB
JavaScript
export { applyShield };
import { shieldApply, shieldIsMissing } from '../shield.js';
import { assertWarning, isProduction } from '../../utils.js';
function applyShield(runContext) {
const { telefunction } = runContext;
const hasShield = !shieldIsMissing(telefunction);
if (isProduction()) {
assertWarning(hasShield || telefunction.length === 0, `The telefunction ${runContext.telefunctionName}() (${runContext.telefuncFilePath}) accepts arguments yet is missing shield(), see https://telefunc.com/shield`, { onlyOnce: true });
}
if (!hasShield) {
return { isValidRequest: true };
}
const applyResult = shieldApply(telefunction, runContext.telefunctionArgs);
if (applyResult === true) {
return { isValidRequest: true };
}
let logShieldErrors = runContext.serverConfig.log.shieldErrors;
if ((logShieldErrors.dev && !isProduction()) || (logShieldErrors.prod && isProduction())) {
const err = new Error([
`The arguments passed to the telefunction ${runContext.telefunctionName}() (${runContext.telefuncFilePath}) have the wrong type.`,
`Arguments: \`${JSON.stringify(runContext.telefunctionArgs)}\`.`,
`Wrong type: ${applyResult}`,
].join(' '));
console.error(err);
}
return { isValidRequest: false };
}