Skip to content

Commit

Permalink
Merge pull request #18 from micahbule/master
Browse files Browse the repository at this point in the history
Changed request module to bent
  • Loading branch information
jofftiquez authored Jan 5, 2021
2 parents 9d03a54 + dc9e8fb commit 82bf3d2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"rollup-plugin-uglify": "^6.0.4"
},
"dependencies": {
"request": "^2.88.2"
"bent": "^7.3.1"
}
}
78 changes: 53 additions & 25 deletions src/utils/rest.js
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,
};
});
});
};
21 changes: 20 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1893,6 +1893,15 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"

bent@^7.3.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/bent/-/bent-7.3.1.tgz#a4d2c83874d3f216baf5c78e7ab13335279ddb45"
integrity sha512-4nv/p7GaItr5FJ/CPiP0t3LZ1gETMaV+zXJU0X9tHiOuAPYZ8NA8ohIyugBMBV7q64X2OiDvBaqoQSUM2LuLjA==
dependencies:
bytesish "^0.4.1"
caseless "~0.12.0"
is-stream "^2.0.0"

binary-extensions@^1.0.0:
version "1.13.1"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-1.13.1.tgz#598afe54755b2868a5330d2aff9d4ebb53209b65"
Expand Down Expand Up @@ -1963,6 +1972,11 @@ buffer-from@^1.0.0:
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==

bytesish@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/bytesish/-/bytesish-0.4.1.tgz#5fe19b076037ffdfb63e083a53495b1d1c063f6f"
integrity sha512-j3l5QmnAbpOfcN/Z2Jcv4poQYfefs8rDdcbc6iEKm+OolvUXAE2APodpWj+DOzqX6Bl5Ys1cQkcIV2/doGvQxg==

cache-base@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/cache-base/-/cache-base-1.0.1.tgz#0a7f46416831c8b662ee36fe4e7c59d76f666ab2"
Expand Down Expand Up @@ -3149,6 +3163,11 @@ is-stream@^1.1.0:
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-1.1.0.tgz#12d4a3dd4e68e0b79ceb8dbc84173ae80d91ca44"
integrity sha1-EtSj3U5o4Lec6428hBc66A2RykQ=

is-stream@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/is-stream/-/is-stream-2.0.0.tgz#bde9c32680d6fae04129d6ac9d921ce7815f78e3"
integrity sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw==

is-symbol@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/is-symbol/-/is-symbol-1.0.3.tgz#38e1014b9e6329be0de9d24a414fd7441ec61937"
Expand Down Expand Up @@ -4465,7 +4484,7 @@ request-promise-native@^1.0.5:
stealthy-require "^1.1.1"
tough-cookie "^2.3.3"

request@^2.87.0, request@^2.88.2:
request@^2.87.0:
version "2.88.2"
resolved "https://registry.yarnpkg.com/request/-/request-2.88.2.tgz#d73c918731cb5a87da047e207234146f664d12b3"
integrity sha512-MsvtOrfG9ZcrOwAW+Qi+F6HbD0CWXEh9ou77uOb7FM2WPhwT7smM833PzanhJLsgXjN89Ir6V2PczXNnMpwKhw==
Expand Down

0 comments on commit 82bf3d2

Please sign in to comment.