@dodopayments/better-auth 1.6.1 → 1.6.2
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/{chunk-65YIVTFE.js → chunk-6VDYFSNJ.js} +6 -6
- package/dist/chunk-6VDYFSNJ.js.map +1 -0
- package/dist/{chunk-TOPOAYYO.js → chunk-EGRIWRWP.js} +6 -3
- package/dist/chunk-EGRIWRWP.js.map +1 -0
- package/dist/{chunk-HHIXOUFD.js → chunk-XXIDSJPK.js} +4 -4
- package/dist/chunk-XXIDSJPK.js.map +1 -0
- package/dist/{chunk-DTOB4IQZ.js → chunk-YCUWFLKK.js} +15 -12
- package/dist/chunk-YCUWFLKK.js.map +1 -0
- package/dist/{chunk-ARA27DRZ.js → chunk-ZA3TNX5K.js} +11 -9
- package/dist/chunk-ZA3TNX5K.js.map +1 -0
- package/dist/client.d.cts +1 -2
- package/dist/client.d.ts +1 -2
- package/dist/hooks/customer.d.cts +1 -2
- package/dist/hooks/customer.d.ts +1 -2
- package/dist/index.cjs +36 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -3
- package/dist/index.d.ts +2 -3
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/plugins/checkout.cjs +5 -5
- package/dist/plugins/checkout.cjs.map +1 -1
- package/dist/plugins/checkout.d.cts +2 -3
- package/dist/plugins/checkout.d.ts +2 -3
- package/dist/plugins/checkout.js +1 -1
- package/dist/plugins/portal.cjs +18 -12
- package/dist/plugins/portal.cjs.map +1 -1
- package/dist/plugins/portal.d.cts +2 -3
- package/dist/plugins/portal.d.ts +2 -3
- package/dist/plugins/portal.js +2 -2
- package/dist/plugins/usage.cjs +14 -9
- package/dist/plugins/usage.cjs.map +1 -1
- package/dist/plugins/usage.d.cts +6 -107
- package/dist/plugins/usage.d.ts +6 -107
- package/dist/plugins/usage.js +2 -2
- package/dist/plugins/webhooks.cjs +3 -3
- package/dist/plugins/webhooks.cjs.map +1 -1
- package/dist/plugins/webhooks.d.cts +2 -3
- package/dist/plugins/webhooks.d.ts +2 -3
- package/dist/plugins/webhooks.js +1 -1
- package/dist/{types-3pb2RGTM.d.cts → types-D0tVSLaf.d.cts} +110 -9
- package/dist/{types-DygjjcRn.d.ts → types-D0tVSLaf.d.ts} +110 -9
- package/dist/types.d.cts +1 -2
- package/dist/types.d.ts +1 -2
- package/dist/utils.cjs +5 -2
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +8 -1
- package/dist/utils.d.ts +8 -1
- package/dist/utils.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-65YIVTFE.js.map +0 -1
- package/dist/chunk-ARA27DRZ.js.map +0 -1
- package/dist/chunk-DTOB4IQZ.js.map +0 -1
- package/dist/chunk-HHIXOUFD.js.map +0 -1
- package/dist/chunk-TOPOAYYO.js.map +0 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { DodoPayments } from 'dodopayments';
|
|
2
2
|
import * as better_auth from 'better-auth';
|
|
3
3
|
import { User, UnionToIntersection } from 'better-auth';
|
|
4
4
|
import { z } from 'zod/v3';
|
|
5
5
|
import { WebhookHandlerConfig } from '@dodopayments/core/webhook';
|
|
6
|
-
import {
|
|
6
|
+
import { Event } from 'dodopayments/resources/usage-events.mjs';
|
|
7
7
|
|
|
8
8
|
interface CheckoutOptions {
|
|
9
9
|
/**
|
|
@@ -19,7 +19,7 @@ interface CheckoutOptions {
|
|
|
19
19
|
*/
|
|
20
20
|
authenticatedUsersOnly?: boolean;
|
|
21
21
|
}
|
|
22
|
-
declare const checkout: (checkoutOptions?: CheckoutOptions) => (
|
|
22
|
+
declare const checkout: (checkoutOptions?: CheckoutOptions) => (options: DodoPaymentsOptions) => {
|
|
23
23
|
/**
|
|
24
24
|
* @deprecated
|
|
25
25
|
*/
|
|
@@ -526,7 +526,7 @@ declare const checkout: (checkoutOptions?: CheckoutOptions) => (dodopayments: Do
|
|
|
526
526
|
}, CreateCheckoutResponse>;
|
|
527
527
|
};
|
|
528
528
|
|
|
529
|
-
declare const portal: () => (
|
|
529
|
+
declare const portal: () => (options: DodoPaymentsOptions) => {
|
|
530
530
|
dodoPortal: better_auth.StrictEndpoint<"/dodopayments/customer/portal", {
|
|
531
531
|
method: "GET";
|
|
532
532
|
use: ((inputContext: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<{
|
|
@@ -639,7 +639,7 @@ declare const portal: () => (dodopayments: DodoPayments$1) => {
|
|
|
639
639
|
}, PaymentItems>;
|
|
640
640
|
};
|
|
641
641
|
|
|
642
|
-
declare const webhooks: (
|
|
642
|
+
declare const webhooks: (webhookOptions: WebhookHandlerConfig) => (_options: DodoPaymentsOptions) => {
|
|
643
643
|
dodopaymentsWebhooks: better_auth.StrictEndpoint<"/dodopayments/webhooks", {
|
|
644
644
|
method: "POST";
|
|
645
645
|
metadata: {
|
|
@@ -651,6 +651,107 @@ declare const webhooks: (options: WebhookHandlerConfig) => (_dodopayments: DodoP
|
|
|
651
651
|
}, WebhookResponse>;
|
|
652
652
|
};
|
|
653
653
|
|
|
654
|
+
declare const usage: () => (options: DodoPaymentsOptions) => {
|
|
655
|
+
dodoUsageIngest: better_auth.StrictEndpoint<"/dodopayments/usage/ingest", {
|
|
656
|
+
method: "POST";
|
|
657
|
+
body: z.ZodObject<{
|
|
658
|
+
event_id: z.ZodString;
|
|
659
|
+
event_name: z.ZodString;
|
|
660
|
+
metadata: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean]>>>>;
|
|
661
|
+
timestamp: z.ZodOptional<z.ZodEffects<z.ZodDate, string, Date>>;
|
|
662
|
+
}, "strip", z.ZodTypeAny, {
|
|
663
|
+
event_id: string;
|
|
664
|
+
event_name: string;
|
|
665
|
+
metadata?: Record<string, string | number | boolean> | null | undefined;
|
|
666
|
+
timestamp?: string | undefined;
|
|
667
|
+
}, {
|
|
668
|
+
event_id: string;
|
|
669
|
+
event_name: string;
|
|
670
|
+
metadata?: Record<string, string | number | boolean> | null | undefined;
|
|
671
|
+
timestamp?: Date | undefined;
|
|
672
|
+
}>;
|
|
673
|
+
use: ((inputContext: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<{
|
|
674
|
+
session: {
|
|
675
|
+
session: Record<string, any> & {
|
|
676
|
+
id: string;
|
|
677
|
+
createdAt: Date;
|
|
678
|
+
updatedAt: Date;
|
|
679
|
+
userId: string;
|
|
680
|
+
expiresAt: Date;
|
|
681
|
+
token: string;
|
|
682
|
+
ipAddress?: string | null | undefined;
|
|
683
|
+
userAgent?: string | null | undefined;
|
|
684
|
+
};
|
|
685
|
+
user: Record<string, any> & {
|
|
686
|
+
id: string;
|
|
687
|
+
createdAt: Date;
|
|
688
|
+
updatedAt: Date;
|
|
689
|
+
email: string;
|
|
690
|
+
emailVerified: boolean;
|
|
691
|
+
name: string;
|
|
692
|
+
image?: string | null | undefined;
|
|
693
|
+
};
|
|
694
|
+
};
|
|
695
|
+
}>)[];
|
|
696
|
+
} & {
|
|
697
|
+
use: any[];
|
|
698
|
+
}, {
|
|
699
|
+
ingested_count: number;
|
|
700
|
+
}>;
|
|
701
|
+
dodoUsageMetersList: better_auth.StrictEndpoint<"/dodopayments/usage/meters/list", {
|
|
702
|
+
method: "GET";
|
|
703
|
+
query: z.ZodOptional<z.ZodObject<{
|
|
704
|
+
page_number: z.ZodOptional<z.ZodNumber>;
|
|
705
|
+
page_size: z.ZodOptional<z.ZodNumber>;
|
|
706
|
+
event_name: z.ZodOptional<z.ZodString>;
|
|
707
|
+
meter_id: z.ZodOptional<z.ZodString>;
|
|
708
|
+
start: z.ZodOptional<z.ZodString>;
|
|
709
|
+
end: z.ZodOptional<z.ZodString>;
|
|
710
|
+
}, "strip", z.ZodTypeAny, {
|
|
711
|
+
event_name?: string | undefined;
|
|
712
|
+
page_number?: number | undefined;
|
|
713
|
+
page_size?: number | undefined;
|
|
714
|
+
meter_id?: string | undefined;
|
|
715
|
+
start?: string | undefined;
|
|
716
|
+
end?: string | undefined;
|
|
717
|
+
}, {
|
|
718
|
+
event_name?: string | undefined;
|
|
719
|
+
page_number?: number | undefined;
|
|
720
|
+
page_size?: number | undefined;
|
|
721
|
+
meter_id?: string | undefined;
|
|
722
|
+
start?: string | undefined;
|
|
723
|
+
end?: string | undefined;
|
|
724
|
+
}>>;
|
|
725
|
+
use: ((inputContext: better_auth.MiddlewareInputContext<better_auth.MiddlewareOptions>) => Promise<{
|
|
726
|
+
session: {
|
|
727
|
+
session: Record<string, any> & {
|
|
728
|
+
id: string;
|
|
729
|
+
createdAt: Date;
|
|
730
|
+
updatedAt: Date;
|
|
731
|
+
userId: string;
|
|
732
|
+
expiresAt: Date;
|
|
733
|
+
token: string;
|
|
734
|
+
ipAddress?: string | null | undefined;
|
|
735
|
+
userAgent?: string | null | undefined;
|
|
736
|
+
};
|
|
737
|
+
user: Record<string, any> & {
|
|
738
|
+
id: string;
|
|
739
|
+
createdAt: Date;
|
|
740
|
+
updatedAt: Date;
|
|
741
|
+
email: string;
|
|
742
|
+
emailVerified: boolean;
|
|
743
|
+
name: string;
|
|
744
|
+
image?: string | null | undefined;
|
|
745
|
+
};
|
|
746
|
+
};
|
|
747
|
+
}>)[];
|
|
748
|
+
} & {
|
|
749
|
+
use: any[];
|
|
750
|
+
}, {
|
|
751
|
+
items: Event[];
|
|
752
|
+
}>;
|
|
753
|
+
};
|
|
754
|
+
|
|
654
755
|
type Product = {
|
|
655
756
|
/**
|
|
656
757
|
* Product Id from DodoPayments Product
|
|
@@ -668,7 +769,7 @@ interface DodoPaymentsOptions {
|
|
|
668
769
|
/**
|
|
669
770
|
* DodoPayments Client
|
|
670
771
|
*/
|
|
671
|
-
client: DodoPayments
|
|
772
|
+
client: DodoPayments;
|
|
672
773
|
/**
|
|
673
774
|
* Enable customer creation when a user signs up
|
|
674
775
|
*/
|
|
@@ -689,8 +790,8 @@ interface DodoPaymentsOptions {
|
|
|
689
790
|
phone_number?: string | null;
|
|
690
791
|
}>;
|
|
691
792
|
}
|
|
692
|
-
type PaymentsList = Awaited<ReturnType<DodoPayments
|
|
693
|
-
type SubscriptionsList = Awaited<ReturnType<DodoPayments
|
|
793
|
+
type PaymentsList = Awaited<ReturnType<DodoPayments["payments"]["list"]>>;
|
|
794
|
+
type SubscriptionsList = Awaited<ReturnType<DodoPayments["subscriptions"]["list"]>>;
|
|
694
795
|
type PaymentItems = {
|
|
695
796
|
items: PaymentsList["items"];
|
|
696
797
|
};
|
|
@@ -709,4 +810,4 @@ type WebhookResponse = {
|
|
|
709
810
|
received: boolean;
|
|
710
811
|
};
|
|
711
812
|
|
|
712
|
-
export { type CreateCheckoutResponse as C, type DodoPaymentsOptions as D, type PaymentItems as P, type SubscriptionItems as S, type WebhookResponse as W, type CustomerPortalResponse as a, type Product as b, type DodoPaymentsPlugin as c, type DodoPaymentsPlugins as d, type DodoPaymentsEndpoints as e, checkout as f, type CheckoutOptions as g, portal as p, webhooks as w };
|
|
813
|
+
export { type CreateCheckoutResponse as C, type DodoPaymentsOptions as D, type PaymentItems as P, type SubscriptionItems as S, type WebhookResponse as W, type CustomerPortalResponse as a, type Product as b, type DodoPaymentsPlugin as c, type DodoPaymentsPlugins as d, type DodoPaymentsEndpoints as e, checkout as f, type CheckoutOptions as g, portal as p, usage as u, webhooks as w };
|
package/dist/types.d.cts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import 'dodopayments';
|
|
2
2
|
import 'better-auth';
|
|
3
|
-
export { C as CreateCheckoutResponse, a as CustomerPortalResponse, e as DodoPaymentsEndpoints, D as DodoPaymentsOptions, c as DodoPaymentsPlugin, d as DodoPaymentsPlugins, P as PaymentItems, b as Product, S as SubscriptionItems, W as WebhookResponse } from './types-
|
|
4
|
-
import './plugins/usage.cjs';
|
|
3
|
+
export { C as CreateCheckoutResponse, a as CustomerPortalResponse, e as DodoPaymentsEndpoints, D as DodoPaymentsOptions, c as DodoPaymentsPlugin, d as DodoPaymentsPlugins, P as PaymentItems, b as Product, S as SubscriptionItems, W as WebhookResponse } from './types-D0tVSLaf.cjs';
|
|
5
4
|
import 'zod/v3';
|
|
6
5
|
import '@dodopayments/core/webhook';
|
|
7
6
|
import 'dodopayments/resources/usage-events.mjs';
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import 'dodopayments';
|
|
2
2
|
import 'better-auth';
|
|
3
|
-
export { C as CreateCheckoutResponse, a as CustomerPortalResponse, e as DodoPaymentsEndpoints, D as DodoPaymentsOptions, c as DodoPaymentsPlugin, d as DodoPaymentsPlugins, P as PaymentItems, b as Product, S as SubscriptionItems, W as WebhookResponse } from './types-
|
|
4
|
-
import './plugins/usage.js';
|
|
3
|
+
export { C as CreateCheckoutResponse, a as CustomerPortalResponse, e as DodoPaymentsEndpoints, D as DodoPaymentsOptions, c as DodoPaymentsPlugin, d as DodoPaymentsPlugins, P as PaymentItems, b as Product, S as SubscriptionItems, W as WebhookResponse } from './types-D0tVSLaf.js';
|
|
5
4
|
import 'zod/v3';
|
|
6
5
|
import '@dodopayments/core/webhook';
|
|
7
6
|
import 'dodopayments/resources/usage-events.mjs';
|
package/dist/utils.cjs
CHANGED
|
@@ -23,7 +23,7 @@ __export(utils_exports, {
|
|
|
23
23
|
getOrCreateCustomerId: () => getOrCreateCustomerId
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(utils_exports);
|
|
26
|
-
async function getOrCreateCustomerId(dodopayments, session, internalAdapter) {
|
|
26
|
+
async function getOrCreateCustomerId(dodopayments, session, internalAdapter, getCustomerParams) {
|
|
27
27
|
const dodoCustomerId = session.user["dodoCustomerId"];
|
|
28
28
|
if (dodoCustomerId) return dodoCustomerId;
|
|
29
29
|
const customers = await dodopayments.customers.list({
|
|
@@ -31,9 +31,12 @@ async function getOrCreateCustomerId(dodopayments, session, internalAdapter) {
|
|
|
31
31
|
});
|
|
32
32
|
let customer = customers.items[0];
|
|
33
33
|
if (!customer) {
|
|
34
|
+
const additionalParams = getCustomerParams ? await getCustomerParams(session.user) : void 0;
|
|
34
35
|
customer = await dodopayments.customers.create({
|
|
35
36
|
email: session.user.email,
|
|
36
|
-
name: session.user.name
|
|
37
|
+
name: session.user.name,
|
|
38
|
+
metadata: additionalParams?.metadata,
|
|
39
|
+
phone_number: additionalParams?.phone_number
|
|
37
40
|
}, { idempotencyKey: session.user.id });
|
|
38
41
|
}
|
|
39
42
|
internalAdapter.updateUser(session.user.id, { dodoCustomerId: customer.customer_id }).catch(() => {
|
package/dist/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import type { DodoPayments } from \"dodopayments\";\n\nexport async function getOrCreateCustomerId(\n dodopayments: DodoPayments,\n session: { user: { id: string; email: string; name: string } & Record<string, unknown> },\n internalAdapter: { updateUser: (id: string, data: Record<string, unknown>) => Promise<unknown> },\n): Promise<string> {\n const dodoCustomerId = session.user[\"dodoCustomerId\"] as string | undefined;\n if (dodoCustomerId) return dodoCustomerId;\n\n // Fallback to get customer from email if dodoCustomerId doesn't exist\n const customers = await dodopayments.customers.list({\n email: session.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n customer = await dodopayments.customers.create({\n email: session.user.email,\n name: session.user.name,\n }, { idempotencyKey: session.user.id });\n }\n\n internalAdapter\n .updateUser(session.user.id, { dodoCustomerId: customer.customer_id })\n .catch(() => {});\n\n return customer.customer_id;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import type { DodoPayments } from \"dodopayments\";\nimport type { User } from \"better-auth\";\n\nexport async function getOrCreateCustomerId(\n dodopayments: DodoPayments,\n session: { user: { id: string; email: string; name: string } & Record<string, unknown> },\n internalAdapter: { updateUser: (id: string, data: Record<string, unknown>) => Promise<unknown> },\n getCustomerParams?: (user: User) => { metadata?: Record<string, string>; phone_number?: string | null } | Promise<{ metadata?: Record<string, string>; phone_number?: string | null }>,\n): Promise<string> {\n const dodoCustomerId = session.user[\"dodoCustomerId\"] as string | undefined;\n if (dodoCustomerId) return dodoCustomerId;\n\n // Fallback to get customer from email if dodoCustomerId doesn't exist\n const customers = await dodopayments.customers.list({\n email: session.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n const additionalParams = getCustomerParams\n ? await getCustomerParams(session.user as User)\n : undefined;\n\n customer = await dodopayments.customers.create({\n email: session.user.email,\n name: session.user.name,\n metadata: additionalParams?.metadata,\n phone_number: additionalParams?.phone_number,\n }, { idempotencyKey: session.user.id });\n }\n\n internalAdapter\n .updateUser(session.user.id, { dodoCustomerId: customer.customer_id })\n .catch(() => {});\n\n return customer.customer_id;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,eAAsB,sBACpB,cACA,SACA,iBACA,mBACiB;AACjB,QAAM,iBAAiB,QAAQ,KAAK,gBAAgB;AACpD,MAAI,eAAgB,QAAO;AAG3B,QAAM,YAAY,MAAM,aAAa,UAAU,KAAK;AAAA,IAClD,OAAO,QAAQ,KAAK;AAAA,EACtB,CAAC;AACD,MAAI,WAAW,UAAU,MAAM,CAAC;AAEhC,MAAI,CAAC,UAAU;AACb,UAAM,mBAAmB,oBACrB,MAAM,kBAAkB,QAAQ,IAAY,IAC5C;AAEJ,eAAW,MAAM,aAAa,UAAU,OAAO;AAAA,MAC7C,OAAO,QAAQ,KAAK;AAAA,MACpB,MAAM,QAAQ,KAAK;AAAA,MACnB,UAAU,kBAAkB;AAAA,MAC5B,cAAc,kBAAkB;AAAA,IAClC,GAAG,EAAE,gBAAgB,QAAQ,KAAK,GAAG,CAAC;AAAA,EACxC;AAEA,kBACG,WAAW,QAAQ,KAAK,IAAI,EAAE,gBAAgB,SAAS,YAAY,CAAC,EACpE,MAAM,MAAM;AAAA,EAAC,CAAC;AAEjB,SAAO,SAAS;AAClB;","names":[]}
|
package/dist/utils.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DodoPayments } from 'dodopayments';
|
|
2
|
+
import { User } from 'better-auth';
|
|
2
3
|
|
|
3
4
|
declare function getOrCreateCustomerId(dodopayments: DodoPayments, session: {
|
|
4
5
|
user: {
|
|
@@ -8,6 +9,12 @@ declare function getOrCreateCustomerId(dodopayments: DodoPayments, session: {
|
|
|
8
9
|
} & Record<string, unknown>;
|
|
9
10
|
}, internalAdapter: {
|
|
10
11
|
updateUser: (id: string, data: Record<string, unknown>) => Promise<unknown>;
|
|
11
|
-
}
|
|
12
|
+
}, getCustomerParams?: (user: User) => {
|
|
13
|
+
metadata?: Record<string, string>;
|
|
14
|
+
phone_number?: string | null;
|
|
15
|
+
} | Promise<{
|
|
16
|
+
metadata?: Record<string, string>;
|
|
17
|
+
phone_number?: string | null;
|
|
18
|
+
}>): Promise<string>;
|
|
12
19
|
|
|
13
20
|
export { getOrCreateCustomerId };
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DodoPayments } from 'dodopayments';
|
|
2
|
+
import { User } from 'better-auth';
|
|
2
3
|
|
|
3
4
|
declare function getOrCreateCustomerId(dodopayments: DodoPayments, session: {
|
|
4
5
|
user: {
|
|
@@ -8,6 +9,12 @@ declare function getOrCreateCustomerId(dodopayments: DodoPayments, session: {
|
|
|
8
9
|
} & Record<string, unknown>;
|
|
9
10
|
}, internalAdapter: {
|
|
10
11
|
updateUser: (id: string, data: Record<string, unknown>) => Promise<unknown>;
|
|
11
|
-
}
|
|
12
|
+
}, getCustomerParams?: (user: User) => {
|
|
13
|
+
metadata?: Record<string, string>;
|
|
14
|
+
phone_number?: string | null;
|
|
15
|
+
} | Promise<{
|
|
16
|
+
metadata?: Record<string, string>;
|
|
17
|
+
phone_number?: string | null;
|
|
18
|
+
}>): Promise<string>;
|
|
12
19
|
|
|
13
20
|
export { getOrCreateCustomerId };
|
package/dist/utils.js
CHANGED
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plugins/checkout.ts"],"sourcesContent":["import type DodoPayments from \"dodopayments\";\nimport { APIError, createAuthEndpoint, getSessionFromCtx } from \"better-auth/api\";\nimport { z } from \"zod/v3\";\nimport type { CreateCheckoutResponse, Product } from \"../types\";\nimport {\n buildCheckoutUrl,\n checkoutSessionPayloadSchema,\n dynamicCheckoutBodySchema,\n} from \"@dodopayments/core/checkout\";\n\nexport interface CheckoutOptions {\n /**\n * Optional list of slug -> productId mappings for easy slug checkouts\n */\n products?: Product[] | (() => Promise<Product[]>);\n /**\n * Checkout Success URL\n */\n successUrl?: string;\n /**\n * Only allow authenticated customers to checkout\n */\n authenticatedUsersOnly?: boolean;\n}\n\nexport const checkout =\n (checkoutOptions: CheckoutOptions = {}) =>\n (dodopayments: DodoPayments) => {\n return {\n /**\n * @deprecated\n */\n dodoCheckout: createAuthEndpoint(\n \"/dodopayments/checkout\",\n {\n method: \"POST\",\n body: dynamicCheckoutBodySchema.extend({\n slug: z.string().optional(),\n referenceId: z.string().optional(),\n }),\n requireRequest: true,\n },\n async (ctx): Promise<CreateCheckoutResponse> => {\n const session = await getSessionFromCtx(ctx);\n\n let dodoPaymentsProductId: string | undefined;\n\n if (ctx.body?.slug) {\n const resolvedProducts =\n typeof checkoutOptions.products === \"function\"\n ? await checkoutOptions.products()\n : checkoutOptions.products;\n\n const productId = resolvedProducts?.find(\n (product) => product.slug === ctx.body.slug,\n )?.productId;\n\n if (!productId) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Product not found\",\n });\n }\n\n dodoPaymentsProductId = productId;\n } else {\n dodoPaymentsProductId = ctx.body.product_id;\n }\n\n if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to checkout\",\n });\n }\n\n try {\n const checkoutUrl = await buildCheckoutUrl({\n body: {\n ...ctx.body,\n product_id: dodoPaymentsProductId,\n customer: {\n email: session?.user.email,\n name: session?.user.name,\n ...ctx.body.customer,\n },\n product_cart: dodoPaymentsProductId\n ? [\n {\n product_id: dodoPaymentsProductId,\n quantity: 1,\n },\n ]\n : undefined,\n metadata: ctx.body.referenceId\n ? {\n referenceId: ctx.body.referenceId,\n ...ctx.body.metadata,\n }\n : ctx.body.metadata,\n },\n bearerToken: dodopayments.bearerToken,\n environment: dodopayments.baseURL.includes(\"test\")\n ? \"test_mode\"\n : \"live_mode\",\n returnUrl: checkoutOptions.successUrl\n ? new URL(\n checkoutOptions.successUrl,\n ctx.request?.url,\n ).toString()\n : undefined,\n type: \"dynamic\",\n });\n\n const redirectUrl = new URL(checkoutUrl);\n\n return ctx.json({\n url: redirectUrl.toString(),\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments checkout creation failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Checkout creation failed\",\n });\n }\n },\n ),\n dodoCheckoutSession: createAuthEndpoint(\n \"/dodopayments/checkout-session\",\n {\n method: \"POST\",\n body: checkoutSessionPayloadSchema\n .extend({\n slug: z.string().optional(),\n referenceId: z.string().optional(),\n })\n .partial({\n product_cart: true,\n }),\n requireRequest: true,\n },\n async (ctx): Promise<CreateCheckoutResponse> => {\n const session = await getSessionFromCtx(ctx);\n\n let dodoPaymentsProductId: string | undefined;\n\n if (ctx.body?.slug) {\n const resolvedProducts =\n typeof checkoutOptions.products === \"function\"\n ? await checkoutOptions.products()\n : checkoutOptions.products;\n\n const productId = resolvedProducts?.find(\n (product) => product.slug === ctx.body.slug,\n )?.productId;\n\n if (!productId) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Product not found\",\n });\n }\n\n dodoPaymentsProductId = productId;\n }\n\n if (checkoutOptions.authenticatedUsersOnly && !session?.user.id) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to checkout\",\n });\n }\n\n // Ensure we have a product_cart\n const product_cart = dodoPaymentsProductId\n ? [{ product_id: dodoPaymentsProductId, quantity: 1 }]\n : ctx.body.product_cart;\n\n if (!product_cart || product_cart.length === 0) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Neither product_cart nor slug was provided\",\n });\n }\n\n try {\n const checkoutUrl = await buildCheckoutUrl({\n sessionPayload: {\n ...ctx.body,\n product_cart,\n customer: session?.user.email\n ? {\n email: session?.user.email,\n name: session?.user.name,\n }\n : ctx.body.customer,\n metadata: ctx.body.referenceId\n ? {\n referenceId: ctx.body.referenceId,\n ...ctx.body.metadata,\n }\n : ctx.body.metadata,\n return_url: checkoutOptions.successUrl\n ? new URL(\n checkoutOptions.successUrl,\n ctx.request?.url,\n ).toString()\n : undefined,\n },\n bearerToken: dodopayments.bearerToken,\n environment: dodopayments.baseURL.includes(\"test\")\n ? \"test_mode\"\n : \"live_mode\",\n type: \"session\",\n });\n\n const redirectUrl = new URL(checkoutUrl);\n\n return ctx.json({\n url: redirectUrl.toString(),\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments checkout creation failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Checkout session creation failed\",\n });\n }\n },\n ),\n };\n };\n"],"mappings":";AACA,SAAS,UAAU,oBAAoB,yBAAyB;AAChE,SAAS,SAAS;AAElB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAiBA,IAAM,WACX,CAAC,kBAAmC,CAAC,MACrC,CAAC,iBAA+B;AAC9B,SAAO;AAAA;AAAA;AAAA;AAAA,IAIL,cAAc;AAAA,MACZ;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,MAAM,0BAA0B,OAAO;AAAA,UACrC,MAAM,EAAE,OAAO,EAAE,SAAS;AAAA,UAC1B,aAAa,EAAE,OAAO,EAAE,SAA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 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/plugins/usage.ts"],"sourcesContent":["import {\n APIError,\n createAuthEndpoint,\n sessionMiddleware,\n} from \"better-auth/api\";\nimport type { DodoPayments } from \"dodopayments\";\nimport { Event } from \"dodopayments/resources/usage-events.mjs\";\nimport { z } from \"zod/v3\";\nimport { getOrCreateCustomerId } from \"../utils\";\n\nconst EventInputSchema = z.object({\n event_id: z.string(),\n event_name: z.string(),\n metadata: z\n .record(z.union([z.string(), z.number(), z.boolean()]))\n .nullable()\n .optional(),\n timestamp: z\n // NOTE: coerce because the date object gets converted to a string over network requests\n // but we still want to enforce that it's a Date type\n .date({ coerce: true })\n .transform((d) => d.toISOString())\n .optional()\n .describe(\n \"Custom Timestamp. Defaults to current timestamp in UTC.\\\n Timestamps that are older that 1 hour or after 5 mins from\\\n current timestamp will be rejected.\",\n ),\n});\n\nexport const usage = () => (dodopayments: DodoPayments) => {\n return {\n // Ingest usage data\n dodoUsageIngest: createAuthEndpoint(\n \"/dodopayments/usage/ingest\",\n {\n method: \"POST\",\n body: EventInputSchema,\n use: [sessionMiddleware],\n },\n async (ctx): Promise<{ ingested_count: number }> => {\n if (!ctx.context.session?.user?.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customerId = await getOrCreateCustomerId(\n dodopayments,\n ctx.context.session,\n ctx.context.internalAdapter,\n );\n\n const result = await dodopayments.usageEvents.ingest({\n events: [\n {\n event_id: ctx.body.event_id,\n customer_id: customerId,\n event_name: ctx.body.event_name,\n timestamp: ctx.body.timestamp,\n metadata: ctx.body.metadata,\n },\n ],\n });\n\n return ctx.json({ ingested_count: result.ingested_count });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `User usage ingestion error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to record the user usage\",\n });\n }\n },\n ),\n\n // List usage meters\n dodoUsageMetersList: createAuthEndpoint(\n \"/dodopayments/usage/meters/list\",\n {\n method: \"GET\",\n query: z\n .object({\n page_number: z.coerce.number().optional(),\n page_size: z.coerce.number().optional(),\n event_name: z.string().optional(),\n meter_id: z.string().optional(),\n start: z.string().optional(),\n end: z.string().optional(),\n })\n .optional(),\n use: [sessionMiddleware],\n },\n async (ctx): Promise<{ items: Event[] }> => {\n if (!ctx.context.session?.user?.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customerId = await getOrCreateCustomerId(\n dodopayments,\n ctx.context.session,\n ctx.context.internalAdapter,\n );\n\n const meters = await dodopayments.usageEvents.list({\n customer_id: customerId,\n ...ctx.query,\n });\n\n return ctx.json({ items: meters.items });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `User usage meter list error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to fetch the user usage\",\n });\n }\n },\n ),\n };\n};\n\n"],"mappings":";;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAGP,SAAS,SAAS;AAGlB,IAAM,mBAAmB,EAAE,OAAO;AAAA,EAChC,UAAU,EAAE,OAAO;AAAA,EACnB,YAAY,EAAE,OAAO;AAAA,EACrB,UAAU,EACP,OAAO,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC,EACrD,SAAS,EACT,SAAS;AAAA,EACZ,WAAW,EAGR,KAAK,EAAE,QAAQ,KAAK,CAAC,EACrB,UAAU,CAAC,MAAM,EAAE,YAAY,CAAC,EAChC,SAAS,EACT;AAAA,IACC;AAAA,EAGF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\";\nimport { APIError, createAuthEndpoint, sessionMiddleware } from \"better-auth/api\";\nimport { z } from \"zod/v3\";\nimport {\n CustomerPortalResponse,\n PaymentItems,\n SubscriptionItems,\n} from \"../types\";\nimport { getOrCreateCustomerId } from \"../utils\";\n\nexport const portal = () => (dodopayments: DodoPayments) => {\n return {\n dodoPortal: createAuthEndpoint(\n \"/dodopayments/customer/portal\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx): Promise<CustomerPortalResponse> => {\n if (!ctx.context.session?.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customerId = await getOrCreateCustomerId(\n dodopayments,\n ctx.context.session,\n ctx.context.internalAdapter,\n );\n\n const customerSession =\n await dodopayments.customers.customerPortal.create(customerId);\n\n return ctx.json({\n url: customerSession.link,\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments customer portal creation failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Customer portal creation failed\",\n });\n }\n },\n ),\n dodoSubscriptions: createAuthEndpoint(\n \"/dodopayments/customer/subscriptions/list\",\n {\n method: \"GET\",\n query: z\n .object({\n page: z.coerce.number().optional(),\n limit: z.coerce.number().optional(),\n status: z\n .enum([\n \"active\",\n \"cancelled\",\n \"on_hold\",\n \"pending\",\n \"failed\",\n \"expired\",\n ])\n .optional(),\n })\n .optional(),\n use: [sessionMiddleware],\n },\n async (ctx): Promise<SubscriptionItems> => {\n if (!ctx.context.session.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customerId = await getOrCreateCustomerId(\n dodopayments,\n ctx.context.session,\n ctx.context.internalAdapter,\n );\n\n const subscriptions = await dodopayments.subscriptions.list({\n customer_id: customerId,\n // page number is 0-indexed\n page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,\n page_size: ctx.query?.limit,\n status: ctx.query?.status,\n });\n\n return ctx.json({ items: subscriptions.items });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments subscriptions list failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"DodoPayments subscriptions list failed\",\n });\n }\n },\n ),\n dodoPayments: createAuthEndpoint(\n \"/dodopayments/customer/payments/list\",\n {\n method: \"GET\",\n query: z\n .object({\n page: z.coerce.number().optional(),\n limit: z.coerce.number().optional(),\n status: z\n .enum([\n \"succeeded\",\n \"failed\",\n \"cancelled\",\n \"processing\",\n \"requires_customer_action\",\n \"requires_merchant_action\",\n \"requires_payment_method\",\n \"requires_confirmation\",\n \"requires_capture\",\n \"partially_captured\",\n \"partially_captured_and_capturable\",\n ])\n .optional(),\n })\n .optional(),\n use: [sessionMiddleware],\n },\n async (ctx): Promise<PaymentItems> => {\n if (!ctx.context.session.user.id) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"User not found\",\n });\n }\n\n if (!ctx.context.session?.user.emailVerified) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"User email not verified\",\n });\n }\n\n try {\n const customerId = await getOrCreateCustomerId(\n dodopayments,\n ctx.context.session,\n ctx.context.internalAdapter,\n );\n\n const payments = await dodopayments.payments.list({\n customer_id: customerId,\n // page number is 0-indexed\n page_number: ctx.query?.page ? ctx.query.page - 1 : undefined,\n page_size: ctx.query?.limit,\n status: ctx.query?.status,\n });\n\n return ctx.json({ items: payments.items });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments orders list failed. Error: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message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ts"],"sourcesContent":["import type { DodoPayments } from \"dodopayments\";\nimport {\n handleWebhookPayload,\n WebhookHandlerConfig,\n} from \"@dodopayments/core/webhook\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\nimport { WebhookPayload } from \"@dodopayments/core/schemas\";\nimport { verifyWebhookPayload } from \"@dodopayments/core/webhook\";\nimport type { WebhookResponse } from \"../types\";\n\nexport const webhooks =\n (options: WebhookHandlerConfig) => (_dodopayments: DodoPayments) => {\n return {\n dodopaymentsWebhooks: createAuthEndpoint(\n \"/dodopayments/webhooks\",\n {\n method: \"POST\",\n metadata: {\n isAction: false,\n },\n cloneRequest: true,\n },\n async (ctx): Promise<WebhookResponse> => {\n const { webhookKey } = options;\n\n if (!ctx.request?.body) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\");\n }\n const buf = await ctx.request.text();\n let event: WebhookPayload;\n try {\n if (!webhookKey) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"DodoPayments webhook webhookKey not found\",\n });\n }\n\n const headers = {\n \"webhook-id\": ctx.request.headers.get(\"webhook-id\") as string,\n \"webhook-timestamp\": ctx.request.headers.get(\n \"webhook-timestamp\",\n ) as string,\n \"webhook-signature\": ctx.request.headers.get(\n \"webhook-signature\",\n ) as string,\n };\n\n event = await verifyWebhookPayload({\n webhookKey,\n headers,\n body: buf,\n });\n } catch (err: unknown) {\n if (err instanceof Error) {\n ctx.context.logger.error(`Webhook Error: ${err.message}`);\n throw new APIError(\"BAD_REQUEST\", {\n message: `Webhook Error: ${err.message}`,\n });\n }\n\n throw new APIError(\"BAD_REQUEST\", {\n message: `Webhook Error: ${err}`,\n });\n }\n\n try {\n await handleWebhookPayload(event, options);\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `DodoPayments webhook failed. Error: ${e.message}`,\n );\n }\n\n ctx.context.logger.error(\n `DodoPayments webhook failed. Error: ${e}`,\n );\n\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Webhook error: See server logs for more information.\",\n });\n }\n\n return ctx.json({ received: true });\n },\n ),\n };\n };\n"],"mappings":";AACA;AAAA,EACE;AAAA,OAEK;AACP,SAAS,UAAU,0BAA0B;AAE7C,SAAS,4BAA4B;AAG9B,IAAM,WACX,CAAC,YAAkC,CAAC,kBAAgC;AAClE,SAAO;AAAA,IACL,sBAAsB;AAAA,MACpB;AAAA,MACA;AAAA,QACE,QAAQ;AAAA,QACR,UAAU;AAAA,UACR,UAAU;AAAA,QACZ;AAAA,QACA,cAAc;AAAA,MAChB;AAAA,MACA,OAAO,QAAkC;AACvC,cAAM,EAAE,WAAW,IAAI;AAEvB,YAAI,CAAC,IAAI,SAAS,MAAM;AACtB,gBAAM,IAAI,SAAS,uBAAuB;AAAA,QAC5C;AACA,cAAM,MAAM,MAAM,IAAI,QAAQ,KAAK;AACnC,YAAI;AACJ,YAAI;AACF,cAAI,CAAC,YAAY;AACf,kBAAM,IAAI,SAAS,yBAAyB;AAAA,cAC1C,SAAS;AAAA,YACX,CAAC;AAAA,UACH;AAEA,gBAAM,UAAU;AAAA,YACd,cAAc,IAAI,QAAQ,QAAQ,IAAI,YAAY;AAAA,YAClD,qBAAqB,IAAI,QAAQ,QAAQ;AAAA,cACvC;AAAA,YACF;AAAA,YACA,qBAAqB,IAAI,QAAQ,QAAQ;AAAA,cACvC;AAAA,YACF;AAAA,UACF;AAEA,kBAAQ,MAAM,qBAAqB;AAAA,YACjC;AAAA,YACA;AAAA,YACA,MAAM;AAAA,UACR,CAAC;AAAA,QACH,SAAS,KAAc;AACrB,cAAI,eAAe,OAAO;AACxB,gBAAI,QAAQ,OAAO,MAAM,kBAAkB,IAAI,OAAO,EAAE;AACxD,kBAAM,IAAI,SAAS,eAAe;AAAA,cAChC,SAAS,kBAAkB,IAAI,OAAO;AAAA,YACxC,CAAC;AAAA,UACH;AAEA,gBAAM,IAAI,SAAS,eAAe;AAAA,YAChC,SAAS,kBAAkB,GAAG;AAAA,UAChC,CAAC;AAAA,QACH;AAEA,YAAI;AACF,gBAAM,qBAAqB,OAAO,OAAO;AAAA,QAC3C,SAAS,GAAY;AACnB,cAAI,aAAa,OAAO;AACtB,gBAAI,QAAQ,OAAO;AAAA,cACjB,uCAAuC,EAAE,OAAO;AAAA,YAClD;AAAA,UACF;AAEA,cAAI,QAAQ,OAAO;AAAA,YACjB,uCAAuC,CAAC;AAAA,UAC1C;AAEA,gBAAM,IAAI,SAAS,eAAe;AAAA,YAChC,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAEA,eAAO,IAAI,KAAK,EAAE,UAAU,KAAK,CAAC;AAAA,MACpC;AAAA,IACF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import type { DodoPayments } from \"dodopayments\";\n\nexport async function getOrCreateCustomerId(\n dodopayments: DodoPayments,\n session: { user: { id: string; email: string; name: string } & Record<string, unknown> },\n internalAdapter: { updateUser: (id: string, data: Record<string, unknown>) => Promise<unknown> },\n): Promise<string> {\n const dodoCustomerId = session.user[\"dodoCustomerId\"] as string | undefined;\n if (dodoCustomerId) return dodoCustomerId;\n\n // Fallback to get customer from email if dodoCustomerId doesn't exist\n const customers = await dodopayments.customers.list({\n email: session.user.email,\n });\n let customer = customers.items[0];\n\n if (!customer) {\n customer = await dodopayments.customers.create({\n email: session.user.email,\n name: session.user.name,\n }, { idempotencyKey: session.user.id });\n }\n\n internalAdapter\n .updateUser(session.user.id, { dodoCustomerId: customer.customer_id })\n .catch(() => {});\n\n return customer.customer_id;\n}\n"],"mappings":";AAEA,eAAsB,sBACpB,cACA,SACA,iBACiB;AACjB,QAAM,iBAAiB,QAAQ,KAAK,gBAAgB;AACpD,MAAI,eAAgB,QAAO;AAG3B,QAAM,YAAY,MAAM,aAAa,UAAU,KAAK;AAAA,IAClD,OAAO,QAAQ,KAAK;AAAA,EACtB,CAAC;AACD,MAAI,WAAW,UAAU,MAAM,CAAC;AAEhC,MAAI,CAAC,UAAU;AACb,eAAW,MAAM,aAAa,UAAU,OAAO;AAAA,MAC7C,OAAO,QAAQ,KAAK;AAAA,MACpB,MAAM,QAAQ,KAAK;AAAA,IACrB,GAAG,EAAE,gBAAgB,QAAQ,KAAK,GAAG,CAAC;AAAA,EACxC;AAEA,kBACG,WAAW,QAAQ,KAAK,IAAI,EAAE,gBAAgB,SAAS,YAAY,CAAC,EACpE,MAAM,MAAM;AAAA,EAAC,CAAC;AAEjB,SAAO,SAAS;AAClB;","names":[]}
|