Guest User

Untitled

a guest
Feb 8th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import * as request from 'request'
  2.  
  3. const Client = {
  4. username: process.env.NODEUSERNAME,
  5. password: process.env.NODEPASSWORD
  6. }
  7.  
  8. interface ClientRequestResponse<R = any> {
  9. "result": R,
  10. "error": string,
  11. "id": string
  12. }
  13.  
  14. export function clientRequest<R = any>(
  15. method: string,
  16. params: string | Object | null
  17. ):Promise<ClientRequestResponse<R>>{
  18. return new Promise(async (resolve, reject) => {
  19. if(!process.env.NODEHOST){
  20. return reject({fail: true, error: 'NODEHOST is not set on .env'})
  21. }
  22. const headers = {
  23. 'content-type': 'application/json;'
  24. }
  25. const dataToSend = {"jsonrpc": 1.0, "id":"curltest", "method": method, "params": params}
  26.  
  27. const dataString = JSON.stringify(dataToSend)
  28.  
  29. const options = {
  30. url: process.env.NODEHOST,
  31. method: 'POST',
  32. headers: headers,
  33. body: dataString,
  34. auth: {
  35. 'user': Client.username,
  36. 'pass': Client.password
  37. }
  38. }
  39. await request(options, (error, response, body)=>{
  40. if(error) return reject({fail: true, error})
  41. resolve(JSON.parse(body))
  42. })
  43.  
  44. })
  45. }
Add Comment
Please, Sign In to add comment