forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5d3116
commit 7bc911e
Showing
18 changed files
with
680 additions
and
851 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,6 @@ | |
"flow-vars/use-flow-type": 1 | ||
}, | ||
"globals": { | ||
"angular": false | ||
}, | ||
"env": { | ||
"browser": true, | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* @flow */ | ||
|
||
import querystring from "querystring"; | ||
|
||
import EventEmitter from "events"; | ||
|
||
let events = new EventEmitter(); | ||
|
||
function makeMethod(method: string, hasBody: boolean = false) { | ||
return function( | ||
urlTemplate: string, | ||
params: { [key:string]: any } = {} | ||
) { | ||
return function( | ||
data: { [key:string]: any }, | ||
options: { [key:string]: any } = {} | ||
): Promise<any> { | ||
let url = urlTemplate; | ||
data = { ...data }; | ||
for (let tag of (url.match(/:\w+/g) || [])) { | ||
let value = data[tag.slice(1)]; | ||
if (value === undefined) { | ||
console.warn("Warning: calling", method, "without", tag); | ||
value = ""; | ||
} | ||
url = url.replace(tag, encodeURIComponent(data[tag.slice(1)])) | ||
delete data[tag.slice(1)]; | ||
} | ||
|
||
let headers: { [key:string]: string } = { | ||
"Accept": "application/json", | ||
}; | ||
|
||
let body; | ||
if (hasBody) { | ||
headers["Content-Type"] = "application/json"; | ||
body = JSON.stringify(data); | ||
} else { | ||
let qs = querystring.stringify(data); | ||
if (qs) { | ||
url += (url.indexOf("?") >= 0 ? "&" : "?") + qs; | ||
} | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
let xhr = new XMLHttpRequest(); | ||
xhr.open(method, url); | ||
for (let headerName in headers) { | ||
xhr.setRequestHeader(headerName, headers[headerName]) | ||
} | ||
xhr.onreadystatechange = function() { | ||
// $FlowFixMe | ||
if (xhr.readyState === XMLHttpRequest.DONE) { | ||
let body = xhr.responseText; | ||
try { body = JSON.parse(body); } catch (e) {} | ||
if (xhr.status >= 200 && xhr.status <= 299) { | ||
resolve(body); | ||
} else { | ||
reject({ | ||
status: xhr.status, | ||
data: body | ||
}); | ||
} | ||
events.emit(xhr.status, url); | ||
} | ||
} | ||
xhr.send(body); | ||
|
||
if (options.cancelled) { | ||
options.cancelled.then(() => xhr.abort()); | ||
} | ||
}) | ||
|
||
} | ||
} | ||
} | ||
|
||
export const GET = makeMethod("GET"); | ||
export const DELETE = makeMethod("DELETE"); | ||
export const POST = makeMethod("POST", true); | ||
export const PUT = makeMethod("PUT", true); | ||
|
||
export default events; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.