From f562445f4bedee26accabc02439f48c6e526d927 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 21 Mar 2022 13:02:32 -0700 Subject: [PATCH 01/12] Move error type definitions into a namespace --- types/2020-08-27/index.d.ts | 4 +- types/Errors.d.ts | 400 ++++++++++++++++++------------------ 2 files changed, 203 insertions(+), 201 deletions(-) diff --git a/types/2020-08-27/index.d.ts b/types/2020-08-27/index.d.ts index b943c01e04..ea5e97d4fb 100644 --- a/types/2020-08-27/index.d.ts +++ b/types/2020-08-27/index.d.ts @@ -217,8 +217,8 @@ declare module 'stripe' { /** * API Errors */ - static errors: Stripe.Errors; - errors: Stripe.Errors; + static errors: Stripe.errors.Errors; + errors: Stripe.errors.Errors; on(event: 'request', handler: (event: Stripe.RequestEvent) => void): void; on(event: 'response', handler: (event: Stripe.ResponseEvent) => void): void; diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 8995ae8808..909b12de2a 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -1,230 +1,232 @@ declare module 'stripe' { namespace Stripe { - export type Errors = { - generate: typeof StripeError.generate; - StripeError: typeof StripeError; - StripeCardError: typeof StripeCardError; - StripeInvalidRequestError: typeof StripeInvalidRequestError; - StripeAPIError: typeof StripeAPIError; - StripeAuthenticationError: typeof StripeAuthenticationError; - StripePermissionError: typeof StripePermissionError; - StripeRateLimitError: typeof StripeRateLimitError; - StripeConnectionError: typeof StripeConnectionError; - StripeSignatureVerificationError: typeof StripeSignatureVerificationError; - StripeIdempotencyError: typeof StripeIdempotencyError; - StripeInvalidGrantError: typeof StripeInvalidGrantError; - }; - - export type RawErrorType = - | 'card_error' - | 'invalid_request_error' - | 'api_error' - | 'idempotency_error' - | 'rate_limit_error' - | 'authentication_error' - | 'invalid_grant'; - - export type StripeRawError = { - message?: string; - - type: RawErrorType; - - headers?: {[header: string]: string}; - statusCode?: number; - requestId?: string; - - code?: string; - doc_code?: string; - decline_code?: string; - param?: string; - detail?: string; - - charge?: string; - payment_intent?: Stripe.PaymentIntent; - payment_method?: Stripe.PaymentMethod; - setup_intent?: Stripe.SetupIntent; - source?: Stripe.Source; - }; - - class StripeError extends Error { - constructor(rawError: StripeRawError); - - static generate( - rawError: StripeRawError & {type: 'card_error'} - ): StripeCardError; - static generate( - rawError: StripeRawError & {type: 'invalid_request_error'} - ): StripeInvalidRequestError; - static generate( - rawError: StripeRawError & {type: 'api_error'} - ): StripeAPIError; - static generate( - rawError: StripeRawError & {type: 'authentication_error'} - ): StripeAuthenticationError; - static generate( - rawError: StripeRawError & {type: 'rate_limit_error'} - ): StripeRateLimitError; - static generate( - rawError: StripeRawError & {type: 'idempotency_error'} - ): StripeIdempotencyError; - static generate( - rawError: StripeRawError & {type: 'invalid_grant'} - ): StripeInvalidGrantError; - static generate( - rawError: StripeRawError & {type: RawErrorType} - ): StripeError; + namespace errors { + export type Errors = { + generate: typeof StripeError.generate; + StripeError: typeof StripeError; + StripeCardError: typeof StripeCardError; + StripeInvalidRequestError: typeof StripeInvalidRequestError; + StripeAPIError: typeof StripeAPIError; + StripeAuthenticationError: typeof StripeAuthenticationError; + StripePermissionError: typeof StripePermissionError; + StripeRateLimitError: typeof StripeRateLimitError; + StripeConnectionError: typeof StripeConnectionError; + StripeSignatureVerificationError: typeof StripeSignatureVerificationError; + StripeIdempotencyError: typeof StripeIdempotencyError; + StripeInvalidGrantError: typeof StripeInvalidGrantError; + }; + + export type RawErrorType = + | 'card_error' + | 'invalid_request_error' + | 'api_error' + | 'idempotency_error' + | 'rate_limit_error' + | 'authentication_error' + | 'invalid_grant'; + + export type StripeRawError = { + message?: string; + + type: RawErrorType; + + headers?: {[header: string]: string}; + statusCode?: number; + requestId?: string; + + code?: string; + doc_code?: string; + decline_code?: string; + param?: string; + detail?: string; + + charge?: string; + payment_intent?: Stripe.PaymentIntent; + payment_method?: Stripe.PaymentMethod; + setup_intent?: Stripe.SetupIntent; + source?: Stripe.Source; + }; + + class StripeError extends Error { + constructor(rawError: StripeRawError); + + static generate( + rawError: StripeRawError & {type: 'card_error'} + ): StripeCardError; + static generate( + rawError: StripeRawError & {type: 'invalid_request_error'} + ): StripeInvalidRequestError; + static generate( + rawError: StripeRawError & {type: 'api_error'} + ): StripeAPIError; + static generate( + rawError: StripeRawError & {type: 'authentication_error'} + ): StripeAuthenticationError; + static generate( + rawError: StripeRawError & {type: 'rate_limit_error'} + ): StripeRateLimitError; + static generate( + rawError: StripeRawError & {type: 'idempotency_error'} + ): StripeIdempotencyError; + static generate( + rawError: StripeRawError & {type: 'invalid_grant'} + ): StripeInvalidGrantError; + static generate( + rawError: StripeRawError & {type: RawErrorType} + ): StripeError; + + /** + * A human-readable message giving more details about the error. For card errors, these messages can + * be shown to your users. + */ + readonly message: string; + + readonly type: keyof Errors; + + /** + * See the "error types" section at https://stripe.com/docs/api/errors + */ + readonly rawType: RawErrorType; + + /** + * For card errors, a short string describing the kind of card error that occurred. + * + * @docs https://stripe.com/docs/error-codes + */ + readonly code?: string; + + /** + * A URL to more information about the error code reported. + * + * @docs https://stripe.com/docs/error-codes + */ + readonly doc_url?: string; + + /** + * Typically a 4xx or 5xx. + */ + readonly statusCode?: number; + + readonly raw: unknown; + + readonly headers: { + [key: string]: string; + }; + + readonly requestId: string; + + /** + * The parameter the error relates to if the error is parameter-specific. You can use this to display a + * message near the correct form field, for example. + */ + readonly param?: string; + + /** @deprecated */ + readonly detail?: unknown; + + readonly charge?: string; + readonly decline_code?: string; + readonly payment_intent?: PaymentIntent; + readonly payment_method?: PaymentMethod; + readonly payment_method_type?: string; + readonly setup_intent?: SetupIntent; + readonly source?: Source; + } /** - * A human-readable message giving more details about the error. For card errors, these messages can - * be shown to your users. + * Card errors are the most common type of error you should expect to handle. + * They result when the user enters a card that can't be charged for some reason. */ - readonly message: string; + class StripeCardError extends StripeError { + readonly type: 'StripeCardError'; + readonly rawType: 'card_error'; - readonly type: keyof Errors; + /** + * @docs https://stripe.com/docs/declines/codes + */ + readonly decline_code: string; + } /** - * See the "error types" section at https://stripe.com/docs/api/errors + * Invalid request errors arise when your request has invalid parameters. */ - readonly rawType: RawErrorType; + class StripeInvalidRequestError extends StripeError { + readonly type: 'StripeInvalidRequestError'; + readonly rawType: 'invalid_request_error'; + } /** - * For card errors, a short string describing the kind of card error that occurred. + * API errors cover any other type of problem (e.g., a temporary problem with Stripe's servers), + * and are extremely uncommon. * - * @docs https://stripe.com/docs/error-codes + * It could also be raised in the case that a new error has been introduced in the API, + * but this version of the library doesn't know how to handle it. */ - readonly code?: string; + class StripeAPIError extends StripeError { + readonly type: 'StripeAPIError'; + readonly rawType: 'api_error'; + } /** - * A URL to more information about the error code reported. - * - * @docs https://stripe.com/docs/error-codes + * Failure to properly authenticate yourself in the request. */ - readonly doc_url?: string; + class StripeAuthenticationError extends StripeError { + readonly type: 'StripeAuthenticationError'; + readonly rawType: 'authentication_error'; + } /** - * Typically a 4xx or 5xx. + * Access was attempted on a resource that wasn't allowed. */ - readonly statusCode?: number; - - readonly raw: unknown; - - readonly headers: { - [key: string]: string; - }; - - readonly requestId: string; + class StripePermissionError extends StripeError { + readonly type: 'StripePermissionError'; + } /** - * The parameter the error relates to if the error is parameter-specific. You can use this to display a - * message near the correct form field, for example. + * Too many requests hit the API too quickly. + * @docs https://stripe.com/docs/rate-limits */ - readonly param?: string; - - /** @deprecated */ - readonly detail?: unknown; - - readonly charge?: string; - readonly decline_code?: string; - readonly payment_intent?: PaymentIntent; - readonly payment_method?: PaymentMethod; - readonly payment_method_type?: string; - readonly setup_intent?: SetupIntent; - readonly source?: Source; - } - - /** - * Card errors are the most common type of error you should expect to handle. - * They result when the user enters a card that can't be charged for some reason. - */ - class StripeCardError extends StripeError { - readonly type: 'StripeCardError'; - readonly rawType: 'card_error'; + class StripeRateLimitError extends StripeError { + readonly type: 'StripeRateLimitError'; + readonly rawType: 'rate_limit_error'; + } /** - * @docs https://stripe.com/docs/declines/codes + * The library cannot connect to Stripe. + * This can happen for a variety of reasons, + * such as loss of network connectivity or a bad TLS certificate. */ - readonly decline_code: string; - } - - /** - * Invalid request errors arise when your request has invalid parameters. - */ - class StripeInvalidRequestError extends StripeError { - readonly type: 'StripeInvalidRequestError'; - readonly rawType: 'invalid_request_error'; - } - - /** - * API errors cover any other type of problem (e.g., a temporary problem with Stripe's servers), - * and are extremely uncommon. - * - * It could also be raised in the case that a new error has been introduced in the API, - * but this version of the library doesn't know how to handle it. - */ - class StripeAPIError extends StripeError { - readonly type: 'StripeAPIError'; - readonly rawType: 'api_error'; - } + class StripeConnectionError extends StripeError { + readonly type: 'StripeConnectionError'; + } - /** - * Failure to properly authenticate yourself in the request. - */ - class StripeAuthenticationError extends StripeError { - readonly type: 'StripeAuthenticationError'; - readonly rawType: 'authentication_error'; - } - - /** - * Access was attempted on a resource that wasn't allowed. - */ - class StripePermissionError extends StripeError { - readonly type: 'StripePermissionError'; - } - - /** - * Too many requests hit the API too quickly. - * @docs https://stripe.com/docs/rate-limits - */ - class StripeRateLimitError extends StripeError { - readonly type: 'StripeRateLimitError'; - readonly rawType: 'rate_limit_error'; - } - - /** - * The library cannot connect to Stripe. - * This can happen for a variety of reasons, - * such as loss of network connectivity or a bad TLS certificate. - */ - class StripeConnectionError extends StripeError { - readonly type: 'StripeConnectionError'; - } + /** + * The signature verification for a webhook failed. + * @docs https://stripe.com/docs/webhooks/signatures + */ + class StripeSignatureVerificationError extends StripeError { + readonly type: 'StripeSignatureVerificationError'; + } - /** - * The signature verification for a webhook failed. - * @docs https://stripe.com/docs/webhooks/signatures - */ - class StripeSignatureVerificationError extends StripeError { - readonly type: 'StripeSignatureVerificationError'; - } + /** + * Idempotency errors occur when an `Idempotency-Key` is re-used on a request that does not match the first request's API endpoint and parameters. + * @docs https://stripe.com/docs/api/idempotent_requests?lang=node + */ + class StripeIdempotencyError extends StripeError { + readonly type: 'StripeIdempotencyError'; + readonly rawType: 'idempotency_error'; + } - /** - * Idempotency errors occur when an `Idempotency-Key` is re-used on a request that does not match the first request's API endpoint and parameters. - * @docs https://stripe.com/docs/api/idempotent_requests?lang=node - */ - class StripeIdempotencyError extends StripeError { - readonly type: 'StripeIdempotencyError'; - readonly rawType: 'idempotency_error'; + /** + * 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 { + readonly type: 'StripeInvalidGrantError'; + readonly rawType: 'invalid_grant'; + } } - - /** - * 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 { - readonly type: 'StripeInvalidGrantError'; - readonly rawType: 'invalid_grant'; } - } } From 8f91a803a266a89b0405189f16d05125243b5dd6 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 21 Mar 2022 13:12:43 -0700 Subject: [PATCH 02/12] lint --- types/Errors.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 909b12de2a..088ae9caac 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -228,5 +228,5 @@ declare module 'stripe' { readonly rawType: 'invalid_grant'; } } - } + } } From 3951ec2de3282daab8380fdcda2ffa7ee95d8885 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 21 Mar 2022 13:20:04 -0700 Subject: [PATCH 03/12] gt --- types/2020-08-27/Cards.d.ts | 5 ----- types/2020-08-27/PaymentIntents.d.ts | 2 -- types/test/typescriptTest.ts | 2 +- 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/types/2020-08-27/Cards.d.ts b/types/2020-08-27/Cards.d.ts index 239f699a78..45426a525d 100644 --- a/types/2020-08-27/Cards.d.ts +++ b/types/2020-08-27/Cards.d.ts @@ -160,11 +160,6 @@ declare module 'stripe' { */ recipient?: string | Stripe.Recipient | null; - /** - * For external accounts, possible values are `new` and `errored`. If a transfer fails, the status is set to `errored` and transfers are stopped until account details are updated. - */ - status?: string | null; - /** * If the card number is tokenized, this is the method that was used. Can be `android_pay` (includes Google Pay), `apple_pay`, `masterpass`, `visa_checkout`, or null. */ diff --git a/types/2020-08-27/PaymentIntents.d.ts b/types/2020-08-27/PaymentIntents.d.ts index 17b94bf023..53b0567e28 100644 --- a/types/2020-08-27/PaymentIntents.d.ts +++ b/types/2020-08-27/PaymentIntents.d.ts @@ -5471,8 +5471,6 @@ declare module 'stripe' { * A PaymentIntent object can be canceled when it is in one of these statuses: requires_payment_method, requires_capture, requires_confirmation, requires_action, or processing. * * Once canceled, no additional charges will be made by the PaymentIntent and any operations on the PaymentIntent will fail with an error. For PaymentIntents with status='requires_capture', the remaining amount_capturable will automatically be refunded. - * - * You cannot cancel the PaymentIntent for a Checkout Session. [Expire the Checkout Session](https://stripe.com/docs/api/checkout/sessions/expire) instead */ cancel( id: string, diff --git a/types/test/typescriptTest.ts b/types/test/typescriptTest.ts index aa8ed12092..648ebbbec7 100644 --- a/types/test/typescriptTest.ts +++ b/types/test/typescriptTest.ts @@ -169,7 +169,7 @@ stripe.setHost('host', 'port', 'protocol'); } })(); -const stripeCardError: Stripe.StripeCardError = Stripe.errors.generate({ +const stripeCardError: Stripe.errors.StripeCardError = Stripe.errors.generate({ type: 'card_error', code: 'card_declined', charge: 'ch_123', From 2fab168c49d98d84f45ed93fb1e02b94ab8c2db9 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Mon, 21 Mar 2022 15:06:12 -0700 Subject: [PATCH 04/12] rev --- types/2020-08-27/Cards.d.ts | 5 +++++ types/2020-08-27/PaymentIntents.d.ts | 2 ++ 2 files changed, 7 insertions(+) diff --git a/types/2020-08-27/Cards.d.ts b/types/2020-08-27/Cards.d.ts index 45426a525d..239f699a78 100644 --- a/types/2020-08-27/Cards.d.ts +++ b/types/2020-08-27/Cards.d.ts @@ -160,6 +160,11 @@ declare module 'stripe' { */ recipient?: string | Stripe.Recipient | null; + /** + * For external accounts, possible values are `new` and `errored`. If a transfer fails, the status is set to `errored` and transfers are stopped until account details are updated. + */ + status?: string | null; + /** * If the card number is tokenized, this is the method that was used. Can be `android_pay` (includes Google Pay), `apple_pay`, `masterpass`, `visa_checkout`, or null. */ diff --git a/types/2020-08-27/PaymentIntents.d.ts b/types/2020-08-27/PaymentIntents.d.ts index 53b0567e28..17b94bf023 100644 --- a/types/2020-08-27/PaymentIntents.d.ts +++ b/types/2020-08-27/PaymentIntents.d.ts @@ -5471,6 +5471,8 @@ declare module 'stripe' { * A PaymentIntent object can be canceled when it is in one of these statuses: requires_payment_method, requires_capture, requires_confirmation, requires_action, or processing. * * Once canceled, no additional charges will be made by the PaymentIntent and any operations on the PaymentIntent will fail with an error. For PaymentIntents with status='requires_capture', the remaining amount_capturable will automatically be refunded. + * + * You cannot cancel the PaymentIntent for a Checkout Session. [Expire the Checkout Session](https://stripe.com/docs/api/checkout/sessions/expire) instead */ cancel( id: string, From 2730aa52afef260016463c2f86640a574023c2e8 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 22 Mar 2022 10:59:10 -0700 Subject: [PATCH 05/12] fb --- types/Errors.d.ts | 92 ++++++++++++++++++------------------ types/test/typescriptTest.ts | 4 ++ 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 088ae9caac..3bce02fe5a 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -1,52 +1,52 @@ declare module 'stripe' { namespace Stripe { - namespace errors { - export type Errors = { - generate: typeof StripeError.generate; - StripeError: typeof StripeError; - StripeCardError: typeof StripeCardError; - StripeInvalidRequestError: typeof StripeInvalidRequestError; - StripeAPIError: typeof StripeAPIError; - StripeAuthenticationError: typeof StripeAuthenticationError; - StripePermissionError: typeof StripePermissionError; - StripeRateLimitError: typeof StripeRateLimitError; - StripeConnectionError: typeof StripeConnectionError; - StripeSignatureVerificationError: typeof StripeSignatureVerificationError; - StripeIdempotencyError: typeof StripeIdempotencyError; - StripeInvalidGrantError: typeof StripeInvalidGrantError; - }; - - export type RawErrorType = - | 'card_error' - | 'invalid_request_error' - | 'api_error' - | 'idempotency_error' - | 'rate_limit_error' - | 'authentication_error' - | 'invalid_grant'; - - export type StripeRawError = { - message?: string; - - type: RawErrorType; - - headers?: {[header: string]: string}; - statusCode?: number; - requestId?: string; - - code?: string; - doc_code?: string; - decline_code?: string; - param?: string; - detail?: string; - - charge?: string; - payment_intent?: Stripe.PaymentIntent; - payment_method?: Stripe.PaymentMethod; - setup_intent?: Stripe.SetupIntent; - source?: Stripe.Source; - }; + export type Errors = { + generate: typeof errors.StripeError.generate; + StripeError: typeof errors.StripeError; + StripeCardError: typeof errors.StripeCardError; + StripeInvalidRequestError: typeof errors.StripeInvalidRequestError; + StripeAPIError: typeof errors.StripeAPIError; + StripeAuthenticationError: typeof errors.StripeAuthenticationError; + StripePermissionError: typeof errors.StripePermissionError; + StripeRateLimitError: typeof errors.StripeRateLimitError; + StripeConnectionError: typeof errors.StripeConnectionError; + StripeSignatureVerificationError: typeof errors.StripeSignatureVerificationError; + StripeIdempotencyError: typeof errors.StripeIdempotencyError; + StripeInvalidGrantError: typeof errors.StripeInvalidGrantError; + }; + + export type RawErrorType = + | 'card_error' + | 'invalid_request_error' + | 'api_error' + | 'idempotency_error' + | 'rate_limit_error' + | 'authentication_error' + | 'invalid_grant'; + + export type StripeRawError = { + message?: string; + + type: RawErrorType; + + headers?: {[header: string]: string}; + statusCode?: number; + requestId?: string; + + code?: string; + doc_code?: string; + decline_code?: string; + param?: string; + detail?: string; + + charge?: string; + payment_intent?: Stripe.PaymentIntent; + payment_method?: Stripe.PaymentMethod; + setup_intent?: Stripe.SetupIntent; + source?: Stripe.Source; + }; + namespace errors { class StripeError extends Error { constructor(rawError: StripeRawError); diff --git a/types/test/typescriptTest.ts b/types/test/typescriptTest.ts index 648ebbbec7..5dfe2bb1cb 100644 --- a/types/test/typescriptTest.ts +++ b/types/test/typescriptTest.ts @@ -267,3 +267,7 @@ async (): Promise => { const event2 = await stripe.events.retrieve(event.id); }; + +// Can reference error types +let errors: Stripe.Errors; +let rawError: Stripe.StripeRawError; From 81698152798def110aa9e156cb1ed84f1368ea0e Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 22 Mar 2022 12:55:01 -0700 Subject: [PATCH 06/12] more --- types/2020-08-27/index.d.ts | 3 +- types/Errors.d.ts | 149 +++++++++++++++++++++++++---------- types/test/typescriptTest.ts | 4 + 3 files changed, 112 insertions(+), 44 deletions(-) diff --git a/types/2020-08-27/index.d.ts b/types/2020-08-27/index.d.ts index ea5e97d4fb..3edaa5f7e9 100644 --- a/types/2020-08-27/index.d.ts +++ b/types/2020-08-27/index.d.ts @@ -217,8 +217,7 @@ declare module 'stripe' { /** * API Errors */ - static errors: Stripe.errors.Errors; - errors: Stripe.errors.Errors; + errors: Stripe.Errors; on(event: 'request', handler: (event: Stripe.RequestEvent) => void): void; on(event: 'response', handler: (event: Stripe.ResponseEvent) => void): void; diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 3bce02fe5a..4dd9c17c9b 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -1,52 +1,117 @@ declare module 'stripe' { namespace Stripe { export type Errors = { - generate: typeof errors.StripeError.generate; - StripeError: typeof errors.StripeError; - StripeCardError: typeof errors.StripeCardError; - StripeInvalidRequestError: typeof errors.StripeInvalidRequestError; - StripeAPIError: typeof errors.StripeAPIError; - StripeAuthenticationError: typeof errors.StripeAuthenticationError; - StripePermissionError: typeof errors.StripePermissionError; - StripeRateLimitError: typeof errors.StripeRateLimitError; - StripeConnectionError: typeof errors.StripeConnectionError; - StripeSignatureVerificationError: typeof errors.StripeSignatureVerificationError; - StripeIdempotencyError: typeof errors.StripeIdempotencyError; - StripeInvalidGrantError: typeof errors.StripeInvalidGrantError; + generate: typeof Stripe.errors.StripeError.generate; + StripeError: typeof Stripe.errors.StripeError; + StripeCardError: typeof Stripe.errors.StripeCardError; + StripeInvalidRequestError: typeof Stripe.errors.StripeInvalidRequestError; + StripeAPIError: typeof Stripe.errors.StripeAPIError; + StripeAuthenticationError: typeof Stripe.errors.StripeAuthenticationError; + StripePermissionError: typeof Stripe.errors.StripePermissionError; + StripeRateLimitError: typeof Stripe.errors.StripeRateLimitError; + StripeConnectionError: typeof Stripe.errors.StripeConnectionError; + StripeSignatureVerificationError: typeof Stripe.errors.StripeSignatureVerificationError; + StripeIdempotencyError: typeof Stripe.errors.StripeIdempotencyError; + StripeInvalidGrantError: typeof Stripe.errors.StripeInvalidGrantError; }; - export type RawErrorType = - | 'card_error' - | 'invalid_request_error' - | 'api_error' - | 'idempotency_error' - | 'rate_limit_error' - | 'authentication_error' - | 'invalid_grant'; - - export type StripeRawError = { - message?: string; - - type: RawErrorType; - - headers?: {[header: string]: string}; - statusCode?: number; - requestId?: string; - - code?: string; - doc_code?: string; - decline_code?: string; - param?: string; - detail?: string; - - charge?: string; - payment_intent?: Stripe.PaymentIntent; - payment_method?: Stripe.PaymentMethod; - setup_intent?: Stripe.SetupIntent; - source?: Stripe.Source; - }; + /** + * @deprecated The {@link Stripe.RawErrorType} type is deprecated, please use {@link Stripe.errors.RawErrorType}. + */ + export type RawErrorType = errors.RawErrorType; + + /** + * @deprecated The {@link Stripe.StripeRawError} type is deprecated, please use {@link Stripe.errors.StripeRawError}. + */ + type StripeRawError = errors.StripeRawError; + + /** + * @deprecated The {@link Stripe.StripeError} type is deprecated, please use {@link Stripe.errors.StripeError}. + */ + type StripeError = errors.StripeError; + + /** + * @deprecated The {@link Stripe.StripeCardError} type is deprecated, please use {@link Stripe.errors.StripeCardError}. + */ + type StripeCardError = errors.StripeCardError; + + /** + * @deprecated The {@link Stripe.StripeInvalidRequestError} type is deprecated, please use {@link Stripe.errors.StripeInvalidRequestError}. + */ + type StripeInvalidRequestError = errors.StripeInvalidRequestError; + + /** + * @deprecated The {@link Stripe.StripeAPIError} type is deprecated, please use {@link Stripe.errors.StripeAPIError}. + */ + type StripeAPIError = errors.StripeAPIError; + + /** + * @deprecated The {@link Stripe.StripeAuthenticationError} type is deprecated, please use {@link Stripe.errors.StripeAuthenticationError}. + */ + type StripeAuthenticationError = errors.StripeAuthenticationError; + + /** + * @deprecated The {@link Stripe.StripePermissionError} type is deprecated, please use {@link Stripe.errors.StripePermissionError}. + */ + type StripePermissionError = errors.StripePermissionError; + + /** + * @deprecated The {@link Stripe.StripeRateLimitError} type is deprecated, please use {@link Stripe.errors.StripeRateLimitError}. + */ + type StripeRateLimitError = errors.StripeRateLimitError; + + /** + * @deprecated The {@link Stripe.StripeConnectionError} type is deprecated, please use {@link Stripe.errors.StripeConnectionError}. + */ + type StripeConnectionError = errors.StripeConnectionError; + + /** + * @deprecated The {@link Stripe.StripeSignatureVerificationError} type is deprecated, please use {@link Stripe.errors.StripeSignatureVerificationError}. + */ + type StripeSignatureVerificationError = errors.StripeSignatureVerificationError; + + /** + * @deprecated The {@link Stripe.StripeIdempotencyError} type is deprecated, please use {@link Stripe.errors.StripeIdempotencyError}. + */ + type StripeIdempotencyError = errors.StripeIdempotencyError; + + /** + * @deprecated The {@link Stripe.StripeInvalidGrantError} type is deprecated, please use {@link Stripe.errors.StripeInvalidGrantError}. + */ + type StripeInvalidGrantError = errors.StripeInvalidGrantError; namespace errors { + export type RawErrorType = + | 'card_error' + | 'invalid_request_error' + | 'api_error' + | 'idempotency_error' + | 'rate_limit_error' + | 'authentication_error' + | 'invalid_grant'; + + export type StripeRawError = { + message?: string; + + type: RawErrorType; + + headers?: {[header: string]: string}; + statusCode?: number; + requestId?: string; + + code?: string; + doc_code?: string; + decline_code?: string; + param?: string; + detail?: string; + + charge?: string; + payment_intent?: Stripe.PaymentIntent; + payment_method?: Stripe.PaymentMethod; + setup_intent?: Stripe.SetupIntent; + source?: Stripe.Source; + }; + class StripeError extends Error { constructor(rawError: StripeRawError); diff --git a/types/test/typescriptTest.ts b/types/test/typescriptTest.ts index 5dfe2bb1cb..7258acf8a2 100644 --- a/types/test/typescriptTest.ts +++ b/types/test/typescriptTest.ts @@ -271,3 +271,7 @@ async (): Promise => { // Can reference error types let errors: Stripe.Errors; let rawError: Stripe.StripeRawError; +let newRawError: Stripe.errors.StripeRawError; + +let oldError: Stripe.StripeError; +let newError: Stripe.errors.StripeError; From 675d1eb5d65396483662398a4a25c1f06e19825a Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 22 Mar 2022 13:01:37 -0700 Subject: [PATCH 07/12] more --- types/2020-08-27/index.d.ts | 1 + types/Errors.d.ts | 30 ++++++++++++++++-------------- types/test/typescriptTest.ts | 3 +++ 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/types/2020-08-27/index.d.ts b/types/2020-08-27/index.d.ts index 3edaa5f7e9..b943c01e04 100644 --- a/types/2020-08-27/index.d.ts +++ b/types/2020-08-27/index.d.ts @@ -217,6 +217,7 @@ declare module 'stripe' { /** * API Errors */ + static errors: Stripe.Errors; errors: Stripe.Errors; on(event: 'request', handler: (event: Stripe.RequestEvent) => void): void; diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 4dd9c17c9b..653b11090d 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -1,19 +1,6 @@ declare module 'stripe' { namespace Stripe { - export type Errors = { - generate: typeof Stripe.errors.StripeError.generate; - StripeError: typeof Stripe.errors.StripeError; - StripeCardError: typeof Stripe.errors.StripeCardError; - StripeInvalidRequestError: typeof Stripe.errors.StripeInvalidRequestError; - StripeAPIError: typeof Stripe.errors.StripeAPIError; - StripeAuthenticationError: typeof Stripe.errors.StripeAuthenticationError; - StripePermissionError: typeof Stripe.errors.StripePermissionError; - StripeRateLimitError: typeof Stripe.errors.StripeRateLimitError; - StripeConnectionError: typeof Stripe.errors.StripeConnectionError; - StripeSignatureVerificationError: typeof Stripe.errors.StripeSignatureVerificationError; - StripeIdempotencyError: typeof Stripe.errors.StripeIdempotencyError; - StripeInvalidGrantError: typeof Stripe.errors.StripeInvalidGrantError; - }; + export type Errors = errors.Errors; /** * @deprecated The {@link Stripe.RawErrorType} type is deprecated, please use {@link Stripe.errors.RawErrorType}. @@ -81,6 +68,21 @@ declare module 'stripe' { type StripeInvalidGrantError = errors.StripeInvalidGrantError; namespace errors { + type Errors = { + generate: typeof StripeError.generate; + StripeError: typeof StripeError; + StripeCardError: typeof StripeCardError; + StripeInvalidRequestError: typeof StripeInvalidRequestError; + StripeAPIError: typeof StripeAPIError; + StripeAuthenticationError: typeof StripeAuthenticationError; + StripePermissionError: typeof StripePermissionError; + StripeRateLimitError: typeof StripeRateLimitError; + StripeConnectionError: typeof StripeConnectionError; + StripeSignatureVerificationError: typeof StripeSignatureVerificationError; + StripeIdempotencyError: typeof StripeIdempotencyError; + StripeInvalidGrantError: typeof StripeInvalidGrantError; + }; + export type RawErrorType = | 'card_error' | 'invalid_request_error' diff --git a/types/test/typescriptTest.ts b/types/test/typescriptTest.ts index 7258acf8a2..4d9a59767f 100644 --- a/types/test/typescriptTest.ts +++ b/types/test/typescriptTest.ts @@ -275,3 +275,6 @@ let newRawError: Stripe.errors.StripeRawError; let oldError: Stripe.StripeError; let newError: Stripe.errors.StripeError; + +const instanceofCheck1 = {} instanceof Stripe.errors.StripeError; +const instanceofCheck2 = {} instanceof Stripe.errors.StripeAPIError; From a9b41d1d5dc3ca5faf66fd84e705aca93c6d4d6f Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Tue, 22 Mar 2022 13:03:26 -0700 Subject: [PATCH 08/12] revert a sample --- types/test/typescriptTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/test/typescriptTest.ts b/types/test/typescriptTest.ts index 4d9a59767f..bda44e6d76 100644 --- a/types/test/typescriptTest.ts +++ b/types/test/typescriptTest.ts @@ -169,7 +169,7 @@ stripe.setHost('host', 'port', 'protocol'); } })(); -const stripeCardError: Stripe.errors.StripeCardError = Stripe.errors.generate({ +const stripeCardError: Stripe.StripeCardError = Stripe.errors.generate({ type: 'card_error', code: 'card_declined', charge: 'ch_123', From 9e9d4b5b5a5bf8a3d2187390bc3db4d5f28f06ef Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 24 Mar 2022 08:51:43 -0700 Subject: [PATCH 09/12] fb --- types/Errors.d.ts | 302 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 225 insertions(+), 77 deletions(-) diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 653b11090d..8c5ef004e7 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -1,119 +1,267 @@ declare module 'stripe' { namespace Stripe { - export type Errors = errors.Errors; + export type Errors = { + generate: typeof errors.StripeError.generate; + StripeError: typeof StripeError; + StripeCardError: typeof errors.StripeCardError; + StripeInvalidRequestError: typeof errors.StripeInvalidRequestError; + StripeAPIError: typeof errors.StripeAPIError; + StripeAuthenticationError: typeof errors.StripeAuthenticationError; + StripePermissionError: typeof errors.StripePermissionError; + StripeRateLimitError: typeof errors.StripeRateLimitError; + StripeConnectionError: typeof errors.StripeConnectionError; + StripeSignatureVerificationError: typeof errors.StripeSignatureVerificationError; + StripeIdempotencyError: typeof errors.StripeIdempotencyError; + StripeInvalidGrantError: typeof errors.StripeInvalidGrantError; + }; + + export type RawErrorType = + | 'card_error' + | 'invalid_request_error' + | 'api_error' + | 'idempotency_error' + | 'rate_limit_error' + | 'authentication_error' + | 'invalid_grant'; + + export type StripeRawError = { + message?: string; + + type: RawErrorType; + + headers?: {[header: string]: string}; + statusCode?: number; + requestId?: string; + + code?: string; + doc_code?: string; + decline_code?: string; + param?: string; + detail?: string; + + charge?: string; + payment_intent?: Stripe.PaymentIntent; + payment_method?: Stripe.PaymentMethod; + setup_intent?: Stripe.SetupIntent; + source?: Stripe.Source; + }; /** - * @deprecated The {@link Stripe.RawErrorType} type is deprecated, please use {@link Stripe.errors.RawErrorType}. + * @deprecated The {@link Stripe.StripeError} type is deprecated, please use {@link Stripe.errors.StripeError}.} + * The runtime StripeError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - export type RawErrorType = errors.RawErrorType; + class StripeError extends Error { + constructor(rawError: StripeRawError); + + static generate( + rawError: StripeRawError & {type: 'card_error'} + ): StripeCardError; + static generate( + rawError: StripeRawError & {type: 'invalid_request_error'} + ): StripeInvalidRequestError; + static generate( + rawError: StripeRawError & {type: 'api_error'} + ): StripeAPIError; + static generate( + rawError: StripeRawError & {type: 'authentication_error'} + ): StripeAuthenticationError; + static generate( + rawError: StripeRawError & {type: 'rate_limit_error'} + ): StripeRateLimitError; + static generate( + rawError: StripeRawError & {type: 'idempotency_error'} + ): StripeIdempotencyError; + static generate( + rawError: StripeRawError & {type: 'invalid_grant'} + ): StripeInvalidGrantError; + static generate( + rawError: StripeRawError & {type: RawErrorType} + ): StripeError; - /** - * @deprecated The {@link Stripe.StripeRawError} type is deprecated, please use {@link Stripe.errors.StripeRawError}. - */ - type StripeRawError = errors.StripeRawError; + /** + * A human-readable message giving more details about the error. For card errors, these messages can + * be shown to your users. + */ + readonly message: string; - /** - * @deprecated The {@link Stripe.StripeError} type is deprecated, please use {@link Stripe.errors.StripeError}. - */ - type StripeError = errors.StripeError; + readonly type: keyof Errors; + + /** + * See the "error types" section at https://stripe.com/docs/api/errors + */ + readonly rawType: RawErrorType; + + /** + * For card errors, a short string describing the kind of card error that occurred. + * + * @docs https://stripe.com/docs/error-codes + */ + readonly code?: string; + + /** + * A URL to more information about the error code reported. + * + * @docs https://stripe.com/docs/error-codes + */ + readonly doc_url?: string; + + /** + * Typically a 4xx or 5xx. + */ + readonly statusCode?: number; + + readonly raw: unknown; + + readonly headers: { + [key: string]: string; + }; + + readonly requestId: string; + + /** + * The parameter the error relates to if the error is parameter-specific. You can use this to display a + * message near the correct form field, for example. + */ + readonly param?: string; + + /** @deprecated */ + readonly detail?charge charge; + + readonly charge?: string; + readonly decline_code?: stringcharge + readonly payment_intent?: PaymentIntent; + readonly payment_method?: PaymentMethod; + readonly payment_method_type?: string; + readonly setup_intent?: SetupIntent; + readonly source?: Source; + } /** - * @deprecated The {@link Stripe.StripeCardError} type is deprecated, please use {@link Stripe.errors.StripeCardError}. + * Card errors are the most common type of error you should expect to handle. + * They result when the user enters a card that can't be charged for some reason. + * + * @deprecated The {@link Stripe.StripeCardError} type is deprecated, please use {@link Stripe.errors.StripeCardError}.} + * The runtime StripeCardError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeCardError = errors.StripeCardError; + class StripeCardError extends StripeError { + readonly type: 'StripeCardError'; + readonly rawType: 'card_error'; + + /** + * @docs https://stripe.com/docs/declines/codes + */ + readonly decline_code: string; + } /** - * @deprecated The {@link Stripe.StripeInvalidRequestError} type is deprecated, please use {@link Stripe.errors.StripeInvalidRequestError}. + * Invalid request errors arise when your request has invalid parameters. + * + * @deprecated The {@link Stripe.StripeInvalidRequestError} type is deprecated, please use {@link Stripe.errors.StripeInvalidRequestError}.} + * The runtime StripeInvalidRequestError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeInvalidRequestError = errors.StripeInvalidRequestError; + class StripeInvalidRequestError extends StripeError { + readonly type: 'StripeInvalidRequestError'; + readonly rawType: 'invalid_request_error'; + } /** - * @deprecated The {@link Stripe.StripeAPIError} type is deprecated, please use {@link Stripe.errors.StripeAPIError}. + * API errors cover any other type of problem (e.g., a temporary problem with Stripe's servers), + * and are extremely uncommon. + * + * It could also be raised in the case that a new error has been introduced in the API, + * but this version of the library doesn't know how to handle it. + * + * @deprecated The {@link Stripe.StripeAPIError} type is deprecated, please use {@link Stripe.errors.StripeAPIError}.} + * The runtime StripeAPIError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeAPIError = errors.StripeAPIError; + class StripeAPIError extends StripeError { + readonly type: 'StripeAPIError'; + readonly rawType: 'api_error'; + } /** - * @deprecated The {@link Stripe.StripeAuthenticationError} type is deprecated, please use {@link Stripe.errors.StripeAuthenticationError}. + * Failure to properly authenticate yourself in the request. + * + * @deprecated The {@link Stripe.StripeAuthenticationError} type is deprecated, please use {@link Stripe.errors.StripeAuthenticationError}.} + * The runtime StripeAuthenticationError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeAuthenticationError = errors.StripeAuthenticationError; + class StripeAuthenticationError extends StripeError { + readonly type: 'StripeAuthenticationError'; + readonly rawType: 'authentication_error'; + } /** - * @deprecated The {@link Stripe.StripePermissionError} type is deprecated, please use {@link Stripe.errors.StripePermissionError}. + * Access was attempted on a resource that wasn't allowed. + * + * @deprecated The {@link Stripe.StripePermissionError} type is deprecated, please use {@link Stripe.errors.StripePermissionError}.} + * The runtime StripePermissionError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripePermissionError = errors.StripePermissionError; + class StripePermissionError extends StripeError { + readonly type: 'StripePermissionError'; + } /** - * @deprecated The {@link Stripe.StripeRateLimitError} type is deprecated, please use {@link Stripe.errors.StripeRateLimitError}. + * Too many requests hit the API too quickly. + * @docs https://stripe.com/docs/rate-limits + * + * @deprecated The {@link Stripe.StripeRateLimitError} type is deprecated, please use {@link Stripe.errors.StripeRateLimitError}.} + * The runtime StripeRateLimitError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeRateLimitError = errors.StripeRateLimitError; + class StripeRateLimitError extends StripeError { + readonly type: 'StripeRateLimitError'; + readonly rawType: 'rate_limit_error'; + } /** - * @deprecated The {@link Stripe.StripeConnectionError} type is deprecated, please use {@link Stripe.errors.StripeConnectionError}. + * The library cannot connect to Stripe. + * This can happen for a variety of reasons, + * such as loss of network connectivity or a bad TLS certificate. + * + * @deprecated The {@link Stripe.StripeConnectionError} type is deprecated, please use {@link Stripe.errors.StripeConnectionError}.} + * The runtime StripeConnectionError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeConnectionError = errors.StripeConnectionError; + class StripeConnectionError extends StripeError { + readonly type: 'StripeConnectionError'; + } /** - * @deprecated The {@link Stripe.StripeSignatureVerificationError} type is deprecated, please use {@link Stripe.errors.StripeSignatureVerificationError}. + * The signature verification for a webhook failed. + * @docs https://stripe.com/docs/webhooks/signatures + * + * @deprecated The {@link Stripe.StripeSignatureVerificationError} type is deprecated, please use {@link Stripe.errors.StripeSignatureVerificationError}.} + * The runtime StripeSignatureVerificationError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeSignatureVerificationError = errors.StripeSignatureVerificationError; + class StripeSignatureVerificationError extends StripeError { + readonly type: 'StripeSignatureVerificationError'; + } /** - * @deprecated The {@link Stripe.StripeIdempotencyError} type is deprecated, please use {@link Stripe.errors.StripeIdempotencyError}. + * Idempotency errors occur when an `Idempotency-Key` is re-used on a request that does not match the first request's API endpoint and parameters. + * @docs https://stripe.com/docs/api/idempotent_requests?lang=node + * + * @deprecated The {@link Stripe.StripeIdempotencyError} type is deprecated, please use {@link Stripe.errors.StripeIdempotencyError}.} + * The runtime StripeIdempotencyError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeIdempotencyError = errors.StripeIdempotencyError; + class StripeIdempotencyError extends StripeError { + readonly type: 'StripeIdempotencyError'; + readonly rawType: 'idempotency_error'; + } /** - * @deprecated The {@link Stripe.StripeInvalidGrantError} type is deprecated, please use {@link Stripe.errors.StripeInvalidGrantError}. + * 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. + * + * @deprecated The {@link Stripe.StripeInvalidGrantError} type is deprecated, please use {@link Stripe.errors.StripeInvalidGrantError}.} + * The runtime StripeInvalidGrantError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ - type StripeInvalidGrantError = errors.StripeInvalidGrantError; + class StripeInvalidGrantError extends StripeError { + readonly type: 'StripeInvalidGrantError'; + readonly rawType: 'invalid_grant'; + } namespace errors { - type Errors = { - generate: typeof StripeError.generate; - StripeError: typeof StripeError; - StripeCardError: typeof StripeCardError; - StripeInvalidRequestError: typeof StripeInvalidRequestError; - StripeAPIError: typeof StripeAPIError; - StripeAuthenticationError: typeof StripeAuthenticationError; - StripePermissionError: typeof StripePermissionError; - StripeRateLimitError: typeof StripeRateLimitError; - StripeConnectionError: typeof StripeConnectionError; - StripeSignatureVerificationError: typeof StripeSignatureVerificationError; - StripeIdempotencyError: typeof StripeIdempotencyError; - StripeInvalidGrantError: typeof StripeInvalidGrantError; - }; - - export type RawErrorType = - | 'card_error' - | 'invalid_request_error' - | 'api_error' - | 'idempotency_error' - | 'rate_limit_error' - | 'authentication_error' - | 'invalid_grant'; - - export type StripeRawError = { - message?: string; - - type: RawErrorType; - - headers?: {[header: string]: string}; - statusCode?: number; - requestId?: string; - - code?: string; - doc_code?: string; - decline_code?: string; - param?: string; - detail?: string; - - charge?: string; - payment_intent?: Stripe.PaymentIntent; - payment_method?: Stripe.PaymentMethod; - setup_intent?: Stripe.SetupIntent; - source?: Stripe.Source; - }; - class StripeError extends Error { constructor(rawError: StripeRawError); From 2b90e8a783da949f15cd89d8e70ff02c40a42104 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 24 Mar 2022 09:41:17 -0700 Subject: [PATCH 10/12] happy linter --- types/Errors.d.ts | 4 ++-- types/test/typescriptTest.ts | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 8c5ef004e7..8a0ca2ff69 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -125,10 +125,10 @@ declare module 'stripe' { readonly param?: string; /** @deprecated */ - readonly detail?charge charge; + readonly detail?: unknown; readonly charge?: string; - readonly decline_code?: stringcharge + readonly decline_code?: string; readonly payment_intent?: PaymentIntent; readonly payment_method?: PaymentMethod; readonly payment_method_type?: string; diff --git a/types/test/typescriptTest.ts b/types/test/typescriptTest.ts index bda44e6d76..c2f75f621f 100644 --- a/types/test/typescriptTest.ts +++ b/types/test/typescriptTest.ts @@ -271,10 +271,11 @@ async (): Promise => { // Can reference error types let errors: Stripe.Errors; let rawError: Stripe.StripeRawError; -let newRawError: Stripe.errors.StripeRawError; let oldError: Stripe.StripeError; let newError: Stripe.errors.StripeError; const instanceofCheck1 = {} instanceof Stripe.errors.StripeError; const instanceofCheck2 = {} instanceof Stripe.errors.StripeAPIError; +const instanceofCheck3 = {} instanceof Stripe.StripeError; +const instanceofCheck4 = {} instanceof Stripe.StripeAPIError; From 49566c230f7daddbf253084e6e7cb54541a11d49 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 24 Mar 2022 10:52:44 -0700 Subject: [PATCH 11/12] use namespace --- types/2020-08-27/index.d.ts | 3 +-- types/Errors.d.ts | 50 ++++++++++++++++++++++++++++-------- types/test/typescriptTest.ts | 15 +++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/types/2020-08-27/index.d.ts b/types/2020-08-27/index.d.ts index b943c01e04..0eb7736d65 100644 --- a/types/2020-08-27/index.d.ts +++ b/types/2020-08-27/index.d.ts @@ -217,8 +217,7 @@ declare module 'stripe' { /** * API Errors */ - static errors: Stripe.Errors; - errors: Stripe.Errors; + errors: typeof Stripe.errors; on(event: 'request', handler: (event: Stripe.RequestEvent) => void): void; on(event: 'response', handler: (event: Stripe.ResponseEvent) => void): void; diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 8a0ca2ff69..37dda1beab 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -1,18 +1,21 @@ declare module 'stripe' { namespace Stripe { + /** + * @deprecated The {@link Stripe.Errors} type is deprecated, please use {@link Stripe.errors} namespace directly.} + */ export type Errors = { - generate: typeof errors.StripeError.generate; + generate: typeof StripeError.generate; StripeError: typeof StripeError; - StripeCardError: typeof errors.StripeCardError; - StripeInvalidRequestError: typeof errors.StripeInvalidRequestError; - StripeAPIError: typeof errors.StripeAPIError; - StripeAuthenticationError: typeof errors.StripeAuthenticationError; - StripePermissionError: typeof errors.StripePermissionError; - StripeRateLimitError: typeof errors.StripeRateLimitError; - StripeConnectionError: typeof errors.StripeConnectionError; - StripeSignatureVerificationError: typeof errors.StripeSignatureVerificationError; - StripeIdempotencyError: typeof errors.StripeIdempotencyError; - StripeInvalidGrantError: typeof errors.StripeInvalidGrantError; + StripeCardError: typeof StripeCardError; + StripeInvalidRequestError: typeof StripeInvalidRequestError; + StripeAPIError: typeof StripeAPIError; + StripeAuthenticationError: typeof StripeAuthenticationError; + StripePermissionError: typeof StripePermissionError; + StripeRateLimitError: typeof StripeRateLimitError; + StripeConnectionError: typeof StripeConnectionError; + StripeSignatureVerificationError: typeof StripeSignatureVerificationError; + StripeIdempotencyError: typeof StripeIdempotencyError; + StripeInvalidGrantError: typeof StripeInvalidGrantError; }; export type RawErrorType = @@ -262,6 +265,31 @@ declare module 'stripe' { } namespace errors { + function generate( + rawError: StripeRawError & {type: 'card_error'} + ): StripeCardError; + function generate( + rawError: StripeRawError & {type: 'invalid_request_error'} + ): StripeInvalidRequestError; + function generate( + rawError: StripeRawError & {type: 'api_error'} + ): StripeAPIError; + function generate( + rawError: StripeRawError & {type: 'authentication_error'} + ): StripeAuthenticationError; + function generate( + rawError: StripeRawError & {type: 'rate_limit_error'} + ): StripeRateLimitError; + function generate( + rawError: StripeRawError & {type: 'idempotency_error'} + ): StripeIdempotencyError; + function generate( + rawError: StripeRawError & {type: 'invalid_grant'} + ): StripeInvalidGrantError; + function generate( + rawError: StripeRawError & {type: RawErrorType} + ): StripeError; + class StripeError extends Error { constructor(rawError: StripeRawError); diff --git a/types/test/typescriptTest.ts b/types/test/typescriptTest.ts index c2f75f621f..0c0cacb449 100644 --- a/types/test/typescriptTest.ts +++ b/types/test/typescriptTest.ts @@ -279,3 +279,18 @@ const instanceofCheck1 = {} instanceof Stripe.errors.StripeError; const instanceofCheck2 = {} instanceof Stripe.errors.StripeAPIError; const instanceofCheck3 = {} instanceof Stripe.StripeError; const instanceofCheck4 = {} instanceof Stripe.StripeAPIError; +const instanceofCheck5 = {} instanceof stripe.errors.StripeError; +const instanceofCheck6 = {} instanceof stripe.errors.StripeAPIError; + +Stripe.errors.generate({ + type: 'card_error', +}); +stripe.errors.generate({ + type: 'card_error', +}); +Stripe.StripeError.generate({ + type: 'card_error', +}); +Stripe.errors.StripeError.generate({ + type: 'card_error', +}); From a1e0203bb628e6c62360d0bda4a99c437e2d1eec Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 24 Mar 2022 11:09:16 -0700 Subject: [PATCH 12/12] fb --- types/Errors.d.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/types/Errors.d.ts b/types/Errors.d.ts index 37dda1beab..bb2fa59095 100644 --- a/types/Errors.d.ts +++ b/types/Errors.d.ts @@ -1,7 +1,7 @@ declare module 'stripe' { namespace Stripe { /** - * @deprecated The {@link Stripe.Errors} type is deprecated, please use {@link Stripe.errors} namespace directly.} + * @deprecated The {@link Stripe.Errors} type is deprecated, please use the {@link Stripe.errors} namespace directly. */ export type Errors = { generate: typeof StripeError.generate; @@ -50,7 +50,7 @@ declare module 'stripe' { }; /** - * @deprecated The {@link Stripe.StripeError} type is deprecated, please use {@link Stripe.errors.StripeError}.} + * @deprecated The {@link Stripe.StripeError} type is deprecated, please use {@link Stripe.errors.StripeError}. * The runtime StripeError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeError extends Error { @@ -143,7 +143,7 @@ declare module 'stripe' { * Card errors are the most common type of error you should expect to handle. * They result when the user enters a card that can't be charged for some reason. * - * @deprecated The {@link Stripe.StripeCardError} type is deprecated, please use {@link Stripe.errors.StripeCardError}.} + * @deprecated The {@link Stripe.StripeCardError} type is deprecated, please use {@link Stripe.errors.StripeCardError}. * The runtime StripeCardError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeCardError extends StripeError { @@ -159,7 +159,7 @@ declare module 'stripe' { /** * Invalid request errors arise when your request has invalid parameters. * - * @deprecated The {@link Stripe.StripeInvalidRequestError} type is deprecated, please use {@link Stripe.errors.StripeInvalidRequestError}.} + * @deprecated The {@link Stripe.StripeInvalidRequestError} type is deprecated, please use {@link Stripe.errors.StripeInvalidRequestError}. * The runtime StripeInvalidRequestError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeInvalidRequestError extends StripeError { @@ -174,7 +174,7 @@ declare module 'stripe' { * It could also be raised in the case that a new error has been introduced in the API, * but this version of the library doesn't know how to handle it. * - * @deprecated The {@link Stripe.StripeAPIError} type is deprecated, please use {@link Stripe.errors.StripeAPIError}.} + * @deprecated The {@link Stripe.StripeAPIError} type is deprecated, please use {@link Stripe.errors.StripeAPIError}. * The runtime StripeAPIError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeAPIError extends StripeError { @@ -185,7 +185,7 @@ declare module 'stripe' { /** * Failure to properly authenticate yourself in the request. * - * @deprecated The {@link Stripe.StripeAuthenticationError} type is deprecated, please use {@link Stripe.errors.StripeAuthenticationError}.} + * @deprecated The {@link Stripe.StripeAuthenticationError} type is deprecated, please use {@link Stripe.errors.StripeAuthenticationError}. * The runtime StripeAuthenticationError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeAuthenticationError extends StripeError { @@ -196,7 +196,7 @@ declare module 'stripe' { /** * Access was attempted on a resource that wasn't allowed. * - * @deprecated The {@link Stripe.StripePermissionError} type is deprecated, please use {@link Stripe.errors.StripePermissionError}.} + * @deprecated The {@link Stripe.StripePermissionError} type is deprecated, please use {@link Stripe.errors.StripePermissionError}. * The runtime StripePermissionError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripePermissionError extends StripeError { @@ -207,7 +207,7 @@ declare module 'stripe' { * Too many requests hit the API too quickly. * @docs https://stripe.com/docs/rate-limits * - * @deprecated The {@link Stripe.StripeRateLimitError} type is deprecated, please use {@link Stripe.errors.StripeRateLimitError}.} + * @deprecated The {@link Stripe.StripeRateLimitError} type is deprecated, please use {@link Stripe.errors.StripeRateLimitError}. * The runtime StripeRateLimitError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeRateLimitError extends StripeError { @@ -220,7 +220,7 @@ declare module 'stripe' { * This can happen for a variety of reasons, * such as loss of network connectivity or a bad TLS certificate. * - * @deprecated The {@link Stripe.StripeConnectionError} type is deprecated, please use {@link Stripe.errors.StripeConnectionError}.} + * @deprecated The {@link Stripe.StripeConnectionError} type is deprecated, please use {@link Stripe.errors.StripeConnectionError}. * The runtime StripeConnectionError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeConnectionError extends StripeError { @@ -231,7 +231,7 @@ declare module 'stripe' { * The signature verification for a webhook failed. * @docs https://stripe.com/docs/webhooks/signatures * - * @deprecated The {@link Stripe.StripeSignatureVerificationError} type is deprecated, please use {@link Stripe.errors.StripeSignatureVerificationError}.} + * @deprecated The {@link Stripe.StripeSignatureVerificationError} type is deprecated, please use {@link Stripe.errors.StripeSignatureVerificationError}. * The runtime StripeSignatureVerificationError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeSignatureVerificationError extends StripeError { @@ -242,7 +242,7 @@ declare module 'stripe' { * Idempotency errors occur when an `Idempotency-Key` is re-used on a request that does not match the first request's API endpoint and parameters. * @docs https://stripe.com/docs/api/idempotent_requests?lang=node * - * @deprecated The {@link Stripe.StripeIdempotencyError} type is deprecated, please use {@link Stripe.errors.StripeIdempotencyError}.} + * @deprecated The {@link Stripe.StripeIdempotencyError} type is deprecated, please use {@link Stripe.errors.StripeIdempotencyError}. * The runtime StripeIdempotencyError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeIdempotencyError extends StripeError { @@ -256,7 +256,7 @@ declare module 'stripe' { * 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. * - * @deprecated The {@link Stripe.StripeInvalidGrantError} type is deprecated, please use {@link Stripe.errors.StripeInvalidGrantError}.} + * @deprecated The {@link Stripe.StripeInvalidGrantError} type is deprecated, please use {@link Stripe.errors.StripeInvalidGrantError}. * The runtime StripeInvalidGrantError type is located in the {@link errors} namespace. This definition was added by mistake but kept for backwards compatibility. */ class StripeInvalidGrantError extends StripeError {