-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (79 loc) · 3.52 KB
/
server.js
File metadata and controls
95 lines (79 loc) · 3.52 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
'use strict';
require('dotenv').config();
const express = require('express');
const dataService = require('./dataService');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') return res.sendStatus(204);
next();
});
// ─── Routes ─────────────────────────────────────────────────────────────────
// GET /api/status — cache + fetch status
app.get('/api/status', (req, res) => {
res.json(dataService.getStatus());
});
// POST /api/refresh — incrementally fetch new PTR filings (skips already-cached)
app.post('/api/refresh', (req, res) => {
dataService.startFetch();
res.json({ message: 'Incremental refresh started. Only new PTR filings will be fetched.' });
});
// POST /api/refresh/full — wipe cache and re-fetch everything from scratch
app.post('/api/refresh/full', (req, res) => {
dataService.startFullRefetch();
res.json({ message: 'Full cache wipe and re-fetch started.' });
});
/*
GET /api/trades — main query endpoint
All params are optional and combinable.
FILTER params:
state — 2-letter state code e.g. state=CA
party — party name or partial e.g. party=Democrat
person — partial name match e.g. person=Pelosi
ticker — exact ticker symbol e.g. ticker=AAPL
type — transaction type code or category e.g. type=P type=buy
category — buy|sell|exchange|gift|etc e.g. category=sell
from — ISO date lower bound (trade date) e.g. from=2025-01-01
to — ISO date upper bound (trade date) e.g. to=2025-12-31
SORT params:
sort — date|oldest|newest|amount|largest|name|ticker|filingdate (default: date/newest)
order — asc|desc (default: desc for date/amount, asc for name/ticker)
PAGINATION params:
limit — max results to return
offset — skip N results (for pagination)
SHORTCUT params:
recent=N — returns N most recently *filed* trades (overrides sort/pagination)
*/
app.get('/api/trades', (req, res) => {
try {
const result = dataService.getTrades(req.query);
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// GET /api/trades/download — download full dataset as JSON file
app.get('/api/trades/download', (req, res) => {
try {
const result = dataService.getTrades(req.query);
res.setHeader('Content-Disposition', 'attachment; filename="trades.json"');
res.setHeader('Content-Type', 'application/json');
res.json(result);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: 'Not found. See /api/trades and /api/status.' });
});
// ─── Start ───────────────────────────────────────────────────────────────────
app.listen(PORT, () => {
console.log(`Capitol API listening on http://localhost:${PORT}`);
console.log(`Years: ${process.env.YEARS_START || 2025}–${process.env.YEARS_END || 2026}`);
dataService.initialize();
});