This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
aurelia-api.js
218 lines (165 loc) · 6.38 KB
/
aurelia-api.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
var _dec, _class3;
import extend from 'extend';
import { buildQueryString, join } from 'aurelia-path';
import { HttpClient, HttpClientConfiguration } from 'aurelia-fetch-client';
import { Container, resolver } from 'aurelia-dependency-injection';
export let Rest = class Rest {
constructor(httpClient, endpoint, useTraditionalUriTemplates) {
this.defaults = {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
};
this.client = httpClient;
this.endpoint = endpoint;
this.useTraditionalUriTemplates = !!useTraditionalUriTemplates;
}
request(method, path, body, options, responseOutput) {
let requestOptions = extend(true, { headers: {} }, this.defaults || {}, options || {}, { method, body });
let contentType = requestOptions.headers['Content-Type'] || requestOptions.headers['content-type'];
if (typeof body === 'object' && body !== null && contentType) {
requestOptions.body = /^application\/(.+\+)?json/.test(contentType.toLowerCase()) ? JSON.stringify(body) : buildQueryString(body);
}
return this.client.fetch(path, requestOptions).then(response => {
if (response.status >= 200 && response.status < 400) {
if (responseOutput) {
responseOutput.response = response;
}
return response.json().catch(() => null);
}
throw response;
});
}
find(resource, idOrCriteria, options, responseOutput) {
return this.request('GET', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), undefined, options, responseOutput);
}
findOne(resource, id, criteria, options, responseOutput) {
return this.request('GET', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), undefined, options, responseOutput);
}
post(resource, body, options, responseOutput) {
return this.request('POST', resource, body, options, responseOutput);
}
update(resource, idOrCriteria, body, options, responseOutput) {
return this.request('PUT', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), body, options, responseOutput);
}
updateOne(resource, id, criteria, body, options, responseOutput) {
return this.request('PUT', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), body, options, responseOutput);
}
patch(resource, idOrCriteria, body, options, responseOutput) {
return this.request('PATCH', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), body, options, responseOutput);
}
patchOne(resource, id, criteria, body, options, responseOutput) {
return this.request('PATCH', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), body, options, responseOutput);
}
destroy(resource, idOrCriteria, options, responseOutput) {
return this.request('DELETE', getRequestPath(resource, this.useTraditionalUriTemplates, idOrCriteria), undefined, options, responseOutput);
}
destroyOne(resource, id, criteria, options, responseOutput) {
return this.request('DELETE', getRequestPath(resource, this.useTraditionalUriTemplates, id, criteria), undefined, options, responseOutput);
}
create(resource, body, options, responseOutput) {
return this.post(resource, body, options, responseOutput);
}
};
function getRequestPath(resource, traditional, idOrCriteria, criteria) {
let hasSlash = resource.slice(-1) === '/';
if (typeof idOrCriteria === 'string' || typeof idOrCriteria === 'number') {
resource = `${join(resource, String(idOrCriteria))}${hasSlash ? '/' : ''}`;
} else {
criteria = idOrCriteria;
}
if (typeof criteria === 'object' && criteria !== null) {
resource += `?${buildQueryString(criteria, traditional)}`;
} else if (criteria) {
resource += `${hasSlash ? '' : '/'}${criteria}${hasSlash ? '/' : ''}`;
}
return resource;
}
export let Config = class Config {
constructor() {
this.endpoints = {};
}
registerEndpoint(name, configureMethod, defaults, restOptions) {
let newClient = new HttpClient();
let useTraditionalUriTemplates;
if (restOptions !== undefined) {
useTraditionalUriTemplates = restOptions.useTraditionalUriTemplates;
}
this.endpoints[name] = new Rest(newClient, name, useTraditionalUriTemplates);
if (defaults !== undefined) {
this.endpoints[name].defaults = defaults;
}
if (typeof configureMethod === 'function') {
newClient.configure(newClientConfig => {
return configureMethod(newClientConfig.withDefaults(this.endpoints[name].defaults));
});
this.endpoints[name].defaults = newClient.defaults;
return this;
}
if (typeof configureMethod !== 'string' && !this.defaultBaseUrl) {
return this;
}
if (this.defaultBaseUrl && typeof configureMethod !== 'string' && typeof configureMethod !== 'function') {
newClient.configure(configure => {
configure.withBaseUrl(this.defaultBaseUrl);
});
return this;
}
newClient.configure(configure => {
configure.withBaseUrl(configureMethod);
});
return this;
}
getEndpoint(name) {
if (!name) {
return this.defaultEndpoint || null;
}
return this.endpoints[name] || null;
}
endpointExists(name) {
return !!this.endpoints[name];
}
setDefaultEndpoint(name) {
this.defaultEndpoint = this.getEndpoint(name);
return this;
}
setDefaultBaseUrl(baseUrl) {
this.defaultBaseUrl = baseUrl;
return this;
}
configure(config) {
if (config.defaultBaseUrl) {
this.defaultBaseUrl = config.defaultBaseUrl;
}
config.endpoints.forEach(endpoint => {
this.registerEndpoint(endpoint.name, endpoint.endpoint, endpoint.config);
if (endpoint.default) {
this.setDefaultEndpoint(endpoint.name);
}
});
if (config.defaultEndpoint) {
this.setDefaultEndpoint(config.defaultEndpoint);
}
return this;
}
};
export function configure(frameworkConfig, configOrConfigure) {
let config = frameworkConfig.container.get(Config);
if (typeof configOrConfigure === 'function') {
configOrConfigure(config);
return;
}
config.configure(configOrConfigure);
}
export let Endpoint = (_dec = resolver(), _dec(_class3 = class Endpoint {
constructor(key) {
this._key = key;
}
get(container) {
return container.get(Config).getEndpoint(this._key);
}
static of(key) {
return new Endpoint(key);
}
}) || _class3);