Repository navigation
Expand file tree
/
Copy pathswaggerGenerator.js
More file actions
313 lines (284 loc) · 7.69 KB
/
Copy pathswaggerGenerator.js
File metadata and controls
313 lines (284 loc) · 7.69 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/**
* Swagger/OpenAPI Specification Generator
*
* Automatically generates API documentation from JSDoc comments
* and route definitions. Hosted at /api/docs with interactive "Try it out" feature.
*/
const swaggerAutogen = require('swagger-autogen')();
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const outputFile = './swagger_output.json';
const yamlOutputFile = './swagger_output.yaml';
// Files containing API routes to document
const routeFiles = [
'./index.js',
'./routes/auth.js',
'./routes/stellarAuth.js',
'./routes/subscription.js',
'./routes/payouts.js',
'./routes/video.js',
'./routes/globalStats.js',
'./routes/price.js',
'./routes/device.js',
'./routes/user.js',
'./routes/posts.js',
'./routes/comments.js',
'./routes/badges.js',
'./routes/analytics.js',
'./routes/feed.js',
'./routes/content.js',
'./routes/storage.js',
'./routes/vault.js',
'./routes/vesting.js',
'./routes/tax.js',
'./routes/legalAgreements.js',
'./routes/registry.js',
];
// Base API documentation
const doc = {
info: {
version: '1.0.0',
title: 'SubStream Protocol API',
description: `
# SubStream Protocol - Decentralized Streaming Platform API
Complete API documentation for the SubStream Protocol, a decentralized streaming platform built on Stellar/Soroban.
## Features
- **Decentralized Payments**: Stellar-based subscription management
- **Video Streaming**: HLS transcoding and CDN integration
- **Fraud Prevention**: Device fingerprinting and Sybil attack detection
- **Analytics**: Real-time creator and subscriber analytics
- **Smart Contracts**: Soroban contract integration
## Authentication
Most endpoints require authentication using one of these methods:
### Sign-In With Stellar (SIWS)
\`\`\`javascript
// 1. Get challenge
GET /auth/stellar/challenge?publicKey=G...
// 2. Sign with wallet
const signed = signChallenge(challenge);
// 3. Login
POST /auth/stellar/login
{
"publicKey": "G...",
"challengeXDR": "signed-xdr"
}
// 4. Use token
Authorization: Bearer <token>
\`\`\`
## Rate Limiting
API requests are rate-limited using a leaky bucket algorithm:
- **Default**: 60 requests per minute per wallet
- **Burst**: Up to 100 requests
- **Block Duration**: 5 minutes when exceeded
## Response Format
All responses follow this structure:
\`\`\`json
{
"success": true,
"data": { ... },
"timestamp": "2024-01-01T00:00:00.000Z"
}
\`\`\`
## Error Handling
Errors include detailed information for debugging:
\`\`\`json
{
"success": false,
"error": "Error message",
"code": "ERROR_CODE",
"details": { ... }
}
\`\`\`
`,
},
host: process.env.API_HOST || 'localhost:3000',
basePath: '/api',
schemes: ['http', 'https'],
consumes: ['application/json'],
produces: ['application/json'],
tags: [
{
name: 'Authentication',
description: 'Stellar wallet authentication and session management',
},
{
name: 'Subscriptions',
description: 'Manage creator subscriptions and payment flows',
},
{
name: 'Video',
description: 'Video upload, transcoding, and streaming',
},
{
name: 'Creator',
description: 'Creator profile and content management',
},
{
name: 'Analytics',
description: 'Statistics and analytics for creators',
},
{
name: 'Payouts',
description: 'Payment processing and payout management',
},
{
name: 'Device',
description: 'Device fingerprinting and fraud prevention',
},
{
name: 'Security',
description: 'Security-related endpoints including Sybil detection',
},
{
name: 'Fraud Prevention',
description: 'Multi-accounting detection and prevention',
},
{
name: 'Admin',
description: 'Administrative endpoints',
},
],
securityDefinitions: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
description: 'JWT token obtained from /auth/stellar/login',
},
stellarAuth: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
description: 'Bearer token with format: Bearer <JWT_TOKEN>',
},
},
definitions: {
// Common data structures
Creator: {
id: 'creator-uuid',
publicKey: 'G...',
createdAt: '2024-01-01T00:00:00.000Z',
subscriberCount: 1250,
flowRate: '1000000',
currency: 'USDC',
},
Subscription: {
creatorId: 'creator-uuid',
walletAddress: 'G...',
active: true,
subscribedAt: '2024-01-01T00:00:00.000Z',
tierLevel: 'gold',
balance: '5000000',
dailySpend: '100000',
},
Video: {
id: 'video-uuid',
creatorId: 'creator-uuid',
title: 'My Video',
description: 'Video description',
status: 'processing',
visibility: 'public',
createdAt: '2024-01-01T00:00:00.000Z',
},
DeviceFingerprint: {
deviceId: 'dev_abc123...',
fingerprint: 'sha256-hash',
confidence: 95,
riskLevel: 'low',
components: {},
},
SybilAnalysis: {
deviceId: 'dev_abc123...',
riskLevel: 'critical',
riskScore: 85,
walletCount: 12,
wallets: ['G...', 'G...'],
flags: ['CRITICAL_MULTI_WALLET'],
flagged: true,
},
Payout: {
id: 'payout-uuid',
creatorId: 'creator-uuid',
amount: '1000000',
asset: 'USDC',
recipientAddress: 'G...',
status: 'pending',
transactionHash: 'tx-hash',
},
ErrorResponse: {
success: false,
error: 'Error message',
code: 'ERROR_CODE',
},
},
parameters: {
// Common parameters
walletAddress: {
name: 'walletAddress',
in: 'path',
required: true,
schema: { type: 'string' },
description: 'Stellar wallet address (G...)',
example: 'GBZKMBXW5VHZQ7YKJ5VXQZ5VXQZ5VXQZ5VXQZ5VXQZ5VXQZ5VXQZ',
},
creatorId: {
name: 'id',
in: 'path',
required: true,
schema: { type: 'string' },
description: 'Creator unique identifier',
},
videoId: {
name: 'videoId',
in: 'path',
required: true,
schema: { type: 'string' },
description: 'Video unique identifier',
},
limit: {
name: 'limit',
in: 'query',
required: false,
schema: { type: 'integer', default: 20 },
description: 'Number of results per page',
},
offset: {
name: 'offset',
in: 'query',
required: false,
schema: { type: 'integer', default: 0 },
description: 'Pagination offset',
},
},
};
/**
* Generate Swagger specification
*/
async function generateSwagger() {
console.log('[Swagger] Generating API documentation...');
try {
// Filter existing files
const existingFiles = routeFiles.filter((file) => fs.existsSync(file));
if (existingFiles.length === 0) {
console.warn('[Swagger] No route files found. Skipping generation.');
return;
}
// Generate spec
await swaggerAutogen(outputFile)(doc, existingFiles);
console.log(`[Swagger] Generated ${outputFile}`);
// Also generate YAML version
const jsonSpec = JSON.parse(fs.readFileSync(outputFile, 'utf8'));
const yamlSpec = yaml.dump(jsonSpec, { indent: 2 });
fs.writeFileSync(yamlOutputFile, yamlSpec, 'utf8');
console.log(`[Swagger] Generated ${yamlOutputFile}`);
console.log(`[Swagger] Documented ${existingFiles.length} route files`);
} catch (error) {
console.error('[Swagger] Generation failed:', error);
}
}
// Auto-generate on module load if in development
if (process.env.NODE_ENV === 'development' || process.env.GENERATE_SWAGGER === 'true') {
generateSwagger();
}
module.exports = { generateSwagger, outputFile, yamlOutputFile };