diff --git a/packages/subscription-client/src/index.ts b/packages/subscription-client/src/index.ts index 461547ce..bd822986 100644 --- a/packages/subscription-client/src/index.ts +++ b/packages/subscription-client/src/index.ts @@ -1,11 +1,20 @@ export interface SubscriptionDoc { id: string; userId: string; + productId?: string; plan: string; - status: "active" | "trialing" | "past_due" | "cancelled" | "none"; + status: 'active' | 'trialing' | 'past_due' | 'cancelled' | 'none'; + currentPeriodStart?: string; currentPeriodEnd: string; cancelAtPeriodEnd: boolean; + monthlyPrice?: number; + tokensIncluded?: number; + tokensUsed?: number; + stripeCustomerId?: string; + stripeSubscriptionId?: string; features?: string[]; + createdAt?: string; + updatedAt?: string; } export interface PlanConfig { @@ -34,25 +43,20 @@ export interface SubscriptionClient { } function trimTrailingSlash(url: string): string { - return url.replace(/\/+$/, ""); + return url.replace(/\/+$/, ''); } -export function createSubscriptionClient( - opts: SubscriptionClientOptions, -): SubscriptionClient { +export function createSubscriptionClient(opts: SubscriptionClientOptions): SubscriptionClient { const base = trimTrailingSlash(opts.baseUrl); let cached: SubscriptionDoc | null | undefined; - async function request( - path: string, - init?: RequestInit, - ): Promise { + async function request(path: string, init?: RequestInit): Promise { const token = opts.getAccessToken(); const headers = new Headers(init?.headers); - headers.set("Authorization", `Bearer ${token}`); - headers.set("X-Product-Id", opts.productId); - if (!headers.has("Content-Type") && init?.body !== undefined) { - headers.set("Content-Type", "application/json"); + headers.set('Authorization', `Bearer ${token}`); + headers.set('X-Product-Id', opts.productId); + if (!headers.has('Content-Type') && init?.body !== undefined) { + headers.set('Content-Type', 'application/json'); } const res = await fetch(`${base}${path}`, { ...init, headers }); if (res.status === 404) { @@ -80,36 +84,31 @@ export function createSubscriptionClient( return { async getMySubscription(): Promise { - const data = await request( - "/billing/subscriptions/me", - ); + const data = await request('/billing/subscriptions/me'); cached = data; return data; }, async getPlans(): Promise { - const data = await request< - PlanConfig[] | { plans?: PlanConfig[] } | null - >("/billing/plans"); + const data = await request('/billing/plans'); if (data == null) { return []; } if (Array.isArray(data)) { return data; } - if (data && typeof data === "object" && "plans" in data && Array.isArray(data.plans)) { + if (data && typeof data === 'object' && 'plans' in data && Array.isArray(data.plans)) { return data.plans; } return []; }, async cancelSubscription(): Promise { - const data = await request( - "/billing/subscriptions/cancel", - { method: "POST" }, - ); + const data = await request('/billing/subscriptions/cancel', { + method: 'POST', + }); if (data === null) { - throw new Error("Cancel subscription returned no body"); + throw new Error('Cancel subscription returned no body'); } cached = data; return data; @@ -120,15 +119,14 @@ export function createSubscriptionClient( if (!sub) { return false; } - const paid = - sub.status === "active" || sub.status === "trialing"; + const paid = sub.status === 'active' || sub.status === 'trialing'; const planLower = sub.plan.toLowerCase(); - const notFree = planLower !== "free" && planLower !== "none"; + const notFree = planLower !== 'free' && planLower !== 'none'; return paid && notFree; }, isTrialing(): boolean { - return subscriptionFromCache()?.status === "trialing"; + return subscriptionFromCache()?.status === 'trialing'; }, hasFeature(feature: string): boolean {