feat: radar rules one api

This commit is contained in:
DIYgod
2024-03-16 01:36:09 +08:00
parent 68bcc8d88c
commit aad06528db
5 changed files with 75 additions and 93 deletions
+4 -2
View File
@@ -1,7 +1,8 @@
// import { route as rulesRoute, handler as rulesHandler } from '@/api/radar/rules';
import { route as namespaceAllRoute, handler as namespaceAllHandler } from '@/api/namespace/all';
import { route as namespaceOneRoute, handler as namespaceOneHandler } from '@/api/namespace/one';
import { route as radarRulesRoute, handler as radarRulesHandler } from '@/api/radar/rules';
import { route as radarRulesAllRoute, handler as radarRulesAllHandler } from '@/api/radar/rules/all';
import { route as radarRulesOneRoute, handler as radarRulesOneHandler } from '@/api/radar/rules/one';
import { OpenAPIHono } from '@hono/zod-openapi';
import { swaggerUI } from '@hono/swagger-ui';
@@ -9,7 +10,8 @@ const app = new OpenAPIHono();
app.openapi(namespaceAllRoute, namespaceAllHandler);
app.openapi(namespaceOneRoute, namespaceOneHandler);
app.openapi(radarRulesRoute, radarRulesHandler);
app.openapi(radarRulesAllRoute, radarRulesAllHandler);
app.openapi(radarRulesOneRoute, radarRulesOneHandler);
const docs = app.getOpenAPI31Document({
openapi: '3.1.0',
@@ -42,7 +42,7 @@ const route = createRoute({
tags: ['Radar'],
responses: {
200: {
description: 'All radar rules',
description: 'All Radar rules',
},
},
});
+68
View File
@@ -0,0 +1,68 @@
import { namespaces } from '@/registry';
import { parse } from 'tldts';
import { Radar } from '@/types';
import { z, createRoute, RouteHandler } from '@hono/zod-openapi';
const radar: Radar = {};
for (const namespace in namespaces) {
for (const path in namespaces[namespace].routes) {
const realPath = `/${namespace}${path}`;
const data = namespaces[namespace].routes[path];
if (data.radar && data.radar.source) {
const parsedDomain = parse(new URL('https://' + data.radar.source[0]).hostname);
const subdomain = parsedDomain.subdomain || '.';
const domain = parsedDomain.domain;
if (domain) {
if (!radar[domain]) {
radar[domain] = {
_name: namespaces[namespace].name,
};
}
if (!radar[domain][subdomain]) {
radar[domain][subdomain] = [];
}
radar[domain][subdomain].push({
title: data.name,
docs: `https://docs.rsshub.app/routes/${data.categories?.[0] || 'other'}`,
source: data.radar.source.map((source) => {
const sourceURL = new URL('https://' + source);
return sourceURL.pathname + sourceURL.search + sourceURL.hash;
}),
target: data.radar.target || realPath,
});
}
}
}
}
const ParamsSchema = z.object({
domain: z.string().openapi({
param: {
name: 'domain',
in: 'path',
},
example: 'github.com',
}),
});
const route = createRoute({
method: 'get',
path: '/radar/rules/{domain}',
tags: ['Radar'],
request: {
params: ParamsSchema,
},
responses: {
200: {
description: 'Radar rules for a domain name (does not support subdomains)',
},
},
});
const handler: RouteHandler<typeof route> = (ctx) => {
const { domain } = ctx.req.valid('param');
return ctx.json(radar[domain]);
};
export { route, handler };
+1 -36
View File
@@ -8,39 +8,4 @@ The API is under active development and is subject to change. All suggestions ar
RSSHub provides the following APIs:
## List of Public Routes
:::tip
This API **will not** return any routes under `lib/protected_router.ts`.
:::
Eg: [https://rsshub.app/api/routes/github](https://rsshub.app/api/routes/github)
Route: `/api/routes/:name?`
Parameters:
- `name`, route's top level name as in [https://github.com/DIYgod/RSSHub/tree/master/lib/routes](https://github.com/DIYgod/RSSHub/tree/master/lib/routes). Optional, **returns all public routes if not specified**.
A successful request returns an HTTP status code `200 OK` with the result in JSON:
```js
{
"status": "success",
"data": {
"github": {
"routes": [
"/github/trending/:since/:language?",
"/github/issue/:user/:repo",
"/github/user/followers/:user",
"/github/stars/:user/:repo"
]
}
},
"message": "request returned 4 routes"
}
```
If no matching results were found, the server returns only an HTTP status code `204 No Content`.
https://rsshub.app/api/ui
@@ -8,57 +8,4 @@ API 仍处于开发状态中,并可能会有改动。欢迎提供建议!
RSSHub 提供下列 API:
## 可用公共路由列表
:::tip
`protected_router.ts`下的路由**不会被**包含在此 API 返回的结果当中.
:::
举例: [https://rsshub.app/api/routes/bilibili](https://rsshub.app/api/routes/bilibili)
路由: `/api/routes/:name?`
参数:
- `name`, 路由一级名称,对应 [https://github.com/DIYgod/RSSHub/tree/master/lib/routes](https://github.com/DIYgod/RSSHub/tree/master/lib/routes) 中的文件夹名称。可选,**缺省则返回所有可用路由**.
成功请求将会返回 HTTP 状态码 `200 OK` 与 JSON 结果,格式如下:
```js
{
"status": "success",
"data": {
"bilibili": {
"routes": [
"/bilibili/user/video/:uid",
"/bilibili/user/article/:uid",
"/bilibili/user/fav/:uid",
"/bilibili/user/coin/:uid",
"/bilibili/user/dynamic/:uid",
"/bilibili/user/followers/:uid",
"/bilibili/user/followings/:uid",
"/bilibili/user/channel/collections/:uid/:sid",
"/bilibili/user/channel/series/:uid/:sid",
"/bilibili/partion/:tid",
"/bilibili/partion/ranking/:tid/:days?",
"/bilibili/bangumi/:seasonid",
"/bilibili/video/page/:aid",
"/bilibili/video/reply/:aid",
"/bilibili/link/news/:product",
"/bilibili/live/room/:roomID",
"/bilibili/live/search/:key/:order",
"/bilibili/live/area/:areaID/:order",
"/bilibili/fav/:uid/:fid",
"/bilibili/mall/new",
"/bilibili/mall/ip/:id",
"/bilibili/ranking/:rid?/:day?"
]
}
},
"message": "request returned 22 routes"
}
```
若无符合请求路由,请求将会返回 HTTP 状态码 `204 No Content`.
https://rsshub.app/api/ui