diff --git a/src/api/subscriptions.ts b/src/api/subscriptions.ts new file mode 100644 index 0000000..a4b5e39 --- /dev/null +++ b/src/api/subscriptions.ts @@ -0,0 +1,103 @@ +import type {} from "../types"; +import { API } from "./raw"; + +type Plan = { + id: string; + userId: string; + appId: string; + + cost: number; + name: string; + description: string; + + // only visible in backend + stripeId: string; +}; + +type NewPlan = Omit; + +type Subscription = { + id: string; + userId: string; + appId: string; + + // plan info + planOwnerId: string; + planId: string; + + started: Date; + nextInvoice: Date; + + // only visible in backend + stripeId: string; +}; + +type NewSubscription = Omit; + +/** + * The class that encapsulates the subscriptions API + * @internal + */ +export class SubscriptionsAPI { + private api: API; + + constructor(api: API) { + this.api = api; + } + + /** + * Creates subscription for a given user, app, plan. + */ + async create(subscription: NewSubscription) { + return this.api.subscriptionsCreate(subscription); + } + + /** + * Lists subscriptions for my plans in current app. + * @returns All subscriptions + */ + async list() { + const res = await this.api.subscriptionsList(options); + return res.data; + } + + /** + * Cancels subscription with a given ID + * Note: must be a subscription belonging to calling user and app + * @param subscriptionId - ID of the subscription to delete + */ + async cancel(subscriptionId: string) { + return this.api.subscriptionsCancel(subscriptionId); + } + + /** + * Plans + */ + public plans = { + /** + * Creates a plan + */ + create: async (plan: NewPlan) => { + return await this.api.plansCreate(plan); + }, + + /** + * Lists plans + */ + list: async ( + options: { + userId?: string; // defaults to calling user + // appId?: string; // TODO needed? Only accept for current app? + } = {}, + ) => { + return await this.api.plansList(options); + }, + + /** + * Deletes plan + */ + delete: async (planId: string) => { + return this.api.plansDelete(planId); + }, + }; +} diff --git a/src/types/index.ts b/src/types/index.ts index 7639ef2..936353c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -54,7 +54,7 @@ export type APIOptions = Omit; export type Permission = { id: string; collectionName: string; - userId: string; + userId: string; // ID of user or * (all users) or $planId // TODO how about #contacts, #connected, or #groupId types: PermissionType[]; filter?: FilterObject; };