-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
133 lines (124 loc) · 3.88 KB
/
api.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
"use strict";
const http = require("node:http");
const process = require("node:process");
const uDB = require("./uDB");
const host = process.env.HOST || "localhost";
const port = process.env.PORT || 8080;
const expectedApiKey = process.env.API_KEY;
if (expectedApiKey == null) {
console.error('Environment variable "API_KEY" not set');
process.exit(1);
}
function makeResponse(response, data) {
response.setHeader("Content-Type", "application/json");
response.end(JSON.stringify(data));
}
function withAuthentication(request, response, func) {
const url = new URL(request.url, `http://${request.headers.host}`);
const apiKey = url.searchParams.get("api_key");
if (apiKey == null || apiKey !== expectedApiKey) {
makeResponse(response, {
status: "fail",
data: {
authentication: 'Invalid or missing URL parameter "api_key"',
},
});
return;
}
func();
}
function doGet(request, response) {
const url = new URL(request.url, `http://${request.headers.host}`);
const store = url.searchParams.get("store");
const id = url.searchParams.get("id");
if (store == null && id != null) {
makeResponse(response, {
status: "fail",
data: {
parameters:
'URL parameter "store" cannot be missing if "id" is present',
},
});
return;
}
let data = null;
if (store == null) data = uDB.getStores();
else if (id == null) data = new uDB(store).getAll();
else data = new uDB(store).get(id);
makeResponse(response, {
status: "success",
data: data,
});
}
function doPost(request, response) {
const url = new URL(request.url, `http://${request.headers.host}`);
const store = url.searchParams.get("store");
const id = url.searchParams.get("id");
if (store == null) {
makeResponse(response, {
status: "fail",
data: { store: 'Missing URL parameter "store"' },
});
return;
}
let requestData = "";
request.on("data", (chunk) => (requestData += chunk.toString()));
request.on("end", () => {
if (
id != null &&
requestData != null &&
requestData.trim().length > 0
) {
makeResponse(response, {
status: "fail",
data: {
id: 'URL parameter "id" may not be given with document in body',
},
});
return;
}
let data = null;
if (id == null && requestData.trim().length == 0)
data = new uDB(store).clear();
else if (id != null) data = new uDB(store).delete(id);
else {
let doc = null;
try {
doc = JSON.parse(requestData);
} catch {
makeResponse(response, {
status: "fail",
data: { postData: "Request body is not valid JSON" },
});
return;
}
data = new uDB(store).put(doc);
}
makeResponse(response, {
status: "success",
data: data,
});
});
}
const requestListener = (request, response) => {
withAuthentication(request, response, () => {
if (request.method === "GET") {
doGet(request, response);
} else if (request.method === "POST") {
doPost(request, response);
} else {
makeResponse(response, {
status: "fail",
data: {
method: `HTTP method "${request.method}" not allowed`,
},
});
}
});
};
const server = http.createServer(requestListener);
server.listen(port, host, () => {
console.log(
`uDB API running on http://${host}:${port}?api_key=${expectedApiKey}`
);
});