-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolka.ts
92 lines (83 loc) · 2.16 KB
/
polka.ts
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
import polka from "polka";
import { encode } from "jwt-simple";
import Cors from "cors";
import * as mocks from "./src/__helpers__/mocks";
const app = polka();
const cors = Cors({ origin: true });
app.use(cors);
interface RouteDefResponseFunction {
(params: any): any;
}
type RouteDef = {
method: "get" | "post";
url: string;
response?: any | RouteDefResponseFunction;
}
const routes: RouteDef[] = [{
method: "get",
url: "/api/user/:username/file/:filetype",
response: params => JSON.stringify(mocks[params.filetype])
}, {
method: "get",
url: "/api/user/:username/profile",
response: JSON.stringify(mocks.profile)
}, {
method: "get",
url: "/api/user/:username/files",
response: JSON.stringify(mocks.files)
}, {
method: "get",
url: "/api/user/:username/all",
response: JSON.stringify(mocks.all)
}, {
method: "post",
url: "/api/user/:username/delete"
}, {
method: "post",
url: "/api/user/:username/changepass"
}, {
method: "get",
url: "/api/users/count",
response: JSON.stringify(mocks.users.length)
}, {
method: "get",
url: "/api/users/list/:limit?",
response: JSON.stringify(mocks.users)
}, {
method: "get",
url: "/api/search/users/:query/:limit",
response: params => JSON.stringify(mocks.users.filter(user => user.username.toLowerCase().includes(params.query.toLowerCase())))
}];
routes.forEach(({ method, url, response }) => {
app[method](url, (req, res) => {
res.end(typeof response === "function" ? response(req.params) : response)
})
});
app.get("/oauth/authorize", (req, res) => {
res.statusCode = 301;
res.setHeader(
"Location",
`${decodeURIComponent(
req.query.redirect_uri
)}oauth/access_token/${encodeURIComponent(
encode(mocks.token, "FAKE_JWT_SECRET")
)}/token_type/Bearer/expires_in/3600`
);
res.end();
});
app.get("/oauth/verify", async (req, res) => {
res.statusCode = 200;
res.end();
});
app.get("*", async (req, res) => {
res.statusCode = 404;
res.end();
});
app.delete("/oauth/user/:username/delete", async (req, res) => {
res.statusCode = 200;
res.end();
});
app.listen(3001, err => {
if(err) throw err;
console.log("Local API running at http://localhost:3001...");
});