Public
Script
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
export class GitHubAPI {
constructor(private token: string) {}
async getRepoContent(repo: string, path: string, branch: string) {
const response = await fetch(`https://api.github.com/repos/${repo}/contents/${path}?ref=${branch}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${this.token}`
}
});
if (!response.ok) {
throw new Error(`Error fetching repo content: ${response.statusText}`);
}
return response.json();
}
async getFileSHA(repo: string, path: string, branch: string) {
const response = await fetch(`https://api.github.com/repos/${repo}/contents/${path}?ref=${branch}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${this.token}`
}
});
if (!response.ok) {
throw new Error(`Error fetching file SHA: ${response.statusText}`);
}
const data = await response.json();
return data.sha;
}
async updateFile(repo: string, path: string, content: string, sha: string, message: string, branch: string) {
const response = await fetch(`https://api.github.com/repos/${repo}/contents/${path}`, {
method: 'PUT',
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: message,
content: btoa(unescape(encodeURIComponent(content))),
sha: sha,
branch: branch
})
});
if (!response.ok) {
const responseBody = await response.json();
console.error(`Error updating file: ${response.statusText}`);
console.error(`Response body: ${JSON.stringify(responseBody)}`);
throw new Error(`Error updating file: ${response.statusText}`);
}
return response.json();
}
async createFile(repo: string, path: string, content: string, message: string, branch: string) {
const url = `https://api.github.com/repos/${repo}/contents/${path}`;
let body: any = {
message: message,
content: btoa(unescape(encodeURIComponent(content))),
branch: branch
};
try {
const response = await fetch(`${url}?ref=${branch}`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json'
}
});
if (response.ok) {
const data = await response.json();
body.sha = data.sha;
}
} catch (error) {
console.error(`Error fetching file SHA: ${error.message}`);
}
const response = await fetch(url, {
method: 'PUT',
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
});
if (!response.ok) {
const responseBody = await response.json();
console.error(`Error creating file: ${response.statusText}`);
console.error(`Response body: ${JSON.stringify(responseBody)}`);
throw new Error(`Error creating file: ${response.statusText}`);
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 14, 2024