-
Notifications
You must be signed in to change notification settings - Fork 0
/
deletesanitydocs.mjs
63 lines (51 loc) · 1.57 KB
/
deletesanitydocs.mjs
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
import fetch from 'node-fetch';
import fs from 'fs';
import readline from 'readline';
// deletes batch sanity posts
async function deletePostFromSanity(documentId) {
const url = os.environ.get('SANITY_URL')
const token = os.environ.get('SANITY_TOKEN')
const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
};
const payload = {
mutations: [
{
delete: {
id: documentId
}
}
]
};
try {
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
});
const responseBody = await response.text();
console.log(response.text);
console.log(`Status Code: ${response.status}`);
console.log(`Response: ${responseBody}`);
if (response.status !== 200) {
console.log(`Error deleting post in Sanity. Status Code: ${response.status}`);
} else {
console.log(`Successfully deleted post with ID: ${documentId}`);
}
} catch (error) {
console.log(`An error occurred: ${error.toString()}`);
}
}
async function processFile() {
const readInterface = readline.createInterface({
input: fs.createReadStream('todelete.csv'),
output: process.stdout,
terminal: false
});
for await (const line of readInterface) {
const postId = `drafts.${line.trim()}`;
await deletePostFromSanity(postId);
}
}
processFile();