From f650b77386508a343e70958bb4f2b1f9ee0471fa Mon Sep 17 00:00:00 2001 From: Alex Rattray Date: Thu, 9 May 2019 22:23:20 -0700 Subject: [PATCH] Refactor makeRequest --- lib/makeRequest.js | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/lib/makeRequest.js b/lib/makeRequest.js index 57f08d5268..c6b105b980 100644 --- a/lib/makeRequest.js +++ b/lib/makeRequest.js @@ -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); @@ -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}\`)` );