rsshub
Version:
Make RSS Great Again!
49 lines (47 loc) • 1.64 kB
JavaScript
import "./dist-Bog6z_OM.mjs";
import { n as setConfig } from "./config-MQ-7ebJ_.mjs";
import { t as ViewType } from "./types-gOySfSXJ.mjs";
import "./logger-C4avouvV.mjs";
import { t as ofetch_default } from "./ofetch-DKl_Ma9g.mjs";
import { n as parseRelativeDate, t as parseDate } from "./parse-date-r5WDWHno.mjs";
//#region lib/pkg.ts
let app = null;
function ensureAppInitialized(app) {
if (!app) throw new Error("RSSHub not initialized. Please call init() first.");
}
async function init(conf) {
setConfig(Object.assign({ IS_PACKAGE: true }, conf));
app = (await import("./app-BFba8brm.mjs")).default;
}
async function request(path) {
ensureAppInitialized(app);
return (await app.request(path)).json();
}
async function registerRoute(namespace, route, namespaceConfig) {
ensureAppInitialized(app);
const { namespaces } = await import("./registry-D8n7M1ow.mjs");
if (!namespaces[namespace]) namespaces[namespace] = {
...namespaceConfig,
name: namespaceConfig?.name || namespace,
routes: {},
apiRoutes: {}
};
const paths = Array.isArray(route.path) ? route.path : [route.path];
const subApp = app.basePath(`/${namespace}`);
const wrappedHandler = async (ctx) => {
if (!ctx.get("data")) {
const response = await route.handler(ctx);
if (response instanceof Response) return response;
ctx.set("data", response);
}
};
for (const path of paths) {
namespaces[namespace].routes[path] = {
...route,
location: `custom/${namespace}`
};
subApp.get(path, wrappedHandler);
}
}
//#endregion
export { ViewType, init, ofetch_default as ofetch, parseDate, parseRelativeDate, registerRoute, request };