@payloadcms/plugin-stripe 3.0.0-canary.fb81f02 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/exports/client.d.ts +2 -0
- package/dist/exports/client.d.ts.map +1 -0
- package/dist/exports/client.js +4 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/fields/getFields.d.ts.map +1 -1
- package/dist/fields/getFields.js +1 -2
- package/dist/fields/getFields.js.map +1 -1
- package/dist/hooks/createNewInStripe.d.ts +1 -1
- package/dist/hooks/createNewInStripe.d.ts.map +1 -1
- package/dist/hooks/createNewInStripe.js +21 -7
- package/dist/hooks/createNewInStripe.js.map +1 -1
- package/dist/hooks/deleteFromStripe.d.ts +1 -1
- package/dist/hooks/deleteFromStripe.d.ts.map +1 -1
- package/dist/hooks/deleteFromStripe.js +12 -4
- package/dist/hooks/deleteFromStripe.js.map +1 -1
- package/dist/hooks/syncExistingWithStripe.d.ts +1 -1
- package/dist/hooks/syncExistingWithStripe.d.ts.map +1 -1
- package/dist/hooks/syncExistingWithStripe.js +12 -4
- package/dist/hooks/syncExistingWithStripe.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/routes/rest.d.ts.map +1 -1
- package/dist/routes/rest.js +0 -2
- package/dist/routes/rest.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/ui/LinkToDoc.d.ts +2 -2
- package/dist/ui/LinkToDoc.d.ts.map +1 -1
- package/dist/ui/LinkToDoc.js +3 -3
- package/dist/ui/LinkToDoc.js.map +1 -1
- package/dist/webhooks/handleCreatedOrUpdated.d.ts +1 -1
- package/dist/webhooks/handleCreatedOrUpdated.d.ts.map +1 -1
- package/dist/webhooks/handleCreatedOrUpdated.js +42 -14
- package/dist/webhooks/handleCreatedOrUpdated.js.map +1 -1
- package/dist/webhooks/handleDeleted.d.ts +1 -1
- package/dist/webhooks/handleDeleted.d.ts.map +1 -1
- package/dist/webhooks/handleDeleted.js +21 -7
- package/dist/webhooks/handleDeleted.js.map +1 -1
- package/dist/webhooks/index.d.ts.map +1 -1
- package/dist/webhooks/index.js +7 -5
- package/dist/webhooks/index.js.map +1 -1
- package/license.md +22 -0
- package/package.json +25 -17
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/exports/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exports/client.ts"],"sourcesContent":["'use client'\n\nexport { LinkToDoc } from '../ui/LinkToDoc.js'\n"],"names":["LinkToDoc"],"mappings":"AAAA;AAEA,SAASA,SAAS,QAAQ,qBAAoB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getFields.d.ts","sourceRoot":"","sources":["../../src/fields/getFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEtD,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"getFields.d.ts","sourceRoot":"","sources":["../../src/fields/getFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEtD,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,aAAa,CAAA;AAE9D,UAAU,IAAI;IACZ,UAAU,EAAE,gBAAgB,CAAA;IAC5B,YAAY,EAAE,2BAA2B,CAAA;IACzC,UAAU,EAAE;QACV,kBAAkB,EAAE,MAAM,CAAA;KAC3B,CAAA;CACF;AAED,eAAO,MAAM,SAAS,6CAA8C,IAAI,KAAG,KAAK,EAyC/E,CAAA"}
|
package/dist/fields/getFields.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { LinkToDoc } from '../ui/LinkToDoc.js';
|
|
2
1
|
export const getFields = ({ collection, pluginConfig, syncConfig })=>{
|
|
3
2
|
const stripeIDField = {
|
|
4
3
|
name: 'stripeID',
|
|
@@ -24,7 +23,7 @@ export const getFields = ({ collection, pluginConfig, syncConfig })=>{
|
|
|
24
23
|
type: 'ui',
|
|
25
24
|
admin: {
|
|
26
25
|
components: {
|
|
27
|
-
Field: LinkToDoc
|
|
26
|
+
Field: '@payloadcms/plugin-stripe/client#LinkToDoc'
|
|
28
27
|
},
|
|
29
28
|
custom: {
|
|
30
29
|
isTestKey: pluginConfig.isTestKey,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/fields/getFields.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload'\n\nimport type { SanitizedStripePluginConfig } from '../types.js'\n\
|
|
1
|
+
{"version":3,"sources":["../../src/fields/getFields.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload'\n\nimport type { SanitizedStripePluginConfig } from '../types.js'\n\ninterface Args {\n collection: CollectionConfig\n pluginConfig: SanitizedStripePluginConfig\n syncConfig: {\n stripeResourceType: string\n }\n}\n\nexport const getFields = ({ collection, pluginConfig, syncConfig }: Args): Field[] => {\n const stripeIDField: Field = {\n name: 'stripeID',\n type: 'text',\n admin: {\n position: 'sidebar',\n readOnly: true,\n },\n label: 'Stripe ID',\n saveToJWT: true,\n }\n\n const skipSyncField: Field = {\n name: 'skipSync',\n type: 'checkbox',\n admin: {\n position: 'sidebar',\n readOnly: true,\n },\n label: 'Skip Sync',\n }\n\n const docUrlField: Field = {\n name: 'docUrl',\n type: 'ui',\n admin: {\n components: {\n Field: '@payloadcms/plugin-stripe/client#LinkToDoc',\n },\n custom: {\n isTestKey: pluginConfig.isTestKey,\n nameOfIDField: 'stripeID',\n stripeResourceType: syncConfig.stripeResourceType,\n },\n position: 'sidebar',\n },\n }\n\n const fields = [...collection.fields, stripeIDField, skipSyncField, docUrlField]\n\n return fields\n}\n"],"names":["getFields","collection","pluginConfig","syncConfig","stripeIDField","name","type","admin","position","readOnly","label","saveToJWT","skipSyncField","docUrlField","components","Field","custom","isTestKey","nameOfIDField","stripeResourceType","fields"],"mappings":"AAYA,OAAO,MAAMA,YAAY,CAAC,EAAEC,UAAU,EAAEC,YAAY,EAAEC,UAAU,EAAQ;IACtE,MAAMC,gBAAuB;QAC3BC,MAAM;QACNC,MAAM;QACNC,OAAO;YACLC,UAAU;YACVC,UAAU;QACZ;QACAC,OAAO;QACPC,WAAW;IACb;IAEA,MAAMC,gBAAuB;QAC3BP,MAAM;QACNC,MAAM;QACNC,OAAO;YACLC,UAAU;YACVC,UAAU;QACZ;QACAC,OAAO;IACT;IAEA,MAAMG,cAAqB;QACzBR,MAAM;QACNC,MAAM;QACNC,OAAO;YACLO,YAAY;gBACVC,OAAO;YACT;YACAC,QAAQ;gBACNC,WAAWf,aAAae,SAAS;gBACjCC,eAAe;gBACfC,oBAAoBhB,WAAWgB,kBAAkB;YACnD;YACAX,UAAU;QACZ;IACF;IAEA,MAAMY,SAAS;WAAInB,WAAWmB,MAAM;QAAEhB;QAAeQ;QAAeC;KAAY;IAEhF,OAAOO;AACT,EAAC"}
|
|
@@ -6,7 +6,7 @@ type HookArgsWithCustomCollection = {
|
|
|
6
6
|
export type CollectionBeforeValidateHookWithArgs = (args: {
|
|
7
7
|
collection?: CollectionConfig;
|
|
8
8
|
pluginConfig?: StripePluginConfig;
|
|
9
|
-
} & HookArgsWithCustomCollection) =>
|
|
9
|
+
} & HookArgsWithCustomCollection) => Promise<Partial<any>>;
|
|
10
10
|
export declare const createNewInStripe: CollectionBeforeValidateHookWithArgs;
|
|
11
11
|
export {};
|
|
12
12
|
//# sourceMappingURL=createNewInStripe.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createNewInStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/createNewInStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAK7E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAQrD,KAAK,4BAA4B,GAAG;IAClC,UAAU,EAAE,gBAAgB,CAAA;CAC7B,GAAG,IAAI,CAAC,UAAU,CAAC,4BAA4B,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAA;AAEnE,MAAM,MAAM,oCAAoC,GAAG,CACjD,IAAI,EAAE;IACJ,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,kBAAkB,CAAA;CAClC,GAAG,4BAA4B,KAC7B,
|
|
1
|
+
{"version":3,"file":"createNewInStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/createNewInStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,4BAA4B,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAK7E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAQrD,KAAK,4BAA4B,GAAG;IAClC,UAAU,EAAE,gBAAgB,CAAA;CAC7B,GAAG,IAAI,CAAC,UAAU,CAAC,4BAA4B,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAA;AAEnE,MAAM,MAAM,oCAAoC,GAAG,CACjD,IAAI,EAAE;IACJ,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,kBAAkB,CAAA;CAClC,GAAG,4BAA4B,KAC7B,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;AAE1B,eAAO,MAAM,iBAAiB,EAAE,oCAuH/B,CAAA"}
|
|
@@ -17,7 +17,9 @@ export const createNewInStripe = async (args)=>{
|
|
|
17
17
|
}
|
|
18
18
|
if (payload) {
|
|
19
19
|
if (data?.skipSync) {
|
|
20
|
-
if (logs)
|
|
20
|
+
if (logs) {
|
|
21
|
+
payload.logger.info(`Bypassing collection-level hooks.`);
|
|
22
|
+
}
|
|
21
23
|
} else {
|
|
22
24
|
// initialize as 'false' so that all Payload admin events sync to Stripe
|
|
23
25
|
// then conditionally set to 'true' to for events that originate from webhooks
|
|
@@ -34,31 +36,43 @@ export const createNewInStripe = async (args)=>{
|
|
|
34
36
|
}, {});
|
|
35
37
|
syncedFields = deepen(syncedFields);
|
|
36
38
|
if (operation === 'update') {
|
|
37
|
-
if (logs)
|
|
39
|
+
if (logs) {
|
|
40
|
+
payload.logger.info(`A '${collectionSlug}' document has changed in Payload with ID: '${data?.id}', syncing with Stripe...`);
|
|
41
|
+
}
|
|
38
42
|
// NOTE: the Stripe document will be created in the "afterChange" hook, so create a new stripe document here if no stripeID exists
|
|
39
43
|
if (!dataRef.stripeID) {
|
|
40
44
|
try {
|
|
41
45
|
// NOTE: Typed as "any" because the "create" method is not standard across all Stripe resources
|
|
42
46
|
const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(// @ts-expect-error
|
|
43
47
|
syncedFields);
|
|
44
|
-
if (logs)
|
|
48
|
+
if (logs) {
|
|
49
|
+
payload.logger.info(`✅ Successfully created new '${syncConfig.stripeResourceType}' resource in Stripe with ID: '${stripeResource.id}'.`);
|
|
50
|
+
}
|
|
45
51
|
dataRef.stripeID = stripeResource.id;
|
|
46
52
|
// NOTE: this is to prevent sync in the "afterChange" hook
|
|
47
53
|
dataRef.skipSync = true;
|
|
48
54
|
} catch (error) {
|
|
49
55
|
const msg = error instanceof Error ? error.message : error;
|
|
50
|
-
if (logs)
|
|
56
|
+
if (logs) {
|
|
57
|
+
payload.logger.error(`- Error creating Stripe document: ${msg}`);
|
|
58
|
+
}
|
|
51
59
|
}
|
|
52
60
|
}
|
|
53
61
|
}
|
|
54
62
|
if (operation === 'create') {
|
|
55
|
-
if (logs)
|
|
63
|
+
if (logs) {
|
|
64
|
+
payload.logger.info(`A new '${collectionSlug}' document was created in Payload with ID: '${data?.id}', syncing with Stripe...`);
|
|
65
|
+
}
|
|
56
66
|
try {
|
|
57
|
-
if (logs)
|
|
67
|
+
if (logs) {
|
|
68
|
+
payload.logger.info(`- Creating new '${syncConfig.stripeResourceType}' resource in Stripe...`);
|
|
69
|
+
}
|
|
58
70
|
// NOTE: Typed as "any" because the "create" method is not standard across all Stripe resources
|
|
59
71
|
const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(// @ts-expect-error
|
|
60
72
|
syncedFields);
|
|
61
|
-
if (logs)
|
|
73
|
+
if (logs) {
|
|
74
|
+
payload.logger.info(`✅ Successfully created new '${syncConfig.stripeResourceType}' resource in Stripe with ID: '${stripeResource.id}'.`);
|
|
75
|
+
}
|
|
62
76
|
dataRef.stripeID = stripeResource.id;
|
|
63
77
|
// IMPORTANT: this is to prevent sync in the "afterChange" hook
|
|
64
78
|
dataRef.skipSync = true;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/createNewInStripe.ts"],"sourcesContent":["import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeValidateHook>[0], 'collection'>\n\nexport type CollectionBeforeValidateHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) =>
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/createNewInStripe.ts"],"sourcesContent":["import type { CollectionBeforeValidateHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeValidateHook>[0], 'collection'>\n\nexport type CollectionBeforeValidateHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<Partial<any>>\n\nexport const createNewInStripe: CollectionBeforeValidateHookWithArgs = async (args) => {\n const { collection, data, operation, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\n\n const payload = req?.payload\n\n const dataRef = data || {}\n\n if (process.env.NODE_ENV === 'test') {\n dataRef.stripeID = 'test'\n return dataRef\n }\n\n if (payload) {\n if (data?.skipSync) {\n if (logs) {\n payload.logger.info(`Bypassing collection-level hooks.`)\n }\n } else {\n // initialize as 'false' so that all Payload admin events sync to Stripe\n // then conditionally set to 'true' to for events that originate from webhooks\n // this will prevent webhook events from triggering an unnecessary sync / infinite loop\n dataRef.skipSync = false\n\n const { slug: collectionSlug } = collection || {}\n const syncConfig = sync?.find((conf) => conf.collection === collectionSlug)\n\n if (syncConfig) {\n // combine all fields of this object and match their respective values within the document\n let syncedFields = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[stripeProperty] = dataRef[fieldPath]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedFields = deepen(syncedFields)\n\n if (operation === 'update') {\n if (logs) {\n payload.logger.info(\n `A '${collectionSlug}' document has changed in Payload with ID: '${data?.id}', syncing with Stripe...`,\n )\n }\n\n // NOTE: the Stripe document will be created in the \"afterChange\" hook, so create a new stripe document here if no stripeID exists\n if (!dataRef.stripeID) {\n try {\n // NOTE: Typed as \"any\" because the \"create\" method is not standard across all Stripe resources\n const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(\n // @ts-expect-error\n syncedFields,\n )\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully created new '${syncConfig.stripeResourceType}' resource in Stripe with ID: '${stripeResource.id}'.`,\n )\n }\n\n dataRef.stripeID = stripeResource.id\n\n // NOTE: this is to prevent sync in the \"afterChange\" hook\n dataRef.skipSync = true\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`- Error creating Stripe document: ${msg}`)\n }\n }\n }\n }\n\n if (operation === 'create') {\n if (logs) {\n payload.logger.info(\n `A new '${collectionSlug}' document was created in Payload with ID: '${data?.id}', syncing with Stripe...`,\n )\n }\n\n try {\n if (logs) {\n payload.logger.info(\n `- Creating new '${syncConfig.stripeResourceType}' resource in Stripe...`,\n )\n }\n\n // NOTE: Typed as \"any\" because the \"create\" method is not standard across all Stripe resources\n const stripeResource = await stripe?.[syncConfig.stripeResourceType]?.create(\n // @ts-expect-error\n syncedFields,\n )\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully created new '${syncConfig.stripeResourceType}' resource in Stripe with ID: '${stripeResource.id}'.`,\n )\n }\n\n dataRef.stripeID = stripeResource.id\n\n // IMPORTANT: this is to prevent sync in the \"afterChange\" hook\n dataRef.skipSync = true\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n throw new APIError(\n `Failed to create new '${syncConfig.stripeResourceType}' resource in Stripe: ${msg}`,\n )\n }\n }\n }\n }\n }\n\n return dataRef\n}\n"],"names":["APIError","Stripe","deepen","stripeSecretKey","process","env","STRIPE_SECRET_KEY","stripe","apiVersion","createNewInStripe","args","collection","data","operation","pluginConfig","req","logs","sync","payload","dataRef","NODE_ENV","stripeID","skipSync","logger","info","slug","collectionSlug","syncConfig","find","conf","syncedFields","fields","reduce","acc","field","fieldPath","stripeProperty","id","stripeResource","stripeResourceType","create","error","msg","Error","message"],"mappings":"AAEA,SAASA,QAAQ,QA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B,EAAEG,WAAWY,kBAAkB,CAAC,+BAA+B,EAAED,eAAeD,EAAE,CAAC,EAAE,CAAC;wBAEvH;wBAEAlB,QAAQE,QAAQ,GAAGiB,eAAeD,EAAE;wBAEpC,+DAA+D;wBAC/DlB,QAAQG,QAAQ,GAAG;oBACrB,EAAE,OAAOmB,OAAgB;wBACvB,MAAMC,MAAMD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAGH;wBACrD,MAAM,IAAIzC,SACR,CAAC,sBAAsB,EAAE2B,WAAWY,kBAAkB,CAAC,sBAAsB,EAAEG,IAAI,CAAC;oBAExF;gBACF;YACF;QACF;IACF;IAEA,OAAOvB;AACT,EAAC"}
|
|
@@ -6,7 +6,7 @@ type HookArgsWithCustomCollection = {
|
|
|
6
6
|
export type CollectionAfterDeleteHookWithArgs = (args: {
|
|
7
7
|
collection?: CollectionConfig;
|
|
8
8
|
pluginConfig?: StripePluginConfig;
|
|
9
|
-
} & HookArgsWithCustomCollection) => void
|
|
9
|
+
} & HookArgsWithCustomCollection) => Promise<void>;
|
|
10
10
|
export declare const deleteFromStripe: CollectionAfterDeleteHookWithArgs;
|
|
11
11
|
export {};
|
|
12
12
|
//# sourceMappingURL=deleteFromStripe.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deleteFromStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/deleteFromStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAK1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAMrD,KAAK,4BAA4B,GAAG;IAClC,UAAU,EAAE,gBAAgB,CAAA;CAC7B,GAAG,IAAI,CAAC,UAAU,CAAC,yBAAyB,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAA;AAEhE,MAAM,MAAM,iCAAiC,GAAG,CAC9C,IAAI,EAAE;IACJ,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,kBAAkB,CAAA;CAClC,GAAG,4BAA4B,KAC7B,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"deleteFromStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/deleteFromStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,yBAAyB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAK1E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAMrD,KAAK,4BAA4B,GAAG;IAClC,UAAU,EAAE,gBAAgB,CAAA;CAC7B,GAAG,IAAI,CAAC,UAAU,CAAC,yBAAyB,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAA;AAEhE,MAAM,MAAM,iCAAiC,GAAG,CAC9C,IAAI,EAAE;IACJ,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,kBAAkB,CAAA;CAClC,GAAG,4BAA4B,KAC7B,OAAO,CAAC,IAAI,CAAC,CAAA;AAElB,eAAO,MAAM,gBAAgB,EAAE,iCA6C9B,CAAA"}
|
|
@@ -10,18 +10,26 @@ export const deleteFromStripe = async (args)=>{
|
|
|
10
10
|
const { logs, sync } = pluginConfig || {};
|
|
11
11
|
const { payload } = req;
|
|
12
12
|
const { slug: collectionSlug } = collection || {};
|
|
13
|
-
if (logs)
|
|
13
|
+
if (logs) {
|
|
14
|
+
payload.logger.info(`Document with ID: '${doc?.id}' in collection: '${collectionSlug}' has been deleted, deleting from Stripe...`);
|
|
15
|
+
}
|
|
14
16
|
if (process.env.NODE_ENV !== 'test') {
|
|
15
|
-
if (logs)
|
|
17
|
+
if (logs) {
|
|
18
|
+
payload.logger.info(`- Deleting Stripe document with ID: '${doc.stripeID}'...`);
|
|
19
|
+
}
|
|
16
20
|
const syncConfig = sync?.find((conf)=>conf.collection === collectionSlug);
|
|
17
21
|
if (syncConfig) {
|
|
18
22
|
try {
|
|
19
23
|
const found = await stripe?.[syncConfig.stripeResourceType]?.retrieve(doc.stripeID);
|
|
20
24
|
if (found) {
|
|
21
25
|
await stripe?.[syncConfig.stripeResourceType]?.del(doc.stripeID);
|
|
22
|
-
if (logs)
|
|
26
|
+
if (logs) {
|
|
27
|
+
payload.logger.info(`✅ Successfully deleted Stripe document with ID: '${doc.stripeID}'.`);
|
|
28
|
+
}
|
|
23
29
|
} else {
|
|
24
|
-
if (logs)
|
|
30
|
+
if (logs) {
|
|
31
|
+
payload.logger.info(`- Stripe document with ID: '${doc.stripeID}' not found, skipping...`);
|
|
32
|
+
}
|
|
25
33
|
}
|
|
26
34
|
} catch (error) {
|
|
27
35
|
const msg = error instanceof Error ? error.message : error;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/deleteFromStripe.ts"],"sourcesContent":["import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'>\n\nexport type CollectionAfterDeleteHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => void
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/deleteFromStripe.ts"],"sourcesContent":["import type { CollectionAfterDeleteHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionAfterDeleteHook>[0], 'collection'>\n\nexport type CollectionAfterDeleteHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<void>\n\nexport const deleteFromStripe: CollectionAfterDeleteHookWithArgs = async (args) => {\n const { collection, doc, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\n\n const { payload } = req\n const { slug: collectionSlug } = collection || {}\n\n if (logs) {\n payload.logger.info(\n `Document with ID: '${doc?.id}' in collection: '${collectionSlug}' has been deleted, deleting from Stripe...`,\n )\n }\n\n if (process.env.NODE_ENV !== 'test') {\n if (logs) {\n payload.logger.info(`- Deleting Stripe document with ID: '${doc.stripeID}'...`)\n }\n\n const syncConfig = sync?.find((conf) => conf.collection === collectionSlug)\n\n if (syncConfig) {\n try {\n const found = await stripe?.[syncConfig.stripeResourceType]?.retrieve(doc.stripeID)\n\n if (found) {\n await stripe?.[syncConfig.stripeResourceType]?.del(doc.stripeID)\n if (logs) {\n payload.logger.info(\n `✅ Successfully deleted Stripe document with ID: '${doc.stripeID}'.`,\n )\n }\n } else {\n if (logs) {\n payload.logger.info(\n `- Stripe document with ID: '${doc.stripeID}' not found, skipping...`,\n )\n }\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n throw new APIError(`Failed to delete Stripe document with ID: '${doc.stripeID}': ${msg}`)\n }\n }\n }\n}\n"],"names":["APIError","Stripe","stripeSecretKey","process","env","STRIPE_SECRET_KEY","stripe","apiVersion","deleteFromStripe","args","collection","doc","pluginConfig","req","logs","sync","payload","slug","collectionSlug","logger","info","id","NODE_ENV","stripeID","syncConfig","find","conf","found","stripeResourceType","retrieve","del","error","msg","Error","message"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAClC,OAAOC,YAAY,SAAQ;AAI3B,MAAMC,kBAAkBC,QAAQC,GAAG,CAAC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EAAE,OAAOQ,OAAgB;gBACvB,MAAMC,MAAMD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAGH;gBACrD,MAAM,IAAI/B,SAAS,CAAC,2CAA2C,EAAEW,IAAIY,QAAQ,CAAC,GAAG,EAAES,IAAI,CAAC;YAC1F;QACF;IACF;AACF,EAAC"}
|
|
@@ -6,7 +6,7 @@ type HookArgsWithCustomCollection = {
|
|
|
6
6
|
export type CollectionBeforeChangeHookWithArgs = (args: {
|
|
7
7
|
collection?: CollectionConfig;
|
|
8
8
|
pluginConfig?: StripePluginConfig;
|
|
9
|
-
} & HookArgsWithCustomCollection) =>
|
|
9
|
+
} & HookArgsWithCustomCollection) => Promise<Partial<any>>;
|
|
10
10
|
export declare const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs;
|
|
11
11
|
export {};
|
|
12
12
|
//# sourceMappingURL=syncExistingWithStripe.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syncExistingWithStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/syncExistingWithStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAK3E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAQrD,KAAK,4BAA4B,GAAG;IAClC,UAAU,EAAE,gBAAgB,CAAA;CAC7B,GAAG,IAAI,CAAC,UAAU,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAA;AAEjE,MAAM,MAAM,kCAAkC,GAAG,CAC/C,IAAI,EAAE;IACJ,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,kBAAkB,CAAA;CAClC,GAAG,4BAA4B,KAC7B,
|
|
1
|
+
{"version":3,"file":"syncExistingWithStripe.d.ts","sourceRoot":"","sources":["../../src/hooks/syncExistingWithStripe.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAK3E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAQrD,KAAK,4BAA4B,GAAG;IAClC,UAAU,EAAE,gBAAgB,CAAA;CAC7B,GAAG,IAAI,CAAC,UAAU,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,CAAA;AAEjE,MAAM,MAAM,kCAAkC,GAAG,CAC/C,IAAI,EAAE;IACJ,UAAU,CAAC,EAAE,gBAAgB,CAAA;IAC7B,YAAY,CAAC,EAAE,kBAAkB,CAAA;CAClC,GAAG,4BAA4B,KAC7B,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;AAE1B,eAAO,MAAM,sBAAsB,EAAE,kCAmEpC,CAAA"}
|
|
@@ -22,15 +22,23 @@ export const syncExistingWithStripe = async (args)=>{
|
|
|
22
22
|
return acc;
|
|
23
23
|
}, {});
|
|
24
24
|
syncedFields = deepen(syncedFields);
|
|
25
|
-
if (logs)
|
|
25
|
+
if (logs) {
|
|
26
|
+
payload.logger.info(`A '${collectionSlug}' document has changed in Payload with ID: '${originalDoc?._id}', syncing with Stripe...`);
|
|
27
|
+
}
|
|
26
28
|
if (!data.stripeID) {
|
|
27
29
|
// NOTE: the "beforeValidate" hook populates this
|
|
28
|
-
if (logs)
|
|
30
|
+
if (logs) {
|
|
31
|
+
payload.logger.error(`- There is no Stripe ID for this document, skipping.`);
|
|
32
|
+
}
|
|
29
33
|
} else {
|
|
30
|
-
if (logs)
|
|
34
|
+
if (logs) {
|
|
35
|
+
payload.logger.info(`- Syncing to Stripe resource with ID: '${data.stripeID}'...`);
|
|
36
|
+
}
|
|
31
37
|
try {
|
|
32
38
|
const stripeResource = await stripe?.[syncConfig?.stripeResourceType]?.update(data.stripeID, syncedFields);
|
|
33
|
-
if (logs)
|
|
39
|
+
if (logs) {
|
|
40
|
+
payload.logger.info(`✅ Successfully synced Stripe resource with ID: '${stripeResource.id}'.`);
|
|
41
|
+
}
|
|
34
42
|
} catch (error) {
|
|
35
43
|
const msg = error instanceof Error ? error.message : error;
|
|
36
44
|
throw new APIError(`Failed to sync document with ID: '${data.id}' to Stripe: ${msg}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/syncExistingWithStripe.ts"],"sourcesContent":["import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeChangeHook>[0], 'collection'>\n\nexport type CollectionBeforeChangeHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) =>
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/syncExistingWithStripe.ts"],"sourcesContent":["import type { CollectionBeforeChangeHook, CollectionConfig } from 'payload'\n\nimport { APIError } from 'payload'\nimport Stripe from 'stripe'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\nconst stripeSecretKey = process.env.STRIPE_SECRET_KEY\n// api version can only be the latest, stripe recommends ts ignoring it\nconst stripe = new Stripe(stripeSecretKey || '', { apiVersion: '2022-08-01' })\n\ntype HookArgsWithCustomCollection = {\n collection: CollectionConfig\n} & Omit<Parameters<CollectionBeforeChangeHook>[0], 'collection'>\n\nexport type CollectionBeforeChangeHookWithArgs = (\n args: {\n collection?: CollectionConfig\n pluginConfig?: StripePluginConfig\n } & HookArgsWithCustomCollection,\n) => Promise<Partial<any>>\n\nexport const syncExistingWithStripe: CollectionBeforeChangeHookWithArgs = async (args) => {\n const { collection, data, operation, originalDoc, pluginConfig, req } = args\n\n const { logs, sync } = pluginConfig || {}\n\n const { payload } = req\n\n const { slug: collectionSlug } = collection || {}\n\n if (process.env.NODE_ENV !== 'test' && !data.skipSync) {\n const syncConfig = sync?.find((conf) => conf.collection === collectionSlug)\n\n if (syncConfig) {\n if (operation === 'update') {\n // combine all fields of this object and match their respective values within the document\n let syncedFields = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[stripeProperty] = data[fieldPath]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedFields = deepen(syncedFields)\n\n if (logs) {\n payload.logger.info(\n `A '${collectionSlug}' document has changed in Payload with ID: '${originalDoc?._id}', syncing with Stripe...`,\n )\n }\n\n if (!data.stripeID) {\n // NOTE: the \"beforeValidate\" hook populates this\n if (logs) {\n payload.logger.error(`- There is no Stripe ID for this document, skipping.`)\n }\n } else {\n if (logs) {\n payload.logger.info(`- Syncing to Stripe resource with ID: '${data.stripeID}'...`)\n }\n\n try {\n const stripeResource = await stripe?.[syncConfig?.stripeResourceType]?.update(\n data.stripeID,\n syncedFields,\n )\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully synced Stripe resource with ID: '${stripeResource.id}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n throw new APIError(`Failed to sync document with ID: '${data.id}' to Stripe: ${msg}`)\n }\n }\n }\n }\n }\n\n // Set back to 'false' so that all changes continue to sync to Stripe, see note in './createNewInStripe.ts'\n data.skipSync = false\n\n return data\n}\n"],"names":["APIError","Stripe","deepen","stripeSecretKey","process","env","STRIPE_SECRET_KEY","stripe","apiVersion","syncExistingWithStripe","args","collection","data","operation","originalDoc","pluginConfig","req","logs","sync","payload","slug","collectionSlug","NODE_ENV","skipSync","syncConfig","find","conf","syncedFields","fields","reduce","acc","field","fieldPath","stripeProperty","logger","info","_id","stripeID","error","stripeResource","stripeResourceType","update","id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,KAAK,CAAC,CAAC,oDAAoD,CAAC;oBAC7E;gBACF,OAAO;oBACL,IAAIrB,MAAM;wBACRE,QAAQe,MAAM,CAACC,IAAI,CAAC,CAAC,uCAAuC,EAAEvB,KAAKyB,QAAQ,CAAC,IAAI,CAAC;oBACnF;oBAEA,IAAI;wBACF,MAAME,iBAAiB,MAAMhC,QAAQ,CAACiB,YAAYgB,mBAAmB,EAAEC,OACrE7B,KAAKyB,QAAQ,EACbV;wBAGF,IAAIV,MAAM;4BACRE,QAAQe,MAAM,CAACC,IAAI,CACjB,CAAC,gDAAgD,EAAEI,eAAeG,EAAE,CAAC,EAAE,CAAC;wBAE5E;oBACF,EAAE,OAAOJ,OAAgB;wBACvB,MAAMK,MAAML,iBAAiBM,QAAQN,MAAMO,OAAO,GAAGP;wBACrD,MAAM,IAAItC,SAAS,CAAC,kCAAkC,EAAEY,KAAK8B,EAAE,CAAC,aAAa,EAAEC,IAAI,CAAC;oBACtF;gBACF;YACF;QACF;IACF;IAEA,2GAA2G;IAC3G/B,KAAKW,QAAQ,GAAG;IAEhB,OAAOX;AACT,EAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Config } from 'payload';
|
|
2
2
|
import type { StripePluginConfig } from './types.js';
|
|
3
|
-
export { LinkToDoc } from './ui/LinkToDoc.js';
|
|
4
3
|
export { stripeProxy } from './utilities/stripeProxy.js';
|
|
5
4
|
export declare const stripePlugin: (incomingStripeConfig: StripePluginConfig) => (config: Config) => Config;
|
|
6
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAY,MAAM,SAAS,CAAA;AAE/C,OAAO,KAAK,EAA+B,kBAAkB,EAAE,MAAM,YAAY,CAAA;AASjF,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAY,MAAM,SAAS,CAAA;AAE/C,OAAO,KAAK,EAA+B,kBAAkB,EAAE,MAAM,YAAY,CAAA;AASjF,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AAExD,eAAO,MAAM,YAAY,yBACA,kBAAkB,cAChC,MAAM,KAAG,MAmGjB,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,6 @@ import { deleteFromStripe } from './hooks/deleteFromStripe.js';
|
|
|
4
4
|
import { syncExistingWithStripe } from './hooks/syncExistingWithStripe.js';
|
|
5
5
|
import { stripeREST } from './routes/rest.js';
|
|
6
6
|
import { stripeWebhooks } from './routes/webhooks.js';
|
|
7
|
-
export { LinkToDoc } from './ui/LinkToDoc.js';
|
|
8
7
|
export { stripeProxy } from './utilities/stripeProxy.js';
|
|
9
8
|
export const stripePlugin = (incomingStripeConfig)=>(config)=>{
|
|
10
9
|
const { collections } = config;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config, Endpoint } from 'payload'\n\nimport type { SanitizedStripePluginConfig, StripePluginConfig } from './types.js'\n\nimport { getFields } from './fields/getFields.js'\nimport { createNewInStripe } from './hooks/createNewInStripe.js'\nimport { deleteFromStripe } from './hooks/deleteFromStripe.js'\nimport { syncExistingWithStripe } from './hooks/syncExistingWithStripe.js'\nimport { stripeREST } from './routes/rest.js'\nimport { stripeWebhooks } from './routes/webhooks.js'\n\nexport {
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config, Endpoint } from 'payload'\n\nimport type { SanitizedStripePluginConfig, StripePluginConfig } from './types.js'\n\nimport { getFields } from './fields/getFields.js'\nimport { createNewInStripe } from './hooks/createNewInStripe.js'\nimport { deleteFromStripe } from './hooks/deleteFromStripe.js'\nimport { syncExistingWithStripe } from './hooks/syncExistingWithStripe.js'\nimport { stripeREST } from './routes/rest.js'\nimport { stripeWebhooks } from './routes/webhooks.js'\n\nexport { stripeProxy } from './utilities/stripeProxy.js'\n\nexport const stripePlugin =\n (incomingStripeConfig: StripePluginConfig) =>\n (config: Config): Config => {\n const { collections } = config\n\n // set config defaults here\n const pluginConfig: SanitizedStripePluginConfig = {\n ...incomingStripeConfig,\n rest: incomingStripeConfig?.rest ?? false,\n sync: incomingStripeConfig?.sync || [],\n }\n\n // NOTE: env variables are never passed to the client, but we need to know if `stripeSecretKey` is a test key\n // unfortunately we must set the 'isTestKey' property on the config instead of using the following code:\n // const isTestKey = stripeConfig.stripeSecretKey?.startsWith('sk_test_');\n\n const endpoints: Endpoint[] = [\n ...(config?.endpoints || []),\n {\n handler: async (req) => {\n const res = await stripeWebhooks({\n config,\n pluginConfig,\n req,\n })\n\n return res\n },\n method: 'post',\n path: '/stripe/webhooks',\n },\n ]\n\n if (incomingStripeConfig?.rest) {\n endpoints.push({\n handler: async (req) => {\n const res = await stripeREST({\n pluginConfig,\n req,\n })\n\n return res\n },\n method: 'post' as Endpoint['method'],\n path: '/stripe/rest',\n })\n }\n\n return {\n ...config,\n collections: collections?.map((collection) => {\n const { hooks: existingHooks } = collection\n\n const syncConfig = pluginConfig.sync?.find((sync) => sync.collection === collection.slug)\n\n if (syncConfig) {\n const fields = getFields({\n collection,\n pluginConfig,\n syncConfig,\n })\n return {\n ...collection,\n fields,\n hooks: {\n ...collection.hooks,\n afterDelete: [\n ...(existingHooks?.afterDelete || []),\n (args) =>\n deleteFromStripe({\n ...args,\n collection,\n pluginConfig,\n }),\n ],\n beforeChange: [\n ...(existingHooks?.beforeChange || []),\n (args) =>\n syncExistingWithStripe({\n ...args,\n collection,\n pluginConfig,\n }),\n ],\n beforeValidate: [\n ...(existingHooks?.beforeValidate || []),\n (args) =>\n createNewInStripe({\n ...args,\n collection,\n pluginConfig,\n }),\n ],\n },\n }\n }\n\n return collection\n }),\n endpoints,\n }\n }\n"],"names":["getFields","createNewInStripe","deleteFromStripe","syncExistingWithStripe","stripeREST","stripeWebhooks","stripeProxy","stripePlugin","incomingStripeConfig","config","collections","pluginConfig","rest","sync","endpoints","handler","req","res","method","path","push","map","collection","hooks","existingHooks","syncConfig","find","slug","fields","afterDelete","args","beforeChange","beforeValidate"],"mappings":"AAIA,SAASA,SAAS,QAAQ,wBAAuB;AACjD,SAASC,iBAAiB,QAAQ,+BAA8B;AAChE,SAASC,gBAAgB,QAAQ,8BAA6B;AAC9D,SAASC,sBAAsB,QAAQ,oCAAmC;AAC1E,SAASC,UAAU,QAAQ,mBAAkB;AAC7C,SAASC,cAAc,QAAQ,uBAAsB;AAErD,SAASC,WAAW,QAAQ,6BAA4B;AAExD,OAAO,MAAMC,eACX,CAACC,uBACD,CAACC;QACC,MAAM,EAAEC,WAAW,EAAE,GAAGD;QAExB,2BAA2B;QAC3B,MAAME,eAA4C;YAChD,GAAGH,oBAAoB;YACvBI,MAAMJ,sBAAsBI,QAAQ;YACpCC,MAAML,sBAAsBK,QAAQ,EAAE;QACxC;QAEA,6GAA6G;QAC7G,wGAAwG;QACxG,0EAA0E;QAE1E,MAAMC,YAAwB;eACxBL,QAAQK,aAAa,EAAE;YAC3B;gBACEC,SAAS,OAAOC;oBACd,MAAMC,MAAM,MAAMZ,eAAe;wBAC/BI;wBACAE;wBACAK;oBACF;oBAEA,OAAOC;gBACT;gBACAC,QAAQ;gBACRC,MAAM;YACR;SACD;QAED,IAAIX,sBAAsBI,MAAM;YAC9BE,UAAUM,IAAI,CAAC;gBACbL,SAAS,OAAOC;oBACd,MAAMC,MAAM,MAAMb,WAAW;wBAC3BO;wBACAK;oBACF;oBAEA,OAAOC;gBACT;gBACAC,QAAQ;gBACRC,MAAM;YACR;QACF;QAEA,OAAO;YACL,GAAGV,MAAM;YACTC,aAAaA,aAAaW,IAAI,CAACC;gBAC7B,MAAM,EAAEC,OAAOC,aAAa,EAAE,GAAGF;gBAEjC,MAAMG,aAAad,aAAaE,IAAI,EAAEa,KAAK,CAACb,OAASA,KAAKS,UAAU,KAAKA,WAAWK,IAAI;gBAExF,IAAIF,YAAY;oBACd,MAAMG,SAAS5B,UAAU;wBACvBsB;wBACAX;wBACAc;oBACF;oBACA,OAAO;wBACL,GAAGH,UAAU;wBACbM;wBACAL,OAAO;4BACL,GAAGD,WAAWC,KAAK;4BACnBM,aAAa;mCACPL,eAAeK,eAAe,EAAE;gCACpC,CAACC,OACC5B,iBAAiB;wCACf,GAAG4B,IAAI;wCACPR;wCACAX;oCACF;6BACH;4BACDoB,cAAc;mCACRP,eAAeO,gBAAgB,EAAE;gCACrC,CAACD,OACC3B,uBAAuB;wCACrB,GAAG2B,IAAI;wCACPR;wCACAX;oCACF;6BACH;4BACDqB,gBAAgB;mCACVR,eAAeQ,kBAAkB,EAAE;gCACvC,CAACF,OACC7B,kBAAkB;wCAChB,GAAG6B,IAAI;wCACPR;wCACAX;oCACF;6BACH;wBACH;oBACF;gBACF;gBAEA,OAAOW;YACT;YACAR;QACF;IACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest.d.ts","sourceRoot":"","sources":["../../src/routes/rest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAK7C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAIrD,eAAO,MAAM,UAAU,SAAgB;IACrC,YAAY,EAAE,kBAAkB,CAAA;IAChC,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,OAAO,CAAC,GAAG,
|
|
1
|
+
{"version":3,"file":"rest.d.ts","sourceRoot":"","sources":["../../src/routes/rest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAK7C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAIrD,eAAO,MAAM,UAAU,SAAgB;IACrC,YAAY,EAAE,kBAAkB,CAAA;IAChC,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,OAAO,CAAC,GAAG,CAiDd,CAAA"}
|
package/dist/routes/rest.js
CHANGED
package/dist/routes/rest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/routes/rest.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { addDataAndFileToRequest } from '@payloadcms/next/utilities'\nimport { Forbidden } from 'payload'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { stripeProxy } from '../utilities/stripeProxy.js'\n\nexport const stripeREST = async (args: {\n pluginConfig: StripePluginConfig\n req: PayloadRequest\n}): Promise<any> => {\n let responseStatus = 200\n let responseJSON\n\n const { pluginConfig, req } = args\n\n await addDataAndFileToRequest(req)\n\n const requestWithData = req\n\n const {\n data: {\n stripeArgs, // example: ['cus_MGgt3Tuj3D66f2'] or [{ limit: 100 }, { stripeAccount: 'acct_1J9Z4pKZ4Z4Z4Z4Z' }]\n stripeMethod, // example: 'subscriptions.list',\n },\n payload,\n user,\n } = requestWithData\n\n const { stripeSecretKey } = pluginConfig\n\n try {\n if (!user) {\n // TODO: make this customizable from the config\n throw new Forbidden(req.t)\n }\n\n responseJSON = await stripeProxy({\n
|
|
1
|
+
{"version":3,"sources":["../../src/routes/rest.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { addDataAndFileToRequest } from '@payloadcms/next/utilities'\nimport { Forbidden } from 'payload'\n\nimport type { StripePluginConfig } from '../types.js'\n\nimport { stripeProxy } from '../utilities/stripeProxy.js'\n\nexport const stripeREST = async (args: {\n pluginConfig: StripePluginConfig\n req: PayloadRequest\n}): Promise<any> => {\n let responseStatus = 200\n let responseJSON\n\n const { pluginConfig, req } = args\n\n await addDataAndFileToRequest(req)\n\n const requestWithData = req\n\n const {\n data: {\n stripeArgs, // example: ['cus_MGgt3Tuj3D66f2'] or [{ limit: 100 }, { stripeAccount: 'acct_1J9Z4pKZ4Z4Z4Z4Z' }]\n stripeMethod, // example: 'subscriptions.list',\n },\n payload,\n user,\n } = requestWithData\n\n const { stripeSecretKey } = pluginConfig\n\n try {\n if (!user) {\n // TODO: make this customizable from the config\n throw new Forbidden(req.t)\n }\n\n responseJSON = await stripeProxy({\n stripeArgs,\n stripeMethod,\n stripeSecretKey,\n })\n\n const { status } = responseJSON\n responseStatus = status\n } catch (error: unknown) {\n const message = `An error has occurred in the Stripe plugin REST handler: '${JSON.stringify(\n error,\n )}'`\n payload.logger.error(message)\n responseStatus = 500\n responseJSON = {\n message,\n }\n }\n\n return Response.json(responseJSON, {\n status: responseStatus,\n })\n}\n"],"names":["addDataAndFileToRequest","Forbidden","stripeProxy","stripeREST","args","responseStatus","responseJSON","pluginConfig","req","requestWithData","data","stripeArgs","stripeMethod","payload","user","stripeSecretKey","t","status","error","message","JSON","stringify","logger","Response","json"],"mappings":"AAEA,SAASA,uBAAuB,QAAQ,6BAA4B;AACpE,SAASC,SAAS,QAAQ,UAAS;AAInC,SAASC,WAAW,QAAQ,8BAA6B;AAEzD,OAAO,MAAMC,aAAa,OAAOC;IAI/B,IAAIC,iBAAiB;IACrB,IAAIC;IAEJ,MAAM,EAAEC,YAAY,EAAEC,GAAG,EAAE,GAAGJ;IAE9B,MAAMJ,wBAAwBQ;IAE9B,MAAMC,kBAAkBD;IAExB,MAAM,EACJE,MAAM,EACJC,UAAU,EACVC,YAAY,EACb,EACDC,OAAO,EACPC,IAAI,EACL,GAAGL;IAEJ,MAAM,EAAEM,eAAe,EAAE,GAAGR;IAE5B,IAAI;QACF,IAAI,CAACO,MAAM;YACT,+CAA+C;YAC/C,MAAM,IAAIb,UAAUO,IAAIQ,CAAC;QAC3B;QAEAV,eAAe,MAAMJ,YAAY;YAC/BS;YACAC;YACAG;QACF;QAEA,MAAM,EAAEE,MAAM,EAAE,GAAGX;QACnBD,iBAAiBY;IACnB,EAAE,OAAOC,OAAgB;QACvB,MAAMC,UAAU,CAAC,0DAA0D,EAAEC,KAAKC,SAAS,CACzFH,OACA,CAAC,CAAC;QACJL,QAAQS,MAAM,CAACJ,KAAK,CAACC;QACrBd,iBAAiB;QACjBC,eAAe;YACba;QACF;IACF;IAEA,OAAOI,SAASC,IAAI,CAAClB,cAAc;QACjCW,QAAQZ;IACV;AACF,EAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Payload, Config as PayloadConfig, PayloadRequest } from 'payload';
|
|
1
|
+
import type { CollectionSlug, Payload, Config as PayloadConfig, PayloadRequest } from 'payload';
|
|
2
2
|
import type Stripe from 'stripe';
|
|
3
3
|
export type StripeWebhookHandler<T = any> = (args: {
|
|
4
4
|
config: PayloadConfig;
|
|
@@ -16,7 +16,7 @@ export type FieldSyncConfig = {
|
|
|
16
16
|
stripeProperty: string;
|
|
17
17
|
};
|
|
18
18
|
export type SyncConfig = {
|
|
19
|
-
collection:
|
|
19
|
+
collection: CollectionSlug;
|
|
20
20
|
fields: FieldSyncConfig[];
|
|
21
21
|
stripeResourceType: 'customers' | 'products';
|
|
22
22
|
stripeResourceTypeSingular: 'customer' | 'product';
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,IAAI,aAAa,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC/
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,IAAI,aAAa,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC/F,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAA;AAEhC,MAAM,MAAM,oBAAoB,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,I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OAAO,CAAC;IACZ,IAAI,CAAC,EAAE,GAAG,CAAA;IACV,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;CACf,CAAC,CAAA"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { Payload, Config as PayloadConfig, PayloadRequest } from 'payload'\nimport type Stripe from 'stripe'\n\nexport type StripeWebhookHandler<T = any> = (args: {\n config: PayloadConfig\n event: T\n payload: Payload\n pluginConfig?: StripePluginConfig\n req: PayloadRequest\n stripe: Stripe\n}) => Promise<void> | void\n\nexport type StripeWebhookHandlers = {\n [webhookName: string]: StripeWebhookHandler\n}\n\nexport type FieldSyncConfig = {\n fieldPath: string\n stripeProperty: string\n}\n\nexport type SyncConfig = {\n collection:
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { CollectionSlug, Payload, Config as PayloadConfig, PayloadRequest } from 'payload'\nimport type Stripe from 'stripe'\n\nexport type StripeWebhookHandler<T = any> = (args: {\n config: PayloadConfig\n event: T\n payload: Payload\n pluginConfig?: StripePluginConfig\n req: PayloadRequest\n stripe: Stripe\n}) => Promise<void> | void\n\nexport type StripeWebhookHandlers = {\n [webhookName: string]: StripeWebhookHandler\n}\n\nexport type FieldSyncConfig = {\n fieldPath: string\n stripeProperty: string\n}\n\nexport type SyncConfig = {\n collection: CollectionSlug\n fields: FieldSyncConfig[]\n stripeResourceType: 'customers' | 'products' // TODO: get this from Stripe types\n stripeResourceTypeSingular: 'customer' | 'product' // TODO: there must be a better way to do this\n}\n\nexport type StripePluginConfig = {\n isTestKey?: boolean\n logs?: boolean\n /** @default false */\n rest?: boolean\n stripeSecretKey: string\n stripeWebhooksEndpointSecret?: string\n sync?: SyncConfig[]\n webhooks?: StripeWebhookHandler | StripeWebhookHandlers\n}\n\nexport type SanitizedStripePluginConfig = {\n sync: SyncConfig[] // convert to required\n} & StripePluginConfig\n\nexport type StripeProxy = (args: {\n stripeArgs: any[]\n stripeMethod: string\n stripeSecretKey: string\n}) => Promise<{\n data?: any\n message?: string\n status: number\n}>\n"],"names":[],"mappings":"AA2CA,WAQE"}
|
package/dist/ui/LinkToDoc.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const LinkToDoc:
|
|
1
|
+
import type { UIFieldClientComponent } from 'payload';
|
|
2
|
+
export declare const LinkToDoc: UIFieldClientComponent;
|
|
3
3
|
//# sourceMappingURL=LinkToDoc.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LinkToDoc.d.ts","sourceRoot":"","sources":["../../src/ui/LinkToDoc.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"LinkToDoc.d.ts","sourceRoot":"","sources":["../../src/ui/LinkToDoc.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAA;AAKrD,eAAO,MAAM,SAAS,EAAE,sBA0CvB,CAAA"}
|
package/dist/ui/LinkToDoc.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { CopyToClipboard,
|
|
3
|
+
import { CopyToClipboard, useFormFields } from '@payloadcms/ui';
|
|
4
4
|
import React from 'react';
|
|
5
|
-
export const LinkToDoc = ()=>{
|
|
6
|
-
const { custom } =
|
|
5
|
+
export const LinkToDoc = (props)=>{
|
|
6
|
+
const { field: { admin: { custom = {} } = {} } } = props;
|
|
7
7
|
const { isTestKey, nameOfIDField, stripeResourceType } = custom;
|
|
8
8
|
const field = useFormFields(([fields])=>fields && fields?.[nameOfIDField] || null);
|
|
9
9
|
const { value: stripeID } = field || {};
|
package/dist/ui/LinkToDoc.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/ui/LinkToDoc.tsx"],"sourcesContent":["'use client'\nimport type {
|
|
1
|
+
{"version":3,"sources":["../../src/ui/LinkToDoc.tsx"],"sourcesContent":["'use client'\nimport type { UIFieldClientComponent } from 'payload'\n\nimport { CopyToClipboard, useFormFields } from '@payloadcms/ui'\nimport React from 'react'\n\nexport const LinkToDoc: UIFieldClientComponent = (props) => {\n const {\n field: { admin: { custom = {} } = {} },\n } = props\n const { isTestKey, nameOfIDField, stripeResourceType } = custom\n\n const field = useFormFields(([fields]) => (fields && fields?.[nameOfIDField]) || null)\n const { value: stripeID } = field || {}\n\n const stripeEnv = isTestKey ? 'test/' : ''\n const href = `https://dashboard.stripe.com/${stripeEnv}${stripeResourceType}/${stripeID}`\n\n if (stripeID) {\n return (\n <div>\n <div>\n <span\n className=\"label\"\n style={{\n color: '#9A9A9A',\n }}\n >\n View in Stripe\n </span>\n <CopyToClipboard value={href} />\n </div>\n <div\n style={{\n fontWeight: '600',\n overflow: 'hidden',\n textOverflow: 'ellipsis',\n }}\n >\n <a href={href} rel=\"noreferrer noopener\" target=\"_blank\">\n {href}\n </a>\n </div>\n </div>\n )\n }\n\n return null\n}\n"],"names":["CopyToClipboard","useFormFields","React","LinkToDoc","props","field","admin","custom","isTestKey","nameOfIDField","stripeResourceType","fields","value","stripeID","stripeEnv","href","div","span","className","style","color","fontWeight","overflow","textOverflow","a","rel","target"],"mappings":"AAAA;;AAGA,SAASA,eAAe,EAAEC,aAAa,QAAQ,iBAAgB;AAC/D,OAAOC,WAAW,QAAO;AAEzB,OAAO,MAAMC,YAAoC,CAACC;IAChD,MAAM,EACJC,OAAO,EAAEC,OAAO,EAAEC,SAAS,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,EAAE,EACvC,GAAGH;IACJ,MAAM,EAAEI,SAAS,EAAEC,aAAa,EAAEC,kBAAkB,EAAE,GAAGH;IAEzD,MAAMF,QAAQJ,cAAc,CAAC,CAACU,OAAO,GAAK,AAACA,UAAUA,QAAQ,CAACF,cAAc,IAAK;IACjF,MAAM,EAAEG,OAAOC,QAAQ,EAAE,GAAGR,SAAS,CAAC;IAEtC,MAAMS,YAAYN,YAAY,UAAU;IACxC,MAAMO,OAAO,CAAC,6BAA6B,EAAED,UAAU,EAAEJ,mBAAmB,CAAC,EAAEG,SAAS,CAAC;IAEzF,IAAIA,UAAU;QACZ,qBACE,MAACG;;8BACC,MAACA;;sCACC,KAACC;4BACCC,WAAU;4BACVC,OAAO;gCACLC,OAAO;4BACT;sCACD;;sCAGD,KAACpB;4BAAgBY,OAAOG;;;;8BAE1B,KAACC;oBACCG,OAAO;wBACLE,YAAY;wBACZC,UAAU;wBACVC,cAAc;oBAChB;8BAEA,cAAA,KAACC;wBAAET,MAAMA;wBAAMU,KAAI;wBAAsBC,QAAO;kCAC7CX;;;;;IAKX;IAEA,OAAO;AACT,EAAC"}
|
|
@@ -2,7 +2,7 @@ import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types
|
|
|
2
2
|
type HandleCreatedOrUpdated = (args: {
|
|
3
3
|
resourceType: string;
|
|
4
4
|
syncConfig: SanitizedStripePluginConfig['sync'][0];
|
|
5
|
-
} & Parameters<StripeWebhookHandler>[0]) => void
|
|
5
|
+
} & Parameters<StripeWebhookHandler>[0]) => Promise<void>;
|
|
6
6
|
export declare const handleCreatedOrUpdated: HandleCreatedOrUpdated;
|
|
7
7
|
export {};
|
|
8
8
|
//# sourceMappingURL=handleCreatedOrUpdated.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleCreatedOrUpdated.d.ts","sourceRoot":"","sources":["../../src/webhooks/handleCreatedOrUpdated.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAIpF,KAAK,sBAAsB,GAAG,CAC5B,IAAI,EAAE;IACJ,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,2BAA2B,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;CACnD,GAAG,UAAU,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,KACpC,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"handleCreatedOrUpdated.d.ts","sourceRoot":"","sources":["../../src/webhooks/handleCreatedOrUpdated.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAIpF,KAAK,sBAAsB,GAAG,CAC5B,IAAI,EAAE;IACJ,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,2BAA2B,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;CACnD,GAAG,UAAU,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,KACpC,OAAO,CAAC,IAAI,CAAC,CAAA;AAElB,eAAO,MAAM,sBAAsB,EAAE,sBAqMpC,CAAA"}
|
|
@@ -14,10 +14,14 @@ export const handleCreatedOrUpdated = async (args)=>{
|
|
|
14
14
|
// stripeID = parentResource;
|
|
15
15
|
// }
|
|
16
16
|
if (isNestedChange) {
|
|
17
|
-
if (logs)
|
|
17
|
+
if (logs) {
|
|
18
|
+
payload.logger.info(`- This change occurred on a nested field of ${resourceType}. Nested fields are not yet supported in auto-sync but can be manually setup.`);
|
|
19
|
+
}
|
|
18
20
|
}
|
|
19
21
|
if (!isNestedChange) {
|
|
20
|
-
if (logs)
|
|
22
|
+
if (logs) {
|
|
23
|
+
payload.logger.info(`- A new document was created or updated in Stripe, now syncing to Payload...`);
|
|
24
|
+
}
|
|
21
25
|
const collectionSlug = syncConfig?.collection;
|
|
22
26
|
const isAuthCollection = Boolean(payloadConfig?.collections?.find((collection)=>collection.slug === collectionSlug)?.auth);
|
|
23
27
|
// First, search for an existing document in Payload
|
|
@@ -42,7 +46,9 @@ export const handleCreatedOrUpdated = async (args)=>{
|
|
|
42
46
|
stripeID
|
|
43
47
|
});
|
|
44
48
|
if (!foundDoc) {
|
|
45
|
-
if (logs)
|
|
49
|
+
if (logs) {
|
|
50
|
+
payload.logger.info(`- No existing '${collectionSlug}' document found with Stripe ID: '${stripeID}', creating new...`);
|
|
51
|
+
}
|
|
46
52
|
// auth docs must use unique emails
|
|
47
53
|
let authDoc = null;
|
|
48
54
|
if (isAuthCollection) {
|
|
@@ -58,7 +64,9 @@ export const handleCreatedOrUpdated = async (args)=>{
|
|
|
58
64
|
});
|
|
59
65
|
authDoc = authQuery.docs[0];
|
|
60
66
|
if (authDoc) {
|
|
61
|
-
if (logs)
|
|
67
|
+
if (logs) {
|
|
68
|
+
payload.logger.info(`- Account already exists with e-mail: ${stripeDoc.email}, updating now...`);
|
|
69
|
+
}
|
|
62
70
|
// account exists by email, so update it instead
|
|
63
71
|
try {
|
|
64
72
|
await payload.update({
|
|
@@ -66,23 +74,33 @@ export const handleCreatedOrUpdated = async (args)=>{
|
|
|
66
74
|
collection: collectionSlug,
|
|
67
75
|
data: syncedData
|
|
68
76
|
});
|
|
69
|
-
if (logs)
|
|
77
|
+
if (logs) {
|
|
78
|
+
payload.logger.info(`✅ Successfully updated '${collectionSlug}' document in Payload with ID: '${authDoc.id}.'`);
|
|
79
|
+
}
|
|
70
80
|
} catch (err) {
|
|
71
81
|
const msg = err instanceof Error ? err.message : err;
|
|
72
|
-
if (logs)
|
|
82
|
+
if (logs) {
|
|
83
|
+
payload.logger.error(`- Error updating existing '${collectionSlug}' document: ${msg}`);
|
|
84
|
+
}
|
|
73
85
|
}
|
|
74
86
|
}
|
|
75
87
|
} else {
|
|
76
|
-
if (logs)
|
|
88
|
+
if (logs) {
|
|
89
|
+
payload.logger.error(`No email was provided from Stripe, cannot create new '${collectionSlug}' document.`);
|
|
90
|
+
}
|
|
77
91
|
}
|
|
78
92
|
} catch (error) {
|
|
79
93
|
const msg = error instanceof Error ? error.message : error;
|
|
80
|
-
if (logs)
|
|
94
|
+
if (logs) {
|
|
95
|
+
payload.logger.error(`Error looking up '${collectionSlug}' document in Payload: ${msg}`);
|
|
96
|
+
}
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
99
|
if (!isAuthCollection || isAuthCollection && !authDoc) {
|
|
84
100
|
try {
|
|
85
|
-
if (logs)
|
|
101
|
+
if (logs) {
|
|
102
|
+
payload.logger.info(`- Creating new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`);
|
|
103
|
+
}
|
|
86
104
|
// generate a strong, unique password for the new user
|
|
87
105
|
const password = uuid();
|
|
88
106
|
await payload.create({
|
|
@@ -94,24 +112,34 @@ export const handleCreatedOrUpdated = async (args)=>{
|
|
|
94
112
|
},
|
|
95
113
|
disableVerificationEmail: isAuthCollection ? true : undefined
|
|
96
114
|
});
|
|
97
|
-
if (logs)
|
|
115
|
+
if (logs) {
|
|
116
|
+
payload.logger.info(`✅ Successfully created new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`);
|
|
117
|
+
}
|
|
98
118
|
} catch (error) {
|
|
99
119
|
const msg = error instanceof Error ? error.message : error;
|
|
100
|
-
if (logs)
|
|
120
|
+
if (logs) {
|
|
121
|
+
payload.logger.error(`Error creating new document in Payload: ${msg}`);
|
|
122
|
+
}
|
|
101
123
|
}
|
|
102
124
|
}
|
|
103
125
|
} else {
|
|
104
|
-
if (logs)
|
|
126
|
+
if (logs) {
|
|
127
|
+
payload.logger.info(`- Existing '${collectionSlug}' document found in Payload with Stripe ID: '${stripeID}', updating now...`);
|
|
128
|
+
}
|
|
105
129
|
try {
|
|
106
130
|
await payload.update({
|
|
107
131
|
id: foundDoc.id,
|
|
108
132
|
collection: collectionSlug,
|
|
109
133
|
data: syncedData
|
|
110
134
|
});
|
|
111
|
-
if (logs)
|
|
135
|
+
if (logs) {
|
|
136
|
+
payload.logger.info(`✅ Successfully updated '${collectionSlug}' document in Payload from Stripe ID: '${stripeID}'.`);
|
|
137
|
+
}
|
|
112
138
|
} catch (error) {
|
|
113
139
|
const msg = error instanceof Error ? error.message : error;
|
|
114
|
-
if (logs)
|
|
140
|
+
if (logs) {
|
|
141
|
+
payload.logger.error(`Error updating '${collectionSlug}' document in Payload: ${msg}`);
|
|
142
|
+
}
|
|
115
143
|
}
|
|
116
144
|
}
|
|
117
145
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/webhooks/handleCreatedOrUpdated.ts"],"sourcesContent":["import { v4 as uuid } from 'uuid'\n\nimport type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\ntype HandleCreatedOrUpdated = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => void\n\nexport const handleCreatedOrUpdated: HandleCreatedOrUpdated = async (args) => {\n const { config: payloadConfig, event, payload, pluginConfig, resourceType, syncConfig } = args\n\n const { logs } = pluginConfig || {}\n\n const stripeDoc: any = event?.data?.object || {}\n\n const { id: stripeID, object: eventObject } = stripeDoc\n\n // NOTE: the Stripe API does not nest fields, everything is an object at the top level\n // if the event object and resource type don't match, this change was not top-level\n const isNestedChange = eventObject !== resourceType\n\n // let stripeID = docID;\n // if (isNestedChange) {\n // const parentResource = stripeDoc[resourceType];\n // stripeID = parentResource;\n // }\n\n if (isNestedChange) {\n if (logs)\n payload.logger.info(\n `- This change occurred on a nested field of ${resourceType}. Nested fields are not yet supported in auto-sync but can be manually setup.`,\n )\n }\n\n if (!isNestedChange) {\n if (logs)\n payload.logger.info(\n `- A new document was created or updated in Stripe, now syncing to Payload...`,\n )\n\n const collectionSlug = syncConfig?.collection\n\n const isAuthCollection = Boolean(\n payloadConfig?.collections?.find((collection) => collection.slug === collectionSlug)?.auth,\n )\n\n // First, search for an existing document in Payload\n const payloadQuery = await payload.find({\n collection: collectionSlug,\n where: {\n stripeID: {\n equals: stripeID,\n },\n },\n })\n\n const foundDoc = payloadQuery.docs[0] as any\n\n // combine all properties of the Stripe doc and match their respective fields within the document\n let syncedData = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[fieldPath] = stripeDoc[stripeProperty]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedData = deepen({\n ...syncedData,\n skipSync: true,\n stripeID,\n })\n\n if (!foundDoc) {\n if (logs)\n payload.logger.info(\n `- No existing '${collectionSlug}' document found with Stripe ID: '${stripeID}', creating new...`,\n )\n\n // auth docs must use unique emails\n let authDoc = null\n\n if (isAuthCollection) {\n try {\n if (stripeDoc?.email) {\n const authQuery = await payload.find({\n collection: collectionSlug,\n where: {\n email: {\n equals: stripeDoc.email,\n },\n },\n })\n\n authDoc = authQuery.docs[0] as any\n\n if (authDoc) {\n if (logs)\n payload.logger.info(\n `- Account already exists with e-mail: ${stripeDoc.email}, updating now...`,\n )\n\n // account exists by email, so update it instead\n try {\n await payload.update({\n id: authDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs)\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload with ID: '${authDoc.id}.'`,\n )\n } catch (err: unknown) {\n const msg = err instanceof Error ? err.message : err\n if (logs)\n payload.logger.error(\n `- Error updating existing '${collectionSlug}' document: ${msg}`,\n )\n }\n }\n } else {\n if (logs)\n payload.logger.error(\n `No email was provided from Stripe, cannot create new '${collectionSlug}' document.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs)\n payload.logger.error(`Error looking up '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n\n if (!isAuthCollection || (isAuthCollection && !authDoc)) {\n try {\n if (logs)\n payload.logger.info(\n `- Creating new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n\n // generate a strong, unique password for the new user\n const password: string = uuid()\n\n await payload.create({\n collection: collectionSlug,\n data: {\n ...syncedData,\n password,\n passwordConfirm: password,\n },\n disableVerificationEmail: isAuthCollection ? true : undefined,\n })\n\n if (logs)\n payload.logger.info(\n `✅ Successfully created new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) payload.logger.error(`Error creating new document in Payload: ${msg}`)\n }\n }\n } else {\n if (logs)\n payload.logger.info(\n `- Existing '${collectionSlug}' document found in Payload with Stripe ID: '${stripeID}', updating now...`,\n )\n\n try {\n await payload.update({\n id: foundDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs)\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload from Stripe ID: '${stripeID}'.`,\n )\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs)\n payload.logger.error(`Error updating '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n }\n}\n"],"names":["v4","uuid","deepen","handleCreatedOrUpdated","args","config","payloadConfig","event","payload","pluginConfig","resourceType","syncConfig","logs","stripeDoc","data","object","id","stripeID","eventObject","isNestedChange","logger","info","collectionSlug","collection","isAuthCollection","Boolean","collections","find","slug","auth","payloadQuery","where","equals","foundDoc","docs","syncedData","fields","reduce","acc","field","fieldPath","stripeProperty","skipSync","authDoc","email","authQuery","update","err","msg","Error","message","error","password","create","passwordConfirm","disableVerificationEmail","undefined"],"mappings":"AAAA,SAASA,MAAMC,IAAI,QAAQ,OAAM;AAIjC,SAASC,MAAM,QAAQ,yBAAwB;AAS/C,OAAO,MAAMC,yBAAiD,OAAOC;IACnE,MAAM,EAAEC,QAAQC,aAAa,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAEC,YAAY,EAAEC,UAAU,EAAE,GAAGP;IAE1F,MAAM,EAAEQ,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAElC,MAAMI,YAAiBN,OAAOO,MAAMC,UAAU,CAAC;IAE/C,MAAM,EAAEC,IAAIC,QAAQ,EAAEF,QAAQG,WAAW,EAAE,GAAGL;IAE9C,sFAAsF;IACtF,mFAAmF;IACnF,MAAMM,iBAAiBD,gBAAgBR;IAEvC,wBAAwB;IACxB,wB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AAQ6C,MAAM,CAAC;wBACnB9B,YAAYD;wBACZR,MAAM;4BACJ,GAAGqB,UAAU;4BACbiB;4BACAE,iBAAiBF;wBACnB;wBACAG,0BAA0B/B,mBAAmB,OAAOgC;oBACtD;oBAEA,IAAI5C,MACFJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,4BAA4B,EAAEC,eAAe,uCAAuC,EAAEL,SAAS,EAAE,CAAC;gBAEzG,EAAE,OAAOkC,OAAgB;oBACvB,MAAMH,MAAMG,iBAAiBF,QAAQE,MAAMD,OAAO,GAAGC;oBACrD,IAAIvC,MAAMJ,QAAQY,MAAM,CAAC+B,KAAK,CAAC,CAAC,wCAAwC,EAAEH,IAAI,CAAC;gBACjF;YACF;QACF,OAAO;YACL,IAAIpC,MACFJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,YAAY,EAAEC,eAAe,6CAA6C,EAAEL,SAAS,kBAAkB,CAAC;YAG7G,IAAI;gBACF,MAAMT,QAAQsC,MAAM,CAAC;oBACnB9B,IAAIiB,SAASjB,EAAE;oBACfO,YAAYD;oBACZR,MAAMqB;gBACR;gBAEA,IAAIvB,MACFJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,wBAAwB,EAAEC,eAAe,uCAAuC,EAAEL,SAAS,EAAE,CAAC;YAErG,EAAE,OAAOkC,OAAgB;gBACvB,MAAMH,MAAMG,iBAAiBF,QAAQE,MAAMD,OAAO,GAAGC;gBACrD,IAAIvC,MACFJ,QAAQY,MAAM,CAAC+B,KAAK,CAAC,CAAC,gBAAgB,EAAE7B,eAAe,uBAAuB,EAAE0B,IAAI,CAAC;YACzF;QACF;IACF;AACF,EAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/webhooks/handleCreatedOrUpdated.ts"],"sourcesContent":["import { v4 as uuid } from 'uuid'\n\nimport type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\nimport { deepen } from '../utilities/deepen.js'\n\ntype HandleCreatedOrUpdated = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => Promise<void>\n\nexport const handleCreatedOrUpdated: HandleCreatedOrUpdated = async (args) => {\n const { config: payloadConfig, event, payload, pluginConfig, resourceType, syncConfig } = args\n\n const { logs } = pluginConfig || {}\n\n const stripeDoc: any = event?.data?.object || {}\n\n const { id: stripeID, object: eventObject } = stripeDoc\n\n // NOTE: the Stripe API does not nest fields, everything is an object at the top level\n // if the event object and resource type don't match, this change was not top-level\n const isNestedChange = eventObject !== resourceType\n\n // let stripeID = docID;\n // if (isNestedChange) {\n // const parentResource = stripeDoc[resourceType];\n // stripeID = parentResource;\n // }\n\n if (isNestedChange) {\n if (logs) {\n payload.logger.info(\n `- This change occurred on a nested field of ${resourceType}. Nested fields are not yet supported in auto-sync but can be manually setup.`,\n )\n }\n }\n\n if (!isNestedChange) {\n if (logs) {\n payload.logger.info(\n `- A new document was created or updated in Stripe, now syncing to Payload...`,\n )\n }\n\n const collectionSlug = syncConfig?.collection\n\n const isAuthCollection = Boolean(\n payloadConfig?.collections?.find((collection) => collection.slug === collectionSlug)?.auth,\n )\n\n // First, search for an existing document in Payload\n const payloadQuery = await payload.find({\n collection: collectionSlug,\n where: {\n stripeID: {\n equals: stripeID,\n },\n },\n })\n\n const foundDoc = payloadQuery.docs[0] as any\n\n // combine all properties of the Stripe doc and match their respective fields within the document\n let syncedData = syncConfig.fields.reduce(\n (acc, field) => {\n const { fieldPath, stripeProperty } = field\n\n acc[fieldPath] = stripeDoc[stripeProperty]\n return acc\n },\n {} as Record<string, any>,\n )\n\n syncedData = deepen({\n ...syncedData,\n skipSync: true,\n stripeID,\n })\n\n if (!foundDoc) {\n if (logs) {\n payload.logger.info(\n `- No existing '${collectionSlug}' document found with Stripe ID: '${stripeID}', creating new...`,\n )\n }\n\n // auth docs must use unique emails\n let authDoc = null\n\n if (isAuthCollection) {\n try {\n if (stripeDoc?.email) {\n const authQuery = await payload.find({\n collection: collectionSlug,\n where: {\n email: {\n equals: stripeDoc.email,\n },\n },\n })\n\n authDoc = authQuery.docs[0] as any\n\n if (authDoc) {\n if (logs) {\n payload.logger.info(\n `- Account already exists with e-mail: ${stripeDoc.email}, updating now...`,\n )\n }\n\n // account exists by email, so update it instead\n try {\n await payload.update({\n id: authDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload with ID: '${authDoc.id}.'`,\n )\n }\n } catch (err: unknown) {\n const msg = err instanceof Error ? err.message : err\n if (logs) {\n payload.logger.error(\n `- Error updating existing '${collectionSlug}' document: ${msg}`,\n )\n }\n }\n }\n } else {\n if (logs) {\n payload.logger.error(\n `No email was provided from Stripe, cannot create new '${collectionSlug}' document.`,\n )\n }\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error looking up '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n }\n\n if (!isAuthCollection || (isAuthCollection && !authDoc)) {\n try {\n if (logs) {\n payload.logger.info(\n `- Creating new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n }\n\n // generate a strong, unique password for the new user\n const password: string = uuid()\n\n await payload.create({\n collection: collectionSlug,\n data: {\n ...syncedData,\n password,\n passwordConfirm: password,\n },\n disableVerificationEmail: isAuthCollection ? true : undefined,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully created new '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error creating new document in Payload: ${msg}`)\n }\n }\n }\n } else {\n if (logs) {\n payload.logger.info(\n `- Existing '${collectionSlug}' document found in Payload with Stripe ID: '${stripeID}', updating now...`,\n )\n }\n\n try {\n await payload.update({\n id: foundDoc.id,\n collection: collectionSlug,\n data: syncedData,\n })\n\n if (logs) {\n payload.logger.info(\n `✅ Successfully updated '${collectionSlug}' document in Payload from Stripe ID: '${stripeID}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error updating '${collectionSlug}' document in Payload: ${msg}`)\n }\n }\n }\n }\n}\n"],"names":["v4","uuid","deepen","handleCreatedOrUpdated","args","config","payloadConfig","event","payload","pluginConfig","resourceType","syncConfig","logs","stripeDoc","data","object","id","stripeID","eventObject","isNestedChange","logger","info","collectionSlug","collection","isAuthCollection","Boolean","collections","find","slug","auth","payloadQuery","where","equals","foundDoc","docs","syncedData","fields","reduce","acc","field","fieldPath","stripeProperty","skipSync","authDoc","email","authQuery","update","err","msg","Error","message","error","password","create","passwordConfirm","disableVerificationEmail","undefined"],"mappings":"AAAA,SAASA,MAAMC,IAAI,QAAQ,OAAM;AAIjC,SAASC,MAAM,QAAQ,yBAAwB;AAS/C,OAAO,MAAMC,yBAAiD,OAAOC;IACnE,MAAM,EAAEC,QAAQC,aAAa,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAEC,YAAY,EAAEC,UAAU,EAAE,GAAGP;IAE1F,MAAM,EAAEQ,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAElC,MAAMI,YAAiBN,OAAOO,MAAMC,UAAU,CAAC;IAE/C,MAAM,EAAEC,IAAIC,QAAQ,EAAEF,QAAQG,WAAW,EAAE,GAAGL;IAE9C,sFAAsF;IACtF,mFAAmF;IACnF,MAAMM,iBAAiBD,gBAAgBR;IAEvC,wBAAwB;IACxB,wBAAwB;IACxB,oDAAoD;IACpD,+BAA+B;IAC/B,IAAI;IAEJ,IAAIS,gBAAgB;QAClB,IAAIP,MAAM;YACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,4CAA4C,EAAEX,aAAa,6EAA6E,CAAC;QAE9I;IACF;IAEA,IAAI,CAACS,gBAAgB;QACnB,IAAIP,MAAM;YACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,4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MAAM;wBACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,4BAA4B,EAAEC,eAAe,uCAAuC,EAAEL,SAAS,EAAE,CAAC;oBAEvG;gBACF,EAAE,OAAOkC,OAAgB;oBACvB,MAAMH,MAAMG,iBAAiBF,QAAQE,MAAMD,OAAO,GAAGC;oBACrD,IAAIvC,MAAM;wBACRJ,QAAQY,MAAM,CAAC+B,KAAK,CAAC,CAAC,wCAAwC,EAAEH,IAAI,CAAC;oBACvE;gBACF;YACF;QACF,OAAO;YACL,IAAIpC,MAAM;gBACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,YAAY,EAAEC,eAAe,6CAA6C,EAAEL,SAAS,kBAAkB,CAAC;YAE7G;YAEA,IAAI;gBACF,MAAMT,QAAQsC,MAAM,CAAC;oBACnB9B,IAAIiB,SAASjB,EAAE;oBACfO,YAAYD;oBACZR,MAAMqB;gBACR;gBAEA,IAAIvB,MAAM;oBACRJ,QAAQY,MAAM,CAACC,IAAI,CACjB,CAAC,wBAAwB,EAAEC,eAAe,uCAAuC,EAAEL,SAAS,EAAE,CAAC;gBAEnG;YACF,EAAE,OAAOkC,OAAgB;gBACvB,MAAMH,MAAMG,iBAAiBF,QAAQE,MAAMD,OAAO,GAAGC;gBACrD,IAAIvC,MAAM;oBACRJ,QAAQY,MAAM,CAAC+B,KAAK,CAAC,CAAC,gBAAgB,EAAE7B,eAAe,uBAAuB,EAAE0B,IAAI,CAAC;gBACvF;YACF;QACF;IACF;AACF,EAAC"}
|
|
@@ -2,7 +2,7 @@ import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types
|
|
|
2
2
|
type HandleDeleted = (args: {
|
|
3
3
|
resourceType: string;
|
|
4
4
|
syncConfig: SanitizedStripePluginConfig['sync'][0];
|
|
5
|
-
} & Parameters<StripeWebhookHandler>[0]) => void
|
|
5
|
+
} & Parameters<StripeWebhookHandler>[0]) => Promise<void>;
|
|
6
6
|
export declare const handleDeleted: HandleDeleted;
|
|
7
7
|
export {};
|
|
8
8
|
//# sourceMappingURL=handleDeleted.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleDeleted.d.ts","sourceRoot":"","sources":["../../src/webhooks/handleDeleted.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAEpF,KAAK,aAAa,GAAG,CACnB,IAAI,EAAE;IACJ,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,2BAA2B,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;CACnD,GAAG,UAAU,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,KACpC,IAAI,CAAA;
|
|
1
|
+
{"version":3,"file":"handleDeleted.d.ts","sourceRoot":"","sources":["../../src/webhooks/handleDeleted.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAEpF,KAAK,aAAa,GAAG,CACnB,IAAI,EAAE;IACJ,YAAY,EAAE,MAAM,CAAA;IACpB,UAAU,EAAE,2BAA2B,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAA;CACnD,GAAG,UAAU,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,KACpC,OAAO,CAAC,IAAI,CAAC,CAAA;AAElB,eAAO,MAAM,aAAa,EAAE,aAmF3B,CAAA"}
|
|
@@ -6,10 +6,14 @@ export const handleDeleted = async (args)=>{
|
|
|
6
6
|
// if the event object and resource type don't match, this deletion was not top-level
|
|
7
7
|
const isNestedDelete = eventObject !== resourceType;
|
|
8
8
|
if (isNestedDelete) {
|
|
9
|
-
if (logs)
|
|
9
|
+
if (logs) {
|
|
10
|
+
payload.logger.info(`- This deletion occurred on a nested field of ${resourceType}. Nested fields are not yet supported.`);
|
|
11
|
+
}
|
|
10
12
|
}
|
|
11
13
|
if (!isNestedDelete) {
|
|
12
|
-
if (logs)
|
|
14
|
+
if (logs) {
|
|
15
|
+
payload.logger.info(`- A '${resourceType}' resource was deleted in Stripe, now deleting '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'...`);
|
|
16
|
+
}
|
|
13
17
|
try {
|
|
14
18
|
const payloadQuery = await payload.find({
|
|
15
19
|
collection: collectionSlug,
|
|
@@ -21,10 +25,14 @@ export const handleDeleted = async (args)=>{
|
|
|
21
25
|
});
|
|
22
26
|
const foundDoc = payloadQuery.docs[0];
|
|
23
27
|
if (!foundDoc) {
|
|
24
|
-
if (logs)
|
|
28
|
+
if (logs) {
|
|
29
|
+
payload.logger.info(`- Nothing to delete, no existing document found with Stripe ID: '${stripeID}'.`);
|
|
30
|
+
}
|
|
25
31
|
}
|
|
26
32
|
if (foundDoc) {
|
|
27
|
-
if (logs)
|
|
33
|
+
if (logs) {
|
|
34
|
+
payload.logger.info(`- Deleting Payload document with ID: '${foundDoc.id}'...`);
|
|
35
|
+
}
|
|
28
36
|
try {
|
|
29
37
|
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
30
38
|
payload.delete({
|
|
@@ -33,15 +41,21 @@ export const handleDeleted = async (args)=>{
|
|
|
33
41
|
});
|
|
34
42
|
// NOTE: the `afterDelete` hook will trigger, which will attempt to delete the document from Stripe and safely error out
|
|
35
43
|
// There is no known way of preventing this from happening. In other hooks we use the `skipSync` field, but here the document is already deleted.
|
|
36
|
-
if (logs)
|
|
44
|
+
if (logs) {
|
|
45
|
+
payload.logger.info(`- ✅ Successfully deleted Payload document with ID: '${foundDoc.id}'.`);
|
|
46
|
+
}
|
|
37
47
|
} catch (error) {
|
|
38
48
|
const msg = error instanceof Error ? error.message : error;
|
|
39
|
-
if (logs)
|
|
49
|
+
if (logs) {
|
|
50
|
+
payload.logger.error(`Error deleting document: ${msg}`);
|
|
51
|
+
}
|
|
40
52
|
}
|
|
41
53
|
}
|
|
42
54
|
} catch (error) {
|
|
43
55
|
const msg = error instanceof Error ? error.message : error;
|
|
44
|
-
if (logs)
|
|
56
|
+
if (logs) {
|
|
57
|
+
payload.logger.error(`Error deleting document: ${msg}`);
|
|
58
|
+
}
|
|
45
59
|
}
|
|
46
60
|
}
|
|
47
61
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/webhooks/handleDeleted.ts"],"sourcesContent":["import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\ntype HandleDeleted = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => void
|
|
1
|
+
{"version":3,"sources":["../../src/webhooks/handleDeleted.ts"],"sourcesContent":["import type { SanitizedStripePluginConfig, StripeWebhookHandler } from '../types.js'\n\ntype HandleDeleted = (\n args: {\n resourceType: string\n syncConfig: SanitizedStripePluginConfig['sync'][0]\n } & Parameters<StripeWebhookHandler>[0],\n) => Promise<void>\n\nexport const handleDeleted: HandleDeleted = async (args) => {\n const { event, payload, pluginConfig, resourceType, syncConfig } = args\n\n const { logs } = pluginConfig || {}\n\n const collectionSlug = syncConfig?.collection\n\n const {\n id: stripeID,\n object: eventObject, // use this to determine if this is a nested field\n }: any = event?.data?.object || {}\n\n // if the event object and resource type don't match, this deletion was not top-level\n const isNestedDelete = eventObject !== resourceType\n\n if (isNestedDelete) {\n if (logs) {\n payload.logger.info(\n `- This deletion occurred on a nested field of ${resourceType}. Nested fields are not yet supported.`,\n )\n }\n }\n\n if (!isNestedDelete) {\n if (logs) {\n payload.logger.info(\n `- A '${resourceType}' resource was deleted in Stripe, now deleting '${collectionSlug}' document in Payload with Stripe ID: '${stripeID}'...`,\n )\n }\n\n try {\n const payloadQuery = await payload.find({\n collection: collectionSlug,\n where: {\n stripeID: {\n equals: stripeID,\n },\n },\n })\n\n const foundDoc = payloadQuery.docs[0] as any\n\n if (!foundDoc) {\n if (logs) {\n payload.logger.info(\n `- Nothing to delete, no existing document found with Stripe ID: '${stripeID}'.`,\n )\n }\n }\n\n if (foundDoc) {\n if (logs) {\n payload.logger.info(`- Deleting Payload document with ID: '${foundDoc.id}'...`)\n }\n\n try {\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n payload.delete({\n id: foundDoc.id,\n collection: collectionSlug,\n })\n\n // NOTE: the `afterDelete` hook will trigger, which will attempt to delete the document from Stripe and safely error out\n // There is no known way of preventing this from happening. In other hooks we use the `skipSync` field, but here the document is already deleted.\n if (logs) {\n payload.logger.info(\n `- ✅ Successfully deleted Payload document with ID: '${foundDoc.id}'.`,\n )\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error deleting document: ${msg}`)\n }\n }\n }\n } catch (error: unknown) {\n const msg = error instanceof Error ? error.message : error\n if (logs) {\n payload.logger.error(`Error deleting document: ${msg}`)\n }\n }\n }\n}\n"],"names":["handleDeleted","args","event","payload","pluginConfig","resourceType","syncConfig","logs","collectionSlug","collection","id","stripeID","object","eventObject","data","isNestedDelete","logger","info","payloadQuery","find","where","equals","foundDoc","docs","delete","error","msg","Error","message"],"mappings":"AASA,OAAO,MAAMA,gBAA+B,OAAOC;IACjD,MAAM,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAEC,YAAY,EAAEC,UAAU,EAAE,GAAGL;IAEnE,MAAM,EAAEM,IAAI,EAAE,GAAGH,gBAAgB,CAAC;IAElC,MAAMI,iBAAiBF,YAAYG;IAEnC,MAAM,EACJC,IAAIC,QAAQ,EACZC,QAAQC,WAAW,EACpB,GAAQX,OAAOY,MAAMF,UAAU,CAAC;IAEjC,qFAAqF;IACrF,MAAMG,iBAAiBF,gBAAgBR;IAEvC,IAAIU,gBAAgB;QAClB,IAAIR,MAAM;YACRJ,QAAQa,MAAM,CAACC,IAAI,CACjB,CAAC,8CAA8C,EAAEZ,aAAa,sCAAsC,CAAC;QAEzG;IACF;IAEA,IAAI,CAACU,gBAAgB;QACnB,IAAIR,MAAM;YACRJ,QAAQa,MAAM,CAACC,IAAI,C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
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/webhooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAKvD,eAAO,MAAM,cAAc,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/webhooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAKvD,eAAO,MAAM,cAAc,EAAE,oBAkD5B,CAAA"}
|
package/dist/webhooks/index.js
CHANGED
|
@@ -2,7 +2,9 @@ import { handleCreatedOrUpdated } from './handleCreatedOrUpdated.js';
|
|
|
2
2
|
import { handleDeleted } from './handleDeleted.js';
|
|
3
3
|
export const handleWebhooks = (args)=>{
|
|
4
4
|
const { event, payload, pluginConfig } = args;
|
|
5
|
-
if (pluginConfig?.logs)
|
|
5
|
+
if (pluginConfig?.logs) {
|
|
6
|
+
payload.logger.info(`🪝 Received Stripe '${event.type}' webhook event with ID: '${event.id}'.`);
|
|
7
|
+
}
|
|
6
8
|
// could also traverse into event.data.object.object to get the type, but that seems unreliable
|
|
7
9
|
// use cli: `stripe resources` to see all available resources
|
|
8
10
|
const resourceType = event.type.split('.')[0];
|
|
@@ -20,9 +22,9 @@ export const handleWebhooks = (args)=>{
|
|
|
20
22
|
});
|
|
21
23
|
break;
|
|
22
24
|
}
|
|
23
|
-
case '
|
|
25
|
+
case 'deleted':
|
|
24
26
|
{
|
|
25
|
-
void
|
|
27
|
+
void handleDeleted({
|
|
26
28
|
...args,
|
|
27
29
|
pluginConfig,
|
|
28
30
|
resourceType,
|
|
@@ -30,9 +32,9 @@ export const handleWebhooks = (args)=>{
|
|
|
30
32
|
});
|
|
31
33
|
break;
|
|
32
34
|
}
|
|
33
|
-
case '
|
|
35
|
+
case 'updated':
|
|
34
36
|
{
|
|
35
|
-
void
|
|
37
|
+
void handleCreatedOrUpdated({
|
|
36
38
|
...args,
|
|
37
39
|
pluginConfig,
|
|
38
40
|
resourceType,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/webhooks/index.ts"],"sourcesContent":["import type { StripeWebhookHandler } from '../types.js'\n\nimport { handleCreatedOrUpdated } from './handleCreatedOrUpdated.js'\nimport { handleDeleted } from './handleDeleted.js'\n\nexport const handleWebhooks: StripeWebhookHandler = (args) => {\n const { event, payload, pluginConfig } = args\n\n if (pluginConfig?.logs)\n payload.logger.info(`🪝 Received Stripe '${event.type}' webhook event with ID: '${event.id}'.`)\n\n // could also traverse into event.data.object.object to get the type, but that seems unreliable\n // use cli: `stripe resources` to see all available resources\n const resourceType = event.type.split('.')[0]\n const method = event.type.split('.').pop()\n\n const syncConfig = pluginConfig?.sync?.find(\n (sync) => sync.stripeResourceTypeSingular === resourceType,\n )\n\n if (syncConfig) {\n switch (method) {\n case 'created': {\n void handleCreatedOrUpdated({\n ...args,\n pluginConfig,\n resourceType,\n syncConfig,\n })\n break\n }\n case '
|
|
1
|
+
{"version":3,"sources":["../../src/webhooks/index.ts"],"sourcesContent":["import type { StripeWebhookHandler } from '../types.js'\n\nimport { handleCreatedOrUpdated } from './handleCreatedOrUpdated.js'\nimport { handleDeleted } from './handleDeleted.js'\n\nexport const handleWebhooks: StripeWebhookHandler = (args) => {\n const { event, payload, pluginConfig } = args\n\n if (pluginConfig?.logs) {\n payload.logger.info(`🪝 Received Stripe '${event.type}' webhook event with ID: '${event.id}'.`)\n }\n\n // could also traverse into event.data.object.object to get the type, but that seems unreliable\n // use cli: `stripe resources` to see all available resources\n const resourceType = event.type.split('.')[0]\n const method = event.type.split('.').pop()\n\n const syncConfig = pluginConfig?.sync?.find(\n (sync) => sync.stripeResourceTypeSingular === resourceType,\n )\n\n if (syncConfig) {\n switch (method) {\n case 'created': {\n void handleCreatedOrUpdated({\n ...args,\n pluginConfig,\n resourceType,\n syncConfig,\n })\n break\n }\n case 'deleted': {\n void handleDeleted({\n ...args,\n pluginConfig,\n resourceType,\n syncConfig,\n })\n break\n }\n case 'updated': {\n void handleCreatedOrUpdated({\n ...args,\n pluginConfig,\n resourceType,\n syncConfig,\n })\n break\n }\n default: {\n break\n }\n }\n }\n}\n"],"names":["handleCreatedOrUpdated","handleDeleted","handleWebhooks","args","event","payload","pluginConfig","logs","logger","info","type","id","resourceType","split","method","pop","syncConfig","sync","find","stripeResourceTypeSingular"],"mappings":"AAEA,SAASA,sBAAsB,QAAQ,8BAA6B;AACpE,SAASC,aAAa,QAAQ,qBAAoB;AAElD,OAAO,MAAMC,iBAAuC,CAACC;IACnD,MAAM,EAAEC,KAAK,EAAEC,OAAO,EAAEC,YAAY,EAAE,GAAGH;IAEzC,IAAIG,cAAcC,MAAM;QACtBF,QAAQG,MAAM,CAACC,IAAI,CAAC,CAAC,oBAAoB,EAAEL,MAAMM,IAAI,CAAC,0BAA0B,EAAEN,MAAMO,EAAE,CAAC,EAAE,CAAC;IAChG;IAEA,+FAA+F;IAC/F,6DAA6D;IAC7D,MAAMC,eAAeR,MAAMM,IAAI,CAACG,KAAK,CAAC,IAAI,CAAC,EAAE;IAC7C,MAAMC,SAASV,MAAMM,IAAI,CAACG,KAAK,CAAC,KAAKE,GAAG;IAExC,MAAMC,aAAaV,cAAcW,MAAMC,KACrC,CAACD,OAASA,KAAKE,0BAA0B,KAAKP;IAGhD,IAAII,YAAY;QACd,OAAQF;YACN,KAAK;gBAAW;oBACd,KAAKd,uBAAuB;wBAC1B,GAAGG,IAAI;wBACPG;wBACAM;wBACAI;oBACF;oBACA;gBACF;YACA,KAAK;gBAAW;oBACd,KAAKf,cAAc;wBACjB,GAAGE,IAAI;wBACPG;wBACAM;wBACAI;oBACF;oBACA;gBACF;YACA,KAAK;gBAAW;oBACd,KAAKhB,uBAAuB;wBAC1B,GAAGG,IAAI;wBACPG;wBACAM;wBACAI;oBACF;oBACA;gBACF;YACA;gBAAS;oBACP;gBACF;QACF;IACF;AACF,EAAC"}
|
package/license.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2024 Payload CMS, Inc. <info@payloadcms.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-stripe",
|
|
3
|
-
"version": "3.0.0
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Stripe plugin for Payload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload",
|
|
@@ -20,6 +20,13 @@
|
|
|
20
20
|
},
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"author": "Payload <dev@payloadcms.com> (https://payloadcms.com)",
|
|
23
|
+
"maintainers": [
|
|
24
|
+
{
|
|
25
|
+
"name": "Payload",
|
|
26
|
+
"email": "info@payloadcms.com",
|
|
27
|
+
"url": "https://payloadcms.com"
|
|
28
|
+
}
|
|
29
|
+
],
|
|
23
30
|
"type": "module",
|
|
24
31
|
"exports": {
|
|
25
32
|
".": {
|
|
@@ -31,6 +38,11 @@
|
|
|
31
38
|
"import": "./dist/exports/types.js",
|
|
32
39
|
"types": "./dist/exports/types.d.ts",
|
|
33
40
|
"default": "./dist/exports/types.js"
|
|
41
|
+
},
|
|
42
|
+
"./client": {
|
|
43
|
+
"import": "./dist/exports/client.js",
|
|
44
|
+
"types": "./dist/exports/client.d.ts",
|
|
45
|
+
"default": "./dist/exports/client.js"
|
|
34
46
|
}
|
|
35
47
|
},
|
|
36
48
|
"main": "./dist/index.js",
|
|
@@ -42,32 +54,28 @@
|
|
|
42
54
|
"lodash.get": "^4.4.2",
|
|
43
55
|
"stripe": "^10.2.0",
|
|
44
56
|
"uuid": "10.0.0",
|
|
45
|
-
"@payloadcms/
|
|
57
|
+
"@payloadcms/translations": "3.0.0",
|
|
58
|
+
"@payloadcms/ui": "3.0.0"
|
|
46
59
|
},
|
|
47
60
|
"devDependencies": {
|
|
48
|
-
"@types/express": "^4.17.9",
|
|
49
61
|
"@types/lodash.get": "^4.4.7",
|
|
50
|
-
"@types/react": "npm:types-react@19.0.0-rc.
|
|
51
|
-
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.
|
|
62
|
+
"@types/react": "npm:types-react@19.0.0-rc.1",
|
|
63
|
+
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.1",
|
|
52
64
|
"@types/uuid": "10.0.0",
|
|
53
|
-
"@payloadcms/
|
|
54
|
-
"
|
|
55
|
-
"@payloadcms/
|
|
56
|
-
"@payloadcms/ui": "3.0.0-canary.fb81f02",
|
|
57
|
-
"payload": "3.0.0-canary.fb81f02"
|
|
65
|
+
"@payloadcms/next": "3.0.0",
|
|
66
|
+
"payload": "3.0.0",
|
|
67
|
+
"@payloadcms/eslint-config": "3.0.0-beta.112"
|
|
58
68
|
},
|
|
59
69
|
"peerDependencies": {
|
|
60
|
-
"
|
|
61
|
-
"@payloadcms/ui": "3.0.0-canary.fb81f02",
|
|
62
|
-
"payload": "3.0.0-canary.fb81f02"
|
|
70
|
+
"payload": "3.0.0"
|
|
63
71
|
},
|
|
64
72
|
"publishConfig": {
|
|
65
73
|
"registry": "https://registry.npmjs.org/"
|
|
66
74
|
},
|
|
67
75
|
"homepage:": "https://payloadcms.com",
|
|
68
76
|
"overrides": {
|
|
69
|
-
"@types/react": "npm:types-react@19.0.0-rc.
|
|
70
|
-
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.
|
|
77
|
+
"@types/react": "npm:types-react@19.0.0-rc.1",
|
|
78
|
+
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.1"
|
|
71
79
|
},
|
|
72
80
|
"scripts": {
|
|
73
81
|
"build": "pnpm copyfiles && pnpm build:types && pnpm build:swc",
|
|
@@ -75,7 +83,7 @@
|
|
|
75
83
|
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
|
76
84
|
"clean": "rimraf {dist,*.tsbuildinfo}",
|
|
77
85
|
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png,json}\" dist/",
|
|
78
|
-
"lint": "eslint
|
|
79
|
-
"lint:fix": "eslint --fix
|
|
86
|
+
"lint": "eslint .",
|
|
87
|
+
"lint:fix": "eslint . --fix"
|
|
80
88
|
}
|
|
81
89
|
}
|