Skip to content

Commit

Permalink
Refactor makeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
rattrayalex-stripe committed May 10, 2019
1 parent 71d5200 commit f650b77
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions lib/makeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,23 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
const urlParams = spec.urlParams || [];
const encode = spec.encode || ((data) => data);
const host = spec.host;
const path = self.createResourcePathWithSymbols(spec.path);

// Don't mutate args externally.
const args = [].slice.call(requestArgs);

// Generate and validate url params.
const urlData = {};
for (let i = 0, l = urlParams.length; i < l; ++i) {
var path;

// Note that we shift the args array after every iteration so this just
// grabs the "next" argument for use as a URL parameter.
const arg = args[0];

const param = urlParams[i];

if (param == 'id' && typeof arg !== 'string') {
path = self.createResourcePathWithSymbols(spec.path);
throw new Error(
`Stripe: "id" must be a string, but got: ${typeof arg} (on API request to \`${requestMethod} ${path}\`)`
);
}

if (!arg) {
path = self.createResourcePathWithSymbols(spec.path);
const urlData = urlParams.reduce((urlData, param) => {
const arg = args.shift();
if (typeof arg !== 'string') {
throw new Error(
`Stripe: Argument "${
urlParams[i]
}" required, but got: ${arg} (on API request to \`${requestMethod} ${path}\`)`
`Stripe: Argument "${param}" must be a string, but got: ${arg} (on API request to \`${requestMethod} ${path}\`)`
);
}

urlData[param] = args.shift();
}
urlData[param] = arg;
return urlData;
}, {});

// Pull request data and options (headers, auth) from args.
const dataFromArgs = utils.getDataFromArgs(args);
Expand All @@ -50,7 +34,6 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {

// Validate that there are no more args.
if (args.length) {
path = self.createResourcePathWithSymbols(spec.path);
throw new Error(
`Stripe: Unknown arguments (${args}). Did you mean to pass an options object? See https://github.com/stripe/stripe-node/wiki/Passing-Options. (on API request to ${requestMethod} \`${path}\`)`
);
Expand Down

0 comments on commit f650b77

Please sign in to comment.