-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathhal.ts
445 lines (367 loc) · 11 KB
/
hal.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import { BaseState } from './base-state';
import { parseLink } from '../http/util';
import { Link, Links } from '../link';
import { resolve } from '../util/uri';
import { ActionInfo } from '../action';
import { Field } from '../field';
import { StateFactory } from './interface';
import Client from '../client';
import * as hal from 'hal-types';
/**
* Represents a resource state in the HAL format
*/
export class HalState<T = any> extends BaseState<T> {
serializeBody(): string {
return JSON.stringify({
_links: this.serializeLinks(),
...this.data
});
}
serializeLinks(): hal.HalResource['_links'] {
const links: hal.HalResource['_links'] = {
self: { href: this.uri },
};
for(const link of this.links.getAll()) {
const { rel, context, ...attributes } = link;
if (rel === 'self') {
// skip
continue;
}
if (links[rel] === undefined) {
// First link of its kind
links[rel] = attributes;
} else if (Array.isArray(links[rel])) {
// Add link to link array.
(links[rel] as hal.HalLink[]).push(attributes);
} else {
// 1 link with this rel existed, so we will transform it to an array.
links[rel] = [links[rel] as hal.HalLink, attributes];
}
}
return links;
}
clone(): HalState<T> {
return new HalState({
client: this.client,
uri: this.uri,
data: this.data,
headers: new Headers(this.headers),
links: new Links(this.links.defaultContext, this.links.getAll()),
actions: this.actionInfo,
});
}
}
/**
* Turns a HTTP response into a HalState
*/
export const factory:StateFactory = async (client, uri, response): Promise<HalState> => {
const body = await response.json();
const links = parseLink(uri, response.headers.get('Link'));
// The HAL factory is also respondible for plain JSON, which might be an
// array.
if (Array.isArray(body)) {
return new HalState({
client,
uri,
data: body,
headers: response.headers,
links,
});
}
links.add(...parseHalLinks(uri, body));
// Remove _links and _embedded from body
const {
_embedded,
_links,
_templates,
...newBody
} = body;
return new HalState({
client,
uri: uri,
data: newBody,
headers: response.headers,
links: links,
embedded: parseHalEmbedded(client, uri, body, response.headers),
actions: parseHalForms(uri, body),
});
};
/**
* Parse the Hal _links object and populate the 'links' property.
*/
export function parseHalLinks(context: string, body: hal.HalResource): Link[] {
if (body._links === undefined) {
return [];
}
const result: Link[] = [];
/**
* We're capturing all rel-link pairs so we don't duplicate them if they
* re-appear in _embedded.
*
* Links that are embedded _should_ appear in both lists, but not everyone
* does this.
*/
const foundLinks = new Set();
for (const [relType, links] of Object.entries(body._links)) {
const linkList = Array.isArray(links) ? links : [links];
for (const link of linkList) {
foundLinks.add(relType + ';' + link.href);
}
result.push(
...parseHalLink(context, relType, linkList)
);
}
if (body._embedded) {
// eslint-disable-next-line prefer-const
for (let [rel, innerBodies] of Object.entries(body._embedded)) {
if (!Array.isArray(innerBodies)) {
innerBodies = [innerBodies];
}
for(const innerBody of innerBodies) {
const href:string = innerBody?._links?.self?.href;
if (!href) {
continue;
}
if (foundLinks.has(rel + ';' + href)) {
continue;
}
result.push({
rel: rel,
href: href,
context: context,
});
}
}
}
return result;
}
/**
* Parses a single HAL link from a _links object
*/
function parseHalLink(context: string, rel: string, links: hal.HalLink[]): Link[] {
const result: Link[] = [];
for (const link of links) {
result.push({
rel,
context,
...link,
});
}
return result;
}
/**
* Parse the HAL _embedded object. Right now we're just grabbing the
* information from _embedded and turn it into links.
*/
function parseHalEmbedded(client: Client, context: string, body: hal.HalResource, headers: Headers): HalState<any>[] {
if (body._embedded === undefined || !body._embedded) {
return [];
}
const result: HalState<any>[] = [];
for (const embedded of Object.values(body._embedded)) {
let embeddedList: hal.HalResource[];
if (!Array.isArray(embedded)) {
embeddedList = [embedded];
} else {
embeddedList = embedded;
}
for (const embeddedItem of embeddedList) {
if (embeddedItem._links?.self?.href === undefined) {
// eslint-disable-next-line no-console
console.warn('An item in _embedded was ignored. Each item must have a single "self" link');
continue;
}
const embeddedSelf = resolve(context, embeddedItem._links.self.href);
// Remove _links and _embedded from body
const {
_embedded,
_links,
...newBody
} = embeddedItem;
result.push(new HalState({
client,
uri: embeddedSelf,
data: newBody,
headers: new Headers({
'Content-Type': headers.get('Content-Type')!,
}),
links: new Links(embeddedSelf, parseHalLinks(context, embeddedItem)),
// Parsing nested embedded items. Note that we assume that the base url is relative to
// the outermost parent, not relative to the embedded item. HAL is not clear on this.
embedded: parseHalEmbedded(client, embeddedSelf, embeddedItem, headers),
actions: parseHalForms(embeddedSelf, embeddedItem)
}));
}
}
return result;
}
function parseHalForms(context: string, body: hal.HalResource): ActionInfo[] {
if (!body._templates) return [];
return Object.entries(body._templates).map( ([key, hf]) => {
return {
uri: resolve(context, hf.target || ''),
name: key,
title: hf.title,
method: hf.method,
contentType: hf.contentType || 'application/json',
fields: hf.properties ? hf.properties.map(prop => parseHalField(prop)) : [],
};
});
}
function parseHalField(halField: hal.HalFormsProperty): Field {
switch(halField.type) {
case undefined:
case 'text' :
case 'search' :
case 'tel' :
case 'url' :
case 'email' :
if (halField.options) {
const baseField = {
name: halField.name,
type: 'select' as const,
label: halField.prompt,
required: halField.required || false,
readOnly: halField.readOnly || false,
multiple: halField.options.multiple as any,
value: (halField.options.selectedValues || halField.value) as any
};
const labelField = halField.options.promptField || 'prompt';
const valueField = halField.options.valueField || 'value';
if (isInlineOptions(halField.options)) {
const options: Record<string, string> = {};
for(const entry of halField.options.inline) {
if (typeof entry === 'string') {
options[entry] = entry;
} else {
options[entry[valueField]] = entry[labelField];
}
}
return {
...baseField,
options
};
} else {
return {
...baseField,
dataSource: {
href: halField.options.link.href,
type: halField.options.link.type,
labelField,
valueField,
}
};
}
} else {
return {
name: halField.name,
type: halField.type ?? 'text',
required: halField.required || false,
readOnly: halField.readOnly || false,
value: halField.value,
pattern: halField.regex ? new RegExp(halField.regex) : undefined,
label: halField.prompt,
placeholder: halField.placeholder,
minLength: halField.minLength,
maxLength: halField.maxLength,
};
}
case 'hidden' :
return {
name: halField.name,
type: 'hidden',
required: halField.required || false,
readOnly: halField.readOnly || false,
value: halField.value,
label: halField.prompt,
placeholder: halField.placeholder,
};
case 'textarea' :
return {
name: halField.name,
type: halField.type,
required: halField.required || false,
readOnly: halField.readOnly || false,
value: halField.value,
label: halField.prompt,
placeholder: halField.placeholder,
cols: halField.cols,
rows: halField.rows,
minLength: halField.minLength,
maxLength: halField.maxLength,
};
case 'password' :
return {
name: halField.name,
type: halField.type,
required: halField.required || false,
readOnly: halField.readOnly || false,
label: halField.prompt,
placeholder: halField.placeholder,
minLength: halField.minLength,
maxLength: halField.maxLength,
};
case 'date' :
case 'month' :
case 'week' :
case 'time' :
return {
name: halField.name,
type: halField.type,
value: halField.value,
required: halField.required || false,
readOnly: halField.readOnly || false,
label: halField.prompt,
min: halField.min,
max: halField.max,
step: halField.step,
};
case 'number' :
case 'range' :
return {
name: halField.name,
type: halField.type,
value: halField.value ? +halField.value : undefined,
required: halField.required || false,
readOnly: halField.readOnly || false,
label: halField.prompt,
min: halField.min,
max: halField.max,
step: halField.step,
};
case 'datetime-local' :
return {
name: halField.name,
type: halField.type,
value: halField.value ? new Date(halField.value) : undefined,
required: halField.required || false,
readOnly: halField.readOnly || false,
label: halField.prompt,
min: halField.min,
max: halField.max,
step: halField.step,
};
case 'color' :
return {
name: halField.name,
type: halField.type,
required: halField.required || false,
readOnly: halField.readOnly || false,
label: halField.prompt,
value: halField.value,
};
case 'radio' :
case 'checkbox' :
return {
name: halField.name,
type: halField.type,
required: halField.required || false,
readOnly: halField.readOnly || false,
label: halField.prompt,
value: !!halField.value,
};
}
}
function isInlineOptions(options: hal.HalFormsSimpleProperty['options']): options is hal.HalFormsOptionsInline {
return (options as any).inline !== undefined;
}