-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattioClient.ts
148 lines (146 loc) · 4.62 KB
/
attioClient.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import currency from 'currency.js'
import { z } from 'zod'
import { makeZodSchemas } from './schemas'
import { buildUrl, R } from './utils'
export function withAttio(opts: {
workspaceSlug: string
fetch: import('@codahq/packs-sdk').Fetcher['fetch']
}) {
const schemas = makeZodSchemas({ workspaceSlug: opts.workspaceSlug })
const zListCollectionEntriesResponse = z.object({
next_page_offset: z.number().nullable(),
data: z.array(schemas.entry),
})
const zListCollectionAttributesResponse = z.object({
next_page_offset: z.number().nullable(),
data: z.array(schemas.v2Attribute),
})
function jsonHttp<T = any>(
method: Parameters<typeof opts['fetch']>[0]['method'],
url: string,
body?: Record<string, unknown>,
) {
return opts
.fetch<T>({
method,
url,
headers: { 'content-type': 'application/json' },
body: body ? JSON.stringify(body) : undefined,
})
.then((r) => r.body)
}
return {
fetchPerson: (person_id: string) =>
jsonHttp('GET', `https://api.attio.com/v1/people/${person_id}`).then(
(r) => schemas.transformRecord(r),
),
fetchCompany: (company_id: string) =>
jsonHttp('GET', `https://api.attio.com/v1/companies/${company_id}`).then(
(r) => schemas.transformRecord(r),
),
/** https://developers.attio.com/#assert-person-record */
assertPerson: (input: {
email_addresses: string[]
first_name?: string
last_name?: string
description?: string
}) =>
jsonHttp('PUT', 'https://api.attio.com/v1/people', input).then((r) =>
schemas.transformRecord(r),
),
/** https://developers.attio.com/#assert-company-record */
assertCompany: (input: {
domains: string[]
name?: string
description?: string
}) =>
jsonHttp('PUT', 'https://api.attio.com/v1/companies', input).then((r) =>
schemas.transformRecord(r),
),
listCollections: () =>
jsonHttp('GET', 'https://api.attio.com/v1/collections').then((r) =>
z.array(schemas.collection).parse(r),
),
listCollectionEntries: (
collectionId: string,
{
limit = DEFAULT_LIMIT,
offset = 0,
}: { limit?: number; offset?: number },
) =>
jsonHttp<z.infer<typeof zListCollectionEntriesResponse>>(
'GET',
buildUrl(
`https://api.attio.com/v1/collections/${collectionId}/entries`,
{ limit, offset },
),
).then((res) => ({
...res,
data: res.data.map((entry) => ({
...entry,
record: schemas.transformRecord(entry.record),
})),
})),
createCollectionEntry: (
collectionId: string,
body: { record_type: 'person' | 'company'; record_id: string },
) =>
jsonHttp<z.infer<typeof schemas.entry>>(
'POST',
`https://api.attio.com/v1/collections/${collectionId}/entries`,
body,
).then((entry) => ({
...entry,
record: schemas.transformRecord(entry.record),
})),
deleteCollectionEntry: (collectionId: string, entryId: string) =>
jsonHttp<{}>(
'DELETE',
`https://api.attio.com/v1/collections/${collectionId}/entries/${entryId}`,
),
patchCollectionEntry: (
collectionId: string,
entryId: string,
valueByAttributeIdOrSlug: Record<string, unknown>,
) =>
jsonHttp<{}>(
'PATCH',
`https://api.attio.com/v2/lists/${collectionId}/entries/${entryId}`,
{
data: {
entry_values: R.mapValues(valueByAttributeIdOrSlug, (v) => [
// Attio seems to only validate the key relevant for attribute type, and ignores
// invalid values for other types. So we can just send all the keys.
// , currency_value: currency(v as currency.Any).value
// update 2023-11-09_0835 appears to be not anymore...
{ value: v },
]),
},
},
),
listCollectionAttributes: (
collectionId: string,
{
limit = DEFAULT_LIMIT,
offset = 0,
show_archived,
}: { limit?: number; offset?: number; show_archived?: boolean },
) =>
jsonHttp<z.infer<typeof zListCollectionAttributesResponse>>(
'GET',
buildUrl(`https://api.attio.com/v2/lists/${collectionId}/attributes`, {
limit,
offset,
show_archived,
}),
).then((res) => ({
...res,
data: res.data.map((attr) => ({
...attr,
attribute_id: attr.id.attribute_id,
collection_id: attr.id.object_id,
})),
})),
}
}
const DEFAULT_LIMIT = 250