@uh-joan/offx-mcp-server
Version:
OFF-X (Target Safety) MCP server for drug safety, adverse event, and target risk analytics.
88 lines • 2.96 kB
JavaScript
import { McpError } from "@modelcontextprotocol/sdk/types.js";
/**
* Removes null and undefined values from an object
*/
export function cleanObject(obj) {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (value !== null && value !== undefined) {
acc[key] = value;
}
return acc;
}, {});
}
/**
* Deep cleans an object or array of null and undefined values
*/
export function deepCleanObject(obj) {
if (Array.isArray(obj)) {
return obj.map(item => deepCleanObject(item));
}
if (obj && typeof obj === 'object') {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (value !== null && value !== undefined) {
acc[key] = deepCleanObject(value);
}
return acc;
}, {});
}
return obj;
}
/**
* Creates a standardized error object
*/
export function createError(message, code = 500) {
return new McpError(code, message);
}
export function pickBySchema(obj, schema) {
if (typeof obj !== 'object' || obj === null)
return obj;
// If the object is an array, process each item
if (Array.isArray(obj)) {
return obj.map(item => pickBySchema(item, schema));
}
const result = {};
for (const key in schema) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const rule = schema[key];
// If the rule is true, copy the value as-is
if (rule === true) {
result[key] = obj[key];
}
// If the rule is an object, apply the schema recursively
else if (typeof rule === 'object' && rule !== null) {
result[key] = pickBySchema(obj[key], rule);
}
}
}
return result;
}
export function flattenArraysInObject(input, inArray = false) {
if (Array.isArray(input)) {
// Process each item in the array with inArray=true so that any object
// inside the array is flattened to a string.
const flatItems = input.map(item => flattenArraysInObject(item, true));
return flatItems.join(', ');
}
else if (typeof input === 'object' && input !== null) {
if (inArray) {
// When inside an array, ignore the keys and flatten the object's values.
const values = Object.values(input).map(value => flattenArraysInObject(value, true));
return values.join(': ');
}
else {
// When not in an array, process each property recursively.
const result = {};
for (const key in input) {
if (Object.prototype.hasOwnProperty.call(input, key)) {
result[key] = flattenArraysInObject(input[key], false);
}
}
return result;
}
}
else {
// For primitives, simply return the value.
return input;
}
}
//# sourceMappingURL=util.js.map