Newer
Older
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
import HttpErrors from 'http-errors'
import Resource from './Resource.mjs'
// TODO: implement a Page class, check for it and handle in the handler methods
// TODO: implement a Range class and handle Range header
export default class CollectionResource extends Resource {
constructor() {
super()
}
async createEntity(json, ...ids) {
throw HttpErrors.MethodNotAllowed()
}
async removeAll(query, ...ids) {
throw HttpErrors.MethodNotAllowed()
}
async replaceAll(query, ...ids) {
throw HttpErrors.MethodNotAllowed()
}
async retrieveAll(query, ...ids) {
throw HttpErrors.MethodNotAllowed()
}
/**
* @override
* @private
*/
async handleDelete(req, res) {
await this.removeAll(req.query, ...this.getOrderedParams(req));
res.sendStatus(204);
}
/**
* @override
* @private
*/
async handleGet(req, res) {
const entities = await this.retrieveAll(req.query, ...this.getOrderedParams(req));
if (entities === undefined) {
throw HttpErrors.NotFound();
}
if (entities.constructor.mediaSubtype) {
res.type(entities.constructor.mimeType);
}
// This overwrites the subresource link headers set in Resource, since the variable path segment wouldn't make sense
if (entities.constructor.schema) {
const links = entities.constructor.schema.links;
if (links) {
links
.map(link => `rel="${link.rel}"; title="${link.title}"; href="${link.href}"`)
.join(',');
res.header('Link', links);
}
}
res.json(entities);
}
/**
* @override
* @private
*/
async handleOptions(req, res) {
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST');
res.sendStatus(204);
}
/**
* @override
* @private
*/
async handlePost(req, res) {
const id = await this.createEntity(req.body, ...this.getOrderedParams(req));
if (id !== undefined) {
res.location(id);
}
res.sendStatus(201);
}
/**
* @override
* @private
*/
async handlePut(req, res) {
await this.replaceAll(req.body, ...this.getOrderedParams(req));
res.sendStatus(201);
}
}