Skip to content

Commit aadb811

Browse files
committed
Added first import of js-framework
1 parent 99474ce commit aadb811

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# Logs
2-
logs
31
*.log
42
npm-debug.log*
53
yarn-debug.log*

js/provider.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,59 @@
11
// Provider class to be subclassed by an actual provider
22

3+
import Error from './error';
4+
5+
// ////////////////////////////////////////////////////////////////////////////
6+
// CONSTANTS
7+
8+
const EVENT_COMPLETED = "mvc.provider.completed";
9+
const EVENT_ERROR = "mvc.provider.error";
10+
11+
// ////////////////////////////////////////////////////////////////////////////
12+
// PROVIDER CLASS
13+
314
export default class Provider {
415
constructor(root) {
516
this.$root = root;
617
}
18+
19+
$fetch(url, req, userInfo) {
20+
var status;
21+
window.fetch(this.$root + url, req).then(response => {
22+
status = response;
23+
switch (status.headers.get("Content-Type").split(";")[0]) {
24+
case "application/json":
25+
case "text/json":
26+
return response.json();
27+
case "text/plain":
28+
return response.text();
29+
default:
30+
return response.blob();
31+
}
32+
}).then(data => {
33+
if (!status.ok) {
34+
if (typeof (data) == "object" && data.reason) {
35+
throw new Error(data.reason, data.code);
36+
} else {
37+
throw new Error(status.statusText, status.status);
38+
}
39+
} else if (typeof (data) == "object" && Array.isArray(data)) {
40+
this.$array(data);
41+
} else {
42+
this.$object(data);
43+
}
44+
}).then(() => {
45+
document.dispatchEvent(new Event(EVENT_COMPLETED, {
46+
userInfo: userInfo,
47+
}));
48+
}).catch(error => {
49+
if (error instanceof Error) {
50+
document.dispatchEvent(new Event(EVENT_ERROR, {
51+
userInfo: userInfo,
52+
error: error,
53+
}));
54+
} else {
55+
throw error;
56+
}
57+
});
58+
}
759
}

0 commit comments

Comments
 (0)