Minor changes

This commit is contained in:
Peng-YM 2022-05-25 11:00:00 +08:00
parent c389aa19a2
commit 0e46d8e14d
10 changed files with 41 additions and 38 deletions

View File

@ -18,6 +18,6 @@ console.log(
`, `,
); );
import serve from './facade'; import serve from './restful';
serve(); serve();

View File

@ -9,14 +9,14 @@ export default async function download(url, ua) {
return cache.get(id); return cache.get(id);
} }
const $http = HTTP({ const http = HTTP({
headers: { headers: {
'User-Agent': ua, 'User-Agent': ua,
}, },
}); });
const result = new Promise((resolve, reject) => { const result = new Promise((resolve, reject) => {
$http.get(url).then((resp) => { http.get(url).then((resp) => {
const body = resp.body; const body = resp.body;
if (body.replace(/\s/g, '').length === 0) if (body.replace(/\s/g, '').length === 0)
reject(new Error('订阅内容为空!')); reject(new Error('订阅内容为空!'));

View File

@ -3,32 +3,35 @@ import { HTTP } from './open-api';
/** /**
* Gist backup * Gist backup
*/ */
export default function Gist({ token, key }) { export default class Gist {
const http = HTTP({ constructor({ token, key }) {
baseURL: 'https://api.github.com', this.http = HTTP({
headers: { baseURL: 'https://api.github.com',
Authorization: `token ${token}`, headers: {
'User-Agent': Authorization: `token ${token}`,
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36', 'User-Agent':
}, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36',
events: {
onResponse: (resp) => {
if (/^[45]/.test(String(resp.statusCode))) {
return Promise.reject(
`ERROR: ${JSON.parse(resp.body).message}`,
);
} else {
return resp;
}
}, },
}, events: {
}); onResponse: (resp) => {
if (/^[45]/.test(String(resp.statusCode))) {
return Promise.reject(
`ERROR: ${JSON.parse(resp.body).message}`,
);
} else {
return resp;
}
},
},
});
this.key = key;
}
async function locate() { async locate() {
return http.get('/gists').then((response) => { return this.http.get('/gists').then((response) => {
const gists = JSON.parse(response.body); const gists = JSON.parse(response.body);
for (let g of gists) { for (let g of gists) {
if (g.description === key) { if (g.description === this.key) {
return g.id; return g.id;
} }
} }
@ -36,42 +39,42 @@ export default function Gist({ token, key }) {
}); });
} }
this.upload = async function (files) { async upload(files) {
const id = await locate(); const id = await this.locate();
if (id === -1) { if (id === -1) {
// create a new gist for backup // create a new gist for backup
return http.post({ return this.http.post({
url: '/gists', url: '/gists',
body: JSON.stringify({ body: JSON.stringify({
description: key, description: this.key,
public: false, public: false,
files, files,
}), }),
}); });
} else { } else {
// update an existing gist // update an existing gist
return http.patch({ return this.http.patch({
url: `/gists/${id}`, url: `/gists/${id}`,
body: JSON.stringify({ files }), body: JSON.stringify({ files }),
}); });
} }
}; }
this.download = async function (filename) { async download(filename) {
const id = await locate(); const id = await this.locate();
if (id === -1) { if (id === -1) {
return Promise.reject('未找到Gist备份'); return Promise.reject('未找到Gist备份');
} else { } else {
try { try {
const { files } = await http const { files } = await this.http
.get(`/gists/${id}`) .get(`/gists/${id}`)
.then((resp) => JSON.parse(resp.body)); .then((resp) => JSON.parse(resp.body));
const url = files[filename].raw_url; const url = files[filename].raw_url;
return await http.get(url).then((resp) => resp.body); return await this.http.get(url).then((resp) => resp.body);
} catch (err) { } catch (err) {
return Promise.reject(err); return Promise.reject(err);
} }
} }
}; }
} }

File diff suppressed because one or more lines are too long