-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwebpack.mock.api.keys.modelids.ts
132 lines (122 loc) · 3.44 KB
/
webpack.mock.api.keys.modelids.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
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
import {
delay,
DELAY_MS,
ERROR_DESCRIPTION,
ORG_ID,
} from "./webpack.mock.globals";
const webpackMockServer = require("webpack-mock-server");
export default webpackMockServer.add((app, helper) => {
const KEY_FIELD = "key";
const MODEL_ID_FIELD = "model_id";
const LAST_UPDATED_FIELD = "last_update";
const keys = new Map<string, { key: string; last_update: Date }>();
const modelIds = new Map<string, { model_id: string; last_update: Date }>();
//API KEY RELATED ENDPOINTS
app.get("/api/v1/wca/apikey/", async (_req, res) => {
await delay(DELAY_MS);
if (keys.has(ORG_ID)) {
// Key response only contains the Last Updated field
const data = keys.get(ORG_ID);
res.json({ last_update: data[LAST_UPDATED_FIELD] });
} else {
res.json({});
}
});
app.get("/api/v1/wca/apikey/test/", async (_req, res) => {
await delay(DELAY_MS);
if (!keys.has(ORG_ID)) {
res.sendStatus(404);
return;
} else {
const data = keys.get(ORG_ID);
const key = data[KEY_FIELD];
if (key === "error-test") {
// Emulate a server-side error
res.status(500).json({ detail: ERROR_DESCRIPTION });
return;
}
if (key === "invalid-test") {
// Emulate an invalid API Key
res.sendStatus(400);
return;
}
}
res.sendStatus(200);
});
app.post("/api/v1/wca/apikey/", async (_req, res) => {
await delay(DELAY_MS);
const key = _req.body["key"];
if (key === "error") {
// Emulate a server-side error
res.status(500).json({ detail: ERROR_DESCRIPTION });
return;
}
if (key === "invalid") {
// Emulate an invalid API Key
res.sendStatus(400);
return;
}
const entry = { key: key, last_update: new Date() };
keys.set(ORG_ID, entry);
res.sendStatus(200);
});
app.delete("/api/v1/wca/apikey/", async (_req, res) => {
await delay(DELAY_MS);
if (keys.has(ORG_ID)) {
keys.delete(ORG_ID);
if (modelIds.has(ORG_ID)) {
modelIds.delete(ORG_ID);
}
res.sendStatus(204);
} else {
res.sendStatus(400);
}
});
//MODEL ID RELATED ENDPOINTS
app.get("/api/v1/wca/modelid/", async (_req, res) => {
await delay(DELAY_MS);
if (modelIds.has(ORG_ID)) {
res.json(modelIds.get(ORG_ID));
} else {
res.json({});
}
});
app.get("/api/v1/wca/modelid/test/", async (_req, res) => {
await delay(DELAY_MS);
if (!modelIds.has(ORG_ID)) {
res.sendStatus(404);
return;
} else {
const data = modelIds.get(ORG_ID);
const modelId = data[MODEL_ID_FIELD];
if (modelId === "error-test") {
// Emulate a server-side error
res.status(500).json({ detail: ERROR_DESCRIPTION });
return;
}
if (modelId === "invalid-test") {
// Emulate an invalid Model Id
res.sendStatus(400);
return;
}
}
res.sendStatus(200);
});
app.post("/api/v1/wca/modelid/", async (_req, res) => {
await delay(DELAY_MS);
const modelId = _req.body[MODEL_ID_FIELD];
if (modelId === "error") {
// Emulate a server-side error
res.status(500).json({ detail: ERROR_DESCRIPTION });
return;
}
if (modelId === "invalid") {
// Emulate an invalid Model Id
res.sendStatus(400);
return;
}
const entry = { model_id: modelId, last_update: new Date() };
modelIds.set(ORG_ID, entry);
res.sendStatus(200);
});
});