Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
148 lines (125 loc) · 3.43 KB
/
Copy pathindex.js
File metadata and controls
148 lines (125 loc) · 3.43 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
/**
* Transform a Collection+JSON resource into a hyper+json representation
*
* @param {Object} resource
* @return {Object}
*/
module.exports = function(resource) {
var root = get('collection', resource);
if (!root) throw new Error('Missing root "collection" property');
var hyperObj = {};
mergeItem(hyperObj, root, null, true);
var template = get('data', get('template', root));
if (template) hyperObj.create = {
enctype: 'application/collection+json',
action: hyperObj.href,
method: 'POST',
input: transformInputs(template)
};
hyperObj.collection = get('items', root, []).map(function(item) {
return mergeItem({}, item, template);
});
get('queries', root, []).forEach(function(query) {
var rel = get('rel', query);
if (!rel) return;
var form = hyperObj[rel] = {
method: 'GET',
input: {}
};
Object.keys(query).forEach(function(key) {
if (key === 'rel') return;
if (key === 'href') return form.action = query[key];
if (key === 'data') return form.input = transformInputs(query[key]);
form[key] = query[key];
});
});
return hyperObj;
};
/**
* Merge an item's properties into a document
*
* @param {Object} doc
* @param {Object} item
* @param {Array} template
* @param {Boolean} isRoot
* @return {Object}
*/
function mergeItem(doc, item, template, isRoot) {
var href = doc.href = get('href', item);
get('links', item, []).forEach(function(link) {
var rel = get('rel', link);
if (!rel) return;
var render = get('render', link);
var out = doc[rel] = {};
Object.keys(link).forEach(function(key) {
if (key === 'rel' || key === 'render') return;
if (key === 'href' && render === 'image') return out.src = link[key];
out[key] = link[key];
});
});
get('data', item, []).forEach(function(datum) {
var name = get('name', datum);
if (!name) return;
doc[name] = Object.keys(datum).reduce(function(acc, key) {
if (key === 'name') return acc;
if (key === 'value') acc.data = datum[key];
else acc[key] = datum[key];
return acc;
}, {});
});
if (template) doc.update = {
enctype: 'application/collection+json',
method: 'PUT',
action: href,
input: transformInputs(template, doc)
}
if (!isRoot) doc.delete = {
method: 'DELETE',
action: href
};
return doc;
}
/**
* Transform CJ inputs into hyper rep, filling in any values
*
* @param {Array} inputs
* @param {Object} values
* @return {Object}
*/
function transformInputs(inputs, values) {
var hyperInputs = {};
inputs.forEach(mergeInput.bind(null, hyperInputs, values));
return hyperInputs;
}
/**
* Merge input into inputs
*
* @param {Object} inputs
* @param {Object} values
* @param {Object} input
*/
function mergeInput(inputs, values, input) {
var name = get('name', input);
if (!name) return;
var out = inputs[name] = {};
Object.keys(input).forEach(function(key) {
if (key === 'name') return;
if (!values || key !== 'value') return out[key] = input[key];
var value = values[name];
if (!value || typeof value !== 'object') return out.value = value;
out.value = value.href || value.src || value.data;
});
}
/**
* Gracefully get a value
*
* @param {String} key
* @param {Any} obj
* @param {Any} fallback
* @return {Any}
*/
function get(key, obj, fallback) {
if (!obj) return undefined;
if (obj.hasOwnProperty(key)) return obj[key];
return fallback;
}