@proteos/sdk 0.93.1 → 0.94.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,512 @@
1
+ import type { ProteosClient } from '../client.js'
2
+ import type {
3
+ CreateEnrichmentConnectionRequest,
4
+ CreateEnrichmentRunRequest,
5
+ CreateResearchDefinitionRequest,
6
+ CreateSignalMonitorRequest,
7
+ CreateWaterfallRequest,
8
+ EnrichmentBlock,
9
+ EnrichmentConnection,
10
+ EnrichmentEntry,
11
+ EnrichmentItem,
12
+ EnrichmentMapping,
13
+ EnrichmentProvenance,
14
+ EnrichmentProvider,
15
+ EnrichmentRun,
16
+ EnrichmentRunList,
17
+ EnrichmentRunResponse,
18
+ EnrichmentTypesResponse,
19
+ EnrichRequest,
20
+ EnrichResponse,
21
+ ListEnrichmentRunsQuery,
22
+ ListResearchRunsQuery,
23
+ ListSignalsQuery,
24
+ PatchEnrichmentConnectionRequest,
25
+ PatchResearchDefinitionRequest,
26
+ PatchSignalMonitorRequest,
27
+ PatchWaterfallRequest,
28
+ PutSignalMappingRequest,
29
+ ReplaceEnrichmentMappingRequest,
30
+ ResearchDefinition,
31
+ ResearchResult,
32
+ ResearchRun,
33
+ ResearchRunList,
34
+ SignalList,
35
+ SignalMapping,
36
+ SignalMonitor,
37
+ Waterfall,
38
+ } from './types.js'
39
+
40
+ export * from './types.js'
41
+
42
+ const ENRICHMENT_BASE_PATH = '/enrichment/v1'
43
+
44
+ const encode = encodeURIComponent
45
+
46
+ /**
47
+ * `POST /enrich` waits for its waterfalls and `POST /enrichment-runs` for up
48
+ * to `wait_seconds`: 30 seconds each at most by default, which is the
49
+ * client's default timeout. They get longer, so the service's answer comes
50
+ * back instead of a client timeout.
51
+ */
52
+ const WAITING_REQUEST_TIMEOUT_MS = 75_000
53
+
54
+ /**
55
+ * Facade for enrichment-service: filling attributes from providers through
56
+ * waterfalls.
57
+ *
58
+ * `enrich` returns values and writes nothing. `runs` enrich records and write
59
+ * or propose what they find (a record draft), as each attribute's apply level
60
+ * says. `mappings` say what each attribute of an entity does in enrichment,
61
+ * `waterfalls` are an org's own step lists, `research` finds companies and
62
+ * people that are no records yet and proposes them as record drafts,
63
+ * `signals` are the changes a scheduled refresh or a web monitor detected and
64
+ * what they propose, and `catalog` is what can be asked for and who supplies
65
+ * it.
66
+ *
67
+ * Single resources come back bare; the lists (run items, waterfalls,
68
+ * providers) are unwrapped from `{data}` and are not paged.
69
+ *
70
+ * ```ts
71
+ * const enrichment = new EnrichmentClient(client)
72
+ * const found = await enrichment.enrich({ entity_slug: 'contact', record_id: id, attributes: ['email'] })
73
+ * const run = await enrichment.runs.create({ entity_slug: 'contact', record_ids: ids, wait_seconds: 20 })
74
+ * const items = await enrichment.runs.listItems(run.enrichment_run_id)
75
+ * await enrichment.mappings.setEntry('contact', 'email', { enrichment_type: 'work_email' })
76
+ * const researchRun = await enrichment.research.startRun(definitionId)
77
+ * const rows = await enrichment.research.listResults(researchRun.id)
78
+ * ```
79
+ */
80
+ export class EnrichmentClient {
81
+ readonly runs: EnrichmentRunService
82
+ readonly mappings: EnrichmentMappingService
83
+ readonly waterfalls: WaterfallService
84
+ readonly connections: EnrichmentConnectionService
85
+ readonly research: ResearchService
86
+ readonly signals: SignalService
87
+ readonly catalog: EnrichmentCatalogService
88
+
89
+ constructor(private readonly client: ProteosClient) {
90
+ this.runs = new EnrichmentRunServiceImpl(client)
91
+ this.mappings = new EnrichmentMappingServiceImpl(client)
92
+ this.waterfalls = new WaterfallServiceImpl(client)
93
+ this.connections = new EnrichmentConnectionServiceImpl(client)
94
+ this.research = new ResearchServiceImpl(client)
95
+ this.signals = new SignalServiceImpl(client)
96
+ this.catalog = new EnrichmentCatalogServiceImpl(client)
97
+ }
98
+
99
+ /** Fills attributes ad hoc: the values are returned, nothing is written. */
100
+ enrich(request: EnrichRequest): Promise<EnrichResponse> {
101
+ return this.client.request('POST', `${ENRICHMENT_BASE_PATH}/enrich`, request, {
102
+ timeout: WAITING_REQUEST_TIMEOUT_MS,
103
+ })
104
+ }
105
+ }
106
+
107
+ /** Runs over records. */
108
+ export interface EnrichmentRunService {
109
+ /**
110
+ * Starts a run. Answers once the run has finished or `wait_seconds` are
111
+ * over, whichever is first; a run that is still running is followed with
112
+ * `get`.
113
+ */
114
+ create(request: CreateEnrichmentRunRequest): Promise<EnrichmentRunResponse>
115
+ /** One page of the org's runs, newest first, without their `resolved_attributes`. */
116
+ list(query?: ListEnrichmentRunsQuery): Promise<EnrichmentRunList>
117
+ get(id: string): Promise<EnrichmentRun>
118
+ /** A run's items with their results. */
119
+ listItems(id: string): Promise<EnrichmentItem[]>
120
+ /** Stops the records whose turn has not come, and the ones that wait for a provider's answer. */
121
+ cancel(id: string): Promise<EnrichmentRun>
122
+ /**
123
+ * Where the values of one record came from: per attribute, the enrichment
124
+ * that found the value the record holds now and wrote it, or suggested it
125
+ * to a reviewer who accepted it.
126
+ */
127
+ provenance(entitySlug: string, recordId: string): Promise<EnrichmentProvenance>
128
+ }
129
+
130
+ /** The enrichment mapping of an entity. */
131
+ export interface EnrichmentMappingService {
132
+ /** An entity's mapping, its orphaned entries marked. */
133
+ get(entitySlug: string): Promise<EnrichmentMapping>
134
+ /** Writes the whole mapping; 409 when it changed meanwhile. */
135
+ replace(entitySlug: string, request: ReplaceEnrichmentMappingRequest): Promise<EnrichmentMapping>
136
+ getEntry(entitySlug: string, attribute: string): Promise<EnrichmentEntry>
137
+ /** Writes one attribute's entry and touches no other. */
138
+ setEntry(entitySlug: string, attribute: string, block: EnrichmentBlock): Promise<EnrichmentEntry>
139
+ deleteEntry(entitySlug: string, attribute: string): Promise<void>
140
+ }
141
+
142
+ /** An org's waterfalls. Platform defaults are listed and read-only. */
143
+ export interface WaterfallService {
144
+ /** The org's waterfalls, active or not, and the platform defaults. */
145
+ list(): Promise<Waterfall[]>
146
+ get(id: string): Promise<Waterfall>
147
+ /** 409 while another active waterfall holds its attribute, or is the default of its enrichment type. */
148
+ create(request: CreateWaterfallRequest): Promise<Waterfall>
149
+ patch(id: string, request: PatchWaterfallRequest): Promise<Waterfall>
150
+ delete(id: string): Promise<void>
151
+ }
152
+
153
+ /**
154
+ * An org's enrichment connections: its bindings to its own connections (its
155
+ * Clay key), which the `connection` steps of its waterfalls run on.
156
+ */
157
+ export interface EnrichmentConnectionService {
158
+ list(): Promise<EnrichmentConnection[]>
159
+ get(id: string): Promise<EnrichmentConnection>
160
+ /** Binds one of the org's connections. A person's own connection can only be bound by that person. */
161
+ create(request: CreateEnrichmentConnectionRequest): Promise<EnrichmentConnection>
162
+ patch(id: string, request: PatchEnrichmentConnectionRequest): Promise<EnrichmentConnection>
163
+ /** A waterfall step that names it is skipped from then on. */
164
+ delete(id: string): Promise<void>
165
+ }
166
+
167
+ /**
168
+ * Research definitions and their runs. A definition says what to find on the
169
+ * web and how a found row maps onto the org's entities; a run finds the rows
170
+ * and proposes the new ones as record drafts. Nothing is written to a record
171
+ * until a reviewer accepts a draft in data-service.
172
+ */
173
+ export interface ResearchService {
174
+ listDefinitions(): Promise<ResearchDefinition[]>
175
+ getDefinition(id: string): Promise<ResearchDefinition>
176
+ /** Every entity the definition maps onto needs an enrichment mapping. */
177
+ createDefinition(request: CreateResearchDefinitionRequest): Promise<ResearchDefinition>
178
+ patchDefinition(id: string, request: PatchResearchDefinitionRequest): Promise<ResearchDefinition>
179
+ /** Removes the definition with its runs and their rows. The record drafts its runs proposed stay. */
180
+ deleteDefinition(id: string): Promise<void>
181
+ /**
182
+ * Starts a run and returns it while it is still running; follow it with
183
+ * `getRun`. 409 while the definition has a run that is still running.
184
+ */
185
+ startRun(definitionId: string): Promise<ResearchRun>
186
+ /** One page of the org's research runs, newest first. */
187
+ listRuns(query?: ListResearchRunsQuery): Promise<ResearchRunList>
188
+ getRun(id: string): Promise<ResearchRun>
189
+ /** The rows a run found, each as a reviewer left it. */
190
+ listResults(runId: string): Promise<ResearchResult[]>
191
+ /** Stops a running run. Drafts it proposed already stay. */
192
+ cancelRun(id: string): Promise<ResearchRun>
193
+ }
194
+
195
+ /**
196
+ * Signals: the log of detected changes, the signal mapping of each kind
197
+ * (which records a signal creates and what it proposes on the record it is
198
+ * about), and signal monitors (standing web monitors for news, funding and
199
+ * hiring about some records). A scheduled refresh detects `field_change`,
200
+ * `job_change` and `headcount_change` by itself; turn it on with the
201
+ * `on_schedule` trigger and `refresh_every_days` of a mapping entry.
202
+ */
203
+ export interface SignalService {
204
+ /** One page of the org's signals, newest first. */
205
+ list(query?: ListSignalsQuery): Promise<SignalList>
206
+ listMappings(): Promise<SignalMapping[]>
207
+ getMapping(kind: string): Promise<SignalMapping>
208
+ /** Writes the whole of what a signal of the kind does to records. */
209
+ putMapping(kind: string, request: PutSignalMappingRequest): Promise<SignalMapping>
210
+ /** The kind's signals are still logged and published, and propose nothing. */
211
+ deleteMapping(kind: string): Promise<void>
212
+ listMonitors(): Promise<SignalMonitor[]>
213
+ getMonitor(id: string): Promise<SignalMonitor>
214
+ /**
215
+ * An active monitor checks for the first time within a minute and then every
216
+ * `schedule.every_days`; every check costs money.
217
+ */
218
+ createMonitor(request: CreateSignalMonitorRequest): Promise<SignalMonitor>
219
+ patchMonitor(id: string, request: PatchSignalMonitorRequest): Promise<SignalMonitor>
220
+ /** The signals the monitor detected stay. */
221
+ deleteMonitor(id: string): Promise<void>
222
+ }
223
+
224
+ /** What can be asked for and who supplies it. The same for every org. */
225
+ export interface EnrichmentCatalogService {
226
+ enrichmentTypes(): Promise<EnrichmentTypesResponse>
227
+ providers(): Promise<EnrichmentProvider[]>
228
+ }
229
+
230
+ class EnrichmentRunServiceImpl implements EnrichmentRunService {
231
+ constructor(private readonly client: ProteosClient) {}
232
+
233
+ create(request: CreateEnrichmentRunRequest): Promise<EnrichmentRunResponse> {
234
+ return this.client.request('POST', `${ENRICHMENT_BASE_PATH}/enrichment-runs`, request, {
235
+ timeout: WAITING_REQUEST_TIMEOUT_MS,
236
+ })
237
+ }
238
+
239
+ list(query: ListEnrichmentRunsQuery = {}): Promise<EnrichmentRunList> {
240
+ return this.client.requestWithQuery('GET', `${ENRICHMENT_BASE_PATH}/enrichment-runs`, query)
241
+ }
242
+
243
+ get(id: string): Promise<EnrichmentRun> {
244
+ return this.client.request('GET', `${ENRICHMENT_BASE_PATH}/enrichment-runs/${encode(id)}`)
245
+ }
246
+
247
+ async listItems(id: string): Promise<EnrichmentItem[]> {
248
+ const response = await this.client.request<{ data: EnrichmentItem[] }>(
249
+ 'GET',
250
+ `${ENRICHMENT_BASE_PATH}/enrichment-runs/${encode(id)}/items`,
251
+ )
252
+ return response.data
253
+ }
254
+
255
+ cancel(id: string): Promise<EnrichmentRun> {
256
+ return this.client.request(
257
+ 'POST',
258
+ `${ENRICHMENT_BASE_PATH}/enrichment-runs/${encode(id)}/cancel`,
259
+ )
260
+ }
261
+
262
+ provenance(entitySlug: string, recordId: string): Promise<EnrichmentProvenance> {
263
+ return this.client.requestWithQuery('GET', `${ENRICHMENT_BASE_PATH}/provenance`, {
264
+ entity_slug: entitySlug,
265
+ record_id: recordId,
266
+ })
267
+ }
268
+ }
269
+
270
+ class EnrichmentMappingServiceImpl implements EnrichmentMappingService {
271
+ constructor(private readonly client: ProteosClient) {}
272
+
273
+ private entryPath(entitySlug: string, attribute: string): string {
274
+ return `${ENRICHMENT_BASE_PATH}/mappings/${encode(entitySlug)}/attributes/${encode(attribute)}`
275
+ }
276
+
277
+ get(entitySlug: string): Promise<EnrichmentMapping> {
278
+ return this.client.request('GET', `${ENRICHMENT_BASE_PATH}/mappings/${encode(entitySlug)}`)
279
+ }
280
+
281
+ replace(
282
+ entitySlug: string,
283
+ request: ReplaceEnrichmentMappingRequest,
284
+ ): Promise<EnrichmentMapping> {
285
+ return this.client.request(
286
+ 'PUT',
287
+ `${ENRICHMENT_BASE_PATH}/mappings/${encode(entitySlug)}`,
288
+ request,
289
+ )
290
+ }
291
+
292
+ getEntry(entitySlug: string, attribute: string): Promise<EnrichmentEntry> {
293
+ return this.client.request('GET', this.entryPath(entitySlug, attribute))
294
+ }
295
+
296
+ setEntry(
297
+ entitySlug: string,
298
+ attribute: string,
299
+ block: EnrichmentBlock,
300
+ ): Promise<EnrichmentEntry> {
301
+ return this.client.request('PUT', this.entryPath(entitySlug, attribute), block)
302
+ }
303
+
304
+ async deleteEntry(entitySlug: string, attribute: string): Promise<void> {
305
+ await this.client.request('DELETE', this.entryPath(entitySlug, attribute))
306
+ }
307
+ }
308
+
309
+ class WaterfallServiceImpl implements WaterfallService {
310
+ constructor(private readonly client: ProteosClient) {}
311
+
312
+ async list(): Promise<Waterfall[]> {
313
+ const response = await this.client.request<{ data: Waterfall[] }>(
314
+ 'GET',
315
+ `${ENRICHMENT_BASE_PATH}/waterfalls`,
316
+ )
317
+ return response.data
318
+ }
319
+
320
+ get(id: string): Promise<Waterfall> {
321
+ return this.client.request('GET', `${ENRICHMENT_BASE_PATH}/waterfalls/${encode(id)}`)
322
+ }
323
+
324
+ create(request: CreateWaterfallRequest): Promise<Waterfall> {
325
+ return this.client.request('POST', `${ENRICHMENT_BASE_PATH}/waterfalls`, request)
326
+ }
327
+
328
+ patch(id: string, request: PatchWaterfallRequest): Promise<Waterfall> {
329
+ return this.client.request('PATCH', `${ENRICHMENT_BASE_PATH}/waterfalls/${encode(id)}`, request)
330
+ }
331
+
332
+ async delete(id: string): Promise<void> {
333
+ await this.client.request('DELETE', `${ENRICHMENT_BASE_PATH}/waterfalls/${encode(id)}`)
334
+ }
335
+ }
336
+
337
+ class EnrichmentConnectionServiceImpl implements EnrichmentConnectionService {
338
+ constructor(private readonly client: ProteosClient) {}
339
+
340
+ async list(): Promise<EnrichmentConnection[]> {
341
+ const response = await this.client.request<{ data: EnrichmentConnection[] }>(
342
+ 'GET',
343
+ `${ENRICHMENT_BASE_PATH}/enrichment-connections`,
344
+ )
345
+ return response.data
346
+ }
347
+
348
+ get(id: string): Promise<EnrichmentConnection> {
349
+ return this.client.request(
350
+ 'GET',
351
+ `${ENRICHMENT_BASE_PATH}/enrichment-connections/${encode(id)}`,
352
+ )
353
+ }
354
+
355
+ create(request: CreateEnrichmentConnectionRequest): Promise<EnrichmentConnection> {
356
+ return this.client.request('POST', `${ENRICHMENT_BASE_PATH}/enrichment-connections`, request)
357
+ }
358
+
359
+ patch(id: string, request: PatchEnrichmentConnectionRequest): Promise<EnrichmentConnection> {
360
+ return this.client.request(
361
+ 'PATCH',
362
+ `${ENRICHMENT_BASE_PATH}/enrichment-connections/${encode(id)}`,
363
+ request,
364
+ )
365
+ }
366
+
367
+ async delete(id: string): Promise<void> {
368
+ await this.client.request(
369
+ 'DELETE',
370
+ `${ENRICHMENT_BASE_PATH}/enrichment-connections/${encode(id)}`,
371
+ )
372
+ }
373
+ }
374
+
375
+ class SignalServiceImpl implements SignalService {
376
+ constructor(private readonly client: ProteosClient) {}
377
+
378
+ private mappingPath(kind: string): string {
379
+ return `${ENRICHMENT_BASE_PATH}/signal-mappings/${encode(kind)}`
380
+ }
381
+
382
+ private monitorPath(id: string): string {
383
+ return `${ENRICHMENT_BASE_PATH}/signal-monitors/${encode(id)}`
384
+ }
385
+
386
+ list(query: ListSignalsQuery = {}): Promise<SignalList> {
387
+ return this.client.requestWithQuery('GET', `${ENRICHMENT_BASE_PATH}/signals`, query)
388
+ }
389
+
390
+ async listMappings(): Promise<SignalMapping[]> {
391
+ const response = await this.client.request<{ data: SignalMapping[] }>(
392
+ 'GET',
393
+ `${ENRICHMENT_BASE_PATH}/signal-mappings`,
394
+ )
395
+ return response.data
396
+ }
397
+
398
+ getMapping(kind: string): Promise<SignalMapping> {
399
+ return this.client.request('GET', this.mappingPath(kind))
400
+ }
401
+
402
+ putMapping(kind: string, request: PutSignalMappingRequest): Promise<SignalMapping> {
403
+ return this.client.request('PUT', this.mappingPath(kind), request)
404
+ }
405
+
406
+ async deleteMapping(kind: string): Promise<void> {
407
+ await this.client.request('DELETE', this.mappingPath(kind))
408
+ }
409
+
410
+ async listMonitors(): Promise<SignalMonitor[]> {
411
+ const response = await this.client.request<{ data: SignalMonitor[] }>(
412
+ 'GET',
413
+ `${ENRICHMENT_BASE_PATH}/signal-monitors`,
414
+ )
415
+ return response.data
416
+ }
417
+
418
+ getMonitor(id: string): Promise<SignalMonitor> {
419
+ return this.client.request('GET', this.monitorPath(id))
420
+ }
421
+
422
+ createMonitor(request: CreateSignalMonitorRequest): Promise<SignalMonitor> {
423
+ return this.client.request('POST', `${ENRICHMENT_BASE_PATH}/signal-monitors`, request)
424
+ }
425
+
426
+ patchMonitor(id: string, request: PatchSignalMonitorRequest): Promise<SignalMonitor> {
427
+ return this.client.request('PATCH', this.monitorPath(id), request)
428
+ }
429
+
430
+ async deleteMonitor(id: string): Promise<void> {
431
+ await this.client.request('DELETE', this.monitorPath(id))
432
+ }
433
+ }
434
+
435
+ class ResearchServiceImpl implements ResearchService {
436
+ constructor(private readonly client: ProteosClient) {}
437
+
438
+ private definitionPath(id: string): string {
439
+ return `${ENRICHMENT_BASE_PATH}/research-definitions/${encode(id)}`
440
+ }
441
+
442
+ private runPath(id: string): string {
443
+ return `${ENRICHMENT_BASE_PATH}/research-runs/${encode(id)}`
444
+ }
445
+
446
+ async listDefinitions(): Promise<ResearchDefinition[]> {
447
+ const response = await this.client.request<{ data: ResearchDefinition[] }>(
448
+ 'GET',
449
+ `${ENRICHMENT_BASE_PATH}/research-definitions`,
450
+ )
451
+ return response.data
452
+ }
453
+
454
+ getDefinition(id: string): Promise<ResearchDefinition> {
455
+ return this.client.request('GET', this.definitionPath(id))
456
+ }
457
+
458
+ createDefinition(request: CreateResearchDefinitionRequest): Promise<ResearchDefinition> {
459
+ return this.client.request('POST', `${ENRICHMENT_BASE_PATH}/research-definitions`, request)
460
+ }
461
+
462
+ patchDefinition(
463
+ id: string,
464
+ request: PatchResearchDefinitionRequest,
465
+ ): Promise<ResearchDefinition> {
466
+ return this.client.request('PATCH', this.definitionPath(id), request)
467
+ }
468
+
469
+ async deleteDefinition(id: string): Promise<void> {
470
+ await this.client.request('DELETE', this.definitionPath(id))
471
+ }
472
+
473
+ startRun(definitionId: string): Promise<ResearchRun> {
474
+ return this.client.request('POST', `${this.definitionPath(definitionId)}/runs`)
475
+ }
476
+
477
+ listRuns(query: ListResearchRunsQuery = {}): Promise<ResearchRunList> {
478
+ return this.client.requestWithQuery('GET', `${ENRICHMENT_BASE_PATH}/research-runs`, query)
479
+ }
480
+
481
+ getRun(id: string): Promise<ResearchRun> {
482
+ return this.client.request('GET', this.runPath(id))
483
+ }
484
+
485
+ async listResults(runId: string): Promise<ResearchResult[]> {
486
+ const response = await this.client.request<{ data: ResearchResult[] }>(
487
+ 'GET',
488
+ `${this.runPath(runId)}/results`,
489
+ )
490
+ return response.data
491
+ }
492
+
493
+ cancelRun(id: string): Promise<ResearchRun> {
494
+ return this.client.request('POST', `${this.runPath(id)}/cancel`)
495
+ }
496
+ }
497
+
498
+ class EnrichmentCatalogServiceImpl implements EnrichmentCatalogService {
499
+ constructor(private readonly client: ProteosClient) {}
500
+
501
+ enrichmentTypes(): Promise<EnrichmentTypesResponse> {
502
+ return this.client.request('GET', `${ENRICHMENT_BASE_PATH}/enrichment-types`)
503
+ }
504
+
505
+ async providers(): Promise<EnrichmentProvider[]> {
506
+ const response = await this.client.request<{ data: EnrichmentProvider[] }>(
507
+ 'GET',
508
+ `${ENRICHMENT_BASE_PATH}/providers`,
509
+ )
510
+ return response.data
511
+ }
512
+ }