-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from micahbule/master
Changed request module to bent
- Loading branch information
Showing
3 changed files
with
74 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,6 @@ | |
"rollup-plugin-uglify": "^6.0.4" | ||
}, | ||
"dependencies": { | ||
"request": "^2.88.2" | ||
"bent": "^7.3.1" | ||
} | ||
} |
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,32 +1,60 @@ | ||
import request from 'request'; | ||
import bent from 'bent'; | ||
const PAYMONGO_API_URL = 'https://api.paymongo.com/v1'; | ||
|
||
export const makeRequest = async (opts) => { | ||
return new Promise((resolve, reject) => { | ||
const { secret, method, path, data } = opts; | ||
const options = { | ||
method, | ||
url: `${PAYMONGO_API_URL}/${path}`, | ||
json: true, | ||
auth: { | ||
username: secret, | ||
password: '', | ||
}, | ||
}; | ||
const { | ||
data, | ||
method, | ||
path, | ||
secret, | ||
} = opts; | ||
|
||
if (data) { | ||
options.body = data; | ||
} | ||
/** | ||
* Construct authorization header using secret key | ||
*/ | ||
const headers = { | ||
'Authorization': `Basic ${Buffer.from(secret).toString('base64')}`, | ||
}; | ||
|
||
/** | ||
* Construct the base request function | ||
*/ | ||
const request = bent(PAYMONGO_API_URL, (method || 'GET'), 'json', headers, 200); | ||
|
||
request(options, (err, res, body) => { | ||
if (err) reject(err); | ||
if (body.errors) { | ||
reject({ | ||
...body.errors[0], | ||
message: body.errors[0].detail, | ||
}); | ||
} | ||
resolve(body); | ||
/** | ||
* Require API endpoint option | ||
*/ | ||
if (!path) throw new Error('API endpoint required'); | ||
|
||
/** | ||
* Define endpoint-specific parameter array | ||
*/ | ||
const requestOptions = [ | ||
path, | ||
]; | ||
|
||
/** | ||
* For requests that requires payloads, push the payload | ||
* to the request parameter array | ||
*/ | ||
if (data) { | ||
requestOptions.push(data); | ||
} | ||
|
||
/** | ||
* Call endpoint and return promise returned | ||
*/ | ||
return request(...requestOptions) | ||
.catch(async err => { | ||
/** | ||
* Error-handling is based on the previous implementation | ||
*/ | ||
const { errors } = await err.json(); | ||
const [firstError] = errors; | ||
|
||
throw { | ||
...firstError, | ||
message: firstError.detail, | ||
}; | ||
}); | ||
}); | ||
}; |
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