forked from LuvDaSun/angular-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
258 lines (197 loc) · 8.87 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
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
/* global angular */
angular.module('angular-hal', [])
.service('halClient', [
'$http', '$q', '$window',
function (
$http, $q, $window
) {
var rfc6570 = $window.rfc6570;
this.$get = function (href, options) {
return callService('GET', href, options);
}; //get
this.$post = function (href, options, data) {
return callService('POST', href, options, data);
}; //post
this.$put = function (href, options, data) {
return callService('PUT', href, options, data);
}; //put
this.$patch = function (href, options, data) {
return callService('PATCH', href, options, data);
}; //patch
this.$del = function (href, options) {
return callService('DELETE', href, options);
}; //del
function Resource(href, options, data) {
var linksAttribute = options.linksAttribute || '_links';
var embeddedAttribute = options.embeddedAttribute || '_embedded';
var ignoreAttributePrefixes = options.ignoreAttributePrefixes || ['_', '$'];
var links = {};
var embedded = {};
href = getSelfLink(href, data).href;
defineHiddenProperty(this, '$href', function (rel, params) {
if (!(rel in links)) return null;
return hrefLink(links[rel], params);
});
defineHiddenProperty(this, '$has', function (rel) {
return rel in links;
});
defineHiddenProperty(this, '$get', function (rel, params) {
var link = links[rel];
return callLink('GET', link, params);
});
defineHiddenProperty(this, '$post', function (rel, params, data) {
var link = links[rel];
return callLink('POST', link, params, data);
});
defineHiddenProperty(this, '$put', function (rel, params, data) {
var link = links[rel];
return callLink('PUT', link, params, data);
});
defineHiddenProperty(this, '$patch', function (rel, params, data) {
var link = links[rel];
return callLink('PATCH', link, params, data);
});
defineHiddenProperty(this, '$del', function (rel, params) {
var link = links[rel];
return callLink('DELETE', link, params);
});
Object.keys(data)
.filter(function (key) {
return key !== linksAttribute && key !== embeddedAttribute && (!~ignoreAttributePrefixes.indexOf(key[0]));
})
.forEach(function (key) {
Object.defineProperty(this, key, {
configurable: false,
enumerable: true,
writable: true, /* https://github.com/LuvDaSun/angular-hal/issues/6
Ultimately, we'd like to pass a writable-parameter when requesting a
resource, that would indicate whether or not the properties of the
returned resource are writable or read-only */
value: data[key]
});
}, this);
if (data[linksAttribute]) {
Object
.keys(data[linksAttribute])
.forEach(function (rel) {
var link = data[linksAttribute][rel];
link = normalizeLink(href, link);
links[rel] = link;
}, this);
}
if (data[embeddedAttribute]) {
Object
.keys(data[embeddedAttribute])
.forEach(function (rel) {
var embedded = data[embeddedAttribute][rel];
var link = getSelfLink(href, embedded);
links[rel] = link;
//console.log(link)
var resource = createResource(href, options, embedded);
embedResource(resource);
}, this);
}
function defineHiddenProperty(target, name, value) {
Object.defineProperty(target, name, {
configurable: false,
enumerable: false,
value: value
});
} //defineHiddenProperty
function embedResource(resource) {
if (Array.isArray(resource)) return resource.map(function (resource) {
return embedResource(resource);
});
var href = resource.$href('self');
embedded[href] = $q.when(resource);
} //embedResource
function hrefLink(link, params) {
var href = link.templated ? new rfc6570.UriTemplate(link.href).stringify(params) : link.href;
return href;
} //hrefLink
function callLink(method, link, params, data) {
var linkHref;
if (Array.isArray(link)) {
return $q.all(link.map(function (link) {
if (method !== 'GET') throw 'method is not supported for arrays';
return callLink(method, link, params, data);
}));
}
linkHref = hrefLink(link, params);
if (method === 'GET') {
if (linkHref in embedded) return embedded[linkHref];
return callService(method, linkHref, options, data);
} else {
return callService(method, linkHref, options, data);
}
} //callLink
function getSelfLink(baseHref, resource) {
if (Array.isArray(resource)) return resource.map(function (resource) {
return getSelfLink(baseHref, resource);
});
return normalizeLink(baseHref, resource && resource[linksAttribute] && resource[linksAttribute].self);
} //getSelfLink
} //Resource
function createResource(href, options, data) {
if (Array.isArray(data)) return data.map(function (data) {
return createResource(href, options, data);
});
var resource = new Resource(href, options, data);
return resource;
} //createResource
function normalizeLink(baseHref, link) {
if (Array.isArray(link)) return link.map(function (link) {
return normalizeLink(baseHref, link);
});
if (link) {
if (typeof link === 'string') link = {
href: link
};
link.href = resolveUrl(baseHref, link.href);
} else {
link = {
href: baseHref
};
}
return link;
} //normalizeLink
function callService(method, href, options, data) {
if (!options) options = {};
if (!options.headers) options.headers = {};
if (!options.headers['Content-Type']) options.headers['Content-Type'] = 'application/json';
if (!options.headers.Accept) options.headers.Accept = 'application/hal+json,application/json';
var resource = (
$http({
method: method,
url: options.transformUrl ? options.transformUrl(href) : href,
headers: options.headers,
data: data
})
.then(function (res) {
switch (Math.floor(res.status / 100)) {
case 2:
if (res.data) return createResource(href, options, res.data);
if (res.headers('Content-Location')) return res.headers('Content-Location');
if (res.headers('Location')) return res.headers('Location');
return null;
default:
return $q.reject(res.status);
}
})
);
return resource;
} //callService
function resolveUrl(baseHref, href) {
var resultHref = '';
var reFullUrl = /^((?:\w+\:)?)((?:\/\/)?)([^\/]*)((?:\/.*)?)$/;
var baseHrefMatch = reFullUrl.exec(baseHref);
var hrefMatch = reFullUrl.exec(href);
for (var partIndex = 1; partIndex < 5; partIndex++) {
if (hrefMatch[partIndex]) resultHref += hrefMatch[partIndex];
else resultHref += baseHrefMatch[partIndex];
}
return resultHref;
} //resolveUrl
}
]); //service
export default 'angular-hal';