From 59b57c33d29af511fa8df040f95f6b3a3dc1b59f Mon Sep 17 00:00:00 2001 From: Giacomo Debidda Date: Wed, 27 Jul 2022 19:10:19 +0200 Subject: [PATCH] fix(stripe-utils): update TS typings --- .../interfaces/DuplicatesConfig.html | 2 +- docs/stripe-utils/modules.html | 37 +++++++---- package-lock.json | 42 +++++++++++- package.json | 3 +- packages/stripe-utils/.ae/stripe-utils.api.md | 21 +----- packages/stripe-utils/.ae/tsdoc-metadata.json | 2 +- packages/stripe-utils/api-docs/index.md | 2 +- ...stripe-utils.createpricewithtaxbehavior.md | 2 +- ...tripe-utils.customerfrompaymentintentid.md | 2 +- .../api-docs/stripe-utils.duplicatesconfig.md | 13 ++-- .../api-docs/stripe-utils.errorfromstripe.md | 4 +- .../api-docs/stripe-utils.isapikeylivemode.md | 2 + .../api-docs/stripe-utils.isapikeytestmode.md | 2 + .../stripe-utils/api-docs/stripe-utils.md | 16 +++-- .../stripe-utils.stripeaccountmode.md | 2 + packages/stripe-utils/src/checks.ts | 10 +++ packages/stripe-utils/src/customers.ts | 8 +++ packages/stripe-utils/src/errors.ts | 10 +-- packages/stripe-utils/src/index.ts | 6 ++ packages/stripe-utils/src/payment_intents.ts | 4 +- packages/stripe-utils/src/prices.ts | 65 +++++++++++++++++-- packages/stripe-utils/src/utils.ts | 11 ++++ packages/stripe-utils/src/webhooks.ts | 3 + 23 files changed, 206 insertions(+), 63 deletions(-) diff --git a/docs/stripe-utils/interfaces/DuplicatesConfig.html b/docs/stripe-utils/interfaces/DuplicatesConfig.html index a5f249b4..3df7626c 100644 --- a/docs/stripe-utils/interfaces/DuplicatesConfig.html +++ b/docs/stripe-utils/interfaces/DuplicatesConfig.html @@ -1 +1 @@ -DuplicatesConfig | @jackdbd/stripe-utils
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface DuplicatesConfig

Hierarchy

  • DuplicatesConfig

Index

Properties

stripe: Stripe
threshold?: number
ts_start: number
ts_stop: number

Generated using TypeDoc

\ No newline at end of file +DuplicatesConfig | @jackdbd/stripe-utils
Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface DuplicatesConfig

Hierarchy

  • DuplicatesConfig

Index

Properties

stripe: Stripe
threshold?: number
ts_start: number
ts_stop: number

Generated using TypeDoc

\ No newline at end of file diff --git a/docs/stripe-utils/modules.html b/docs/stripe-utils/modules.html index 3d0501e5..4d3fb373 100644 --- a/docs/stripe-utils/modules.html +++ b/docs/stripe-utils/modules.html @@ -1,22 +1,33 @@ -@jackdbd/stripe-utils
Options
All
  • Public
  • Public/Protected
  • All
Menu

@jackdbd/stripe-utils

Index

Functions

  • createPriceWithTaxBehavior(__namedParameters: Config): Promise<Response<Price>>
  • errorFromStripe(err: StripeError): { code: undefined | string; message: string; param: undefined | string; status_code: number }
  • isApiKeyLiveMode(s: string): boolean
  • +

    Checks whether the given string is an API key for a Stripe account in live mode.

    +

    Parameters

    • s: string

    Returns boolean

  • isApiKeyTestMode(s: string): boolean
  • +

    Checks whether the given string is an API key for a Stripe account in test mode.

    +

    Parameters

    • s: string

    Returns boolean

  • stripeAccountMode(api_key: string): "live" | "test"

Generated using TypeDoc

\ No newline at end of file diff --git a/package-lock.json b/package-lock.json index bc11d2d9..142ad915 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,6 +42,7 @@ "@types/pollyjs__persister-fs": "^2.0.1", "@types/simple-oauth2": "^4.1.1", "@typescript-eslint/eslint-plugin": "^5.27.1", + "@typescript-eslint/parser": "^5.31.0", "ajv": "^8.11.0", "ajv-formats": "^2.1.1", "better-sqlite3": "^7.5.3", @@ -70,7 +71,7 @@ "stripe": "^9.8.0", "ts-node": "^10.8.1", "typedoc": "^0.22.17", - "typescript": "^4.7.3", + "typescript": "^4.7.4", "wait-port": "^0.2.9", "yargs": "^17.5.1", "zx": "^6.2.4" @@ -4642,6 +4643,33 @@ } } }, + "node_modules/@typescript-eslint/parser": { + "version": "5.31.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.31.0.tgz", + "integrity": "sha512-UStjQiZ9OFTFReTrN+iGrC6O/ko9LVDhreEK5S3edmXgR396JGq7CoX2TWIptqt/ESzU2iRKXAHfSF2WJFcWHw==", + "dev": true, + "dependencies": { + "@typescript-eslint/scope-manager": "5.31.0", + "@typescript-eslint/types": "5.31.0", + "@typescript-eslint/typescript-estree": "5.31.0", + "debug": "^4.3.4" + }, + "engines": { + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "eslint": "^6.0.0 || ^7.0.0 || ^8.0.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, "node_modules/@typescript-eslint/scope-manager": { "version": "5.31.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.31.0.tgz", @@ -28312,6 +28340,18 @@ "tsutils": "^3.21.0" } }, + "@typescript-eslint/parser": { + "version": "5.31.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.31.0.tgz", + "integrity": "sha512-UStjQiZ9OFTFReTrN+iGrC6O/ko9LVDhreEK5S3edmXgR396JGq7CoX2TWIptqt/ESzU2iRKXAHfSF2WJFcWHw==", + "dev": true, + "requires": { + "@typescript-eslint/scope-manager": "5.31.0", + "@typescript-eslint/types": "5.31.0", + "@typescript-eslint/typescript-estree": "5.31.0", + "debug": "^4.3.4" + } + }, "@typescript-eslint/scope-manager": { "version": "5.31.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.31.0.tgz", diff --git a/package.json b/package.json index c9a35a87..bd9b7388 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "@types/pollyjs__persister-fs": "^2.0.1", "@types/simple-oauth2": "^4.1.1", "@typescript-eslint/eslint-plugin": "^5.27.1", + "@typescript-eslint/parser": "^5.31.0", "ajv": "^8.11.0", "ajv-formats": "^2.1.1", "better-sqlite3": "^7.5.3", @@ -114,7 +115,7 @@ "stripe": "^9.8.0", "ts-node": "^10.8.1", "typedoc": "^0.22.17", - "typescript": "^4.7.3", + "typescript": "^4.7.4", "wait-port": "^0.2.9", "yargs": "^17.5.1", "zx": "^6.2.4" diff --git a/packages/stripe-utils/.ae/stripe-utils.api.md b/packages/stripe-utils/.ae/stripe-utils.api.md index db7e77d0..c8a5b6a2 100644 --- a/packages/stripe-utils/.ae/stripe-utils.api.md +++ b/packages/stripe-utils/.ae/stripe-utils.api.md @@ -7,13 +7,11 @@ import Stripe from 'stripe'; // Warning: (ae-forgotten-export) The symbol "Config" needs to be exported by the entry point index.d.ts -// Warning: (ae-missing-release-tag) "createPriceWithTaxBehavior" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public export const createPriceWithTaxBehavior: ({ behavior, price, stripe, created_at, created_by }: Config_2) => Promise>; // Warning: (ae-forgotten-export) The symbol "Config" needs to be exported by the entry point index.d.ts -// Warning: (ae-missing-release-tag) "customerFromPaymentIntentId" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public export const customerFromPaymentIntentId: ({ stripe, pi_id }: Config) => Promise<{ @@ -25,13 +23,10 @@ export const customerFromPaymentIntentId: ({ stripe, pi_id }: Config) => Promise }>; // Warning: (ae-forgotten-export) The symbol "EmailIds" needs to be exported by the entry point index.d.ts -// Warning: (ae-missing-release-tag) "duplicates" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public export const duplicates: ({ stripe, threshold, ts_start, ts_stop }: DuplicatesConfig) => Promise; -// Warning: (ae-missing-release-tag) "DuplicatesConfig" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public (undocumented) export interface DuplicatesConfig { // (undocumented) @@ -50,8 +45,6 @@ export interface DuplicatesConfig { // @public export const enabledEventsForWebhookEndpoint: ({ stripe, url }: EnabledEventsForWebhookEndpoint) => Promise; -// Warning: (ae-missing-release-tag) "errorFromStripe" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public export const errorFromStripe: (err: Stripe.StripeError) => { code: string | undefined; @@ -60,21 +53,13 @@ export const errorFromStripe: (err: Stripe.StripeError) => { status_code: number; }; -// Warning: (ae-missing-release-tag) "isApiKeyLiveMode" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) +// @public export const isApiKeyLiveMode: (s: string) => boolean; -// Warning: (ae-missing-release-tag) "isApiKeyTestMode" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) +// @public export const isApiKeyTestMode: (s: string) => boolean; -// Warning: (ae-missing-release-tag) "stripeAccountMode" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// -// @public (undocumented) +// @public export const stripeAccountMode: (api_key: string) => "live" | "test"; -// (No @packageDocumentation comment for this package) - ``` diff --git a/packages/stripe-utils/.ae/tsdoc-metadata.json b/packages/stripe-utils/.ae/tsdoc-metadata.json index 1d96152a..944d6ce8 100644 --- a/packages/stripe-utils/.ae/tsdoc-metadata.json +++ b/packages/stripe-utils/.ae/tsdoc-metadata.json @@ -5,7 +5,7 @@ "toolPackages": [ { "packageName": "@microsoft/api-extractor", - "packageVersion": "7.25.0" + "packageVersion": "7.28.6" } ] } diff --git a/packages/stripe-utils/api-docs/index.md b/packages/stripe-utils/api-docs/index.md index 1853dc5e..d9e94091 100644 --- a/packages/stripe-utils/api-docs/index.md +++ b/packages/stripe-utils/api-docs/index.md @@ -8,5 +8,5 @@ | Package | Description | | --- | --- | -| [@jackdbd/stripe-utils](./stripe-utils.md) | | +| [@jackdbd/stripe-utils](./stripe-utils.md) | Utility functions that might be useful when working with Stripe. | diff --git a/packages/stripe-utils/api-docs/stripe-utils.createpricewithtaxbehavior.md b/packages/stripe-utils/api-docs/stripe-utils.createpricewithtaxbehavior.md index 5c5781eb..6129e142 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.createpricewithtaxbehavior.md +++ b/packages/stripe-utils/api-docs/stripe-utils.createpricewithtaxbehavior.md @@ -6,7 +6,7 @@ Create a new `Stripe.Price` with a defined `tax_behavior`. -In Stripe we can update only `nickname` and `metadata` of a `Price`, so if we need to define `tax_behavior` we have to create a new `Price`. https://stripe.com/docs/billing/subscriptions/products-and-prices\#changing-prices +In Stripe we can update only `nickname` and `metadata` of a `Price`, so if we need to define `tax_behavior` we have to create a new `Price`. Signature: diff --git a/packages/stripe-utils/api-docs/stripe-utils.customerfrompaymentintentid.md b/packages/stripe-utils/api-docs/stripe-utils.customerfrompaymentintentid.md index e4c4041f..3c56c8c7 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.customerfrompaymentintentid.md +++ b/packages/stripe-utils/api-docs/stripe-utils.customerfrompaymentintentid.md @@ -4,7 +4,7 @@ ## customerFromPaymentIntentId variable -Extract the Stripe Customer from a Payment Intent Id. +Extracts the Stripe Customer from a Payment Intent Id. Signature: diff --git a/packages/stripe-utils/api-docs/stripe-utils.duplicatesconfig.md b/packages/stripe-utils/api-docs/stripe-utils.duplicatesconfig.md index 6ca19f2a..72bd95df 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.duplicatesconfig.md +++ b/packages/stripe-utils/api-docs/stripe-utils.duplicatesconfig.md @@ -4,6 +4,7 @@ ## DuplicatesConfig interface + Signature: ```typescript @@ -12,10 +13,10 @@ export interface DuplicatesConfig ## Properties -| Property | Type | Description | -| --- | --- | --- | -| [stripe](./stripe-utils.duplicatesconfig.stripe.md) | Stripe | | -| [threshold?](./stripe-utils.duplicatesconfig.threshold.md) | number | (Optional) | -| [ts\_start](./stripe-utils.duplicatesconfig.ts_start.md) | number | | -| [ts\_stop](./stripe-utils.duplicatesconfig.ts_stop.md) | number | | +| Property | Modifiers | Type | Description | +| --- | --- | --- | --- | +| [stripe](./stripe-utils.duplicatesconfig.stripe.md) | | Stripe | | +| [threshold?](./stripe-utils.duplicatesconfig.threshold.md) | | number | (Optional) | +| [ts\_start](./stripe-utils.duplicatesconfig.ts_start.md) | | number | | +| [ts\_stop](./stripe-utils.duplicatesconfig.ts_stop.md) | | number | | diff --git a/packages/stripe-utils/api-docs/stripe-utils.errorfromstripe.md b/packages/stripe-utils/api-docs/stripe-utils.errorfromstripe.md index 02219756..9e18396d 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.errorfromstripe.md +++ b/packages/stripe-utils/api-docs/stripe-utils.errorfromstripe.md @@ -4,9 +4,7 @@ ## errorFromStripe variable -Convert an error coming from Stripe into an object with a consistent shape. - -https://github.com/stripe/stripe-node/blob/4e82ccafda2017654ac264c070e7ebfa0e662fcd/lib/Error.js https://github.com/stripe/stripe-node/wiki/Error-Handling https://stripe.com/docs/error-codes +Converts an error coming from Stripe into an object with a consistent shape. Signature: diff --git a/packages/stripe-utils/api-docs/stripe-utils.isapikeylivemode.md b/packages/stripe-utils/api-docs/stripe-utils.isapikeylivemode.md index 32b3778c..63f731a7 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.isapikeylivemode.md +++ b/packages/stripe-utils/api-docs/stripe-utils.isapikeylivemode.md @@ -4,6 +4,8 @@ ## isApiKeyLiveMode variable +Checks whether the given string is an API key for a Stripe account in `live` mode. + Signature: ```typescript diff --git a/packages/stripe-utils/api-docs/stripe-utils.isapikeytestmode.md b/packages/stripe-utils/api-docs/stripe-utils.isapikeytestmode.md index 615c14c1..31df8ad6 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.isapikeytestmode.md +++ b/packages/stripe-utils/api-docs/stripe-utils.isapikeytestmode.md @@ -4,6 +4,8 @@ ## isApiKeyTestMode variable +Checks whether the given string is an API key for a Stripe account in `test` mode. + Signature: ```typescript diff --git a/packages/stripe-utils/api-docs/stripe-utils.md b/packages/stripe-utils/api-docs/stripe-utils.md index e9192a34..ebca3465 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.md +++ b/packages/stripe-utils/api-docs/stripe-utils.md @@ -4,6 +4,8 @@ ## stripe-utils package +Utility functions that might be useful when working with Stripe. + ## Interfaces | Interface | Description | @@ -14,12 +16,12 @@ | Variable | Description | | --- | --- | -| [createPriceWithTaxBehavior](./stripe-utils.createpricewithtaxbehavior.md) | Create a new Stripe.Price with a defined tax_behavior.In Stripe we can update only nickname and metadata of a Price, so if we need to define tax_behavior we have to create a new Price. https://stripe.com/docs/billing/subscriptions/products-and-prices\#changing-prices | -| [customerFromPaymentIntentId](./stripe-utils.customerfrompaymentintentid.md) | Extract the Stripe Customer from a Payment Intent Id. | +| [createPriceWithTaxBehavior](./stripe-utils.createpricewithtaxbehavior.md) |

Create a new Stripe.Price with a defined tax_behavior.

In Stripe we can update only nickname and metadata of a Price, so if we need to define tax_behavior we have to create a new Price.

| +| [customerFromPaymentIntentId](./stripe-utils.customerfrompaymentintentid.md) | Extracts the Stripe Customer from a Payment Intent Id. | | [duplicates](./stripe-utils.duplicates.md) | Emails that appear more than threshold times in the given Stripe account. The search is restricted to the time range starting from ts_start to ts_stop (both excluded, and both expressed in Unix timestamps in seconds). | -| [enabledEventsForWebhookEndpoint](./stripe-utils.enabledeventsforwebhookendpoint.md) | List of webhook events that the Stripe account stripe is allowed to send to the webhook endpoint url.\*Note\*: you have to configure the events that Stripe sends to a webhook endpoint when you create/update a webhook endpoint in your Stripe account. | -| [errorFromStripe](./stripe-utils.errorfromstripe.md) | Convert an error coming from Stripe into an object with a consistent shape.https://github.com/stripe/stripe-node/blob/4e82ccafda2017654ac264c070e7ebfa0e662fcd/lib/Error.js https://github.com/stripe/stripe-node/wiki/Error-Handling https://stripe.com/docs/error-codes | -| [isApiKeyLiveMode](./stripe-utils.isapikeylivemode.md) | | -| [isApiKeyTestMode](./stripe-utils.isapikeytestmode.md) | | -| [stripeAccountMode](./stripe-utils.stripeaccountmode.md) | | +| [enabledEventsForWebhookEndpoint](./stripe-utils.enabledeventsforwebhookendpoint.md) |

List of webhook events that the Stripe account stripe is allowed to send to the webhook endpoint url.

\*Note\*: you have to configure the events that Stripe sends to a webhook endpoint when you create/update a webhook endpoint in your Stripe account.

| +| [errorFromStripe](./stripe-utils.errorfromstripe.md) | Converts an error coming from Stripe into an object with a consistent shape. | +| [isApiKeyLiveMode](./stripe-utils.isapikeylivemode.md) | Checks whether the given string is an API key for a Stripe account in live mode. | +| [isApiKeyTestMode](./stripe-utils.isapikeytestmode.md) | Checks whether the given string is an API key for a Stripe account in test mode. | +| [stripeAccountMode](./stripe-utils.stripeaccountmode.md) | Given a Stripe API key, infers whether the Stripe client was initialized in live mode or test mode. | diff --git a/packages/stripe-utils/api-docs/stripe-utils.stripeaccountmode.md b/packages/stripe-utils/api-docs/stripe-utils.stripeaccountmode.md index b9d82590..94a4e704 100644 --- a/packages/stripe-utils/api-docs/stripe-utils.stripeaccountmode.md +++ b/packages/stripe-utils/api-docs/stripe-utils.stripeaccountmode.md @@ -4,6 +4,8 @@ ## stripeAccountMode variable +Given a Stripe API key, infers whether the Stripe client was initialized in `live` mode or `test` mode. + Signature: ```typescript diff --git a/packages/stripe-utils/src/checks.ts b/packages/stripe-utils/src/checks.ts index 2bd5734e..d5a9aa3a 100644 --- a/packages/stripe-utils/src/checks.ts +++ b/packages/stripe-utils/src/checks.ts @@ -1,7 +1,17 @@ +/** + * Checks whether the given string is an API key for a Stripe account in `live` mode. + * + * @public + */ export const isApiKeyLiveMode = (s: string) => { return s.slice(0, 8) === 'sk_live_' } +/** + * Checks whether the given string is an API key for a Stripe account in `test` mode. + * + * @public + */ export const isApiKeyTestMode = (s: string) => { return s.slice(0, 8) === 'sk_test_' } diff --git a/packages/stripe-utils/src/customers.ts b/packages/stripe-utils/src/customers.ts index b2ccfa0d..de201a42 100644 --- a/packages/stripe-utils/src/customers.ts +++ b/packages/stripe-utils/src/customers.ts @@ -1,9 +1,15 @@ import type Stripe from 'stripe' +/** + * @public + */ export interface EmailIds { [email: string]: string[] } +/** + * @public + */ export interface DuplicatesConfig { stripe: Stripe threshold?: number @@ -15,6 +21,8 @@ export interface DuplicatesConfig { * Emails that appear more than `threshold` times in the given Stripe account. * The search is restricted to the time range starting from `ts_start` to * `ts_stop` (both excluded, and both expressed in Unix timestamps in seconds). + * + * @public */ export const duplicates = async ({ stripe, diff --git a/packages/stripe-utils/src/errors.ts b/packages/stripe-utils/src/errors.ts index ddc8be35..237fd8c7 100644 --- a/packages/stripe-utils/src/errors.ts +++ b/packages/stripe-utils/src/errors.ts @@ -1,11 +1,13 @@ import Stripe from 'stripe' /** - * Convert an error coming from Stripe into an object with a consistent shape. + * Converts an error coming from Stripe into an object with a consistent shape. * - * https://github.com/stripe/stripe-node/blob/4e82ccafda2017654ac264c070e7ebfa0e662fcd/lib/Error.js - * https://github.com/stripe/stripe-node/wiki/Error-Handling - * https://stripe.com/docs/error-codes + * @public + * + * @see [Error.js - stripe-node](https://github.com/stripe/stripe-node/blob/4e82ccafda2017654ac264c070e7ebfa0e662fcd/lib/Error.js) + * @see [Error Handling - stripe-node Wiki](https://github.com/stripe/stripe-node/wiki/Error-Handling) + * @see [Error codes - Stripe Docs](https://stripe.com/docs/error-codes) */ export const errorFromStripe = (err: Stripe.StripeError) => { const prefix = '[stripe] ' diff --git a/packages/stripe-utils/src/index.ts b/packages/stripe-utils/src/index.ts index 385a436e..a32cd2de 100644 --- a/packages/stripe-utils/src/index.ts +++ b/packages/stripe-utils/src/index.ts @@ -1,3 +1,9 @@ +/** + * Utility functions that might be useful when working with Stripe. + * + * @packageDocumentation + */ + export { isApiKeyLiveMode, isApiKeyTestMode } from './checks.js' export { duplicates } from './customers.js' diff --git a/packages/stripe-utils/src/payment_intents.ts b/packages/stripe-utils/src/payment_intents.ts index 8e37ad18..32d1b4f7 100644 --- a/packages/stripe-utils/src/payment_intents.ts +++ b/packages/stripe-utils/src/payment_intents.ts @@ -6,7 +6,9 @@ interface Config { } /** - * Extract the Stripe Customer from a Payment Intent Id. + * Extracts the Stripe Customer from a Payment Intent Id. + * + * @public */ export const customerFromPaymentIntentId = async ({ stripe, diff --git a/packages/stripe-utils/src/prices.ts b/packages/stripe-utils/src/prices.ts index 8604ca0f..5557e905 100644 --- a/packages/stripe-utils/src/prices.ts +++ b/packages/stripe-utils/src/prices.ts @@ -1,8 +1,14 @@ import type Stripe from 'stripe' +/** + * @public + */ export const reasonResourceWasDeleted = (id: string) => `Stripe resource ${id} was deleted.` +/** + * @public + */ export interface Config { behavior: 'inclusive' | 'exclusive' price: Stripe.Price @@ -16,7 +22,11 @@ export interface Config { * * In Stripe we can update only `nickname` and `metadata` of a `Price`, so if we * need to define `tax_behavior` we have to create a new `Price`. - * https://stripe.com/docs/billing/subscriptions/products-and-prices#changing-prices + * + * @public + * @experimental + * + * @see [Products and prices - Stripe Docs](https://stripe.com/docs/products-prices/overview) */ export const createPriceWithTaxBehavior = async ({ behavior, @@ -65,7 +75,10 @@ export const createPriceWithTaxBehavior = async ({ ...original_price } = price - let custom_unit_amount = undefined + let custom_unit_amount: + | Stripe.PriceCreateParams.CustomUnitAmount + | undefined = undefined + if (price.custom_unit_amount) { custom_unit_amount = { enabled: true, @@ -86,6 +99,48 @@ export const createPriceWithTaxBehavior = async ({ } } + let currency_options = undefined + if (original_price.currency_options) { + currency_options = {} as { + [k: string]: Stripe.PriceCreateParams.CurrencyOptions + } + for (const [k, v] of Object.entries(original_price.currency_options)) { + let custom_unit_amount: + | Stripe.PriceCreateParams.CurrencyOptions.CustomUnitAmount + | undefined = undefined + + if (v.custom_unit_amount) { + custom_unit_amount = { + enabled: true, + maximum: v.custom_unit_amount.maximum || undefined, + minimum: v.custom_unit_amount.minimum || undefined, + preset: v.custom_unit_amount.preset || undefined + } + } + + let tiers: Stripe.PriceCreateParams.Tier[] | undefined = undefined + if (v.tiers) { + tiers = v.tiers.map((tier) => { + return { + flat_amount: tier.flat_amount || undefined, + flat_amount_decimal: tier.flat_amount_decimal || undefined, + unit_amount: tier.unit_amount || undefined, + unit_amount_decimal: tier.unit_amount_decimal || undefined, + up_to: tier.up_to || 'inf' + } + }) + } + + currency_options[k] = { + custom_unit_amount, + tax_behavior: v.tax_behavior || undefined, + tiers, + unit_amount: v.unit_amount || undefined, + unit_amount_decimal: v.unit_amount_decimal || undefined + } + } + } + let metadata = original_price.metadata if (created_at) { metadata = { ...metadata, created_at } @@ -94,8 +149,9 @@ export const createPriceWithTaxBehavior = async ({ metadata = { ...metadata, created_by } } - const new_price = await stripe.prices.create({ + const params: Stripe.PriceCreateParams = { ...original_price, + currency_options, custom_unit_amount, metadata, nickname: nickname || `nickname of original price ${price.id}`, @@ -103,7 +159,8 @@ export const createPriceWithTaxBehavior = async ({ recurring: price_recurring, tax_behavior: behavior, unit_amount: price.unit_amount - }) + } + const new_price = await stripe.prices.create(params) return new_price } diff --git a/packages/stripe-utils/src/utils.ts b/packages/stripe-utils/src/utils.ts index cb1fa8e9..137419f6 100644 --- a/packages/stripe-utils/src/utils.ts +++ b/packages/stripe-utils/src/utils.ts @@ -1,3 +1,12 @@ +/** + * Given a Stripe API key, infers whether the Stripe client was initialized in + * `live` mode or `test` mode. + * + * @public + * + * @see [Testing - Stripe Docs](https://stripe.com/docs/testing) + * @see [Test and live modes overview - Stripe Docs](https://stripe.com/docs/keys#test-live-modes) + */ export const stripeAccountMode = (api_key: string) => { return api_key.slice(0, 8) === 'sk_live_' ? 'live' : 'test' } @@ -8,6 +17,8 @@ export const stripeAccountMode = (api_key: string) => { * A few methods in stripe-node (e.g. create, retrieve) need `this` to be * stripe.customers when they are called, so if we need to pass just the method * and not the entire Stripe client, we need to bind the method first. + * + * @public */ export function boundMethod(thisObject: any, method_name: string) { return thisObject[method_name].bind(thisObject) diff --git a/packages/stripe-utils/src/webhooks.ts b/packages/stripe-utils/src/webhooks.ts index f335d126..73351fc3 100644 --- a/packages/stripe-utils/src/webhooks.ts +++ b/packages/stripe-utils/src/webhooks.ts @@ -8,6 +8,9 @@ interface EnabledEventsForWebhookEndpoint { url: string } +/** + * @public + */ export const notWebhookEnpointForStripeAccount = (url: string) => `The URL you passed (${url}) is not a Stripe webhook endpoint for this Stripe account. Maybe you passed a Stripe client in TEST mode and a URL which is a webhook endpoint for Stripe in LIVE mode, or vice versa?`