-
Notifications
You must be signed in to change notification settings - Fork 0
/
gist.js
156 lines (139 loc) · 5.08 KB
/
gist.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
export default class Gist {
static GIST_TOKEN = "GIST_TOKEN";
// https://docs.github.com/en/rest/gists/gists?apiVersion=2022-11-28
static type = {
"getGist": "getGist", // https://api.github.com/gists/:gist_id // GET
"getGists": "getGists", // https://api.github.com/gists // GET
"getStarredGists": "getStarredGists", // https://api.github.com/gists/starred // GET
"getGistHistory": "getGistHistory", // https://api.github.com/gists/:gist_id/:sha // GET
"createGist": "createGist", // https://api.github.com/gists // POST
"updateGist": "updateGist", // https://api.github.com/gists/:gist_id // PATCH
"deleteGist": "deleteGist", // https://api.github.com/gists/:gist_id // DELETE
"starGist": "starGist", // https://api.github.com/gists/:gist_id/star // PUT
"unStarGist": "unStarGist", // https://api.github.com/gists/:gist_id/star // DELETE
}
static headers = {
"Accept": "application/vnd.github+json",
"Authorization": `Bearer ${this.GIST_TOKEN}`
}
static getURLAndMethod(type, gist_id = "", sha = "") {
let API_URL = "https://api.github.com/gists";
if (type === this.type.getGist) {
return { url: `${API_URL}/${gist_id}`, method: "GET" };
}
else if (type === this.type.getGists) {
return { url: `${API_URL}`, method: "GET" };
}
else if (type === this.type.getStarredGists) {
return { url: `${API_URL}/starred`, method: "GET" };
}
else if (type === this.type.getGistHistory) {
return { url: `${API_URL}/${gist_id}/${sha}`, method: "GET" };
}
else if (type === this.type.createGist) {
return { url: `${API_URL}`, method: "POST" };
}
else if (type === this.type.updateGist) {
return { url: `${API_URL}/${gist_id}`, method: "PATCH" };
}
else if (type === this.type.deleteGist) {
return { url: `${API_URL}/${gist_id}`, method: "DELETE" };
}
else if (type === this.type.starGist) {
return { url: `${API_URL}/${gist_id}/star`, method: "PUT" };
}
else if (type === this.type.unStarGist) {
return { url: `${API_URL}/${gist_id}/star`, method: "DELETE" };
}
else {
throw new Error("type not found");
}
}
static async get(data) {
const { type, gist_id, sha } = data;
const { url, method } = this.getURLAndMethod(type, gist_id, sha);
const options = {
method: method,
headers: this.headers,
};
console.log(url, options);
return new Promise((resolve, reject) => {
fetch(url, options)
.then(response => response.json())
.then(data => {
//console.log(data);
resolve(data);
})
.catch(error => {
console.error(error);
reject(error);
});
});
}
static async post(data) {
const { type, content, gist_id, sha } = data;
const { url, method } = this.getURLAndMethod(type, gist_id, sha);
const options = {
method: method,
headers: this.headers,
body: JSON.stringify(content)
};
return new Promise((resolve, reject) => {
fetch(url, options)
.then(response => response.json())
.then(data => {
//console.log(data);
resolve(data);
})
.catch(error => {
console.error(error);
reject(error);
});
});
}
static async patch(data) {
return new Promise((resolve, reject) => {
this.post(data)
.then(data => {
//console.log(data);
resolve(data);
})
.catch(error => {
console.error(error);
reject(error);
});
});
}
static async put(data) {
const { type, gist_id, sha } = data;
const { url, method } = this.getURLAndMethod(type, gist_id, sha);
const options = {
method: method,
headers: this.headers,
};
return new Promise((resolve, reject) => {
fetch(url, options)
.then(response => response.status)
.then(data => {
//console.log(data)
resolve(data);
})
.catch(error => {
console.error(error);
reject(error);
});
});
}
static async delete(data) {
return new Promise((resolve, reject) => {
this.put(data)
.then(data => {
resolve(data);
})
.catch(error => {
console.error(error);
reject(error);
});
});
}
}