| 1 |
( function () { |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
const defaultOptions = { |
| 15 |
ajax: { |
| 16 |
url: mw.util.wikiScript( 'rest' ), |
| 17 |
timeout: 30 * 1000 |
| 18 |
} |
| 19 |
}; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
function objectKeysToLowerCase( headers ) { |
| 29 |
return Object.keys( headers || {} ).reduce( ( updatedHeaders, key ) => { |
| 30 |
updatedHeaders[ key.toLowerCase() ] = headers[ key ]; |
| 31 |
return updatedHeaders; |
| 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 |
mw.Rest = function ( options ) { |
| 65 |
const defaults = Object.assign( {}, options ); |
| 66 |
defaults.ajax = Object.assign( {}, defaultOptions.ajax, defaults.ajax ); |
| 67 |
|
| 68 |
this.url = defaults.ajax.url; |
| 69 |
delete defaults.ajax.url; |
| 70 |
|
| 71 |
this.defaults = defaults; |
| 72 |
this.requests = []; |
| 73 |
}; |
| 74 |
|
| 75 |
mw.Rest.prototype = { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
abort: function () { |
| 82 |
this.requests.forEach( ( request ) => { |
| 83 |
if ( request ) { |
| 84 |
request.abort(); |
| 85 |
} |
| 86 |
} ); |
| 87 |
}, |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
get: function ( path, query, headers ) { |
| 99 |
return this.ajax( path, { |
| 100 |
type: 'GET', |
| 101 |
data: query, |
| 102 |
headers: headers || {} |
| 103 |
} ); |
| 104 |
}, |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
post: function ( path, body, headers ) { |
| 118 |
if ( body === undefined ) { |
| 119 |
body = {}; |
| 120 |
} |
| 121 |
|
| 122 |
headers = objectKeysToLowerCase( headers ); |
| 123 |
return this.ajax( path, { |
| 124 |
type: 'POST', |
| 125 |
headers: Object.assign( headers, { 'content-type': 'application/json' } ), |
| 126 |
data: JSON.stringify( body ) |
| 127 |
} ); |
| 128 |
}, |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
put: function ( path, body, headers ) { |
| 142 |
headers = objectKeysToLowerCase( headers ); |
| 143 |
return this.ajax( path, { |
| 144 |
type: 'PUT', |
| 145 |
headers: Object.assign( headers, { 'content-type': 'application/json' } ), |
| 146 |
data: JSON.stringify( body ) |
| 147 |
} ); |
| 148 |
}, |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
delete: function ( path, body, headers ) { |
| 162 |
headers = objectKeysToLowerCase( headers ); |
| 163 |
return this.ajax( path, { |
| 164 |
type: 'DELETE', |
| 165 |
headers: Object.assign( headers, { 'content-type': 'application/json' } ), |
| 166 |
data: JSON.stringify( body ) |
| 167 |
} ); |
| 168 |
}, |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
ajax: function ( path, ajaxOptions ) { |
| 180 |
const self = this, |
| 181 |
apiDeferred = $.Deferred(); |
| 182 |
|
| 183 |
ajaxOptions = Object.assign( {}, this.defaults.ajax, ajaxOptions ); |
| 184 |
ajaxOptions.url = this.url + path; |
| 185 |
|
| 186 |
|
| 187 |
const xhr = $.ajax( ajaxOptions ); |
| 188 |
|
| 189 |
|
| 190 |
const requestIndex = this.requests.length; |
| 191 |
this.requests.push( xhr ); |
| 192 |
xhr.always( () => { |
| 193 |
self.requests[ requestIndex ] = null; |
| 194 |
} ); |
| 195 |
|
| 196 |
xhr.then( |
| 197 |
|
| 198 |
( result, textStatus, jqXHR ) => { |
| 199 |
apiDeferred.resolve( result, jqXHR ); |
| 200 |
}, |
| 201 |
|
| 202 |
|
| 203 |
( jqXHR, textStatus, exception ) => { |
| 204 |
apiDeferred.reject( 'http', { |
| 205 |
xhr: jqXHR, |
| 206 |
textStatus: textStatus, |
| 207 |
exception: exception |
| 208 |
} ); |
| 209 |
} |
| 210 |
); |
| 211 |
|
| 212 |
|
| 213 |
return apiDeferred.promise( { abort: xhr.abort } ); |
| 214 |
} |
| 215 |
}; |
| 216 |
}() ); |