|
1 | 1 | // Provider class to be subclassed by an actual provider |
2 | 2 |
|
3 | 3 | import Error from './error'; |
| 4 | +import Emitter from './events'; |
4 | 5 |
|
5 | 6 | // //////////////////////////////////////////////////////////////////////////// |
6 | 7 | // CONSTANTS |
7 | 8 |
|
8 | | -const EVENT_COMPLETED = "mvc.provider.completed"; |
9 | | -const EVENT_ERROR = "mvc.provider.error"; |
| 9 | +const EVENT_ROOT = 'mvc.provider'; |
| 10 | +const EVENT_COMPLETED = `${EVENT_ROOT}.completed`; |
| 11 | +const EVENT_ERROR = `${EVENT_ROOT}.error`; |
10 | 12 |
|
11 | 13 | // //////////////////////////////////////////////////////////////////////////// |
12 | 14 | // PROVIDER CLASS |
13 | 15 |
|
14 | | -export default class Provider { |
| 16 | +export default class Provider extends Emitter { |
15 | 17 | constructor(root) { |
16 | | - this.$root = root; |
| 18 | + super(); |
| 19 | + this.$root = root || ''; |
17 | 20 | } |
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); |
| 21 | + |
| 22 | + fetch(url, req, userInfo) { |
| 23 | + let status; |
| 24 | + fetch(this.$root + url, req) |
| 25 | + .then((response) => { |
| 26 | + status = response; |
| 27 | + const contentType = response.headers ? response.headers.get('Content-Type') || '' : ''; |
| 28 | + switch (contentType.split(';')[0]) { |
| 29 | + case 'application/json': |
| 30 | + case 'text/json': |
| 31 | + return response.json(); |
| 32 | + case 'text/plain': |
| 33 | + return response.text(); |
| 34 | + default: |
| 35 | + return response.blob(); |
| 36 | + } |
| 37 | + }) |
| 38 | + .then((data) => { |
| 39 | + if (!status.ok) { |
| 40 | + if (typeof (data) === 'object' && data.reason) { |
| 41 | + throw new Error(data.reason, data.code); |
| 42 | + } else { |
| 43 | + throw new Error(status.statusText, status.status); |
| 44 | + } |
| 45 | + } else if (typeof (data) === 'object' && Array.isArray(data)) { |
| 46 | + this.$array(data); |
| 47 | + } else { |
| 48 | + this.$object(data); |
| 49 | + } |
| 50 | + }) |
| 51 | + .then(() => { |
| 52 | + this.dispatchEvent(EVENT_COMPLETED, this, userInfo); |
| 53 | + }) |
| 54 | + .catch((error) => { |
| 55 | + if (error instanceof Error) { |
| 56 | + this.dispatchEvent(EVENT_ERROR, this, error, userInfo); |
36 | 57 | } else { |
37 | | - throw new Error(status.statusText, status.status); |
| 58 | + throw error; |
38 | 59 | } |
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 | | - }); |
| 60 | + }); |
58 | 61 | } |
59 | 62 | } |
0 commit comments