Update package dependencies, refactor TypeScript configuration, and enhance API server code with improved type definitions. Modify GitHub Actions workflow to verify API build success and streamline build scripts for better maintainability.
This commit is contained in:
@ -1,11 +1,11 @@
|
||||
import express from 'express';
|
||||
import express, { Request, Response, NextFunction } from 'express';
|
||||
import cors from 'cors';
|
||||
import { createUser, getUserByEmail, updateUser, getAllUsers, getUserById } from './lib/api/users.js';
|
||||
import { getScripts, getScriptById, createScript, updateScript, deleteScript, moderateScript } from './lib/api/scripts.js';
|
||||
import { login, register, refreshToken } from './lib/api/auth.js';
|
||||
import { rateScript, getUserRating, getScriptRatingStats } from './lib/api/ratings.js';
|
||||
import { getPlatformAnalytics, getScriptAnalytics, trackEvent } from './lib/api/analytics.js';
|
||||
import { createCollection, getUserCollections, getPublicCollections, addScriptToCollection } from './lib/api/collections.js';
|
||||
import { getAllUsers, getUserById } from './lib/api/users.js';
|
||||
import { getScripts, getScriptById, createScript } from './lib/api/scripts.js';
|
||||
import { login, register } from './lib/api/auth.js';
|
||||
import { rateScript, getScriptRatingStats } from './lib/api/ratings.js';
|
||||
import { getPlatformAnalytics, trackEvent } from './lib/api/analytics.js';
|
||||
import { getUserCollections, getPublicCollections } from './lib/api/collections.js';
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
@ -19,12 +19,12 @@ app.use(express.json({ limit: '10mb' }));
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// Health check endpoint
|
||||
app.get('/api/health', (req, res) => {
|
||||
app.get('/api/health', (_req: Request, res: Response) => {
|
||||
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
||||
});
|
||||
|
||||
// Auth routes
|
||||
app.post('/api/auth/login', async (req, res) => {
|
||||
app.post('/api/auth/login', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await login(req.body);
|
||||
res.json(result);
|
||||
@ -34,7 +34,7 @@ app.post('/api/auth/login', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/auth/register', async (req, res) => {
|
||||
app.post('/api/auth/register', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await register(req.body);
|
||||
res.json(result);
|
||||
@ -45,7 +45,7 @@ app.post('/api/auth/register', async (req, res) => {
|
||||
});
|
||||
|
||||
// Scripts routes
|
||||
app.get('/api/scripts', async (req, res) => {
|
||||
app.get('/api/scripts', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await getScripts(req.query);
|
||||
res.json(result);
|
||||
@ -55,7 +55,7 @@ app.get('/api/scripts', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/scripts/:id', async (req, res) => {
|
||||
app.get('/api/scripts/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const script = await getScriptById(req.params.id);
|
||||
if (!script) {
|
||||
@ -68,13 +68,13 @@ app.get('/api/scripts/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/scripts', async (req, res) => {
|
||||
app.post('/api/scripts', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] as string;
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: 'Unauthorized' });
|
||||
}
|
||||
const result = await createScript(req.body, userId);
|
||||
const result = await createScript(req.body);
|
||||
res.json(result);
|
||||
} catch (error) {
|
||||
console.error('Create script error:', error);
|
||||
@ -83,7 +83,7 @@ app.post('/api/scripts', async (req, res) => {
|
||||
});
|
||||
|
||||
// Users routes
|
||||
app.get('/api/users', async (req, res) => {
|
||||
app.get('/api/users', async (_req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await getAllUsers();
|
||||
res.json(result);
|
||||
@ -93,7 +93,7 @@ app.get('/api/users', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/users/:id', async (req, res) => {
|
||||
app.get('/api/users/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const user = await getUserById(req.params.id);
|
||||
if (!user) {
|
||||
@ -107,7 +107,7 @@ app.get('/api/users/:id', async (req, res) => {
|
||||
});
|
||||
|
||||
// Analytics routes
|
||||
app.get('/api/analytics/platform', async (req, res) => {
|
||||
app.get('/api/analytics/platform', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const days = parseInt(req.query.days as string) || 30;
|
||||
const result = await getPlatformAnalytics(days);
|
||||
@ -118,7 +118,7 @@ app.get('/api/analytics/platform', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/analytics/track', async (req, res) => {
|
||||
app.post('/api/analytics/track', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await trackEvent(req.body);
|
||||
res.json(result);
|
||||
@ -129,7 +129,7 @@ app.post('/api/analytics/track', async (req, res) => {
|
||||
});
|
||||
|
||||
// Collections routes
|
||||
app.get('/api/collections', async (req, res) => {
|
||||
app.get('/api/collections', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const userId = req.headers['x-user-id'] as string;
|
||||
const result = userId ? await getUserCollections(userId) : await getPublicCollections();
|
||||
@ -141,7 +141,7 @@ app.get('/api/collections', async (req, res) => {
|
||||
});
|
||||
|
||||
// Ratings routes
|
||||
app.post('/api/ratings', async (req, res) => {
|
||||
app.post('/api/ratings', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await rateScript(req.body);
|
||||
res.json(result);
|
||||
@ -151,7 +151,7 @@ app.post('/api/ratings', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/scripts/:id/ratings', async (req, res) => {
|
||||
app.get('/api/scripts/:id/ratings', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const result = await getScriptRatingStats(req.params.id);
|
||||
res.json(result);
|
||||
@ -162,13 +162,13 @@ app.get('/api/scripts/:id/ratings', async (req, res) => {
|
||||
});
|
||||
|
||||
// Error handling middleware
|
||||
app.use((error: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
app.use((error: any, _req: Request, res: Response, _next: NextFunction) => {
|
||||
console.error('Unhandled error:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
});
|
||||
|
||||
// 404 handler
|
||||
app.use('*', (req, res) => {
|
||||
app.use('*', (_req: Request, res: Response) => {
|
||||
res.status(404).json({ error: 'Endpoint not found' });
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user