-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequest.ts
More file actions
328 lines (297 loc) · 10 KB
/
Copy pathrequest.ts
File metadata and controls
328 lines (297 loc) · 10 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import {
MANUFACTURER_FIELD_ALLOWLIST,
MODULE_FIELD_ALLOWLIST,
STANDARD_FIELD_ALLOWLIST,
TAG_FIELD_ALLOWLIST,
} from './catalogue-mapping.ts';
import type { CursorToken, ModuleInclude, SortMode } from './catalogue-types.ts';
export type RouteKind =
| 'modules:list'
| 'modules:detail'
| 'manufacturers:list'
| 'manufacturers:detail'
| 'standards:list'
| 'tags:list';
export interface NormalizedRoute {
kind: RouteKind;
id: number | null;
}
export type RequestNormalizationResult =
| { ok: true; url: URL; cacheKey: string; route: NormalizedRoute | null }
| {
ok: false;
code: 'unknown_parameter' | 'invalid_parameter' | 'unsupported_parameter';
parameter: string;
};
type NormalizedValueResult =
| { ok: true; value: string }
| Extract<RequestNormalizationResult, { ok: false }>;
const MODULE_LIST_PARAMETERS = new Set([
'cursor',
'fields',
'hp',
'include',
'limit',
'manufacturer_id',
'q',
'sort',
'standard',
'tag',
]);
const MODULE_DETAIL_PARAMETERS = new Set(['fields', 'include']);
const MANUFACTURER_LIST_PARAMETERS = new Set(['cursor', 'fields', 'limit', 'q', 'sort']);
const MANUFACTURER_DETAIL_PARAMETERS = new Set(['fields', 'include']);
const REFERENCE_PARAMETERS = new Set(['cursor', 'fields', 'limit', 'q', 'sort']);
const MODULE_INCLUDE_VALUES = new Set(['ins', 'outs', 'panels', 'tags']);
const MANUFACTURER_INCLUDE_VALUES = new Set(['modules']);
const MAX_LIMIT = 100;
const DEFAULT_LIMIT = 50;
const DEFAULT_SORT: SortMode = 'name';
export function normalizeApiRequest(requestUrl: string): RequestNormalizationResult {
const url = new URL(requestUrl);
const route = routeForPath(url.pathname);
const allowedParameters = route ? allowedParametersForRoute(route) : new Set<string>();
for (const parameter of url.searchParams.keys()) {
if (!allowedParameters.has(parameter)) {
return { ok: false, code: 'unknown_parameter', parameter };
}
}
const normalizedPath = url.pathname.replace(/\/+$/, '');
const normalized = new URL(url.origin + normalizedPath);
const entries: [string, string][] = [];
for (const [key, rawValue] of url.searchParams.entries()) {
const value = rawValue.trim();
if (value === '') {
continue;
}
const normalizedValue = normalizeValue(key, value, route);
if (!normalizedValue.ok) {
return normalizedValue;
}
entries.push([key, normalizedValue.value]);
}
if (route && isListRoute(route) && !entries.some(([key]) => key === 'limit')) {
entries.push(['limit', String(DEFAULT_LIMIT)]);
}
if (route && isListRoute(route) && !entries.some(([key]) => key === 'sort')) {
entries.push(['sort', DEFAULT_SORT]);
}
const sort = entries.find(([key]) => key === 'sort')?.[1] ?? DEFAULT_SORT;
const cursor = entries.find(([key]) => key === 'cursor')?.[1] ?? null;
if (cursor && !cursorMatchesSort(cursor, sort)) {
return { ok: false, code: 'invalid_parameter', parameter: 'cursor' };
}
for (const [key, value] of entries.sort(([left], [right]) => left.localeCompare(right))) {
normalized.searchParams.set(key, value);
}
normalized.searchParams.sort();
return {
ok: true,
url: normalized,
route,
cacheKey: `GET ${normalized.pathname}${normalized.search}`,
};
}
export function decodeCursor(value: string | null): CursorToken | null {
if (!value) {
return null;
}
try {
const base64 = value.replace(/-/g, '+').replace(/_/g, '/');
const binary = atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, '='));
const decoded = new TextDecoder().decode(
Uint8Array.from(binary, character => character.charCodeAt(0))
);
const cursor = JSON.parse(decoded) as unknown;
return isCursor(cursor) ? cursor : null;
} catch {
return null;
}
}
export function parseCsv(value: string | null): string[] {
if (!value) {
return [];
}
return value.split(',').map(item => item.trim()).filter(Boolean);
}
export function parseOptionalPositiveInteger(value: string | null): number | null {
if (value === null) {
return null;
}
const parsed = Number(value);
return Number.isInteger(parsed) && parsed > 0 ? parsed : null;
}
export function parseOptionalNonnegativeInteger(value: string | null): number | null {
if (value === null) {
return null;
}
const parsed = Number(value);
return Number.isInteger(parsed) && parsed >= 0 ? parsed : null;
}
export function parseLimit(value: string | null): number {
const parsed = Number(value ?? DEFAULT_LIMIT);
return Number.isInteger(parsed) && parsed > 0 ? Math.min(parsed, MAX_LIMIT) : DEFAULT_LIMIT;
}
export function parseSort(value: string | null): SortMode {
return value === 'id' ? 'id' : 'name';
}
export function parseModuleIncludes(value: string | null): ModuleInclude[] {
return parseCsv(value) as ModuleInclude[];
}
function normalizeValue(
key: string,
value: string,
route: NormalizedRoute | null
): NormalizedValueResult {
if (key === 'q') {
return { ok: false, code: 'unsupported_parameter', parameter: key };
}
if (key === 'sort') {
const normalized = value.toLowerCase();
return normalized === 'name' || normalized === 'id'
? { ok: true, value: normalized }
: { ok: false, code: 'unsupported_parameter', parameter: key };
}
if (key === 'limit') {
const parsed = Number(value);
return Number.isInteger(parsed) && parsed > 0
? { ok: true, value: String(Math.min(parsed, MAX_LIMIT)) }
: { ok: false, code: 'invalid_parameter', parameter: key };
}
if (key === 'standard') {
const parsed = Number(value);
return Number.isInteger(parsed) && parsed >= 0
? { ok: true, value: String(parsed) }
: { ok: false, code: 'invalid_parameter', parameter: key };
}
if (key === 'hp' || key === 'manufacturer_id' || key === 'tag') {
const parsed = Number(value);
return Number.isInteger(parsed) && parsed > 0
? { ok: true, value: String(parsed) }
: { ok: false, code: 'invalid_parameter', parameter: key };
}
if (key === 'include') {
return normalizeInclude(value, route);
}
if (key === 'cursor') {
const normalized = normalizeCursor(value);
return normalized
? { ok: true, value: normalized }
: { ok: false, code: 'invalid_parameter', parameter: key };
}
if (key === 'fields') {
return normalizeFields(value, route);
}
return { ok: true, value };
}
function normalizeInclude(
value: string,
route: NormalizedRoute | null
): NormalizedValueResult {
const allowed = route?.kind === 'manufacturers:detail'
? MANUFACTURER_INCLUDE_VALUES
: MODULE_INCLUDE_VALUES;
const values = Array.from(new Set(value.split(',').map(item => item.trim().toLowerCase())))
.sort();
return values.length > 0 && values.every(item => allowed.has(item))
? { ok: true, value: values.join(',') }
: { ok: false, code: 'invalid_parameter', parameter: 'include' };
}
function normalizeFields(
value: string,
route: NormalizedRoute | null
): NormalizedValueResult {
const allowed = fieldAllowlistForRoute(route);
const fields = Array.from(new Set(value.split(',').map(item => item.trim()).filter(Boolean)))
.sort();
if (fields.length === 0 || !fields.every(field => allowed.has(field))) {
return { ok: false, code: 'invalid_parameter', parameter: 'fields' };
}
return { ok: true, value: fields.join(',') };
}
function normalizeCursor(value: string): string | null {
const cursor = decodeCursor(value);
if (!cursor) {
return null;
}
const canonical = JSON.stringify({ v: 1, s: cursor.s, id: cursor.id });
const encoded = btoa(
Array.from(new TextEncoder().encode(canonical), byte => String.fromCharCode(byte)).join('')
);
return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
}
function cursorMatchesSort(value: string, sort: string): boolean {
const cursor = decodeCursor(value);
if (!cursor) {
return false;
}
return sort === 'id' ? typeof cursor.s === 'number' : typeof cursor.s === 'string';
}
function routeForPath(pathname: string): NormalizedRoute | null {
const path = pathname.replace(/\/+$/, '');
if (path === '/v1/modules') {
return { kind: 'modules:list', id: null };
}
if (path === '/v1/manufacturers') {
return { kind: 'manufacturers:list', id: null };
}
if (path === '/v1/standards') {
return { kind: 'standards:list', id: null };
}
if (path === '/v1/tags') {
return { kind: 'tags:list', id: null };
}
const moduleDetail = /^\/v1\/modules\/(\d+)$/.exec(path);
if (moduleDetail) {
return { kind: 'modules:detail', id: Number(moduleDetail[1]) };
}
const manufacturerDetail = /^\/v1\/manufacturers\/(\d+)$/.exec(path);
if (manufacturerDetail) {
return { kind: 'manufacturers:detail', id: Number(manufacturerDetail[1]) };
}
return null;
}
function allowedParametersForRoute(route: NormalizedRoute): Set<string> {
switch (route.kind) {
case 'modules:list':
return MODULE_LIST_PARAMETERS;
case 'modules:detail':
return MODULE_DETAIL_PARAMETERS;
case 'manufacturers:list':
return MANUFACTURER_LIST_PARAMETERS;
case 'manufacturers:detail':
return MANUFACTURER_DETAIL_PARAMETERS;
case 'standards:list':
case 'tags:list':
return REFERENCE_PARAMETERS;
}
}
function fieldAllowlistForRoute(route: NormalizedRoute | null): Set<string> {
switch (route?.kind) {
case 'modules:list':
case 'modules:detail':
return MODULE_FIELD_ALLOWLIST;
case 'manufacturers:list':
case 'manufacturers:detail':
return MANUFACTURER_FIELD_ALLOWLIST;
case 'standards:list':
return STANDARD_FIELD_ALLOWLIST;
case 'tags:list':
return TAG_FIELD_ALLOWLIST;
default:
return new Set();
}
}
function isListRoute(route: NormalizedRoute): boolean {
return route.kind.endsWith(':list');
}
function isCursor(value: unknown): value is CursorToken {
if (!value || typeof value !== 'object') {
return false;
}
const cursor = value as Record<string, unknown>;
return cursor.v === 1
&& (typeof cursor.s === 'string' || typeof cursor.s === 'number')
&& Number.isInteger(cursor.id)
&& Number(cursor.id) >= 0;
}