-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·88 lines (74 loc) · 2.46 KB
/
Copy pathcli.js
File metadata and controls
executable file
·88 lines (74 loc) · 2.46 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
#!/usr/bin/env node
const { program } = require('commander');
const fetch = require('node-fetch');
// Your worker URL
const WORKER_URL = 'https://autoblogger-agent.wzmcghee.workers.dev';
let accessToken = null;
async function getAccessToken() {
if (!process.env.CF_ACCESS_EMAIL) {
throw new Error('Please set CF_ACCESS_EMAIL environment variable');
}
const response = await fetch('https://autoblogger.cloudflareaccess.com/cdn-cgi/access/get-token', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: process.env.CF_ACCESS_EMAIL })
});
if (!response.ok) {
throw new Error('Failed to get access token');
}
const data = await response.json();
return data.token;
}
async function makeAuthenticatedRequest(path, method, body) {
if (!accessToken) {
accessToken = await getAccessToken();
}
const response = await fetch(`${WORKER_URL}${path}`, {
method,
headers: {
'Content-Type': 'application/json',
'Cf-Access-Jwt-Assertion': accessToken
},
body: body ? JSON.stringify(body) : undefined
});
if (response.status === 401) {
// Token might be expired, try once more
accessToken = await getAccessToken();
return makeAuthenticatedRequest(path, method, body);
}
return response;
}
program
.command('generate <topic>')
.description('Generate a new blog post')
.action(async (topic) => {
try {
const response = await makeAuthenticatedRequest('/generate', 'POST', { topic });
console.log('Generated:', await response.json());
} catch (error) {
console.error('Error:', error);
}
});
program
.command('publish <url>')
.description('Publish a draft post')
.action(async (url) => {
try {
const response = await makeAuthenticatedRequest('/publish', 'POST', { postUrl: url });
console.log('Published:', await response.json());
} catch (error) {
console.error('Error:', error);
}
});
program
.command('list')
.description('List all posts')
.action(async () => {
try {
const response = await makeAuthenticatedRequest('/posts', 'GET');
console.log('Posts:', await response.json());
} catch (error) {
console.error('Error:', error);
}
});
program.parse(process.argv);