From abd9eea75723530a88ab8762afa700b19019698c Mon Sep 17 00:00:00 2001 From: Annie Li Date: Thu, 1 Sep 2022 13:49:18 -0700 Subject: [PATCH 1/3] Migrate StripeMethod to Typescript --- src/{StripeMethod.js => StripeMethod.ts} | 2 +- src/StripeResource.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/{StripeMethod.js => StripeMethod.ts} (98%) diff --git a/src/StripeMethod.js b/src/StripeMethod.ts similarity index 98% rename from src/StripeMethod.js rename to src/StripeMethod.ts index 78018da23a..5cee634e68 100644 --- a/src/StripeMethod.js +++ b/src/StripeMethod.ts @@ -1,6 +1,6 @@ 'use strict'; -const utils = require('./utils'); +import * as utils from './utils'; const makeRequest = require('./makeRequest'); const makeAutoPaginationMethods = require('./autoPagination') .makeAutoPaginationMethods; diff --git a/src/StripeResource.ts b/src/StripeResource.ts index 5f54991289..06d9d1945f 100644 --- a/src/StripeResource.ts +++ b/src/StripeResource.ts @@ -1,6 +1,6 @@ 'use strict'; -const utils = require('./utils'); +import * as utils from './utils'; const { StripeConnectionError, StripeAuthenticationError, From 796d947243dfa3a3683f0f1ba439145ba7cc697d Mon Sep 17 00:00:00 2001 From: Annie Li Date: Fri, 2 Sep 2022 09:52:47 -0700 Subject: [PATCH 2/3] Migrate autoPagination, makeRequest, stripe, StripeMethod to TS. Adjust eslint and tsconfig --- .eslintrc.js | 1 - src/StripeMethod.ts | 9 ++--- src/StripeResource.ts | 16 ++++---- src/{autoPagination.js => autoPagination.ts} | 15 +++++--- src/{makeRequest.js => makeRequest.ts} | 4 +- src/{stripe.js => stripe.ts} | 39 +++++++++++--------- tsconfig.json | 1 + 7 files changed, 46 insertions(+), 39 deletions(-) rename src/{autoPagination.js => autoPagination.ts} (96%) rename src/{makeRequest.js => makeRequest.ts} (98%) rename src/{stripe.js => stripe.ts} (94%) diff --git a/.eslintrc.js b/.eslintrc.js index e32873c89e..0f3c0a64f4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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', diff --git a/src/StripeMethod.ts b/src/StripeMethod.ts index 5cee634e68..3102ff95a9 100644 --- a/src/StripeMethod.ts +++ b/src/StripeMethod.ts @@ -1,9 +1,8 @@ 'use strict'; -import * as utils from './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. @@ -55,4 +54,4 @@ function stripeMethod(spec) { }; } -module.exports = stripeMethod; +export = stripeMethod; diff --git a/src/StripeResource.ts b/src/StripeResource.ts index 06d9d1945f..4cdfd0f6b8 100644 --- a/src/StripeResource.ts +++ b/src/StripeResource.ts @@ -1,16 +1,16 @@ 'use strict'; -import * as utils from './utils'; -const { - StripeConnectionError, +import utils = require('./utils'); +import { + StripeAPIError, StripeAuthenticationError, + StripeConnectionError, + StripeError, StripePermissionError, StripeRateLimitError, - StripeError, - StripeAPIError, -} = require('./Error'); +} from './Error'; -const {HttpClient} = require('./net/HttpClient'); +import {HttpClient} from './net/HttpClient'; type Settings = { timeout?: number; @@ -627,4 +627,4 @@ StripeResource.prototype = { }, }; -module.exports = StripeResource; +export = StripeResource; diff --git a/src/autoPagination.js b/src/autoPagination.ts similarity index 96% rename from src/autoPagination.js rename to src/autoPagination.ts index fe6f1f15b6..e0530606a1 100644 --- a/src/autoPagination.js +++ b/src/autoPagination.ts @@ -1,9 +1,14 @@ 'use strict'; -const makeRequest = require('./makeRequest'); -const utils = require('./utils'); +import * as utils from './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; @@ -94,8 +99,6 @@ function makeAutoPaginationMethods(self, requestArgs, spec, firstPagePromise) { return autoPaginationMethods; } -module.exports.makeAutoPaginationMethods = makeAutoPaginationMethods; - /** * ---------------- * Private Helpers: @@ -242,7 +245,7 @@ function makeAutoPagingToArray(autoPagingEach) { } function wrapAsyncIteratorWithCallback(asyncIteratorNext, onItem) { - return new Promise((resolve, reject) => { + return new Promise((resolve, reject) => { function handleIteration(iterResult) { if (iterResult.done) { resolve(); diff --git a/src/makeRequest.js b/src/makeRequest.ts similarity index 98% rename from src/makeRequest.js rename to src/makeRequest.ts index 7ea0737b73..b37d106ce5 100644 --- a/src/makeRequest.js +++ b/src/makeRequest.ts @@ -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. @@ -119,4 +119,4 @@ function makeRequest(self, requestArgs, spec, overrideData) { }); } -module.exports = makeRequest; +export = makeRequest; diff --git a/src/stripe.js b/src/stripe.ts similarity index 94% rename from src/stripe.js rename to src/stripe.ts index f928ee5a51..c0a6253c66 100644 --- a/src/stripe.js +++ b/src/stripe.ts @@ -1,6 +1,15 @@ 'use strict'; -const resources = require('./resources'); +import resources = require('./resources'); +import utils = require('./utils'); + +import {HttpClient, HttpClientResponse} from './net/HttpClient'; +import {FetchHttpClient} from './net/FetchHttpClient'; +import {NodeHttpClient} from './net/NodeHttpClient'; + +import CryptoProvider = require('./crypto/CryptoProvider'); +import NodeCryptoProvider = require('./crypto/NodeCryptoProvider'); +import SubtleCryptoProvider = require('./crypto/SubtleCryptoProvider'); const DEFAULT_HOST = 'api.stripe.com'; const DEFAULT_PORT = '443'; @@ -11,7 +20,6 @@ const DEFAULT_TIMEOUT = 80000; Stripe.PACKAGE_VERSION = require('../package.json').version; -const utils = require('./utils'); const {determineProcessUserAgentProperties, emitWarning} = utils; Stripe.USER_AGENT = { @@ -50,16 +58,14 @@ const EventEmitter = require('events').EventEmitter; Stripe.StripeResource = require('./StripeResource'); Stripe.resources = resources; -const {HttpClient, HttpClientResponse} = require('./net/HttpClient'); Stripe.HttpClient = HttpClient; Stripe.HttpClientResponse = HttpClientResponse; -const CryptoProvider = require('./crypto/CryptoProvider'); 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); @@ -138,7 +144,6 @@ Stripe.errors = require('./Error'); Stripe.webhooks = require('./Webhooks'); Stripe.createNodeHttpClient = (agent) => { - const {NodeHttpClient} = require('./net/NodeHttpClient'); return new NodeHttpClient(agent); }; @@ -150,7 +155,6 @@ Stripe.createNodeHttpClient = (agent) => { * passed, will default to the default `fetch` function in the global scope. */ Stripe.createFetchHttpClient = (fetchFn) => { - const {FetchHttpClient} = require('./net/FetchHttpClient'); return new FetchHttpClient(fetchFn); }; @@ -159,7 +163,6 @@ Stripe.createFetchHttpClient = (fetchFn) => { * its crypto operations. */ Stripe.createNodeCryptoProvider = () => { - const NodeCryptoProvider = require('./crypto/NodeCryptoProvider'); return new NodeCryptoProvider(); }; @@ -172,7 +175,6 @@ Stripe.createNodeCryptoProvider = () => { * scope. */ Stripe.createSubtleCryptoProvider = (subtleCrypto) => { - const SubtleCryptoProvider = require('./crypto/SubtleCryptoProvider'); return new SubtleCryptoProvider(subtleCrypto); }; @@ -326,15 +328,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, 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; }, @@ -483,7 +488,7 @@ Stripe.prototype = { */ getClientUserAgentSeeded(seed, cb) { this.getUname((uname) => { - const userAgent = {}; + const userAgent: any = {}; for (const field in seed) { userAgent[field] = encodeURIComponent(seed[field]); } diff --git a/tsconfig.json b/tsconfig.json index 50bb8a13ae..96e742a5a0 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "outDir": "./lib", "allowJs": true, "target": "es6", + "module": "commonjs", "checkJs": false, }, "include": ["./src/**/*"] From aa07eb4b4081958c10e72bb9c4c4c12917a23589 Mon Sep 17 00:00:00 2001 From: Annie Li Date: Fri, 2 Sep 2022 16:12:10 -0700 Subject: [PATCH 3/3] Migrate Error, update import/export in StripeResource and autoPagination --- src/{Error.js => Error.ts} | 92 +++++++++++++++++++++++++++----------- src/StripeResource.ts | 2 +- src/autoPagination.ts | 2 +- 3 files changed, 67 insertions(+), 29 deletions(-) rename src/{Error.js => Error.ts} (64%) diff --git a/src/Error.js b/src/Error.ts similarity index 64% rename from src/Error.js rename to src/Error.ts index 2ad52c931a..5a2ebaa72e 100644 --- a/src/Error.js +++ b/src/Error.ts @@ -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; @@ -60,13 +112,13 @@ 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 @@ -74,45 +126,45 @@ class StripeInvalidRequestError extends StripeError {} * 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 @@ -120,23 +172,9 @@ class StripeIdempotencyError extends StripeError {} * 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 {} diff --git a/src/StripeResource.ts b/src/StripeResource.ts index 4cdfd0f6b8..1943251201 100644 --- a/src/StripeResource.ts +++ b/src/StripeResource.ts @@ -627,4 +627,4 @@ StripeResource.prototype = { }, }; -export = StripeResource; +module.exports = StripeResource; diff --git a/src/autoPagination.ts b/src/autoPagination.ts index e0530606a1..e956bf0036 100644 --- a/src/autoPagination.ts +++ b/src/autoPagination.ts @@ -1,6 +1,6 @@ 'use strict'; -import * as utils from './utils'; +import utils = require('./utils'); import makeRequest = require('./makeRequest'); export function makeAutoPaginationMethods(