UNPKG

@opengis/fastify-table

Version:

core-plugins

31 lines (30 loc) 1.14 kB
import isText from "./typeguards/isText.js"; import isPath from "./typeguards/isPath.js"; import isBuffer from "./typeguards/isBuffer.js"; import isReadableStream from "./typeguards/isReadableStream.js"; import isArray from "./typeguards/isArray.js"; import convertHandlers from "./handlers/index.js"; const getValidData = async ({ data, types }) => { // chain of responsibility const validators = [isReadableStream, isBuffer, isPath, isText, isArray]; const values = await Promise.all(validators.map(async (func) => ({ name: func.name, bool: await func(data), }))); const isValid = !values.reduce((acc, { name, bool }) => { let valid = false; types.forEach((dataType) => { if (name === dataType) valid = bool; }); return acc && !valid; }, true); if (isValid) return data; const [value] = values.filter(({ bool }) => bool === true); if (!value) throw new Error("No support for this data type"); const handler = convertHandlers[value.name]; return handler({ data, types }); }; export default getValidData;