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();

View File

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

View File

@ -3,32 +3,35 @@ import { HTTP } from './open-api';
/**
* Gist backup
*/
export default function Gist({ token, key }) {
const http = HTTP({
baseURL: 'https://api.github.com',
headers: {
Authorization: `token ${token}`,
'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;
}
export default class Gist {
constructor({ token, key }) {
this.http = HTTP({
baseURL: 'https://api.github.com',
headers: {
Authorization: `token ${token}`,
'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;
}
},
},
});
this.key = key;
}
async function locate() {
return http.get('/gists').then((response) => {
async locate() {
return this.http.get('/gists').then((response) => {
const gists = JSON.parse(response.body);
for (let g of gists) {
if (g.description === key) {
if (g.description === this.key) {
return g.id;
}
}
@ -36,42 +39,42 @@ export default function Gist({ token, key }) {
});
}
this.upload = async function (files) {
const id = await locate();
async upload(files) {
const id = await this.locate();
if (id === -1) {
// create a new gist for backup
return http.post({
return this.http.post({
url: '/gists',
body: JSON.stringify({
description: key,
description: this.key,
public: false,
files,
}),
});
} else {
// update an existing gist
return http.patch({
return this.http.patch({
url: `/gists/${id}`,
body: JSON.stringify({ files }),
});
}
};
}
this.download = async function (filename) {
const id = await locate();
async download(filename) {
const id = await this.locate();
if (id === -1) {
return Promise.reject('未找到Gist备份');
} else {
try {
const { files } = await http
const { files } = await this.http
.get(`/gists/${id}`)
.then((resp) => JSON.parse(resp.body));
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) {
return Promise.reject(err);
}
}
};
}
}

File diff suppressed because one or more lines are too long