-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-client-request-behavior.html
More file actions
117 lines (93 loc) · 3.42 KB
/
api-client-request-behavior.html
File metadata and controls
117 lines (93 loc) · 3.42 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
<script>
window.PolymerApigility = window.PolymerApigility || {};
PolymerApigility.ApiClientRequestBehavior = {
/**
* An object that maps header names to header values
*
* @return {Object}
*/
requestHeaders: function(headers) {
var mergedHeaders = {};
if (this.$.ironAjax.headers !== null && this.$.ironAjax.headers instanceof Object) {
for (var header in this.$.ironAjax.headers) {
mergedHeaders[header] = this.$.ironAjax.headers[header].toString();
}
}
if (this.headers !== null && this.headers instanceof Object) {
for (var header in this.headers) {
mergedHeaders[header] = this.headers[header].toString();
}
}
if (headers !== null && headers instanceof Object) {
for (var header in headers) {
mergedHeaders[header] = headers[header].toString();
}
}
return mergedHeaders;
},
/**
* An object that maps query names to query values
*
* @return {Object}
*/
requestQuery: function(queries) {
var mergedQueries = {};
if (this.queries !== null && this.queries instanceof Object) {
for (var query in this.queries) {
mergedQueries[query] = this.queries[query].toString();
}
}
if (queries !== null && queries instanceof Object) {
for (var query in queries) {
mergedQueries[query] = queries[query].toString();
}
}
return mergedQueries;
},
requestUrl: function () {
var url = this.baseUrl;
var resource = this.getResource();
if (resource) {
url += resource.getFullPath();
}
return url;
},
/**
* Create request
*
* @param method string
* @param data object
* @param headers object
* @param queries object
*/
createRequest: function (method, data, headers, queries) {
this.$.ironAjax.url = this.requestUrl();
this.$.ironAjax.headers = this.requestHeaders(headers);
this.$.ironAjax.params = this.requestQuery(queries);
this.$.ironAjax.method = (method) ? method : this.method;
this.$.ironAjax.body = (data) ? data : '';
var event = this.fire('api-request-presend', this.$.ironAjax, {}, {cancelable: true});
if(!event.defaultPrevented) {
event.detail.generateRequest();
}
},
setNestedId: function (id) {
return this.getResource().setNestedId(id);
},
isEntity: function () {
return this.getResource().isEntity();
},
getResource: function () {
if (!this.resource) {
this.resource = this.queryEffectiveChildren('apigility-resource');
if (!this.resource) {
this.resource = this.querySelector('apigility-resource');
}
}
if (!this.resource) {
throw 'Resource not config in ' + this.is;
}
return this.resource;
}
};
</script>