feat: use OpenAPI
This commit is contained in:
+26
-6
@@ -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;
|
||||
|
||||
@@ -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 };
|
||||
@@ -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 };
|
||||
+14
-3
@@ -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 = {};
|
||||
|
||||
@@ -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 };
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
import type { Handler } from 'hono';
|
||||
import { namespaces } from '@/registry';
|
||||
|
||||
const handler: Handler = (ctx) => ctx.json(namespaces);
|
||||
|
||||
export default handler;
|
||||
+5
-1
@@ -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",
|
||||
@@ -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.106.1",
|
||||
@@ -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",
|
||||
|
||||
Generated
+59
-1
@@ -8,6 +8,12 @@ dependencies:
|
||||
'@hono/node-server':
|
||||
specifier: 1.8.2
|
||||
version: 1.8.2
|
||||
'@hono/swagger-ui':
|
||||
specifier: 0.2.1
|
||||
version: 0.2.1(hono@4.1.0)
|
||||
'@hono/zod-openapi':
|
||||
specifier: 0.9.8
|
||||
version: 0.9.8(hono@4.1.0)(zod@3.22.4)
|
||||
'@notionhq/client':
|
||||
specifier: 2.2.14
|
||||
version: 2.2.14
|
||||
@@ -215,6 +221,9 @@ dependencies:
|
||||
xxhash-wasm:
|
||||
specifier: 1.0.2
|
||||
version: 1.0.2
|
||||
zod:
|
||||
specifier: 3.22.4
|
||||
version: 3.22.4
|
||||
|
||||
devDependencies:
|
||||
'@babel/preset-env':
|
||||
@@ -422,6 +431,15 @@ packages:
|
||||
'@jridgewell/trace-mapping': 0.3.22
|
||||
dev: true
|
||||
|
||||
/@asteasolutions/zod-to-openapi@5.5.0(zod@3.22.4):
|
||||
resolution: {integrity: sha512-d5HwrvM6dOKr3XdeF+DmashGvfEc+1oiEfbscugsiwSTrFtuMa7ETpW9sTNnVgn+hJaz+PRxPQUYD7q9/5dUig==}
|
||||
peerDependencies:
|
||||
zod: ^3.20.2
|
||||
dependencies:
|
||||
openapi3-ts: 4.2.2
|
||||
zod: 3.22.4
|
||||
dev: false
|
||||
|
||||
/@babel/code-frame@7.0.0:
|
||||
resolution: {integrity: sha512-OfC2uemaknXr87bdLUkWog7nYuliM9Ij5HUcajsVcMCpQrcLmtxRbVFTIqmcSkSeYRBFBRxs2FiUqFJDLdiebA==}
|
||||
dependencies:
|
||||
@@ -1897,6 +1915,37 @@ packages:
|
||||
engines: {node: '>=18.14.1'}
|
||||
dev: false
|
||||
|
||||
/@hono/swagger-ui@0.2.1(hono@4.1.0):
|
||||
resolution: {integrity: sha512-wBxVMRe3/v8xH4o6icmwztiIq0DG0s7+jHVMHVUAoFFCWEQNL2iskMmQtrhSDtsFmBZUeUFQUaaJ6Ir6DOmHLA==}
|
||||
peerDependencies:
|
||||
hono: '*'
|
||||
dependencies:
|
||||
hono: 4.1.0
|
||||
dev: false
|
||||
|
||||
/@hono/zod-openapi@0.9.8(hono@4.1.0)(zod@3.22.4):
|
||||
resolution: {integrity: sha512-NS6lvQEGnsjyQkp+aQjMARREz3WGf19y0+RiiJMVrihWlmKbGaJkPuOWpfFQG6FU9q+FVUyUCryXK+3J07GPAw==}
|
||||
engines: {node: '>=16.0.0'}
|
||||
peerDependencies:
|
||||
hono: '>=3.11.3'
|
||||
zod: 3.*
|
||||
dependencies:
|
||||
'@asteasolutions/zod-to-openapi': 5.5.0(zod@3.22.4)
|
||||
'@hono/zod-validator': 0.1.11(hono@4.1.0)(zod@3.22.4)
|
||||
hono: 4.1.0
|
||||
zod: 3.22.4
|
||||
dev: false
|
||||
|
||||
/@hono/zod-validator@0.1.11(hono@4.1.0)(zod@3.22.4):
|
||||
resolution: {integrity: sha512-PQXeHUP0+36qpRt8yfeD7N2jbK3ETlGvSN6dMof/HwUC/APRokQRjpXZm4rrlG71Ft0aWE01+Bm4XejqPie5Uw==}
|
||||
peerDependencies:
|
||||
hono: '>=3.9.0'
|
||||
zod: ^3.19.1
|
||||
dependencies:
|
||||
hono: 4.1.0
|
||||
zod: 3.22.4
|
||||
dev: false
|
||||
|
||||
/@humanwhocodes/config-array@0.11.14:
|
||||
resolution: {integrity: sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg==}
|
||||
engines: {node: '>=10.10.0'}
|
||||
@@ -7712,6 +7761,12 @@ packages:
|
||||
mimic-fn: 4.0.0
|
||||
dev: true
|
||||
|
||||
/openapi3-ts@4.2.2:
|
||||
resolution: {integrity: sha512-+9g4actZKeb3czfi9gVQ4Br2Ju3KwhCAQJBNaKgye5KggqcBLIhFHH+nIkcm0BUX00TrAJl6dH4JWgM4G4JWrw==}
|
||||
dependencies:
|
||||
yaml: 2.3.4
|
||||
dev: false
|
||||
|
||||
/optionator@0.8.3:
|
||||
resolution: {integrity: sha512-+IW9pACdk3XWmmTXG8m3upGUJst5XRGzxMRjXzAuJ1XnIFNvfhjjIuYkDvysnPQ7qzqVzLt78BCruntqRhWQbA==}
|
||||
engines: {node: '>= 0.8.0'}
|
||||
@@ -10399,7 +10454,6 @@ packages:
|
||||
/yaml@2.3.4:
|
||||
resolution: {integrity: sha512-8aAvwVUSHpfEqTQ4w/KMlf3HcRdt50E5ODIQJBw1fQ5RL34xabzxtUlzTXVqc4rkZsPbvrXKWnABCD7kWSmocA==}
|
||||
engines: {node: '>= 14'}
|
||||
dev: true
|
||||
|
||||
/yargs-parser@15.0.3:
|
||||
resolution: {integrity: sha512-/MVEVjTXy/cGAjdtQf8dW3V9b97bPN7rNn8ETj6BmAQL7ibC7O1Q9SPJbGjgh3SlwoBNXMzj/ZGIj8mBgl12YA==}
|
||||
@@ -10443,6 +10497,10 @@ packages:
|
||||
engines: {node: '>=12.20'}
|
||||
dev: true
|
||||
|
||||
/zod@3.22.4:
|
||||
resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==}
|
||||
dev: false
|
||||
|
||||
/zwitch@2.0.4:
|
||||
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
|
||||
dev: true
|
||||
|
||||
Reference in New Issue
Block a user