-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudflareClient.ts
More file actions
46 lines (37 loc) · 1.33 KB
/
cloudflareClient.ts
File metadata and controls
46 lines (37 loc) · 1.33 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
// src/utils/cloudflareClient.ts
import fetch from "node-fetch"; // Needed for Node/Electron
interface CloudflareResponse {
success: boolean;
errors?: any[];
messages?: any[];
result?: any;
}
/**
* Uploads a script to Cloudflare Workers using user-provided credentials.
* @param userCode The JavaScript code to upload
*/
export const uploadToCloudflare = async (userCode: string) => {
// Step 1: Get stored credentials from localStorage
const config = JSON.parse(localStorage.getItem("envConfig") || "{}");
if (!config.apiKey || !config.accountId || !config.scriptName) {
throw new Error("Missing API key, Account ID, or Script Name. Please fill out the config panel.");
}
// Step 2: Construct Cloudflare endpoint
const url = `https://api.cloudflare.com/client/v4/accounts/${config.accountId}/workers/scripts/${config.scriptName}`;
// Step 3: Make the PUT request
const res = await fetch(url, {
method: "PUT",
headers: {
Authorization: `Bearer ${config.apiKey}`,
"Content-Type": "application/javascript",
},
body: userCode,
});
const result = (await res.json()) as CloudflareResponse;
// Step 4: Check for success
if (!result.success) {
console.error("Cloudflare upload failed:", result.errors);
throw new Error("Upload failed. See console for details.");
}
return result;
};