-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
171 lines (144 loc) · 4.18 KB
/
server.ts
File metadata and controls
171 lines (144 loc) · 4.18 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
import { always, both as and, cond as _cond, propEq, pathEq, T } from 'ramda';
import express from 'express';
import bodyParser from 'body-parser';
import readline from 'readline';
import cors from 'cors';
const cond: any = _cond;
const mapReq = fn => (req, res) => {
const { status = 200, headers = {}, body = {} } = fn(req);
res.status(status);
res.removeHeader('X-Powered-By');
res.removeHeader('ETag');
res.removeHeader('Date');
res.set('Access-Control-Allow-Origin', '*');
Object.keys(headers).forEach(key => res.set(key, headers[key]));
res.json(body);
return res;
};
const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
const notAuthorized = [T, always({ status: 403, body: { error: 'Not authorized' } })];
const checkToken = pathEq(['headers', 'authtoken'], 'THE-S3KRIT-T0K3N');
const checkPerf = pathEq(['query', 'performance'], 'true');
app.use((req, { }, next) => {
console.log(req.method.toUpperCase(), req.url);
next();
});
app.get('/', ({ }, res) => {
res.send('Very Real Financial Services, LLC — API v1.0');
})
app.post('/token', mapReq(cond([
[
propEq('body', { username: 'test@account', password: 's3krit' }),
always({ body: { token: 'THE-S3KRIT-T0K3N' } })
],
notAuthorized
])));
app.get('/accounts', mapReq(cond([
[and(checkToken, checkPerf), always({
body: [
{
_links: { details: { href: 'http://localhost:1138/accounts/1', title: 'The Important One' } },
performance: { qtd: 1.5, mtd: -1.1, ytd: 5 }
},
{
_links: { details: { href: 'http://localhost:1138/accounts/2', title: 'The Other One' } },
performance: { ytd: 1.5, mtd: -2.1, qtd: 12 }
}
]
})],
[checkToken, always({
body: [
{ _links: { details: { href: 'http://localhost:1138/accounts/1', title: 'The Important One' } } },
{ _links: { details: { href: 'http://localhost:1138/accounts/2', title: 'The Other One' } } },
]
})],
notAuthorized
])));
app.get('/v2/accounts', mapReq(cond([
[and(checkToken, checkPerf), always({
body: [
{
_links: { details: { href: 'http://localhost:1138/accounts/1', title: 'The Important One', version: 2 } },
performance: { qtd: 1.5, mtd: -1.1, ytd: 5 }
},
{
_links: { details: { href: 'http://localhost:1138/accounts/2', title: 'The Other One', version: 2 } },
performance: { ytd: 1.5, mtd: -2.1, qtd: 12 }
}
]
})],
[checkToken, always({
body: [
{ _links: { details: { href: 'http://localhost:1138/accounts/1', title: 'The Important One', version: 2 } } },
{ _links: { details: { href: 'http://localhost:1138/accounts/2', title: 'The Other One', version: 2 } } },
]
})],
notAuthorized
])));
app.get('/accounts/1', mapReq(cond([
[checkToken, always({
body: {
overview: {
holdings: [{
name: 'Acme, Inc.',
ticker: 'ACM',
quantity: 5,
price: 1.11
}, {
name: 'Macrosaft',
ticker: 'MASF',
quantity: 11,
price: 25.01
}],
institution: '✨ Savings Bank',
lastUpdated: 1551753053,
number: 11001001001
}
}
})],
notAuthorized
])));
app.get('/accounts/2', mapReq(cond([
[checkToken, always({
body: {
_links: {
holdings: { href: 'http://localhost:1138/accounts/2/holdings' }
},
institution: 'Investments Iz Us',
lastUpdated: 1551753053,
value: 101.09,
number: 11001001002
}
})],
notAuthorized
])));
app.get('/accounts/2/holdings', mapReq(cond([
[checkToken, always({
body: [{
name: 'Goodnight Moonshot',
ticker: 'GMI',
quantity: 5000,
price: 0.22
}, {
name: 'BearBNB',
ticker: 'BBNB',
quantity: 40,
price: 15.28
}]
})],
notAuthorized
])));
app.get('/interactive', (req, res) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Test value: ', (result) => {
res.json({ result }).status(200);
rl.close();
});
});
app.listen(1138, console.log.bind(console, `Test service running at http://localhost:1138`));