interface ContactFilter {
email?: {
equals?: string;
contains?: string;
startsWith?: string;
endsWith?: string;
};
tags?: {
has?: string;
hasEvery?: string;
hasSome?: string;
isEmpty?: boolean;
};
createdAt?: {
gt?: string;
gte?: string;
lt?: string;
lte?: string;
};
customProperties?: {
[key: string]: {
equals?: any;
gt?: number;
contains?: string;
};
};
}
function buildFilterParams(filters: ContactFilter): URLSearchParams {
const params = new URLSearchParams();
Object.entries(filters).forEach(([field, operators]) => {
Object.entries(operators).forEach(([operator, value]) => {
if (field === "customProperties") {
// Handle nested properties
Object.entries(value).forEach(([prop, propOperators]) => {
Object.entries(propOperators).forEach(([op, val]) => {
params.append(`filter.${field}.${prop}.${op}`, String(val));
});
});
} else {
params.append(`filter.${field}.${operator}`, String(value));
}
});
});
return params;
}
// Usage
const filters: ContactFilter = {
email: { endsWith: "@company.com" },
tags: { hasSome: "customer,lead" },
createdAt: { gte: "2024-01-01" },
customProperties: {
industry: { equals: "technology" },
employeeCount: { gt: 100 },
},
};
const params = buildFilterParams(filters);
const response = await fetch(`https://api.us.firstquadrant.ai/v5/contacts?${params}`);