-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathclearbit.js
More file actions
executable file
·163 lines (148 loc) · 4.95 KB
/
Copy pathclearbit.js
File metadata and controls
executable file
·163 lines (148 loc) · 4.95 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env node
const API_KEY = process.env.CLEARBIT_API_KEY
if (!API_KEY) {
console.error(JSON.stringify({ error: 'CLEARBIT_API_KEY environment variable required' }))
process.exit(1)
}
async function api(method, baseUrl, path, body) {
const auth = 'Basic ' + Buffer.from(`${API_KEY}:`).toString('base64')
if (args['dry-run']) {
return { _dry_run: true, method, url: `${baseUrl}${path}`, headers: { 'Authorization': '***', 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body || undefined }
}
const res = await fetch(`${baseUrl}${path}`, {
method,
headers: {
'Authorization': auth,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
})
const text = await res.text()
try {
return JSON.parse(text)
} catch {
return { status: res.status, body: text }
}
}
function parseArgs(args) {
const result = { _: [] }
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg.startsWith('--')) {
const key = arg.slice(2)
const next = args[i + 1]
if (next && !next.startsWith('--')) {
result[key] = next
i++
} else {
result[key] = true
}
} else {
result._.push(arg)
}
}
return result
}
const args = parseArgs(process.argv.slice(2))
const [cmd, sub, ...rest] = args._
async function main() {
let result
switch (cmd) {
case 'person':
switch (sub) {
case 'find': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
result = await api('GET', 'https://person-stream.clearbit.com', `/v2/people/find?email=${encodeURIComponent(email)}`)
break
}
default:
result = { error: 'Unknown person subcommand. Use: find' }
}
break
case 'company':
switch (sub) {
case 'find': {
const domain = args.domain
if (!domain) { result = { error: '--domain required' }; break }
result = await api('GET', 'https://company-stream.clearbit.com', `/v2/companies/find?domain=${encodeURIComponent(domain)}`)
break
}
default:
result = { error: 'Unknown company subcommand. Use: find' }
}
break
case 'combined':
switch (sub) {
case 'find': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
result = await api('GET', 'https://person-stream.clearbit.com', `/v2/combined/find?email=${encodeURIComponent(email)}`)
break
}
default:
result = { error: 'Unknown combined subcommand. Use: find' }
}
break
case 'reveal':
switch (sub) {
case 'find': {
const ip = args.ip
if (!ip) { result = { error: '--ip required' }; break }
result = await api('GET', 'https://reveal.clearbit.com', `/v1/companies/find?ip=${encodeURIComponent(ip)}`)
break
}
default:
result = { error: 'Unknown reveal subcommand. Use: find' }
}
break
case 'name-to-domain':
switch (sub) {
case 'find': {
const name = args.name
if (!name) { result = { error: '--name required' }; break }
result = await api('GET', 'https://company.clearbit.com', `/v1/domains/find?name=${encodeURIComponent(name)}`)
break
}
default:
result = { error: 'Unknown name-to-domain subcommand. Use: find' }
}
break
case 'prospector':
switch (sub) {
case 'search': {
const domain = args.domain
if (!domain) { result = { error: '--domain required' }; break }
const params = new URLSearchParams({ domain })
if (args.role) params.set('role', args.role)
if (args.seniority) params.set('seniority', args.seniority)
if (args.title) params.set('title', args.title)
if (args.page) params.set('page', args.page)
if (args['page-size']) params.set('page_size', args['page-size'])
result = await api('GET', 'https://prospector.clearbit.com', `/v1/people/search?${params.toString()}`)
break
}
default:
result = { error: 'Unknown prospector subcommand. Use: search' }
}
break
default:
result = {
error: 'Unknown command',
usage: {
person: 'person find --email <email>',
company: 'company find --domain <domain>',
combined: 'combined find --email <email>',
reveal: 'reveal find --ip <ip>',
'name-to-domain': 'name-to-domain find --name <company_name>',
prospector: 'prospector search --domain <domain> [--role <role>] [--seniority <level>] [--title <title>]',
}
}
}
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(JSON.stringify({ error: err.message }))
process.exit(1)
})