telefunc
Version:
Remote functions. Instead of API.
24 lines (23 loc) • 942 B
JavaScript
export { serializeTelefunctionResult };
import { stringify } from '@brillout/json-serializer/stringify';
import { assert, assertUsage, hasProp, lowercaseFirstLetter } from '../../utils.js';
function serializeTelefunctionResult(runContext) {
const bodyValue = {
ret: runContext.telefunctionReturn,
};
if (runContext.telefunctionAborted) {
bodyValue.abort = true;
}
try {
const httpResponseBody = stringify(bodyValue, { forbidReactElements: true });
return httpResponseBody;
}
catch (err) {
assert(hasProp(err, 'message', 'string'));
assertUsage(false, [
`Cannot serialize value returned by telefunction ${runContext.telefunctionName}() (${runContext.telefuncFilePath}).`,
'Make sure that telefunctions always return a serializable value.',
`Serialization error: ${lowercaseFirstLetter(err.message)}`,
].join(' '));
}
}