-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (113 loc) · 2.94 KB
/
index.js
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
/**
* Module dependencies
*/
var merge = require('utils-merge');
/**
* Transform a hal+json resource into a hyper+json representation
*
* @param {Object} resource
* @return {Object}
*/
exports = module.exports = function(resource, curies) {
curies = merge(transformCuries(get('curies', get('_links', resource), [])), curies);
return Object.keys(resource).reduce(function(hyperObj, key) {
var value = resource[key];
if (key === '_links') return transformLinks(hyperObj, value, curies);
if (key === '_embedded') return transformEmbedded(hyperObj, value, curies);
hyperObj[key] = value;
return hyperObj;
}, {});
};
/**
* Transform the curies into an Object
*
* @param {Array} curies
* @return {Object}
*/
function transformCuries(curies) {
return curies.reduce(function(acc, curie) {
var name = get('name', curie);
if (!name) return acc;
acc[name] = curie;
return acc;
}, {});
}
/**
* Transform the _links object
*
* @param {Object} hyperObj
* @param {Object} links
* @param {Object} curies
* @return {Object}
*/
function transformLinks(hyperObj, links, curies) {
Object.keys(links).forEach(function(name) {
if (name === 'curies') return;
var value = links[name];
if (name === 'self') return hyperObj.href = get('href', value);
applyCurie(hyperObj, curies, name, transformTemplated(value));
});
return hyperObj;
}
/**
* Transform templated links
*
* @param {Object} link
* @return {Object}
*/
function transformTemplated(link) {
if (Array.isArray(link)) return {collection: link.map(transformTemplated)};
// TODO support templated links
if (link.templated) console.warn('Templated links are not supported at this time');
return link;
}
/**
* Transform _embedded resources
*
* @param {Object} hyperObj
* @param {Object} embedded
* @param {Object} curies
* @return {Object}
*/
function transformEmbedded(hyperObj, embedded, curies) {
Object.keys(embedded).forEach(function(rel) {
applyCurie(hyperObj, curies, rel, {
collection: embedded[rel].map(function(item) {
return exports(item, curies);
})
});
});
return hyperObj;
}
/**
* Apply curie transformations to a link
*
* @param {Object} hyperObj
* @param {Object} curies
* @param {String} name
* @param {Object} link
*/
function applyCurie(hyperObj, curies, name, link) {
var parts = name.split(':');
if (parts.length === 1) return hyperObj[name] = link;
var curie = get('href', curies[parts[0]]);
if (!curie) return hyperObj[name] = link;
var rel = parts[1];
return hyperObj[rel] = merge({
// TODO we need to have a formalized property in hyper+json for docs
'//documentation': curie.replace(/\{ *rel *\}/g, rel)
}, link);
}
/**
* 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;
}