File tree Expand file tree Collapse file tree 2 files changed +52
-2
lines changed Expand file tree Collapse file tree 2 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 1- # Logs
2- logs
31* .log
42npm-debug.log *
53yarn-debug.log *
Original file line number Diff line number Diff line change 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+
314export 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}
You can’t perform that action at this time.
0 commit comments