/
index.js
148 lines (127 loc) · 3.5 KB
/
index.js
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
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const app = express();
const rateLimit = require("express-rate-limit");
const { range, interval, zip } = require("rxjs");
const {
map,
mergeMap,
pluck,
take,
bufferCount,
windowCount,
concatMap,
concatAll,
retry,
tap
} = require("rxjs/operators");
const axios = require("axios-observable").Axios;
const colors = require("colors");
function getEnvironmentPort() {
defaultPort = 4000;
if (process.env.PORT) {
return process.env.PORT;
} else {
return defaultPort;
}
}
const USER_DATA_JSON = require("./mocked_data/user-data.json");
const USER_DATA_PRIVATE_JSON = require("./mocked_data/user-data-private.json");
const USER_OWNED_GAMES_JSON = require("./mocked_data/user-owned-games.json");
const USER_OWNED_GAMES_PRIVATE_JSON = require("./mocked_data/user-owned-games-private.json");
const CUSTOM_STEAMID_JSON = require("./mocked_data/custom-steamid.json");
const ALL_VALID_GAMES_JSON = require("./mocked_data/custom-steamid.json");
const USERS_JSON = require("./mocked_data/users.json");
const port = getEnvironmentPort();
app.use(cors()); // Not suited for production usage
const jsonParser = bodyParser.json({ type: "application/*+json" });
app.get("/wakeup", function(req, res) {
res.send("API IS AVAILABLE ✅");
});
app.get("/user/profile/:steamID/games", function(req, res) {
res.send();
});
app.get("/steam/profile/custom/:steamUsername", function(req, res) {
res.send(CUSTOM_STEAMID_JSON);
});
app.get("/steam/profile/:steamID", function(req, res) {
const steamID = parseInt(req.params.steamID);
if (steamID === 1) {
res.send(USER_DATA_PRIVATE_JSON);
} else if (steamID === 2) {
res.send(USER_DATA_JSON);
} else {
res.send(USER_DATA_JSON);
}
});
app.get("/steam/profile/:steamID/games", function(req, res) {
const steamID = parseInt(req.params.steamID);
if (steamID === 1) {
res.send(USER_OWNED_GAMES_PRIVATE_JSON);
} else if (steamID === 2) {
res.send(USER_OWNED_GAMES_PRIVATE_JSON);
} else {
res.send(USER_OWNED_GAMES_JSON);
}
});
app.post("/user/profile/:steamID/games", function(req, res) {
res.send();
});
app.post("/user/profile/:steamID", function(req, res) {
res.send();
});
const limiter = rateLimit({
windowMs: 1000,
max: 4
});
app.get("/limited/:id", limiter, function(req, res) {
const id = req.params.id;
if (parseInt(id) === 3) {
console.log("NICE");
throw Error("IDK");
res.status(500).send("AWAS");
} else {
res.send(USERS_JSON[id]);
}
});
app.get("/try", limiter, function(req, res) {
console.log("starting");
const baseURL = "http://localhost:4000";
const $urls = range(0, 100).pipe(map(value => `${baseURL}/limited/${value}`));
const requests$ = zip(
$urls.pipe(bufferCount(4)),
interval(1000),
(a, b) => a
).pipe(
concatAll(),
concatMap(url => axios.get(url)),
retry(3),
pluck("data")
);
requests$.subscribe(
value => {
console.log("response", value);
},
error => {
if (error.response && error.response.status) {
console.log(
colors.red(
error.response.status,
error.response.statusText,
error.response.message
)
);
} else {
console.log(colors.red(error));
}
},
() => {
console.log("COMPLETE");
res.send("COMPLETE");
}
);
});
app.listen(port, function() {
console.log("Mocked API is listening on port: " + port + "!");
});