-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-contacts-csv.js
37 lines (34 loc) · 972 Bytes
/
upload-contacts-csv.js
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
var fetch = require('node-fetch')
var Papa = require('papaparse')
var chalk = require('chalk')
var error = chalk.bold.red
var success = chalk.bold.green
var key = encodeURIComponent('8631d820')
var project = encodeURIComponent('Octave')
var datasetName = 'contacts'
var name = encodeURIComponent(datasetName)
var generated = require('./generated/contacts.js')
var minContacts = generated.contacts.map(contact => {
const { _id, displayName, title } = contact
return {
_id,
displayName,
title
}
})
var csv = Papa.unparse(minContacts)
fetch(`https://api.mockaroo.com/api/datasets/${name}?filename=${name}.csv&key=${key}&project=${project}`, {
method: 'post',
body: csv,
headers: {
'content-type': 'text/csv'
}
}).then(res => {
if (res.ok) {
console.log(success(datasetName, 'upload success'))
// console.log('Mockaroo API response:')
// console.dir(res)
} else {
console.log(error('upload error:', res.statusText))
}
})