Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(mobile): add billing pro helpers
Richie McIlroy5e58bfa6bfa5
diff --git a/apps/mobile/src/billing/pro.test.ts b/apps/mobile/src/billing/pro.test.tsnew file mode 100644index 000000000..277fd855c--- /dev/null+++ b/apps/mobile/src/billing/pro.test.ts@@ -0,0 +1,72 @@+import { describe, expect, it } from "vitest";+import { getProPlan, MobileBillingError } from "./pro";++describe("mobile Pro plan", () => {+ it("loads the signed-in account plan", async () => {+ const calls: Array<{ input: RequestInfo | URL; init?: RequestInit }> = [];+ const originalFetch = globalThis.fetch;+ globalThis.fetch = (async (+ input: RequestInfo | URL,+ init?: RequestInit,+ ) => {+ calls.push({ input, init });+ return new Response(+ JSON.stringify({+ upgraded: true,+ stripeSubscriptionStatus: "active",+ }),+ { status: 200 },+ );+ }) as typeof fetch;++ try {+ await expect(+ getProPlan({ apiKey: "mobile-key", baseUrl: "https://cap.so/" }),+ ).resolves.toEqual({+ upgraded: true,+ stripeSubscriptionStatus: "active",+ });+ expect(String(calls[0]?.input)).toBe("https://cap.so/api/desktop/plan");+ expect(calls[0]?.init?.headers).toEqual({+ Authorization: "Bearer mobile-key",+ });+ } finally {+ globalThis.fetch = originalFetch;+ }+ });++ it("preserves error response details", async () => {+ const originalFetch = globalThis.fetch;+ globalThis.fetch = (async () =>+ new Response(JSON.stringify({ code: "billing_unavailable" }), {+ status: 503,+ })) as typeof fetch;++ try {+ await expect(+ getProPlan({ apiKey: "mobile-key", baseUrl: "https://cap.so" }),+ ).rejects.toMatchObject({+ status: 503,+ payload: { code: "billing_unavailable" },+ });+ } finally {+ globalThis.fetch = originalFetch;+ }+ });++ it("rejects invalid plan responses", async () => {+ const originalFetch = globalThis.fetch;+ globalThis.fetch = (async () =>+ new Response(JSON.stringify({ upgraded: "yes" }), {+ status: 200,+ })) as typeof fetch;++ try {+ await expect(+ getProPlan({ apiKey: "mobile-key", baseUrl: "https://cap.so" }),+ ).rejects.toBeInstanceOf(MobileBillingError);+ } finally {+ globalThis.fetch = originalFetch;+ }+ });+});diff --git a/apps/mobile/src/billing/pro.ts b/apps/mobile/src/billing/pro.tsnew file mode 100644index 000000000..593f83b6a--- /dev/null+++ b/apps/mobile/src/billing/pro.ts@@ -0,0 +1,68 @@+type BillingRequest = {+ apiKey: string;+ baseUrl: string;+};++export type ProPlan = {+ upgraded: boolean;+ stripeSubscriptionStatus: string | null;+};++export class MobileBillingError extends Error {+ constructor(+ message: string,+ readonly status: number,+ readonly payload: unknown,+ ) {+ super(message);+ this.name = "MobileBillingError";+ }+}++const trimBaseUrl = (baseUrl: string) => baseUrl.replace(/\/+$/, "");++const isRecord = (value: unknown): value is Record<string, unknown> =>+ typeof value === "object" && value !== null && !Array.isArray(value);++const parseJson = async (response: Response) => {+ const text = await response.text();+ if (!text) return null;+ try {+ return JSON.parse(text) as unknown;+ } catch {+ return text;+ }+};++export const getProPlan = async ({+ apiKey,+ baseUrl,+}: BillingRequest): Promise<ProPlan> => {+ const response = await fetch(`${trimBaseUrl(baseUrl)}/api/desktop/plan`, {+ headers: {+ Authorization: `Bearer ${apiKey}`,+ },+ });+ const payload = await parseJson(response);++ if (!response.ok) {+ throw new MobileBillingError(+ `Mobile billing request failed with ${response.status}`,+ response.status,+ payload,+ );+ }+ if (+ !isRecord(payload) ||+ typeof payload.upgraded !== "boolean" ||+ (payload.stripeSubscriptionStatus !== null &&+ typeof payload.stripeSubscriptionStatus !== "string")+ ) {+ throw new MobileBillingError("Invalid mobile plan response", 200, payload);+ }++ return {+ upgraded: payload.upgraded,+ stripeSubscriptionStatus: payload.stripeSubscriptionStatus,+ };+};