UNPKG

1.62 kBJavaScriptView Raw
1'use strict';
2
3const utils = require('./utils');
4const makeRequest = require('./makeRequest');
5const makeAutoPaginationMethods = require('./autoPagination')
6 .makeAutoPaginationMethods;
7
8/**
9 * Create an API method from the declared spec.
10 *
11 * @param [spec.method='GET'] Request Method (POST, GET, DELETE, PUT)
12 * @param [spec.path=''] Path to be appended to the API BASE_PATH, joined with
13 * the instance's path (e.g. 'charges' or 'customers')
14 * @param [spec.urlParams=[]] Array of required arguments in the order that they
15 * must be passed by the consumer of the API. Subsequent optional arguments are
16 * optionally passed through a hash (Object) as the penultimate argument
17 * (preceding the also-optional callback argument
18 * @param [spec.encode] Function for mutating input parameters to a method.
19 * Usefully for applying transforms to data on a per-method basis.
20 * @param [spec.host] Hostname for the request.
21 */
22function stripeMethod(spec) {
23 return function(...args) {
24 const callback = typeof args[args.length - 1] == 'function' && args.pop();
25
26 spec.urlParams = utils.extractUrlParams(
27 this.createResourcePathWithSymbols(spec.path || '')
28 );
29
30 const requestPromise = utils.callbackifyPromiseWithTimeout(
31 makeRequest(this, args, spec, {}),
32 callback
33 );
34
35 if (spec.methodType === 'list') {
36 const autoPaginationMethods = makeAutoPaginationMethods(
37 this,
38 args,
39 spec,
40 requestPromise
41 );
42 Object.assign(requestPromise, autoPaginationMethods);
43 }
44
45 return requestPromise;
46 };
47}
48
49module.exports = stripeMethod;