decred.org/dcrdex@v1.0.5/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 noAuthErr, 42 walletBalanceErr, 43 dupeDEXErr, 44 assetSupportErr, 45 registerErr, 46 signatureErr, 47 zeroFeeErr, 48 feeMismatchErr, 49 feeSendErr, 50 passwordErr, 51 emptyHostErr, 52 connectionErr, 53 acctKeyErr, 54 unknownOrderErr, 55 orderParamsErr, 56 dbErr, 57 authErr, 58 connectWalletErr, 59 missingWalletErr, 60 encryptionErr, 61 decodeErr, 62 accountVerificationErr, 63 accountProofErr, 64 parseKeyErr, 65 marketErr, 66 addressParseErr, 67 addrErr, 68 fileReadErr, 69 unknownDEXErr, 70 accountRetrieveErr, 71 accountDisableErr, 72 suspendedAcctErr, 73 existenceCheckErr, 74 createWalletErr, 75 activeOrdersErr, 76 newAddrErr, 77 }