//native
export type DynSelect_object_type = string
// Dropdown of the standard CRM object types plus the account's custom objects.
export async function object_type(auth: RT.Hubspot) {
const standard = [
"contacts",
"companies",
"deals",
"tickets",
"line_items",
"products",
"quotes",
"leads",
"notes",
"tasks",
"meetings",
"calls",
"emails",
"communications",
].map((o) => ({ value: o, label: o }))
const response = await fetch("https://api.hubapi.com/crm/v3/schemas", {
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
// A token without crm.schemas.custom.read still gets the standard objects.
if (!response.ok) return standard
const { results } = (await response.json()) as {
results: { fullyQualifiedName: string; labels: { plural: string } }[]
}
return [
...standard,
...results.map((s) => ({
value: s.fullyQualifiedName,
label: `${s.labels.plural} (custom)`,
})),
]
}
export type DynMultiselect_properties = string[]
// Dependent checklist: the properties of the chosen object type.
export async function properties(
auth: RT.Hubspot,
object_type: DynSelect_object_type
) {
if (!object_type) return []
const response = await fetch(
`https://api.hubapi.com/crm/v3/properties/${object_type}`,
{
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { results } = (await response.json()) as {
results: { name: string; label: string }[]
}
return results.map((p) => ({
value: p.name,
label: `${p.label} (${p.name})`,
}))
}
/**
* Search CRM Objects
* Search records of a CRM object type by free text (`query`) and/or property filters. Filters in one group are ANDed, groups are ORed, e.g. [{"filters": [{"propertyName": "email", "operator": "EQ", "value": "a@b.com"}]}]. Max 200 per page and 10,000 results per search. Object types other than contacts, companies and deals need their own scope on the connection (e.g. tickets, crm.objects.line_items.read, crm.objects.custom.read).
*/
export async function main(
auth: RT.Hubspot,
object_type: DynSelect_object_type,
query: string | undefined,
filter_groups:
| {
filters: {
propertyName: string
operator: string
value?: any
values?: any[]
highValue?: any
}[]
}[]
| undefined,
properties: DynMultiselect_properties | undefined,
sorts:
| { propertyName: string; direction: "ASCENDING" | "DESCENDING" }[]
| undefined,
limit: number | undefined,
after: string | undefined
) {
const url = new URL(
`https://api.hubapi.com/crm/v3/objects/${object_type}/search`
)
const body: { [key: string]: any } = {}
if (query != null && query !== "") body.query = query
if (filter_groups?.length) body.filterGroups = filter_groups
if (properties?.length) body.properties = properties
if (sorts?.length) body.sorts = sorts
if (limit != null) body.limit = limit
if (after != null && after !== "") body.after = after
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 5 hours ago