-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
105 lines (91 loc) · 2.73 KB
/
index.ts
File metadata and controls
105 lines (91 loc) · 2.73 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Octokit } from "@octokit/rest";
import * as base64 from "nodejs-base64-converter";
export function makeId(length: number = 10): string {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
export function formatSize(bytes: number): string {
if (bytes < 1024) return bytes + " B";
if (bytes < 1024 ** 2) return (bytes / 1024).toFixed(1) + " KB";
if (bytes < 1024 ** 3) return (bytes / 1024 ** 2).toFixed(1) + " MB";
if (bytes < 1024 ** 4) return (bytes / 1024 ** 3).toFixed(1) + " GB";
return (bytes / 1024 ** 4).toFixed(1) + " TB";
}
export function isJson(data: string): boolean {
if (!data || typeof data !== "string") return false;
try {
JSON.parse(data);
return true;
} catch {
return false;
}
}
export interface DriverOptions {
username: string;
repository: string;
token: string;
path?: string;
message?: string;
[key: string]: any;
}
export function createDriver(options: DriverOptions) {
const opts = {
owner: options.username,
repo: options.repository,
path: options.path || "file.json",
message: options.message || makeId(10),
...options,
};
const baseUrl = `https://api.github.com/repos/${opts.owner}/${opts.repo}/contents/${opts.path}`;
const octokit = new Octokit({ auth: opts.token });
return { opts, baseUrl, octokit };
}
export async function fetchFile(driver: any) {
const { baseUrl, octokit } = driver;
try {
const { data } = await octokit.request("GET " + baseUrl);
return {
status: true,
size: formatSize(data.size),
sha: data.sha,
content: JSON.parse(base64.decode(data.content)),
};
} catch (err: any) {
return { status: false, msg: err.message };
}
}
export async function saveFile(driver: any, data: any, extraOptions: any = {}) {
const { baseUrl, octokit, opts } = driver;
const jsonData =
typeof data === "object" || Array.isArray(data)
? JSON.stringify(data)
: data;
if (!isJson(jsonData)) {
return {
status: false,
msg: "data type is invalid, make sure the data is in json format",
};
}
const existing = await fetchFile(driver);
try {
const { data: response } = await octokit.request("PUT " + baseUrl, {
sha: existing.status ? existing.sha : undefined,
...opts,
content: base64.encode(jsonData),
...extraOptions,
});
return {
status: true,
filename: response.content.name,
path: response.content.path,
size: formatSize(response.content.size),
commit: response.commit.message,
};
} catch (err: any) {
return { status: false, msg: err.message };
}
}