//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)`,
})),
]
}
/**
* Get Associated Objects
* Fetch the records of one object type associated with a CRM record, e.g. the emails, meetings, notes or deals of a contact. Returns up to `limit` (max 100) records with the properties you list. Both object types need their read scope on the connection (emails need sales-email-read).
*/
export async function main(
auth: RT.Hubspot,
object_type: DynSelect_object_type,
object_id: string,
to_object_type: DynSelect_object_type,
properties: string[] | undefined,
limit: number | undefined
) {
const url = new URL(
`https://api.hubapi.com/crm/v4/objects/${object_type}/${object_id}/associations/${to_object_type}`
)
url.searchParams.append("limit", String(limit ?? 100))
const associations = await fetch(url, {
headers: {
Authorization: `Bearer ${auth.token}`,
Accept: "application/json",
},
})
if (!associations.ok) {
throw new Error(`${associations.status} ${await associations.text()}`)
}
const { results } = (await associations.json()) as {
results: { toObjectId: string | number }[]
}
if (!results.length) return []
const body: { [key: string]: any } = {
inputs: results.map((r) => ({ id: String(r.toObjectId) })),
}
if (properties?.length) body.properties = properties
const response = await fetch(
`https://api.hubapi.com/crm/v3/objects/${to_object_type}/batch/read`,
{
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()) as { results: any[] }).results
}
Submitted by hugo989 4 hours ago