trifid-core
Version:
Trifid Core
54 lines (53 loc) • 1.31 kB
JavaScript
import cloneDeep from 'lodash/cloneDeep.js';
// see: https://fastify.dev/docs/latest/Reference/Routes/#routes-options
const supportedMethods = [
'DELETE',
'GET',
'HEAD',
'PATCH',
'POST',
'PUT',
'OPTIONS',
'SEARCH',
'TRACE',
'PROPFIND',
'PROPPATCH',
'MKCOL',
'COPY',
'MOVE',
'LOCK',
'UNLOCK',
];
const toArray = (value) => {
if (typeof value === 'string') {
return [value];
}
if (Array.isArray(value)) {
return value;
}
return [];
};
const standardize = (plugin) => {
const m = cloneDeep(plugin);
// make sure order is defined
const order = m.order === undefined ? 100 : m.order;
// make sure paths is defined and is an array
const paths = toArray(m.paths);
// make sure methods is defined and is an array of valid supported methods
const methods = toArray(m.methods)
.map((method) => method.toLocaleUpperCase())
.filter((method) => supportedMethods.includes(method));
// make sure hosts is defined and is an array
const hosts = toArray(m.hosts);
// make sure config is defined
const config = m.config ?? {};
return {
...m,
order,
paths,
methods,
hosts,
config,
};
};
export default standardize;