decred.org/dcrdex@v1.0.3/client/webserver/site/src/js/http.ts (about) 1 /* 2 * requestJSON encodes the object and sends the JSON to the specified address. 3 */ 4 export async function requestJSON (method: string, addr: string, reqBody?: any): Promise<any> { 5 try { 6 const response = await window.fetch(addr, { 7 method: method, 8 headers: new window.Headers({ 'content-type': 'application/json' }), 9 // credentials: "same-origin", 10 body: reqBody 11 }) 12 if (response.status !== 200) { throw response } 13 const obj = await response.json() 14 obj.requestSuccessful = true 15 return obj 16 } catch (response) { 17 response.requestSuccessful = false 18 response.msg = await response.text() 19 return response 20 } 21 } 22 23 /* 24 * postJSON sends a POST request with JSON-formatted data and returns the 25 * response. 26 */ 27 export async function postJSON (addr: string, data?: any) { 28 return requestJSON('POST', addr, JSON.stringify(data)) 29 } 30 31 /* 32 * getJSON sends a GET request and returns the response. 33 */ 34 export async function getJSON (addr: string): Promise<any> { 35 return requestJSON('GET', addr) 36 } 37 38 export enum Errors { 39 walletErr, 40 walletAuthErr, 41 walletBalanceErr, 42 dupeDEXErr, 43 assetSupportErr, 44 registerErr, 45 signatureErr, 46 zeroFeeErr, 47 feeMismatchErr, 48 feeSendErr, 49 passwordErr, 50 emptyHostErr, 51 connectionErr, 52 acctKeyErr, 53 unknownOrderErr, 54 orderParamsErr, 55 dbErr, 56 authErr, 57 connectWalletErr, 58 missingWalletErr, 59 encryptionErr, 60 decodeErr, 61 accountVerificationErr, 62 accountProofErr, 63 parseKeyErr, 64 marketErr, 65 addressParseErr, 66 addrErr, 67 fileReadErr, 68 unknownDEXErr, 69 accountRetrieveErr, 70 accountDisableErr, 71 suspendedAcctErr, 72 existenceCheckErr, 73 createWalletErr, 74 activeOrdersErr, 75 newAddrErr, 76 }