-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitApiIO.js
More file actions
84 lines (76 loc) · 2.89 KB
/
gitApiIO.js
File metadata and controls
84 lines (76 loc) · 2.89 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
import { createRequire } from "module";
const require = createRequire(import.meta.url);
var axios = require('axios');
var fs = require('fs');
var base64 = require('base-64');
class gitApiIO{
constructor(serverConf) {
this.serverConf = serverConf
console.log("config is:", serverConf)
}
async uploadFileApi() {
return new Promise((res,rej)=> {
let serverConf = this.serverConf
var configGetFile = {
method: 'get',
url: `https://api.github.com/repos/${serverConf.githubUser}/${serverConf.githubRepo}/contents/${serverConf.fileName}`,
headers: {
'Authorization': `Bearer ${serverConf.githubToken}`,
'Content-Type': 'application/json'
}
};
let sha = ""
axios(configGetFile)
.then(function (response) {
// console.log(response.data.sha);
sha = response.data.sha
var data = JSON.stringify({
"message": "txt file",
"content": `${serverConf.content}`,
"sha": sha,
});
var configPutFile = {
method: 'put',
url: `https://api.github.com/repos/${serverConf.githubUser}/${serverConf.githubRepo}/contents/${serverConf.fileName}`,
headers: {
'Authorization': `Bearer ${serverConf.githubToken}`,
'Content-Type': 'application/json',
},
message: "Commit message",
data: data,
};
axios(configPutFile)
.then(function (response) {
res()
})
.catch(function (error) {
rej(error)
});
})
.catch(function (error) {
rej(error)
});
})
}
async retrieveFileAPI() {
return new Promise((res,rej)=> {
var configGetFile = {
method: 'get',
url: `https://api.github.com/repos/${this.serverConf.githubUser}/${this.serverConf.githubRepo}/contents/${this.serverConf.fileName}`,
headers: {
'Authorization': `Bearer ${this.serverConf.githubToken}`,
'Content-Type': 'application/json'
}
};
axios(configGetFile)
.then(function (response) {
res(base64.decode(response.data.content))
// return base64.decode(response.data.content)
})
.catch(function (error) {
rej(error);
});
})
}
}
export default gitApiIO