Skip to content
← Public packages

@kentcdodds/stripe

Stripe helpers for customers, payments, invoices, subscriptions, products, payment links, refunds, and balance.

src/index.ts

185 lines · 4.2 KB · TypeScript
export {
	StripeApiError,
	formatStripeAmount,
	stripeDate,
	stripeList,
	stripeParams,
	stripeRequest,
	stripeSearch,
	stripeUploadFile,
} from './stripe-core.ts'
export {
	getAccount,
	getBalance,
	listBalanceTransactions,
	listEvents,
	listPayouts,
} from './account.ts'
export {
	createCustomer,
	deleteCustomer,
	getCustomer,
	listCustomers,
	searchCustomers,
	updateCustomer,
} from './customers.ts'
export {
	createRefund,
	getCharge,
	getPaymentIntent,
	listCharges,
	listPaymentIntents,
	listRefunds,
	searchCharges,
	searchPaymentIntents,
} from './payments.ts'
export {
	createInvoice,
	deleteDraftInvoice,
	finalizeInvoice,
	getInvoice,
	listInvoices,
	searchInvoices,
	sendInvoice,
	voidInvoice,
} from './invoices.ts'
export {
	cancelSubscription,
	createSubscription,
	getSubscription,
	listSubscriptions,
	searchSubscriptions,
} from './subscriptions.ts'
export {
	archiveProduct,
	createPaymentLink,
	createPrice,
	createProduct,
	deactivatePaymentLink,
	getProduct,
	listPaymentLinks,
	listPrices,
	listProducts,
} from './products.ts'
export { smokeTest } from './smoke-test.ts'

import {
	getAccount,
	getBalance,
	listBalanceTransactions,
	listEvents,
	listPayouts,
} from './account.ts'
import {
	createCustomer,
	deleteCustomer,
	getCustomer,
	listCustomers,
	searchCustomers,
	updateCustomer,
} from './customers.ts'
import {
	createInvoice,
	deleteDraftInvoice,
	finalizeInvoice,
	getInvoice,
	listInvoices,
	searchInvoices,
	sendInvoice,
	voidInvoice,
} from './invoices.ts'
import {
	createRefund,
	getCharge,
	getPaymentIntent,
	listCharges,
	listPaymentIntents,
	listRefunds,
	searchCharges,
	searchPaymentIntents,
} from './payments.ts'
import {
	archiveProduct,
	createPaymentLink,
	createPrice,
	createProduct,
	deactivatePaymentLink,
	getProduct,
	listPaymentLinks,
	listPrices,
	listProducts,
} from './products.ts'
import { smokeTest } from './smoke-test.ts'
import { stripeRequest } from './stripe-core.ts'
import {
	cancelSubscription,
	createSubscription,
	getSubscription,
	listSubscriptions,
	searchSubscriptions,
} from './subscriptions.ts'

const actions = {
	'smoke-test': smokeTest,
	'get-account': getAccount,
	'get-balance': getBalance,
	'list-balance-transactions': listBalanceTransactions,
	'list-payouts': listPayouts,
	'list-events': listEvents,
	'list-customers': listCustomers,
	'search-customers': searchCustomers,
	'get-customer': getCustomer,
	'create-customer': createCustomer,
	'update-customer': updateCustomer,
	'delete-customer': deleteCustomer,
	'list-payment-intents': listPaymentIntents,
	'get-payment-intent': getPaymentIntent,
	'search-payment-intents': searchPaymentIntents,
	'list-charges': listCharges,
	'get-charge': getCharge,
	'search-charges': searchCharges,
	'list-refunds': listRefunds,
	'create-refund': createRefund,
	'list-invoices': listInvoices,
	'get-invoice': getInvoice,
	'search-invoices': searchInvoices,
	'create-invoice': createInvoice,
	'finalize-invoice': finalizeInvoice,
	'send-invoice': sendInvoice,
	'void-invoice': voidInvoice,
	'delete-draft-invoice': deleteDraftInvoice,
	'list-subscriptions': listSubscriptions,
	'get-subscription': getSubscription,
	'search-subscriptions': searchSubscriptions,
	'create-subscription': createSubscription,
	'cancel-subscription': cancelSubscription,
	'list-products': listProducts,
	'get-product': getProduct,
	'create-product': createProduct,
	'archive-product': archiveProduct,
	'list-prices': listPrices,
	'create-price': createPrice,
	'list-payment-links': listPaymentLinks,
	'create-payment-link': createPaymentLink,
	'deactivate-payment-link': deactivatePaymentLink,
	request: stripeRequest,
} as const

export type StripeAction = keyof typeof actions

/**
 * Root dispatcher for Stripe helpers. Defaults to the read-only smoke test.
 * @example
 * import stripe from 'kody:@kentcdodds/stripe'
 * const result = await stripe({ action: 'list-customers', email: 'ada@example.com' })
 */
export default async function stripe(input: Record<string, unknown> = {}) {
	const action = String(input.action ?? 'smoke-test') as StripeAction
	const handler = actions[action]
	if (!handler) {
		throw new Error(
			'Unknown Stripe action: ' + action + '. Valid actions: ' + Object.keys(actions).join(', '),
		)
	}
	return await handler(input as never)
}