generated from MuiseDestiny/zotero-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 124
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
00bee9a
commit ba878c1
Showing
6 changed files
with
61 additions
and
115 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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
export default class Requests { | ||
/** | ||
* Record api response | ||
*/ | ||
public cache: { [key: string]: any } = {} | ||
|
||
async get(url: string, responseType: string = "json", headers: object = {}) { | ||
const k = JSON.stringify(arguments) | ||
if (this.cache[k]) { | ||
return this.cache[k] | ||
} | ||
let res = await Zotero.HTTP.request( | ||
"GET", | ||
url, | ||
{ | ||
responseType: responseType, | ||
headers, | ||
credentials: "include" | ||
} | ||
) | ||
if (res.status == 200) { | ||
this.cache[k] = res.response | ||
return res.response | ||
} else { | ||
console.log(`get ${url} error`, res) | ||
} | ||
} | ||
|
||
async post(url: string, body: object = {}, responseType: string = "json") { | ||
const k = JSON.stringify(arguments) | ||
if (this.cache[k]) { | ||
return this.cache[k] | ||
} | ||
let res = await Zotero.HTTP.request( | ||
"POST", | ||
url, | ||
Object.assign({ | ||
responseType: responseType, | ||
}, (Object.keys(body).length > 0 ? { | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify(body), | ||
// credentials: "include" | ||
} : {})) | ||
) | ||
if (res.status == 200) { | ||
this.cache[k] = res.response | ||
return res.response | ||
} else { | ||
// window.alert("error" + res.status) | ||
console.log(`post ${url} error`, res) | ||
} | ||
} | ||
} |
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