UNPKG

@tribute-tg/better-auth

Version:

Tribute integration for better-auth

484 lines (483 loc) 15 kB
import * as zod0 from "zod"; import { z } from "zod"; import * as better_auth0 from "better-auth"; import { InferOptionSchema, UnionToIntersection } from "better-auth"; import * as better_call11 from "better-call"; import { Currency, Period, Tribute, WebhookEvent, WebhookSubscriptionPayload } from "@tribute-tg/sdk"; //#region src/webhooks.d.ts declare const webhooks: (webhooksOptions: Pick<TributeOptions, "onNewSubscription" | "onCancelledSubscription" | "onEvent">) => (tribute: Tribute) => { tributeWebhooks: better_call11.StrictEndpoint<"/tribute/webhooks", { method: "POST"; body: z.ZodObject<{ name: z.ZodString; created_at: z.ZodString; sent_at: z.ZodString; payload: z.ZodAny; }, z.core.$strip>; } & { use: any[]; }, { status: ("OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED") | better_call11.Status; body: ({ message?: string; code?: string; cause?: unknown; } & Record<string, any>) | undefined; headers: HeadersInit; statusCode: number; name: string; message: string; stack?: string; cause?: unknown; } | undefined>; }; //#endregion //#region src/subscription.d.ts declare const subscription: (options: Pick<TributeOptions, "subscriptions">) => (tribute: Tribute) => { upgradeSubscription: better_call11.StrictEndpoint<"/subscription/upgrade", { method: "POST"; body: z.ZodObject<{ subscriptionId: z.ZodOptional<z.ZodNumber>; period: z.ZodOptional<z.ZodString>; currency: z.ZodOptional<z.ZodString>; slug: z.ZodOptional<z.ZodString>; }, z.core.$strip>; } & { use: any[]; }, { url: string; redirect: boolean; }>; listActiveSubscriptions: better_call11.StrictEndpoint<"/subscription/list", { method: "GET"; use: ((inputContext: better_call11.MiddlewareInputContext<better_call11.MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; } & { use: any[]; }, Subscription[]>; portal: better_call11.StrictEndpoint<"/customer/portal", { method: "GET"; use: ((inputContext: better_call11.MiddlewareInputContext<better_call11.MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; } & { use: any[]; }, { url: string; redirect: boolean; }>; state: better_call11.StrictEndpoint<"/customer/state", { method: "GET"; use: ((inputContext: better_call11.MiddlewareInputContext<better_call11.MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; } & { use: any[]; }, { activeSubscriptions: Subscription[]; id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }>; }; //#endregion //#region src/schema.d.ts declare const subscriptions: { subscription: { fields: { tributeSubscriptionId: { type: "number"; required: true; }; tributeSubscriptionName: { type: "string"; required: true; }; tributeUserId: { type: "number"; required: true; }; telegramUserId: { type: "number"; required: true; }; channelId: { type: "number"; required: true; }; period: { type: "string"; required: true; }; price: { type: "number"; required: true; }; amount: { type: "number"; required: true; }; currency: { type: "string"; required: true; }; expiresAt: { type: "string"; required: true; }; status: { type: "string"; defaultValue: string; }; }; }; }; declare const user: { user: { fields: { tributeUserId: { type: "number"; required: false; }; }; }; }; //#endregion //#region src/types.d.ts interface CheckoutSubscription { /** * Subscription Id from Tribute Subscription */ subscriptionId: number; /** * Easily identifiable slug for the subscription */ slug: string; /** * Period (onetime, monthly, yearly, etc.) */ period?: string; currency?: Currency; redirectUrl?: string; } interface Subscription { tributeSubscriptionId: number; tributeSubscriptionName: string; tributeUserId: number; telegramUserId: number; userId: string; channelId: number; period: Period; price: number; amount: number; currency: Currency; expiresAt: string; status: string; } type TributePlugin = ReturnType<typeof subscription> | ReturnType<typeof webhooks>; type TributeEndpoints = UnionToIntersection<ReturnType<TributePlugin>>; interface TributeOptions { /** * Tribute Client */ tributeClient: Tribute; /** * A callback to run after a user has subscribed * @param payload - Tribute Subscription Data * @returns */ onNewSubscription?: (payload: WebhookSubscriptionPayload) => Promise<void>; /** * A callback to run after a user is about to cancel their subscription * @returns */ onCancelledSubscription?: (payload: WebhookSubscriptionPayload) => Promise<void>; /** * A callback to run after a Tribute event is received * @param event - Tribute Event * @returns */ onEvent?: (event: WebhookEvent) => Promise<void>; /** * Optional list of slug -> subscriptionId mappings for easy slug checkouts */ subscriptions?: CheckoutSubscription[] | (() => Promise<CheckoutSubscription[]>); /** * Schema for the tribute plugin */ schema?: InferOptionSchema<typeof subscriptions & typeof user>; } //#endregion //#region src/client.d.ts declare const tributeClient: () => { id: "tribute-client"; $InferServerPlugin: ReturnType<typeof tribute>; }; //#endregion //#region src/index.d.ts declare const tribute: (options: TributeOptions) => { id: "tribute"; endpoints: { tributeWebhooks: better_call11.StrictEndpoint<"/tribute/webhooks", { method: "POST"; body: zod0.ZodObject<{ name: zod0.ZodString; created_at: zod0.ZodString; sent_at: zod0.ZodString; payload: zod0.ZodAny; }, better_auth0.$strip>; } & { use: any[]; }, { status: ("OK" | "CREATED" | "ACCEPTED" | "NO_CONTENT" | "MULTIPLE_CHOICES" | "MOVED_PERMANENTLY" | "FOUND" | "SEE_OTHER" | "NOT_MODIFIED" | "TEMPORARY_REDIRECT" | "BAD_REQUEST" | "UNAUTHORIZED" | "PAYMENT_REQUIRED" | "FORBIDDEN" | "NOT_FOUND" | "METHOD_NOT_ALLOWED" | "NOT_ACCEPTABLE" | "PROXY_AUTHENTICATION_REQUIRED" | "REQUEST_TIMEOUT" | "CONFLICT" | "GONE" | "LENGTH_REQUIRED" | "PRECONDITION_FAILED" | "PAYLOAD_TOO_LARGE" | "URI_TOO_LONG" | "UNSUPPORTED_MEDIA_TYPE" | "RANGE_NOT_SATISFIABLE" | "EXPECTATION_FAILED" | "I'M_A_TEAPOT" | "MISDIRECTED_REQUEST" | "UNPROCESSABLE_ENTITY" | "LOCKED" | "FAILED_DEPENDENCY" | "TOO_EARLY" | "UPGRADE_REQUIRED" | "PRECONDITION_REQUIRED" | "TOO_MANY_REQUESTS" | "REQUEST_HEADER_FIELDS_TOO_LARGE" | "UNAVAILABLE_FOR_LEGAL_REASONS" | "INTERNAL_SERVER_ERROR" | "NOT_IMPLEMENTED" | "BAD_GATEWAY" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "HTTP_VERSION_NOT_SUPPORTED" | "VARIANT_ALSO_NEGOTIATES" | "INSUFFICIENT_STORAGE" | "LOOP_DETECTED" | "NOT_EXTENDED" | "NETWORK_AUTHENTICATION_REQUIRED") | better_call11.Status; body: ({ message?: string; code?: string; cause?: unknown; } & Record<string, any>) | undefined; headers: HeadersInit; statusCode: number; name: string; message: string; stack?: string; cause?: unknown; } | undefined>; } | { upgradeSubscription: better_call11.StrictEndpoint<"/subscription/upgrade", { method: "POST"; body: zod0.ZodObject<{ subscriptionId: zod0.ZodOptional<zod0.ZodNumber>; period: zod0.ZodOptional<zod0.ZodString>; currency: zod0.ZodOptional<zod0.ZodString>; slug: zod0.ZodOptional<zod0.ZodString>; }, better_auth0.$strip>; } & { use: any[]; }, { url: string; redirect: boolean; }>; listActiveSubscriptions: better_call11.StrictEndpoint<"/subscription/list", { method: "GET"; use: ((inputContext: better_call11.MiddlewareInputContext<better_call11.MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; } & { use: any[]; }, Subscription[]>; portal: better_call11.StrictEndpoint<"/customer/portal", { method: "GET"; use: ((inputContext: better_call11.MiddlewareInputContext<better_call11.MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; } & { use: any[]; }, { url: string; redirect: boolean; }>; state: better_call11.StrictEndpoint<"/customer/state", { method: "GET"; use: ((inputContext: better_call11.MiddlewareInputContext<better_call11.MiddlewareOptions>) => Promise<{ session: { session: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; userId: string; expiresAt: Date; token: string; ipAddress?: string | null | undefined; userAgent?: string | null | undefined; }; user: Record<string, any> & { id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }; }; }>)[]; } & { use: any[]; }, { activeSubscriptions: Subscription[]; id: string; createdAt: Date; updatedAt: Date; email: string; emailVerified: boolean; name: string; image?: string | null | undefined; }>; }; schema: { user: { fields: { tributeUserId: { type: "number"; required: false; }; }; }; subscription: { fields: { tributeSubscriptionId: { type: "number"; required: true; }; tributeSubscriptionName: { type: "string"; required: true; }; tributeUserId: { type: "number"; required: true; }; telegramUserId: { type: "number"; required: true; }; channelId: { type: "number"; required: true; }; period: { type: "string"; required: true; }; price: { type: "number"; required: true; }; amount: { type: "number"; required: true; }; currency: { type: "string"; required: true; }; expiresAt: { type: "string"; required: true; }; status: { type: "string"; defaultValue: string; }; }; }; }; }; //#endregion export { CheckoutSubscription, Subscription, TributeEndpoints, TributeOptions, TributePlugin, subscription, tribute, tributeClient, webhooks }; //# sourceMappingURL=index.d.mts.map