This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
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
d57e81b
commit c73cfc9
Showing
2 changed files
with
40 additions
and
33 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 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 |
---|---|---|
@@ -1,53 +1,59 @@ | ||
import request from 'request' | ||
import request from 'request'; | ||
import { assembleError, tryJson } from './Utils'; | ||
|
||
export function rawRequest(params) { | ||
return new Promise((resolve, reject) => { | ||
request(params, (error, response, body) => { | ||
error = assembleError({error, response}) | ||
error = assembleError({ error, response }); | ||
error && reject(error); | ||
|
||
console.log(`✓ Request succeeded.`); | ||
console.log(`Server responded with: \n${JSON.stringify(response, null, 2)}`); | ||
console.log( | ||
`Server responded with: \n${JSON.stringify(response, null, 2)}` | ||
); | ||
const resp = tryJson(body); | ||
resolve(resp); | ||
}) | ||
}) | ||
}); | ||
}); | ||
} | ||
|
||
export function req( method, params ) { | ||
export function req(method, params) { | ||
const { url, headers, body, auth, query, rest } = params; | ||
return new Promise((resolve, reject) => { | ||
const j = request.jar(); | ||
request ({ | ||
url, | ||
headers, | ||
auth, | ||
qs: query, | ||
method: method, | ||
json: body, | ||
jar: j, | ||
...rest | ||
}, function(error, response, body){ | ||
error = assembleError({error, response}) | ||
if(error) { | ||
reject(error); | ||
} else { | ||
console.log(`✓ ${method} succeeded.`); | ||
console.log(`Server responded with: \n${JSON.stringify(response, null, 2)}`); | ||
const resp = tryJson(body); | ||
if(rest.keepCookie) { | ||
const __cookie = j.getCookieString(url); | ||
resolve({ | ||
__cookie, | ||
...resp | ||
}); | ||
request( | ||
{ | ||
url, | ||
headers, | ||
auth, | ||
qs: query, | ||
method: method, | ||
json: body, | ||
formData: formData, | ||
jar: j, | ||
...rest, | ||
}, | ||
function(error, response, body) { | ||
error = assembleError({ error, response }); | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve( | ||
resp | ||
console.log(`✓ ${method} succeeded.`); | ||
console.log( | ||
`Server responded with: \n${JSON.stringify(response, null, 2)}` | ||
); | ||
const resp = tryJson(body); | ||
if (rest.keepCookie) { | ||
const __cookie = j.getCookieString(url); | ||
resolve({ | ||
__cookie, | ||
...resp, | ||
}); | ||
} else { | ||
resolve(resp); | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
); | ||
}); | ||
} |