integreat
Version:
Node.js integration layer
34 lines • 1.14 kB
JavaScript
import { isShape } from '../utils/is.js';
const expandField = (val) => typeof val === 'string'
? { $type: val }
: isShape(val)
? expandFields(val)
: val;
function expandFields(shapeDef) {
return Object.fromEntries(Object.entries(shapeDef).map(([key, def]) => (def ? [key, expandField(def)] : []), {}));
}
function validateShape(shape) {
const errors = [];
if (shape.id && shape.id.$type !== 'string') {
errors.push("'id' must be a string");
}
if (shape.createdAt && shape.createdAt.$type !== 'date') {
errors.push("'createdAt' must be a date");
}
if (shape.updatedAt && shape.updatedAt.$type !== 'date') {
errors.push("'updatedAt' must be a date");
}
return errors.length > 0 ? errors.join('. ') : undefined;
}
export default function expandShape(shapeDef) {
const shape = expandFields(shapeDef);
const validationError = validateShape(shape);
if (validationError) {
throw new Error(validationError);
}
if (!shape.id) {
shape.id = { $type: 'string' };
}
return shape;
}
//# sourceMappingURL=expandShape.js.map