1 | 'use strict';
|
2 |
|
3 | const utils = require('./utils');
|
4 |
|
5 | function getRequestOpts(self, requestArgs, spec, overrideData) {
|
6 |
|
7 | const commandPath = utils.makeURLInterpolator(spec.path || '');
|
8 | const requestMethod = (spec.method || 'GET').toUpperCase();
|
9 | const urlParams = spec.urlParams || [];
|
10 | const encode = spec.encode || ((data) => data);
|
11 | const host = spec.host;
|
12 | const path = self.createResourcePathWithSymbols(spec.path);
|
13 |
|
14 |
|
15 | const args = [].slice.call(requestArgs);
|
16 |
|
17 |
|
18 | const urlData = urlParams.reduce((urlData, param) => {
|
19 | const arg = args.shift();
|
20 | if (typeof arg !== 'string') {
|
21 | throw new Error(
|
22 | `Stripe: Argument "${param}" must be a string, but got: ${arg} (on API request to \`${requestMethod} ${path}\`)`
|
23 | );
|
24 | }
|
25 |
|
26 | urlData[param] = arg;
|
27 | return urlData;
|
28 | }, {});
|
29 |
|
30 |
|
31 | const dataFromArgs = utils.getDataFromArgs(args);
|
32 | const data = encode(Object.assign({}, dataFromArgs, overrideData));
|
33 | const options = utils.getOptionsFromArgs(args);
|
34 |
|
35 |
|
36 | if (args.filter((x) => x != null).length) {
|
37 | throw new Error(
|
38 | `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}\`)`
|
39 | );
|
40 | }
|
41 |
|
42 | const requestPath = self.createFullPath(commandPath, urlData);
|
43 | const headers = Object.assign(options.headers, spec.headers);
|
44 |
|
45 | if (spec.validator) {
|
46 | spec.validator(data, {headers});
|
47 | }
|
48 |
|
49 | const dataInQuery = spec.method === 'GET' || spec.method === 'DELETE';
|
50 | const bodyData = dataInQuery ? {} : data;
|
51 | const queryData = dataInQuery ? data : {};
|
52 |
|
53 | return {
|
54 | requestMethod,
|
55 | requestPath,
|
56 | bodyData,
|
57 | queryData,
|
58 | auth: options.auth,
|
59 | headers,
|
60 | host,
|
61 | settings: options.settings,
|
62 | };
|
63 | }
|
64 |
|
65 | function makeRequest(self, requestArgs, spec, overrideData) {
|
66 | return new Promise((resolve, reject) => {
|
67 | try {
|
68 | var opts = getRequestOpts(self, requestArgs, spec, overrideData);
|
69 | } catch (err) {
|
70 | reject(err);
|
71 | return;
|
72 | }
|
73 |
|
74 | function requestCallback(err, response) {
|
75 | if (err) {
|
76 | reject(err);
|
77 | } else {
|
78 | resolve(
|
79 | spec.transformResponseData
|
80 | ? spec.transformResponseData(response)
|
81 | : response
|
82 | );
|
83 | }
|
84 | }
|
85 |
|
86 | const emptyQuery = Object.keys(opts.queryData).length === 0;
|
87 | const path = [
|
88 | opts.requestPath,
|
89 | emptyQuery ? '' : '?',
|
90 | utils.stringifyRequestData(opts.queryData),
|
91 | ].join('');
|
92 |
|
93 | const {headers, settings} = opts;
|
94 |
|
95 | self._request(
|
96 | opts.requestMethod,
|
97 | opts.host,
|
98 | path,
|
99 | opts.bodyData,
|
100 | opts.auth,
|
101 | {headers, settings},
|
102 | requestCallback
|
103 | );
|
104 | });
|
105 | }
|
106 |
|
107 | module.exports = makeRequest;
|