mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-08-11 16:28:57 +08:00
Refactored OpenAPI
This commit is contained in:
parent
0e46d8e14d
commit
bbe29a7fac
@ -1,4 +1,4 @@
|
||||
import { API } from '../utils/open-api';
|
||||
const $ = API('sub-store');
|
||||
import { OpenAPI } from '../utils/open-api';
|
||||
|
||||
const $ = new OpenAPI('sub-store');
|
||||
export default $;
|
||||
|
@ -1,9 +1,223 @@
|
||||
/* eslint-disable no-undef */
|
||||
const isQX = typeof $task !== 'undefined';
|
||||
const isLoon = typeof $loon !== 'undefined';
|
||||
const isSurge = typeof $httpClient !== 'undefined' && !isLoon;
|
||||
const isNode = eval(`typeof process !== "undefined"`);
|
||||
|
||||
export class OpenAPI {
|
||||
constructor(name = 'untitled', debug = false) {
|
||||
this.name = name;
|
||||
this.debug = debug;
|
||||
|
||||
this.http = HTTP();
|
||||
this.env = ENV();
|
||||
|
||||
this.node = (() => {
|
||||
if (isNode) {
|
||||
const fs = eval("require('fs')");
|
||||
|
||||
return {
|
||||
fs,
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
this.initCache();
|
||||
|
||||
const delay = (t, v) =>
|
||||
new Promise(function (resolve) {
|
||||
setTimeout(resolve.bind(null, v), t);
|
||||
});
|
||||
|
||||
Promise.prototype.delay = async function (t) {
|
||||
const v = await this;
|
||||
return await delay(t, v);
|
||||
};
|
||||
}
|
||||
|
||||
// persistence
|
||||
// initialize cache
|
||||
initCache() {
|
||||
if (isQX)
|
||||
this.cache = JSON.parse($prefs.valueForKey(this.name) || '{}');
|
||||
if (isLoon || isSurge)
|
||||
this.cache = JSON.parse($persistentStore.read(this.name) || '{}');
|
||||
|
||||
if (isNode) {
|
||||
// create a json for root cache
|
||||
let fpath = 'root.json';
|
||||
if (!this.node.fs.existsSync(fpath)) {
|
||||
this.node.fs.writeFileSync(
|
||||
fpath,
|
||||
JSON.stringify({}),
|
||||
{ flag: 'wx' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
}
|
||||
this.root = {};
|
||||
|
||||
// create a json file with the given name if not exists
|
||||
fpath = `${this.name}.json`;
|
||||
if (!this.node.fs.existsSync(fpath)) {
|
||||
this.node.fs.writeFileSync(
|
||||
fpath,
|
||||
JSON.stringify({}),
|
||||
{ flag: 'wx' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
this.cache = {};
|
||||
} else {
|
||||
this.cache = JSON.parse(
|
||||
this.node.fs.readFileSync(`${this.name}.json`),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// store cache
|
||||
persistCache() {
|
||||
const data = JSON.stringify(this.cache, null, 2);
|
||||
if (isQX) $prefs.setValueForKey(data, this.name);
|
||||
if (isLoon || isSurge) $persistentStore.write(data, this.name);
|
||||
if (isNode) {
|
||||
this.node.fs.writeFileSync(
|
||||
`${this.name}.json`,
|
||||
data,
|
||||
{ flag: 'w' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
this.node.fs.writeFileSync(
|
||||
'root.json',
|
||||
JSON.stringify(this.root, null, 2),
|
||||
{ flag: 'w' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
write(data, key) {
|
||||
this.log(`SET ${key}`);
|
||||
if (key.indexOf('#') !== -1) {
|
||||
key = key.substr(1);
|
||||
if (isSurge || isLoon) {
|
||||
return $persistentStore.write(data, key);
|
||||
}
|
||||
if (isQX) {
|
||||
return $prefs.setValueForKey(data, key);
|
||||
}
|
||||
if (isNode) {
|
||||
this.root[key] = data;
|
||||
}
|
||||
} else {
|
||||
this.cache[key] = data;
|
||||
}
|
||||
this.persistCache();
|
||||
}
|
||||
|
||||
read(key) {
|
||||
this.log(`READ ${key}`);
|
||||
if (key.indexOf('#') !== -1) {
|
||||
key = key.substr(1);
|
||||
if (isSurge || isLoon) {
|
||||
return $persistentStore.read(key);
|
||||
}
|
||||
if (isQX) {
|
||||
return $prefs.valueForKey(key);
|
||||
}
|
||||
if (isNode) {
|
||||
return this.root[key];
|
||||
}
|
||||
} else {
|
||||
return this.cache[key];
|
||||
}
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
this.log(`DELETE ${key}`);
|
||||
if (key.indexOf('#') !== -1) {
|
||||
key = key.substr(1);
|
||||
if (isSurge || isLoon) {
|
||||
return $persistentStore.write(null, key);
|
||||
}
|
||||
if (isQX) {
|
||||
return $prefs.removeValueForKey(key);
|
||||
}
|
||||
if (isNode) {
|
||||
delete this.root[key];
|
||||
}
|
||||
} else {
|
||||
delete this.cache[key];
|
||||
}
|
||||
this.persistCache();
|
||||
}
|
||||
|
||||
// notification
|
||||
notify(title, subtitle = '', content = '', options = {}) {
|
||||
const openURL = options['open-url'];
|
||||
const mediaURL = options['media-url'];
|
||||
|
||||
if (isQX) $notify(title, subtitle, content, options);
|
||||
if (isSurge) {
|
||||
$notification.post(
|
||||
title,
|
||||
subtitle,
|
||||
content + `${mediaURL ? '\n多媒体:' + mediaURL : ''}`,
|
||||
{
|
||||
url: openURL,
|
||||
},
|
||||
);
|
||||
}
|
||||
if (isLoon) {
|
||||
let opts = {};
|
||||
if (openURL) opts['openUrl'] = openURL;
|
||||
if (mediaURL) opts['mediaUrl'] = mediaURL;
|
||||
if (JSON.stringify(opts) === '{}') {
|
||||
$notification.post(title, subtitle, content);
|
||||
} else {
|
||||
$notification.post(title, subtitle, content, opts);
|
||||
}
|
||||
}
|
||||
if (isNode) {
|
||||
const content_ =
|
||||
content +
|
||||
(openURL ? `\n点击跳转: ${openURL}` : '') +
|
||||
(mediaURL ? `\n多媒体: ${mediaURL}` : '');
|
||||
console.log(`${title}\n${subtitle}\n${content_}\n\n`);
|
||||
}
|
||||
}
|
||||
|
||||
// other helper functions
|
||||
log(msg) {
|
||||
if (this.debug) console.log(`[${this.name}] LOG: ${msg}`);
|
||||
}
|
||||
|
||||
info(msg) {
|
||||
console.log(`[${this.name}] INFO: ${msg}`);
|
||||
}
|
||||
|
||||
error(msg) {
|
||||
console.log(`[${this.name}] ERROR: ${msg}`);
|
||||
}
|
||||
|
||||
wait(millisec) {
|
||||
return new Promise((resolve) => setTimeout(resolve, millisec));
|
||||
}
|
||||
|
||||
done(value = {}) {
|
||||
if (isQX || isLoon || isSurge) {
|
||||
$done(value);
|
||||
} else if (isNode) {
|
||||
if (typeof $context !== 'undefined') {
|
||||
$context.headers = value.headers;
|
||||
$context.statusCode = value.statusCode;
|
||||
$context.body = value.body;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function ENV() {
|
||||
const isQX = typeof $task !== 'undefined';
|
||||
const isLoon = typeof $loon !== 'undefined';
|
||||
const isSurge = typeof $httpClient !== 'undefined' && !isLoon;
|
||||
const isNode = eval(`typeof process !== "undefined"`);
|
||||
return { isQX, isLoon, isSurge, isNode };
|
||||
}
|
||||
|
||||
@ -98,222 +312,3 @@ export function HTTP(defaultOptions = { baseURL: '' }) {
|
||||
);
|
||||
return http;
|
||||
}
|
||||
|
||||
export function API(name = 'untitled', debug = false) {
|
||||
const { isQX, isLoon, isSurge, isNode } = ENV();
|
||||
return new (class {
|
||||
constructor(name, debug) {
|
||||
this.name = name;
|
||||
this.debug = debug;
|
||||
|
||||
this.http = HTTP();
|
||||
this.env = ENV();
|
||||
|
||||
this.node = (() => {
|
||||
if (isNode) {
|
||||
const fs = eval("require('fs')");
|
||||
|
||||
return {
|
||||
fs,
|
||||
};
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
this.initCache();
|
||||
|
||||
const delay = (t, v) =>
|
||||
new Promise(function (resolve) {
|
||||
setTimeout(resolve.bind(null, v), t);
|
||||
});
|
||||
|
||||
Promise.prototype.delay = function (t) {
|
||||
return this.then(function (v) {
|
||||
return delay(t, v);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// persistence
|
||||
// initialize cache
|
||||
initCache() {
|
||||
if (isQX)
|
||||
this.cache = JSON.parse($prefs.valueForKey(this.name) || '{}');
|
||||
if (isLoon || isSurge)
|
||||
this.cache = JSON.parse(
|
||||
$persistentStore.read(this.name) || '{}',
|
||||
);
|
||||
|
||||
if (isNode) {
|
||||
// create a json for root cache
|
||||
let fpath = 'root.json';
|
||||
if (!this.node.fs.existsSync(fpath)) {
|
||||
this.node.fs.writeFileSync(
|
||||
fpath,
|
||||
JSON.stringify({}),
|
||||
{ flag: 'wx' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
}
|
||||
this.root = {};
|
||||
|
||||
// create a json file with the given name if not exists
|
||||
fpath = `${this.name}.json`;
|
||||
if (!this.node.fs.existsSync(fpath)) {
|
||||
this.node.fs.writeFileSync(
|
||||
fpath,
|
||||
JSON.stringify({}),
|
||||
{ flag: 'wx' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
this.cache = {};
|
||||
} else {
|
||||
this.cache = JSON.parse(
|
||||
this.node.fs.readFileSync(`${this.name}.json`),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// store cache
|
||||
persistCache() {
|
||||
const data = JSON.stringify(this.cache, null, 2);
|
||||
if (isQX) $prefs.setValueForKey(data, this.name);
|
||||
if (isLoon || isSurge) $persistentStore.write(data, this.name);
|
||||
if (isNode) {
|
||||
this.node.fs.writeFileSync(
|
||||
`${this.name}.json`,
|
||||
data,
|
||||
{ flag: 'w' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
this.node.fs.writeFileSync(
|
||||
'root.json',
|
||||
JSON.stringify(this.root, null, 2),
|
||||
{ flag: 'w' },
|
||||
(err) => console.log(err),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
write(data, key) {
|
||||
this.log(`SET ${key}`);
|
||||
if (key.indexOf('#') !== -1) {
|
||||
key = key.substr(1);
|
||||
if (isSurge || isLoon) {
|
||||
return $persistentStore.write(data, key);
|
||||
}
|
||||
if (isQX) {
|
||||
return $prefs.setValueForKey(data, key);
|
||||
}
|
||||
if (isNode) {
|
||||
this.root[key] = data;
|
||||
}
|
||||
} else {
|
||||
this.cache[key] = data;
|
||||
}
|
||||
this.persistCache();
|
||||
}
|
||||
|
||||
read(key) {
|
||||
this.log(`READ ${key}`);
|
||||
if (key.indexOf('#') !== -1) {
|
||||
key = key.substr(1);
|
||||
if (isSurge || isLoon) {
|
||||
return $persistentStore.read(key);
|
||||
}
|
||||
if (isQX) {
|
||||
return $prefs.valueForKey(key);
|
||||
}
|
||||
if (isNode) {
|
||||
return this.root[key];
|
||||
}
|
||||
} else {
|
||||
return this.cache[key];
|
||||
}
|
||||
}
|
||||
|
||||
delete(key) {
|
||||
this.log(`DELETE ${key}`);
|
||||
if (key.indexOf('#') !== -1) {
|
||||
key = key.substr(1);
|
||||
if (isSurge || isLoon) {
|
||||
return $persistentStore.write(null, key);
|
||||
}
|
||||
if (isQX) {
|
||||
return $prefs.removeValueForKey(key);
|
||||
}
|
||||
if (isNode) {
|
||||
delete this.root[key];
|
||||
}
|
||||
} else {
|
||||
delete this.cache[key];
|
||||
}
|
||||
this.persistCache();
|
||||
}
|
||||
|
||||
// notification
|
||||
notify(title, subtitle = '', content = '', options = {}) {
|
||||
const openURL = options['open-url'];
|
||||
const mediaURL = options['media-url'];
|
||||
|
||||
if (isQX) $notify(title, subtitle, content, options);
|
||||
if (isSurge) {
|
||||
$notification.post(
|
||||
title,
|
||||
subtitle,
|
||||
content + `${mediaURL ? '\n多媒体:' + mediaURL : ''}`,
|
||||
{
|
||||
url: openURL,
|
||||
},
|
||||
);
|
||||
}
|
||||
if (isLoon) {
|
||||
let opts = {};
|
||||
if (openURL) opts['openUrl'] = openURL;
|
||||
if (mediaURL) opts['mediaUrl'] = mediaURL;
|
||||
if (JSON.stringify(opts) === '{}') {
|
||||
$notification.post(title, subtitle, content);
|
||||
} else {
|
||||
$notification.post(title, subtitle, content, opts);
|
||||
}
|
||||
}
|
||||
if (isNode) {
|
||||
const content_ =
|
||||
content +
|
||||
(openURL ? `\n点击跳转: ${openURL}` : '') +
|
||||
(mediaURL ? `\n多媒体: ${mediaURL}` : '');
|
||||
console.log(`${title}\n${subtitle}\n${content_}\n\n`);
|
||||
}
|
||||
}
|
||||
|
||||
// other helper functions
|
||||
log(msg) {
|
||||
if (this.debug) console.log(`[${this.name}] LOG: ${msg}`);
|
||||
}
|
||||
|
||||
info(msg) {
|
||||
console.log(`[${this.name}] INFO: ${msg}`);
|
||||
}
|
||||
|
||||
error(msg) {
|
||||
console.log(`[${this.name}] ERROR: ${msg}`);
|
||||
}
|
||||
|
||||
wait(millisec) {
|
||||
return new Promise((resolve) => setTimeout(resolve, millisec));
|
||||
}
|
||||
|
||||
done(value = {}) {
|
||||
if (isQX || isLoon || isSurge) {
|
||||
$done(value);
|
||||
} else if (isNode) {
|
||||
if (typeof $context !== 'undefined') {
|
||||
$context.headers = value.headers;
|
||||
$context.statusCode = value.statusCode;
|
||||
$context.body = value.body;
|
||||
}
|
||||
}
|
||||
}
|
||||
})(name, debug);
|
||||
}
|
||||
|
4
backend/sub-store.min.js
vendored
4
backend/sub-store.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user