-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswagger.js
More file actions
122 lines (115 loc) · 3.1 KB
/
Copy pathswagger.js
File metadata and controls
122 lines (115 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
/**
* swagger.js — Swagger/OpenAPI 文档配置
*/
const swaggerJsdoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Alpha-Radar API',
version: '2.0.0',
description: 'Web3/Crypto 行业情报聚合系统 API 文档',
contact: {
name: 'Alpha-Radar Team',
url: 'https://github.com/Beltran12138/industry-feeds',
},
license: {
name: 'MIT',
url: 'https://opensource.org/licenses/MIT',
},
},
servers: [
{
url: 'http://localhost:3001',
description: '本地开发服务器',
},
{
url: 'https://alpha-radar.vercel.app',
description: '生产服务器',
},
],
components: {
securitySchemes: {
ApiKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'X-API-Key',
description: 'API 密钥认证',
},
BearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
schemas: {
News: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
title: { type: 'string' },
content: { type: 'string' },
source: { type: 'string' },
url: { type: 'string', format: 'uri' },
published_at: { type: 'string', format: 'date-time' },
category: { type: 'string' },
importance: { type: 'integer' },
is_important: { type: 'boolean' },
created_at: { type: 'string', format: 'date-time' },
},
},
Stats: {
type: 'object',
properties: {
total: { type: 'integer' },
important: { type: 'integer' },
sources: {
type: 'array',
items: { type: 'object' },
},
categories: {
type: 'array',
items: { type: 'object' },
},
},
},
Error: {
type: 'object',
properties: {
status: { type: 'string' },
message: { type: 'string' },
errors: {
type: 'array',
items: {
type: 'object',
properties: {
field: { type: 'string' },
message: { type: 'string' },
},
},
},
},
},
},
},
security: [
{ ApiKeyAuth: [] },
],
},
apis: ['./server.js', './routes/*.js'],
};
const swaggerSpec = swaggerJsdoc(options);
const setupSwagger = (app) => {
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec, {
customCss: '.swagger-ui .topbar { display: none }',
customSiteTitle: 'Alpha-Radar API Docs',
customfavIcon: '/favicon.ico',
}));
app.get('/api-docs.json', (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
};
module.exports = { setupSwagger, swaggerSpec };