|
1 | 1 | // src/app.ts |
2 | | -import express from 'express' |
3 | | -import cors from 'cors' |
4 | | -import multer from 'multer' |
5 | | -import { json, urlencoded } from 'body-parser' |
6 | | -import routes from './routes' |
7 | | -import { errorHandler } from './utils/apiError' |
8 | | -import { createClient } from '@supabase/supabase-js' |
9 | | -import config from './config' |
10 | | -import path from 'path' |
11 | | - |
| 2 | +import express from "express"; |
| 3 | +import cors from "cors"; |
| 4 | +import multer from "multer"; |
| 5 | +import { json, urlencoded } from "body-parser"; |
| 6 | +import routes from "./routes"; |
| 7 | +import { errorHandler } from "./utils/apiError"; |
| 8 | +import { createClient } from "@supabase/supabase-js"; |
| 9 | +import config from "./config"; |
| 10 | +import path from "path"; |
12 | 11 |
|
13 | 12 | // Initialize Supabase client for storage operations |
14 | 13 | export const supabase = createClient( |
15 | 14 | config.SUPABASE_URL, |
16 | | - config.SUPABASE_SERVICE_ROLE_KEY |
17 | | -) |
| 15 | + config.SUPABASE_SERVICE_ROLE_KEY, |
| 16 | +); |
18 | 17 |
|
19 | | -const app = express() |
| 18 | +const app = express(); |
20 | 19 |
|
21 | 20 | // 1) Enable CORS for your domains |
22 | | -app.use(cors({ |
23 | | - origin: config.ALLOWED_ORIGINS.split(','), // e.g. 'https://club.example.com' |
24 | | - methods: ['GET','POST','PATCH','DELETE','OPTIONS'], |
25 | | - credentials: true, |
26 | | -})) |
| 21 | +app.use( |
| 22 | + cors({ |
| 23 | + origin: config.ALLOWED_ORIGINS.split(","), // e.g. 'https://club.example.com' |
| 24 | + methods: ["GET", "POST", "PATCH", "DELETE", "OPTIONS"], |
| 25 | + credentials: true, |
| 26 | + }), |
| 27 | +); |
27 | 28 |
|
28 | 29 | // 2) Parse JSON and form data |
29 | | -app.use(json()) |
30 | | -app.use(urlencoded({ extended: true })) |
| 30 | +app.use(json()); |
| 31 | +app.use(urlencoded({ extended: true })); |
31 | 32 |
|
32 | 33 | // 3) Handle file uploads (in-memory) |
33 | | -const upload = multer({ storage: multer.memoryStorage() }) |
| 34 | +const upload = multer({ storage: multer.memoryStorage() }); |
34 | 35 |
|
35 | 36 | // 4) Mount your routes, injecting `upload` middleware where needed |
36 | 37 | // For endpoints that accept file uploads, you can do e.g.: |
37 | 38 | // router.post('/members/:memberId/photo', upload.single('photo'), ...) |
38 | 39 |
|
39 | | -app.use('/api/v1', routes(upload, supabase)) |
| 40 | +app.use("/api/v1", routes(upload, supabase)); |
40 | 41 |
|
41 | 42 | // 5) 404 handler |
42 | 43 | app.use((req, res) => { |
43 | | - res.status(404).json({ message: 'Not Found' }) |
44 | | -}) |
| 44 | + res.status(404).json({ message: "Not Found" }); |
| 45 | +}); |
45 | 46 |
|
46 | 47 | // 6) Global error handler |
47 | | -app.use(errorHandler) |
| 48 | +app.use(errorHandler); |
48 | 49 |
|
49 | 50 | // 7) do 'npm run apidoc to generate the documentation, I have added it in the scripts |
50 | 51 | // then you can go to localhost:3000/docs to see the docs |
51 | | -app.use('/docs', express.static(path.join(__dirname, '..', 'docs/apidoc'))) |
52 | | -export default app |
| 52 | +app.use("/docs", express.static(path.join(__dirname, "..", "docs/apidoc"))); |
| 53 | +export default app; |
0 commit comments