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 b523144
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 70 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ module.exports = {
'@typescript-eslint/ban-ts-ignore': 0,
'@typescript-eslint/no-empty-function': 0,
'@typescript-eslint/camelcase': 0,
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'prefer-rest-params': 'off',
Expand Down
92 changes: 65 additions & 27 deletions 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 = {}) {
export class StripeError extends Error {
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 Expand Up @@ -60,83 +112,69 @@ class StripeError extends Error {
* CardError is raised when a user enters a card that can't be charged for
* some reason.
*/
class StripeCardError extends StripeError {}
export class StripeCardError extends StripeError {}

/**
* InvalidRequestError is raised when a request is initiated with invalid
* parameters.
*/
class StripeInvalidRequestError extends StripeError {}
export class StripeInvalidRequestError extends StripeError {}

/**
* APIError is a generic error that may be raised in cases where none of the
* other named errors cover the problem. It could also be raised in the case
* that a new error has been introduced in the API, but this version of the
* Node.JS SDK doesn't know how to handle it.
*/
class StripeAPIError extends StripeError {}
export class StripeAPIError extends StripeError {}

/**
* AuthenticationError is raised when invalid credentials are used to connect
* to Stripe's servers.
*/
class StripeAuthenticationError extends StripeError {}
export class StripeAuthenticationError extends StripeError {}

/**
* PermissionError is raised in cases where access was attempted on a resource
* that wasn't allowed.
*/
class StripePermissionError extends StripeError {}
export class StripePermissionError extends StripeError {}

/**
* RateLimitError is raised in cases where an account is putting too much load
* on Stripe's API servers (usually by performing too many requests). Please
* back off on request rate.
*/
class StripeRateLimitError extends StripeError {}
export class StripeRateLimitError extends StripeError {}

/**
* StripeConnectionError is raised in the event that the SDK can't connect to
* Stripe's servers. That can be for a variety of different reasons from a
* downed network to a bad TLS certificate.
*/
class StripeConnectionError extends StripeError {}
export class StripeConnectionError extends StripeError {}

/**
* SignatureVerificationError is raised when the signature verification for a
* webhook fails
*/
class StripeSignatureVerificationError extends StripeError {}
export class StripeSignatureVerificationError extends StripeError {}

/**
* IdempotencyError is raised in cases where an idempotency key was used
* improperly.
*/
class StripeIdempotencyError extends StripeError {}
export class StripeIdempotencyError extends StripeError {}

/**
* InvalidGrantError is raised when a specified code doesn't exist, is
* expired, has been used, or doesn't belong to you; a refresh token doesn't
* exist, or doesn't belong to you; or if an API key's mode (live or test)
* doesn't match the mode of a code or refresh token.
*/
class StripeInvalidGrantError extends StripeError {}
export class StripeInvalidGrantError extends StripeError {}

/**
* Any other error from Stripe not specifically captured above
*/
class StripeUnknownError extends StripeError {}

module.exports.generate = StripeError.generate;
module.exports.StripeError = StripeError;
module.exports.StripeCardError = StripeCardError;
module.exports.StripeInvalidRequestError = StripeInvalidRequestError;
module.exports.StripeAPIError = StripeAPIError;
module.exports.StripeAuthenticationError = StripeAuthenticationError;
module.exports.StripePermissionError = StripePermissionError;
module.exports.StripeRateLimitError = StripeRateLimitError;
module.exports.StripeConnectionError = StripeConnectionError;
module.exports.StripeSignatureVerificationError = StripeSignatureVerificationError;
module.exports.StripeIdempotencyError = StripeIdempotencyError;
module.exports.StripeInvalidGrantError = StripeInvalidGrantError;
module.exports.StripeUnknownError = StripeUnknownError;
export class StripeUnknownError extends StripeError {}
9 changes: 4 additions & 5 deletions src/StripeMethod.js → src/StripeMethod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict';

const utils = require('./utils');
const makeRequest = require('./makeRequest');
const makeAutoPaginationMethods = require('./autoPagination')
.makeAutoPaginationMethods;
import utils = require('./utils');
import makeRequest = require('./makeRequest');
import {makeAutoPaginationMethods} from './autoPagination';

/**
* Create an API method from the declared spec.
Expand Down Expand Up @@ -55,4 +54,4 @@ function stripeMethod(spec) {
};
}

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

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

import {HttpClient} from './net/HttpClient';

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 +123,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 +293,7 @@ StripeResource.prototype = {
this,
new StripeConnectionError({
message: this._generateConnectionErrorMessage(requestRetries),
detail: error,
detail,
}),
null
);
Expand Down Expand Up @@ -364,7 +374,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 +490,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 +513,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 +610,7 @@ StripeResource.prototype = {
options.settings
);

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

Expand Down
15 changes: 9 additions & 6 deletions src/autoPagination.js → src/autoPagination.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict';

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

function makeAutoPaginationMethods(self, requestArgs, spec, firstPagePromise) {
export function makeAutoPaginationMethods(
self,
requestArgs,
spec,
firstPagePromise
) {
const promiseCache = {currentPromise: null};
const reverseIteration = isReverseIteration(requestArgs);
let pagePromise = firstPagePromise;
Expand Down Expand Up @@ -94,8 +99,6 @@ function makeAutoPaginationMethods(self, requestArgs, spec, firstPagePromise) {
return autoPaginationMethods;
}

module.exports.makeAutoPaginationMethods = makeAutoPaginationMethods;

/**
* ----------------
* Private Helpers:
Expand Down Expand Up @@ -242,7 +245,7 @@ function makeAutoPagingToArray(autoPagingEach) {
}

function wrapAsyncIteratorWithCallback(asyncIteratorNext, onItem) {
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
function handleIteration(iterResult) {
if (iterResult.done) {
resolve();
Expand Down
4 changes: 2 additions & 2 deletions src/makeRequest.js → src/makeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const utils = require('./utils');
import utils = require('./utils');

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

module.exports = makeRequest;
export = makeRequest;
Loading

0 comments on commit b523144

Please sign in to comment.