Skip to content

Commit

Permalink
Merge pull request #134 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored Mar 15, 2024
2 parents 7596213 + 68bcc8d commit feab7aa
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 18 deletions.
32 changes: 26 additions & 6 deletions lib/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { Hono } from 'hono';
import rules from '@/api/radar/rules';
import routes from '@/api/routes';
// 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 { OpenAPIHono } from '@hono/zod-openapi';
import { swaggerUI } from '@hono/swagger-ui';

const app = new Hono();
app.get('/radar/rules', rules);
app.get('/routes', routes);
const app = new OpenAPIHono();

app.openapi(namespaceAllRoute, namespaceAllHandler);
app.openapi(namespaceOneRoute, namespaceOneHandler);
app.openapi(radarRulesRoute, radarRulesHandler);

const docs = app.getOpenAPI31Document({
openapi: '3.1.0',
info: {
version: '0.0.1',
title: 'RSSHub API',
},
});
for (const path in docs.paths) {
docs.paths[`/api${path}`] = docs.paths[path];
delete docs.paths[path];
}
app.get('/docs', (ctx) => ctx.json(docs));

app.get('/ui', swaggerUI({ url: '/api/docs' }));

export default app;
17 changes: 17 additions & 0 deletions lib/api/namespace/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { namespaces } from '@/registry';
import { createRoute, RouteHandler } from '@hono/zod-openapi';

const route = createRoute({
method: 'get',
path: '/namespace',
tags: ['Namespace'],
responses: {
200: {
description: 'Information about all namespaces',
},
},
});

const handler: RouteHandler<typeof route> = (ctx) => ctx.json(namespaces);

export { route, handler };
33 changes: 33 additions & 0 deletions lib/api/namespace/one.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { namespaces } from '@/registry';
import { z, createRoute, RouteHandler } from '@hono/zod-openapi';

const ParamsSchema = z.object({
namespace: z.string().openapi({
param: {
name: 'namespace',
in: 'path',
},
example: 'github',
}),
});

const route = createRoute({
method: 'get',
path: '/namespace/{namespace}',
tags: ['Namespace'],
request: {
params: ParamsSchema,
},
responses: {
200: {
description: 'Information about a namespace',
},
},
});

const handler: RouteHandler<typeof route> = (ctx) => {
const { namespace } = ctx.req.valid('param');
return ctx.json(namespaces[namespace]);
};

export { route, handler };
17 changes: 14 additions & 3 deletions lib/api/radar/rules.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Handler } from 'hono';
import { namespaces } from '@/registry';
import { parse } from 'tldts';
import { Radar } from '@/types';
import { createRoute, RouteHandler } from '@hono/zod-openapi';

const radar: Radar = {};

Expand Down Expand Up @@ -36,6 +36,17 @@ for (const namespace in namespaces) {
}
}

const handler: Handler = (ctx) => ctx.json(radar);
const route = createRoute({
method: 'get',
path: '/radar/rules',
tags: ['Radar'],
responses: {
200: {
description: 'All radar rules',
},
},
});

export default handler;
const handler: RouteHandler<typeof route> = (ctx) => ctx.json(radar);

export { route, handler };
6 changes: 0 additions & 6 deletions lib/api/routes/index.ts

This file was deleted.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"scripts": {
"build": "tsx scripts/workflow/build-routes.ts",
"dev": "cross-env NODE_ENV=dev tsx watch --no-cache lib/index.ts",
"dev:cache": "cross-env NODE_ENV=production tsx watch lib/index.ts",
"format": "eslint --cache --fix \"**/*.{ts,js,yml}\" && prettier \"**/*.{ts,js,json}\" --write",
"format:check": "eslint --cache \"**/*.{ts,js,yml}\" && prettier \"**/*.{ts,js,json}\" --check",
"format:staged": "lint-staged",
Expand All @@ -47,6 +48,8 @@
},
"dependencies": {
"@hono/node-server": "1.8.2",
"@hono/swagger-ui": "0.2.1",
"@hono/zod-openapi": "0.9.8",
"@notionhq/client": "2.2.14",
"@postlight/parser": "2.2.3",
"@sentry/node": "7.107.0",
Expand Down Expand Up @@ -115,7 +118,8 @@
"twitter-api-v2": "1.16.1",
"uuid": "9.0.1",
"winston": "3.12.0",
"xxhash-wasm": "1.0.2"
"xxhash-wasm": "1.0.2",
"zod": "3.22.4"
},
"devDependencies": {
"@babel/preset-env": "7.24.0",
Expand Down
60 changes: 59 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion website/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ In addition to the two functions of generating RSS and obtaining data, RSSHub al

### Configuration file

Radar has two types of configuration files, one is a full-featured [js file](https://github.com/DIYgod/RSSHub/blob/gh-pages/build/radar-rules.js), and the other is a simplified [json file]((https://github.com/DIYgod/RSSHub/blob/gh-pages/build/radar-rules.json)).
Radar has two types of configuration files, one is a full-featured [js file](https://github.com/DIYgod/RSSHub/blob/gh-pages/build/radar-rules.js), and the other is a simplified [json file](https://github.com/DIYgod/RSSHub/blob/gh-pages/build/radar-rules.json).

### Usage

Expand Down

0 comments on commit feab7aa

Please sign in to comment.