Skip to content

Commit

Permalink
Migrate stripe, StripeResource, StripeMethod, autoPagination, makeReq…
Browse files Browse the repository at this point in the history
…uest, and Error
  • Loading branch information
anniel-stripe committed Sep 13, 2022
1 parent dcba64a commit cec1ab3
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 36 deletions.
54 changes: 53 additions & 1 deletion src/Error.js → src/Error.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
'use strict';

type RawErrorType =
| 'card_error'
| 'invalid_request_error'
| 'api_error'
| 'idempotency_error'
| 'rate_limit_error'
| 'authentication_error'
| 'invalid_grant';

type StripeRawError = {
message?: string;
type?: RawErrorType;

headers?: {[header: string]: string};
statusCode?: number;
requestId?: string;
code?: string;
doc_url?: string;
decline_code?: string;
param?: string;
detail?: string;
charge?: string;
payment_method_type?: string;

payment_intent?: any;
payment_method?: any;
setup_intent?: any;
source?: any;
exception?: any;
};

/**
* StripeError is the base error from which all other more specific Stripe errors derive.
* Specifically for errors returned from Stripe's REST API.
*/
class StripeError extends Error {
constructor(raw = {}) {
readonly message: string;
readonly type: string;
readonly raw: unknown;
readonly rawType: RawErrorType;
readonly headers: {[header: string]: string};
readonly requestId: string;

readonly code?: string;
readonly doc_url?: string;
readonly param?: string;
readonly detail?: string;
readonly statusCode?: number;
readonly charge?: string;
readonly decline_code?: string;
readonly payment_method_type?: string;

readonly payment_intent?: any;
readonly payment_method?: any;
readonly setup_intent?: any;
readonly source?: any;

constructor(raw: StripeRawError) {
super(raw.message);
this.type = this.constructor.name;

Expand Down
8 changes: 3 additions & 5 deletions src/StripeMethod.js → src/StripeMethod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const utils = require('./utils');
const makeRequest = require('./makeRequest');
import * as utils from './utils';
import makeRequest = require('./makeRequest');
const makeAutoPaginationMethods = require('./autoPagination')
.makeAutoPaginationMethods;

Expand Down Expand Up @@ -55,4 +53,4 @@ function stripeMethod(spec) {
};
}

module.exports = stripeMethod;
export = stripeMethod;
33 changes: 21 additions & 12 deletions src/StripeResource.js → src/StripeResource.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
'use strict';

const utils = require('./utils');
import * as utils from './utils';
const {
StripeConnectionError,
StripeAPIError,
StripeAuthenticationError,
StripeConnectionError,
StripeError,
StripePermissionError,
StripeRateLimitError,
StripeError,
StripeAPIError,
} = require('./Error');

const {HttpClient} = require('./net/HttpClient');

type Settings = {
timeout?: number;
};

type Options = {
settings?: Settings;
streaming?: boolean;
headers?: Record<string, string>;
};

// Provide extension mechanism for Stripe Resource Sub-Classes
StripeResource.extend = utils.protoExtend;

Expand Down Expand Up @@ -113,7 +121,7 @@ StripeResource.prototype = {
_timeoutHandler(timeout, req, callback) {
return () => {
const timeoutErr = new TypeError('ETIMEDOUT');
timeoutErr.code = 'ETIMEDOUT';
(timeoutErr as any).code = 'ETIMEDOUT';

req.destroy(timeoutErr);
};
Expand Down Expand Up @@ -283,7 +291,7 @@ StripeResource.prototype = {
this,
new StripeConnectionError({
message: this._generateConnectionErrorMessage(requestRetries),
detail: error,
detail,
}),
null
);
Expand Down Expand Up @@ -364,7 +372,7 @@ StripeResource.prototype = {
},

// Max retries can be set on a per request basis. Favor those over the global setting
_getMaxNetworkRetries(settings = {}) {
_getMaxNetworkRetries(settings: {maxNetworkRetries?: number} = {}) {
return settings.maxNetworkRetries &&
Number.isInteger(settings.maxNetworkRetries)
? settings.maxNetworkRetries
Expand Down Expand Up @@ -480,7 +488,7 @@ StripeResource.prototype = {
}
},

_request(method, host, path, data, auth, options = {}, callback) {
_request(method, host, path, data, auth, options: Options = {}, callback) {
let requestData;

const retryRequest = (
Expand All @@ -503,6 +511,7 @@ StripeResource.prototype = {
// timeout can be set on a per-request basis. Favor that over the global setting
const timeout =
options.settings &&
options.settings.timeout &&
Number.isInteger(options.settings.timeout) &&
options.settings.timeout >= 0
? options.settings.timeout
Expand Down Expand Up @@ -599,7 +608,7 @@ StripeResource.prototype = {
options.settings
);

makeRequest(apiVersion, headers);
makeRequest(apiVersion, headers, 0);
});
};

Expand All @@ -616,4 +625,4 @@ StripeResource.prototype = {
},
};

module.exports = StripeResource;
export = StripeResource;
10 changes: 5 additions & 5 deletions src/autoPagination.js → src/autoPagination.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const makeRequest = require('./makeRequest');
const utils = require('./utils');
const utils_1 = require('./utils');

function makeAutoPaginationMethods(self, requestArgs, spec, firstPagePromise) {
const promiseCache = {currentPromise: null};
Expand Down Expand Up @@ -207,7 +207,7 @@ function makeAutoPagingEach(asyncIteratorNext) {
asyncIteratorNext,
onItem
);
return utils.callbackifyPromiseWithTimeout(autoPagePromise, onDone);
return utils_1.callbackifyPromiseWithTimeout(autoPagePromise, onDone);
};
}

Expand Down Expand Up @@ -237,12 +237,12 @@ function makeAutoPagingToArray(autoPagingEach) {
})
.catch(reject);
});
return utils.callbackifyPromiseWithTimeout(promise, onDone);
return utils_1.callbackifyPromiseWithTimeout(promise, onDone);
};
}

function wrapAsyncIteratorWithCallback(asyncIteratorNext, onItem) {
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
function handleIteration(iterResult) {
if (iterResult.done) {
resolve();
Expand Down Expand Up @@ -272,7 +272,7 @@ function wrapAsyncIteratorWithCallback(asyncIteratorNext, onItem) {

function isReverseIteration(requestArgs) {
const args = [].slice.call(requestArgs);
const dataFromArgs = utils.getDataFromArgs(args);
const dataFromArgs = utils_1.getDataFromArgs(args);

return !!dataFromArgs.ending_before;
}
6 changes: 2 additions & 4 deletions src/makeRequest.js → src/makeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const utils = require('./utils');
import * as utils from './utils';

function getRequestOpts(self, requestArgs, spec, overrideData) {
// Extract spec values with defaults.
Expand Down Expand Up @@ -119,4 +117,4 @@ function makeRequest(self, requestArgs, spec, overrideData) {
});
}

module.exports = makeRequest;
export = makeRequest;
21 changes: 12 additions & 9 deletions src/stripe.js → src/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Stripe.CryptoProvider = CryptoProvider;

function Stripe(key, config = {}) {
if (!(this instanceof Stripe)) {
return new Stripe(key, config);
return new (Stripe as any)(key, config);
}

const props = this._getPropsFromConfig(config);
Expand Down Expand Up @@ -326,15 +326,18 @@ Stripe.prototype = {

info = info || {};

const appInfo = APP_INFO_PROPERTIES.reduce((accum, prop) => {
if (typeof info[prop] == 'string') {
accum = accum || {};
const appInfo = APP_INFO_PROPERTIES.reduce(
(accum: Record<string, any>, prop) => {
if (typeof info[prop] == 'string') {
accum = accum || {};

accum[prop] = info[prop];
}
accum[prop] = info[prop];
}

return accum;
}, undefined);
return accum;
},
undefined
);

this._appInfo = appInfo;
},
Expand Down Expand Up @@ -483,7 +486,7 @@ Stripe.prototype = {
*/
getClientUserAgentSeeded(seed, cb) {
this.getUname((uname) => {
const userAgent = {};
const userAgent: any = {};
for (const field in seed) {
userAgent[field] = encodeURIComponent(seed[field]);
}
Expand Down

0 comments on commit cec1ab3

Please sign in to comment.