chore(plugin-stripe)!: add types exports and rename types to be more consistent with other plugins (#6216)

Co-authored-by: Elliot DeNolf <denolfe@gmail.com>
This commit is contained in:
Paul
2024-05-06 11:37:44 -03:00
committed by GitHub
parent 3c13df3c2d
commit 7f72006020
16 changed files with 80 additions and 70 deletions

View File

@@ -26,14 +26,17 @@
"import": "./src/index.ts",
"types": "./src/index.ts",
"default": "./src/index.ts"
},
"./*": {
"import": "./src/exports/*.ts",
"require": "./src/exports/*.ts",
"types": "./src/exports/*.ts"
}
},
"main": "./src/index.ts",
"types": "./src/index.ts",
"files": [
"dist",
"types.js",
"types.d.ts"
"dist"
],
"scripts": {
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:types",

View File

@@ -1,20 +1,20 @@
import type { Config } from 'payload/config'
import type { SanitizedStripeConfig, StripeConfig } from './types.js'
import type { SanitizedStripePluginConfig, StripePluginConfig } from './types.js'
import { getFields } from './fields/getFields.js'
export const stripePlugin =
(incomingStripeConfig: StripeConfig) =>
(incomingPluginConfig: StripePluginConfig) =>
(config: Config): Config => {
const { collections } = config
// set config defaults here
const stripeConfig: SanitizedStripeConfig = {
...incomingStripeConfig,
const pluginConfig: SanitizedStripePluginConfig = {
...incomingPluginConfig,
// TODO: in the next major version, default this to `false`
rest: incomingStripeConfig?.rest ?? true,
sync: incomingStripeConfig?.sync || [],
rest: incomingPluginConfig?.rest ?? true,
sync: incomingPluginConfig?.sync || [],
}
// NOTE: env variables are never passed to the client, but we need to know if `stripeSecretKey` is a test key
@@ -24,12 +24,12 @@ export const stripePlugin =
return {
...config,
collections: collections?.map((collection) => {
const syncConfig = stripeConfig.sync?.find((sync) => sync.collection === collection.slug)
const syncConfig = pluginConfig.sync?.find((sync) => sync.collection === collection.slug)
if (syncConfig) {
const fields = getFields({
collection,
stripeConfig,
pluginConfig,
syncConfig,
})
return {

View File

@@ -0,0 +1,9 @@
export type {
FieldSyncConfig,
SanitizedStripePluginConfig,
StripePluginConfig,
StripeProxy,
StripeWebhookHandler,
StripeWebhookHandlers,
SyncConfig,
} from '../types.js'

View File

@@ -1,18 +1,18 @@
import type { CollectionConfig, Field } from 'payload/types'
import type { SanitizedStripeConfig } from '../types.js'
import type { SanitizedStripePluginConfig } from '../types.js'
import { LinkToDoc } from '../ui/LinkToDoc.js'
interface Args {
collection: CollectionConfig
stripeConfig: SanitizedStripeConfig
pluginConfig: SanitizedStripePluginConfig
syncConfig: {
stripeResourceType: string
}
}
export const getFields = ({ collection, stripeConfig, syncConfig }: Args): Field[] => {
export const getFields = ({ collection, pluginConfig, syncConfig }: Args): Field[] => {
const stripeIDField: Field = {
name: 'stripeID',
type: 'text',
@@ -42,7 +42,7 @@ export const getFields = ({ collection, stripeConfig, syncConfig }: Args): Field
Field: LinkToDoc,
},
custom: {
isTestKey: stripeConfig.isTestKey,
isTestKey: pluginConfig.isTestKey,
nameOfIDField: 'stripeID',
stripeResourceType: syncConfig.stripeResourceType,
},

View File

@@ -3,7 +3,7 @@ import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload/typ
import { APIError } from 'payload/errors'
import Stripe from 'stripe'
import type { StripeConfig } from '../types.js'
import type { StripePluginConfig } from '../types.js'
import { deepen } from '../utilities/deepen.js'
@@ -21,14 +21,14 @@ type HookArgsWithCustomCollection = Omit<
export type CollectionBeforeValidateHookWithArgs = (
args: HookArgsWithCustomCollection & {
collection?: CollectionConfig
stripeConfig?: StripeConfig
pluginConfig?: StripePluginConfig
},
) => void
export const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (args) => {
const { collection, data, operation, req, stripeConfig } = args
const { collection, data, operation, pluginConfig, req } = args
const { logs, sync } = stripeConfig || {}
const { logs, sync } = pluginConfig || {}
const payload = req?.payload

View File

@@ -3,7 +3,7 @@ import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload/types'
import { APIError } from 'payload/errors'
import Stripe from 'stripe'
import type { StripeConfig } from '../types.js'
import type { StripePluginConfig } from '../types.js'
const stripeSecretKey = process.env.STRIPE_SECRET_KEY
// api version can only be the latest, stripe recommends ts ignoring it
@@ -16,14 +16,14 @@ type HookArgsWithCustomCollection = Omit<Parameters<CollectionAfterDeleteHook>[0
export type CollectionAfterDeleteHookWithArgs = (
args: HookArgsWithCustomCollection & {
collection?: CollectionConfig
stripeConfig?: StripeConfig
pluginConfig?: StripePluginConfig
},
) => void
export const deleteFromStripe: CollectionAfterDeleteHookWithArgs = async (args) => {
const { collection, doc, req, stripeConfig } = args
const { collection, doc, pluginConfig, req } = args
const { logs, sync } = stripeConfig || {}
const { logs, sync } = pluginConfig || {}
const { payload } = req
const { slug: collectionSlug } = collection || {}

View File

@@ -3,7 +3,7 @@ import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload/types
import { APIError } from 'payload/errors'
import Stripe from 'stripe'
import type { StripeConfig } from '../types.js'
import type { StripePluginConfig } from '../types.js'
import { deepen } from '../utilities/deepen.js'
@@ -21,14 +21,14 @@ type HookArgsWithCustomCollection = Omit<
export type CollectionBeforeChangeHookWithArgs = (
args: HookArgsWithCustomCollection & {
collection?: CollectionConfig
stripeConfig?: StripeConfig
pluginConfig?: StripePluginConfig
},
) => void
export const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async (args) => {
const { collection, data, operation, originalDoc, req, stripeConfig } = args
const { collection, data, operation, originalDoc, pluginConfig, req } = args
const { logs, sync } = stripeConfig || {}
const { logs, sync } = pluginConfig || {}
const { payload } = req

View File

@@ -1,6 +1,6 @@
import type { Config, Endpoint } from 'payload/config'
import type { SanitizedStripeConfig, StripeConfig } from './types.js'
import type { SanitizedStripePluginConfig, StripePluginConfig } from './types.js'
import { getFields } from './fields/getFields.js'
import { createNewInStripe } from './hooks/createNewInStripe.js'
@@ -13,12 +13,12 @@ export { LinkToDoc } from './ui/LinkToDoc.js'
export { stripeProxy } from './utilities/stripeProxy.js'
export const stripePlugin =
(incomingStripeConfig: StripeConfig) =>
(incomingStripeConfig: StripePluginConfig) =>
(config: Config): Config => {
const { collections } = config
// set config defaults here
const stripeConfig: SanitizedStripeConfig = {
const pluginConfig: SanitizedStripePluginConfig = {
...incomingStripeConfig,
// TODO: in the next major version, default this to `false`
rest: incomingStripeConfig?.rest ?? true,
@@ -35,8 +35,8 @@ export const stripePlugin =
handler: async (req) => {
const res = await stripeWebhooks({
config,
pluginConfig,
req,
stripeConfig,
})
return res
@@ -50,8 +50,8 @@ export const stripePlugin =
endpoints.push({
handler: async (req) => {
const res = await stripeREST({
pluginConfig,
req,
stripeConfig,
})
return res
@@ -66,12 +66,12 @@ export const stripePlugin =
collections: collections?.map((collection) => {
const { hooks: existingHooks } = collection
const syncConfig = stripeConfig.sync?.find((sync) => sync.collection === collection.slug)
const syncConfig = pluginConfig.sync?.find((sync) => sync.collection === collection.slug)
if (syncConfig) {
const fields = getFields({
collection,
stripeConfig,
pluginConfig,
syncConfig,
})
return {
@@ -85,7 +85,7 @@ export const stripePlugin =
deleteFromStripe({
...args,
collection,
stripeConfig,
pluginConfig,
}),
],
beforeChange: [
@@ -94,7 +94,7 @@ export const stripePlugin =
syncExistingWithStripe({
...args,
collection,
stripeConfig,
pluginConfig,
}),
],
beforeValidate: [
@@ -103,7 +103,7 @@ export const stripePlugin =
createNewInStripe({
...args,
collection,
stripeConfig,
pluginConfig,
}),
],
},

View File

@@ -2,18 +2,18 @@ import type { PayloadRequestWithData } from 'payload/types'
import { Forbidden } from 'payload/errors'
import type { StripeConfig } from '../types.js'
import type { StripePluginConfig } from '../types.js'
import { stripeProxy } from '../utilities/stripeProxy.js'
export const stripeREST = async (args: {
pluginConfig: StripePluginConfig
req: PayloadRequestWithData
stripeConfig: StripeConfig
}): Promise<any> => {
let responseStatus = 200
let responseJSON
const { req, stripeConfig } = args
const { pluginConfig, req } = args
const {
data: {
@@ -24,7 +24,7 @@ export const stripeREST = async (args: {
user,
} = req
const { stripeSecretKey } = stripeConfig
const { stripeSecretKey } = pluginConfig
try {
if (!user) {

View File

@@ -3,19 +3,19 @@ import type { PayloadRequestWithData } from 'payload/types'
import Stripe from 'stripe'
import type { StripeConfig } from '../types.js'
import type { StripePluginConfig } from '../types.js'
import { handleWebhooks } from '../webhooks/index.js'
export const stripeWebhooks = async (args: {
config: PayloadConfig
pluginConfig: StripePluginConfig
req: PayloadRequestWithData
stripeConfig: StripeConfig
}): Promise<any> => {
const { config, req, stripeConfig } = args
const { config, pluginConfig, req } = args
let returnStatus = 200
const { stripeSecretKey, stripeWebhooksEndpointSecret, webhooks } = stripeConfig
const { stripeSecretKey, stripeWebhooksEndpointSecret, webhooks } = pluginConfig
if (stripeWebhooksEndpointSecret) {
const stripe = new Stripe(stripeSecretKey, {
@@ -46,8 +46,8 @@ export const stripeWebhooks = async (args: {
config,
event,
payload: req.payload,
pluginConfig,
stripe,
stripeConfig,
})
// Fire external webhook handlers if they exist
@@ -56,8 +56,8 @@ export const stripeWebhooks = async (args: {
config,
event,
payload: req.payload,
pluginConfig,
stripe,
stripeConfig,
})
}
@@ -68,8 +68,8 @@ export const stripeWebhooks = async (args: {
config,
event,
payload: req.payload,
pluginConfig,
stripe,
stripeConfig,
})
}
}

View File

@@ -6,27 +6,27 @@ export type StripeWebhookHandler<T = any> = (args: {
config: PayloadConfig
event: T
payload: Payload
pluginConfig?: StripePluginConfig
stripe: Stripe
stripeConfig?: StripeConfig
}) => void
export interface StripeWebhookHandlers {
export type StripeWebhookHandlers = {
[webhookName: string]: StripeWebhookHandler
}
export interface FieldSyncConfig {
export type FieldSyncConfig = {
fieldPath: string
stripeProperty: string
}
export interface SyncConfig {
export type SyncConfig = {
collection: string
fields: FieldSyncConfig[]
stripeResourceType: 'customers' | 'products' // TODO: get this from Stripe types
stripeResourceTypeSingular: 'customer' | 'product' // TODO: there must be a better way to do this
}
export interface StripeConfig {
export type StripePluginConfig = {
isTestKey?: boolean
logs?: boolean
// @deprecated this will default as `false` in the next major version release
@@ -37,7 +37,7 @@ export interface StripeConfig {
webhooks?: StripeWebhookHandler | StripeWebhookHandlers
}
export type SanitizedStripeConfig = StripeConfig & {
export type SanitizedStripePluginConfig = StripePluginConfig & {
sync: SyncConfig[] // convert to required
}

View File

@@ -1,20 +1,20 @@
import { v4 as uuid } from 'uuid'
import type { SanitizedStripeConfig, StripeWebhookHandler } from '../types.js'
import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'
import { deepen } from '../utilities/deepen.js'
type HandleCreatedOrUpdated = (
args: Parameters<StripeWebhookHandler>[0] & {
resourceType: string
syncConfig: SanitizedStripeConfig['sync'][0]
syncConfig: SanitizedStripePluginConfig['sync'][0]
},
) => void
export const handleCreatedOrUpdated: HandleCreatedOrUpdated = async (args) => {
const { config: payloadConfig, event, payload, resourceType, stripeConfig, syncConfig } = args
const { config: payloadConfig, event, payload, pluginConfig, resourceType, syncConfig } = args
const { logs } = stripeConfig || {}
const { logs } = pluginConfig || {}
const stripeDoc: any = event?.data?.object || {}

View File

@@ -1,16 +1,16 @@
import type { SanitizedStripeConfig, StripeWebhookHandler } from '../types.js'
import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'
type HandleDeleted = (
args: Parameters<StripeWebhookHandler>[0] & {
resourceType: string
syncConfig: SanitizedStripeConfig['sync'][0]
syncConfig: SanitizedStripePluginConfig['sync'][0]
},
) => void
export const handleDeleted: HandleDeleted = async (args) => {
const { event, payload, resourceType, stripeConfig, syncConfig } = args
const { event, payload, pluginConfig, resourceType, syncConfig } = args
const { logs } = stripeConfig || {}
const { logs } = pluginConfig || {}
const collectionSlug = syncConfig?.collection

View File

@@ -4,9 +4,9 @@ import { handleCreatedOrUpdated } from './handleCreatedOrUpdated.js'
import { handleDeleted } from './handleDeleted.js'
export const handleWebhooks: StripeWebhookHandler = (args) => {
const { event, payload, stripeConfig } = args
const { event, payload, pluginConfig } = args
if (stripeConfig?.logs)
if (pluginConfig?.logs)
payload.logger.info(`🪝 Received Stripe '${event.type}' webhook event with ID: '${event.id}'.`)
// could also traverse into event.data.object.object to get the type, but that seems unreliable
@@ -14,7 +14,7 @@ export const handleWebhooks: StripeWebhookHandler = (args) => {
const resourceType = event.type.split('.')[0]
const method = event.type.split('.').pop()
const syncConfig = stripeConfig?.sync?.find(
const syncConfig = pluginConfig?.sync?.find(
(sync) => sync.stripeResourceTypeSingular === resourceType,
)
@@ -23,8 +23,8 @@ export const handleWebhooks: StripeWebhookHandler = (args) => {
case 'created': {
void handleCreatedOrUpdated({
...args,
pluginConfig,
resourceType,
stripeConfig,
syncConfig,
})
break
@@ -32,8 +32,8 @@ export const handleWebhooks: StripeWebhookHandler = (args) => {
case 'updated': {
void handleCreatedOrUpdated({
...args,
pluginConfig,
resourceType,
stripeConfig,
syncConfig,
})
break
@@ -41,8 +41,8 @@ export const handleWebhooks: StripeWebhookHandler = (args) => {
case 'deleted': {
void handleDeleted({
...args,
pluginConfig,
resourceType,
stripeConfig,
syncConfig,
})
break

View File

@@ -1 +0,0 @@
export * from './dist/types'

View File

@@ -1 +0,0 @@
module.exports = require('./dist/types')