Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(mobile): add mobile API client

Richie McIlroy43d090196221
diff --git a/apps/mobile/src/api/mobile.test.ts b/apps/mobile/src/api/mobile.test.ts
new file mode 100644
index 000000000..a4d1407a0
--- /dev/null
+++ b/apps/mobile/src/api/mobile.test.ts
@@ -0,0 +1,447 @@
+import { describe, expect, it, vi } from "vitest";
+import {
+ createMobileApiClient,
+ createSessionRequestUrl,
+ uploadToTarget,
+} from "./mobile";
+
+const fileSystemMock = vi.hoisted(() => ({
+ FileSystemUploadType: {
+ BINARY_CONTENT: 0,
+ MULTIPART: 1,
+ },
+ createUploadTask: vi.fn(),
+ getInfoAsync: vi.fn(),
+}));
+
+vi.mock("expo-file-system/legacy", () => fileSystemMock);
+
+describe("createMobileApiClient", () => {
+ it("decodes bootstrap responses through shared schemas", async () => {
+ const calls: RequestInfo[] = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (input: RequestInfo | URL) => {
+ calls.push(input as RequestInfo);
+ return new Response(
+ JSON.stringify({
+ user: {
+ id: "user_123",
+ name: "Richie",
+ email: "[email protected]",
+ imageUrl: null,
+ activeOrganizationId: "org_123",
+ },
+ organizations: [
+ {
+ id: "org_123",
+ name: "Cap",
+ iconUrl: null,
+ role: "owner",
+ },
+ ],
+ activeOrganizationId: "org_123",
+ rootFolders: [],
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+ const result = await client.bootstrap();
+ expect(result.user.email).toBe("[email protected]");
+ expect(String(calls[0])).toBe("https://cap.so/api/mobile/bootstrap");
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("decodes public auth provider config", async () => {
+ const calls: RequestInfo[] = [];
+ const originalFetch = globalThis.fetch;
+ globalThis.fetch = (async (input: RequestInfo | URL) => {
+ calls.push(input as RequestInfo);
+ return new Response(
+ JSON.stringify({
+ googleAuthAvailable: false,
+ workosAuthAvailable: true,
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => null,
+ });
+ const result = await client.getAuthConfig();
+ expect(result.googleAuthAvailable).toBe(false);
+ expect(result.workosAuthAvailable).toBe(true);
+ expect(String(calls[0])).toBe("https://cap.so/api/mobile/session/config");
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("builds Google session request URLs", () => {
+ expect(
+ createSessionRequestUrl("https://cap.so/", "cap://auth", "google"),
+ ).toBe(
+ "https://cap.so/api/mobile/session/request?redirectUri=cap%3A%2F%2Fauth&provider=google",
+ );
+ });
+
+ it("builds WorkOS session request URLs", () => {
+ expect(
+ createSessionRequestUrl(
+ "https://cap.so/",
+ "cap://auth",
+ "workos",
+ "org_123",
+ ),
+ ).toBe(
+ "https://cap.so/api/mobile/session/request?redirectUri=cap%3A%2F%2Fauth&provider=workos&organizationId=org_123",
+ );
+ });
+
+ it("updates Cap sharing with the authenticated PATCH endpoint", 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({
+ id: "video_123",
+ shareUrl: "https://cap.so/s/video_123",
+ title: "Launch review",
+ createdAt: "2026-05-18T10:00:00.000Z",
+ updatedAt: "2026-05-18T10:30:00.000Z",
+ ownerName: "Richie",
+ durationSeconds: null,
+ thumbnailUrl: null,
+ folderId: null,
+ public: false,
+ protected: false,
+ viewCount: 0,
+ commentCount: 0,
+ reactionCount: 0,
+ upload: null,
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+ const result = await client.updateCapSharing("video_123", {
+ public: false,
+ });
+ const body = calls[0]?.init?.body;
+
+ expect(result.public).toBe(false);
+ expect(String(calls[0]?.input)).toBe(
+ "https://cap.so/api/mobile/caps/video_123/sharing",
+ );
+ expect(calls[0]?.init?.method).toBe("PATCH");
+ expect(calls[0]?.init?.headers).toBeInstanceOf(Headers);
+ expect((calls[0]?.init?.headers as Headers).get("authorization")).toBe(
+ "Bearer api-key",
+ );
+ expect(typeof body).toBe("string");
+ expect(JSON.parse(body as string)).toEqual({ public: false });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("creates folders with the authenticated POST endpoint", 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({
+ id: "folder_123",
+ name: "Product",
+ color: "blue",
+ parentId: null,
+ videoCount: 0,
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+ const result = await client.createFolder({
+ name: "Product",
+ color: "blue",
+ });
+ const body = calls[0]?.init?.body;
+
+ expect(result.name).toBe("Product");
+ expect(String(calls[0]?.input)).toBe("https://cap.so/api/mobile/folders");
+ expect(calls[0]?.init?.method).toBe("POST");
+ expect(typeof body).toBe("string");
+ expect(JSON.parse(body as string)).toEqual({
+ name: "Product",
+ color: "blue",
+ });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("updates Cap titles with the authenticated PATCH endpoint", 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({
+ id: "video_123",
+ shareUrl: "https://cap.so/s/video_123",
+ title: "Roadmap review",
+ createdAt: "2026-05-18T10:00:00.000Z",
+ updatedAt: "2026-05-18T10:30:00.000Z",
+ ownerName: "Richie",
+ durationSeconds: null,
+ thumbnailUrl: null,
+ folderId: null,
+ public: true,
+ protected: false,
+ viewCount: 0,
+ commentCount: 0,
+ reactionCount: 0,
+ upload: null,
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+ const result = await client.updateCapTitle("video_123", {
+ title: "Roadmap review",
+ });
+ const body = calls[0]?.init?.body;
+
+ expect(result.title).toBe("Roadmap review");
+ expect(String(calls[0]?.input)).toBe(
+ "https://cap.so/api/mobile/caps/video_123/title",
+ );
+ expect(calls[0]?.init?.method).toBe("PATCH");
+ expect(typeof body).toBe("string");
+ expect(JSON.parse(body as string)).toEqual({ title: "Roadmap review" });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("updates Cap passwords with the authenticated PATCH endpoint", 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({
+ id: "video_123",
+ shareUrl: "https://cap.so/s/video_123",
+ title: "Launch review",
+ createdAt: "2026-05-18T10:00:00.000Z",
+ updatedAt: "2026-05-18T10:30:00.000Z",
+ ownerName: "Richie",
+ durationSeconds: null,
+ thumbnailUrl: null,
+ folderId: null,
+ public: true,
+ protected: true,
+ viewCount: 0,
+ commentCount: 0,
+ reactionCount: 0,
+ upload: null,
+ }),
+ { status: 200 },
+ );
+ }) as typeof fetch;
+
+ try {
+ const client = createMobileApiClient({
+ baseUrl: "https://cap.so/",
+ getToken: () => "api-key",
+ });
+ const result = await client.updateCapPassword("video_123", {
+ password: "secret",
+ });
+ const body = calls[0]?.init?.body;
+
+ expect(result.protected).toBe(true);
+ expect(String(calls[0]?.input)).toBe(
+ "https://cap.so/api/mobile/caps/video_123/password",
+ );
+ expect(calls[0]?.init?.method).toBe("PATCH");
+ expect(typeof body).toBe("string");
+ expect(JSON.parse(body as string)).toEqual({ password: "secret" });
+ } finally {
+ globalThis.fetch = originalFetch;
+ }
+ });
+
+ it("uploads local files with native transfer progress", async () => {
+ const uploadAsync = vi.fn(() =>
+ Promise.resolve({
+ body: "",
+ headers: {},
+ mimeType: null,
+ status: 200,
+ }),
+ );
+ const onProgress = vi.fn();
+
+ fileSystemMock.createUploadTask.mockImplementation(
+ (
+ url: string,
+ fileUri: string,
+ options: unknown,
+ callback?: (data: {
+ totalBytesExpectedToSend: number;
+ totalBytesSent: number;
+ }) => void,
+ ) => {
+ callback?.({
+ totalBytesExpectedToSend: 3,
+ totalBytesSent: 2,
+ });
+ return { uploadAsync, url, fileUri, options };
+ },
+ );
+
+ await uploadToTarget(
+ {
+ type: "driveResumable",
+ url: "https://uploads.example/drive",
+ headers: {
+ "Content-Type": "video/mp4",
+ },
+ },
+ {
+ uri: "file:///tmp/video.mp4",
+ name: "video.mp4",
+ type: "video/mp4",
+ size: 3,
+ },
+ onProgress,
+ );
+
+ expect(fileSystemMock.createUploadTask).toHaveBeenCalledWith(
+ "https://uploads.example/drive",
+ "file:///tmp/video.mp4",
+ {
+ headers: {
+ "Content-Range": "bytes 0-2/3",
+ "Content-Type": "video/mp4",
+ },
+ httpMethod: "PUT",
+ uploadType: fileSystemMock.FileSystemUploadType.BINARY_CONTENT,
+ },
+ expect.any(Function),
+ );
+ expect(uploadAsync).toHaveBeenCalled();
+ expect(onProgress).toHaveBeenCalledWith({ loaded: 2, total: 3 });
+ });
+
+ it("sets the Drive resumable upload byte range for remote blobs", async () => {
+ class MockXMLHttpRequest {
+ static instances: MockXMLHttpRequest[] = [];
+ upload: {
+ onprogress:
+ | ((event: ProgressEvent<XMLHttpRequestEventTarget>) => void)
+ | null;
+ } = { onprogress: null };
+ status = 200;
+ responseText = "";
+ onload: (() => void) | null = null;
+ onerror: (() => void) | null = null;
+ method = "";
+ url = "";
+ headers = new Map<string, string>();
+ body: BodyInit | null = null;
+
+ constructor() {
+ MockXMLHttpRequest.instances.push(this);
+ }
+
+ open(method: string, url: string) {
+ this.method = method;
+ this.url = url;
+ }
+
+ setRequestHeader(key: string, value: string) {
+ this.headers.set(key, value);
+ }
+
+ send(body: BodyInit) {
+ this.body = body;
+ this.onload?.();
+ }
+ }
+
+ const originalFetch = globalThis.fetch;
+ const originalXhr = globalThis.XMLHttpRequest;
+ globalThis.fetch = (async () =>
+ new Response(new Uint8Array([1, 2, 3]))) as typeof fetch;
+ globalThis.XMLHttpRequest =
+ MockXMLHttpRequest as unknown as typeof XMLHttpRequest;
+
+ try {
+ await uploadToTarget(
+ {
+ type: "driveResumable",
+ url: "https://uploads.example/drive",
+ headers: {
+ "Content-Type": "video/mp4",
+ },
+ },
+ {
+ uri: "https://cache.example/video.mp4",
+ name: "video.mp4",
+ type: "video/mp4",
+ size: 3,
+ },
+ );
+
+ const request = MockXMLHttpRequest.instances[0];
+ expect(request?.method).toBe("PUT");
+ expect(request?.headers.get("content-type")).toBe("video/mp4");
+ expect(request?.headers.get("content-range")).toBe("bytes 0-2/3");
+ } finally {
+ globalThis.fetch = originalFetch;
+ globalThis.XMLHttpRequest = originalXhr;
+ }
+ });
+});
diff --git a/apps/mobile/src/api/mobile.ts b/apps/mobile/src/api/mobile.ts
new file mode 100644
index 000000000..d4d352469
--- /dev/null
+++ b/apps/mobile/src/api/mobile.ts
@@ -0,0 +1,479 @@
+import { Mobile, type Storage } from "@cap/web-domain";
+import { Schema } from "effect";
+import * as FileSystem from "expo-file-system/legacy";
+
+export type MobileApiKeyResponse = typeof Mobile.MobileApiKeyResponse.Type;
+export type MobileSuccessResponse = typeof Mobile.MobileSuccessResponse.Type;
+export type MobileAuthConfigResponse =
+ typeof Mobile.MobileAuthConfigResponse.Type;
+export type MobileBootstrapResponse =
+ typeof Mobile.MobileBootstrapResponse.Type;
+export type MobileCapsListResponse = typeof Mobile.MobileCapsListResponse.Type;
+export type MobileCapSummary = typeof Mobile.MobileCapSummary.Type;
+export type MobileFolder = typeof Mobile.MobileFolder.Type;
+export type MobileCapDetail = typeof Mobile.MobileCapDetail.Type;
+export type MobileComment = typeof Mobile.MobileComment.Type;
+export type MobilePlaybackResponse = typeof Mobile.MobilePlaybackResponse.Type;
+export type MobileDownloadResponse = typeof Mobile.MobileDownloadResponse.Type;
+export type MobileCapSharingInput = typeof Mobile.MobileCapSharingInput.Type;
+export type MobileCapTitleInput = typeof Mobile.MobileCapTitleInput.Type;
+export type MobileCapPasswordInput = typeof Mobile.MobileCapPasswordInput.Type;
+export type MobileFolderCreateInput =
+ typeof Mobile.MobileFolderCreateInput.Type;
+export type MobileUploadCreateInput =
+ typeof Mobile.MobileUploadCreateInput.Type;
+export type MobileUploadCreateResponse =
+ typeof Mobile.MobileUploadCreateResponse.Type;
+
+export type MobileApiClient = ReturnType<typeof createMobileApiClient>;
+
+export type UploadFile = {
+ uri: string;
+ name: string;
+ type: string;
+ size?: number;
+ durationSeconds?: number;
+ width?: number;
+ height?: number;
+};
+
+export type UploadProgress = {
+ loaded: number;
+ total: number;
+};
+
+type ClientOptions = {
+ baseUrl: string;
+ getToken: () => string | Promise<string | null> | null;
+};
+
+type RequestOptions = {
+ method?: "GET" | "POST" | "PATCH" | "DELETE";
+ query?: Record<string, string | number | null | undefined>;
+ body?: unknown;
+};
+
+export class MobileApiError extends Error {
+ constructor(
+ message: string,
+ readonly status: number,
+ readonly payload: unknown,
+ ) {
+ super(message);
+ this.name = "MobileApiError";
+ }
+}
+
+const trimBaseUrl = (baseUrl: string) => baseUrl.replace(/\/+$/, "");
+
+const decode = async <A, I>(
+ schema: Schema.Schema<A, I, never>,
+ value: unknown,
+): Promise<A> => Schema.decodeUnknownPromise(schema)(value);
+
+const appendQuery = (
+ url: URL,
+ query: Record<string, string | number | null | undefined> | undefined,
+) => {
+ if (!query) return;
+ for (const [key, value] of Object.entries(query)) {
+ if (value !== null && value !== undefined && value !== "") {
+ url.searchParams.set(key, String(value));
+ }
+ }
+};
+
+const parseJson = async (response: Response) => {
+ const text = await response.text();
+ if (text.length === 0) return null;
+ return JSON.parse(text) as unknown;
+};
+
+export const createSessionRequestUrl = (
+ baseUrl: string,
+ redirectUri: string,
+ provider?: "google" | "workos",
+ organizationId?: string,
+) => {
+ const url = new URL("/api/mobile/session/request", trimBaseUrl(baseUrl));
+ url.searchParams.set("redirectUri", redirectUri);
+ if (provider) url.searchParams.set("provider", provider);
+ if (organizationId) url.searchParams.set("organizationId", organizationId);
+ return url.toString();
+};
+
+export const createMobileApiClient = ({ baseUrl, getToken }: ClientOptions) => {
+ const origin = trimBaseUrl(baseUrl);
+
+ const request = async <A, I>(
+ path: string,
+ schema: Schema.Schema<A, I, never>,
+ options: RequestOptions = {},
+ ): Promise<A> => {
+ const token = await getToken();
+ if (!token) {
+ throw new MobileApiError("Missing mobile session", 401, null);
+ }
+
+ const url = new URL(path, origin);
+ appendQuery(url, options.query);
+ const headers = new Headers({
+ Authorization: `Bearer ${token}`,
+ });
+ let body: BodyInit | undefined;
+ if (options.body !== undefined) {
+ headers.set("Content-Type", "application/json");
+ body = JSON.stringify(options.body);
+ }
+
+ const response = await fetch(url.toString(), {
+ method: options.method ?? "GET",
+ headers,
+ body,
+ });
+ const payload = await parseJson(response);
+ if (!response.ok) {
+ throw new MobileApiError(
+ `Mobile API request failed with ${response.status}`,
+ response.status,
+ payload,
+ );
+ }
+ return decode(schema, payload);
+ };
+
+ const publicRequest = async <A, I>(
+ path: string,
+ schema: Schema.Schema<A, I, never>,
+ options: Omit<RequestOptions, "query"> = {},
+ ): Promise<A> => {
+ const url = new URL(path, origin);
+ const headers = new Headers();
+ let body: BodyInit | undefined;
+ if (options.body !== undefined) {
+ headers.set("Content-Type", "application/json");
+ body = JSON.stringify(options.body);
+ }
+
+ const response = await fetch(url.toString(), {
+ method: options.method ?? "GET",
+ headers,
+ body,
+ });
+ const payload = await parseJson(response);
+ if (!response.ok) {
+ throw new MobileApiError(
+ `Mobile API request failed with ${response.status}`,
+ response.status,
+ payload,
+ );
+ }
+ return decode(schema, payload);
+ };
+
+ return {
+ getAuthConfig: () =>
+ publicRequest(
+ "/api/mobile/session/config",
+ Mobile.MobileAuthConfigResponse,
+ ),
+ requestEmailCode: (email: string) =>
+ publicRequest(
+ "/api/mobile/session/email/request",
+ Mobile.MobileSuccessResponse,
+ {
+ method: "POST",
+ body: { email },
+ },
+ ),
+ verifyEmailCode: (input: { email: string; code: string }) =>
+ publicRequest(
+ "/api/mobile/session/email/verify",
+ Mobile.MobileApiKeyResponse,
+ {
+ method: "POST",
+ body: input,
+ },
+ ),
+ bootstrap: () =>
+ request("/api/mobile/bootstrap", Mobile.MobileBootstrapResponse),
+ setActiveOrganization: (organizationId: string) =>
+ request(
+ "/api/mobile/user/active-organization",
+ Mobile.MobileBootstrapResponse,
+ {
+ method: "PATCH",
+ body: { organizationId },
+ },
+ ),
+ listCaps: (params: {
+ folderId?: string | null;
+ page?: number;
+ limit?: number;
+ }) =>
+ request("/api/mobile/caps", Mobile.MobileCapsListResponse, {
+ query: params,
+ }),
+ createFolder: (input: MobileFolderCreateInput) =>
+ request("/api/mobile/folders", Mobile.MobileFolder, {
+ method: "POST",
+ body: input,
+ }),
+ getCap: (id: string) =>
+ request(`/api/mobile/caps/${id}`, Mobile.MobileCapDetail),
+ updateCapSharing: (id: string, input: MobileCapSharingInput) =>
+ request(`/api/mobile/caps/${id}/sharing`, Mobile.MobileCapSummary, {
+ method: "PATCH",
+ body: input,
+ }),
+ updateCapTitle: (id: string, input: MobileCapTitleInput) =>
+ request(`/api/mobile/caps/${id}/title`, Mobile.MobileCapSummary, {
+ method: "PATCH",
+ body: input,
+ }),
+ updateCapPassword: (id: string, input: MobileCapPasswordInput) =>
+ request(`/api/mobile/caps/${id}/password`, Mobile.MobileCapSummary, {
+ method: "PATCH",
+ body: input,
+ }),
+ deleteCap: (id: string) =>
+ request(`/api/mobile/caps/${id}`, Mobile.MobileSuccessResponse, {
+ method: "DELETE",
+ }),
+ getPlayback: (id: string) =>
+ request(`/api/mobile/caps/${id}/playback`, Mobile.MobilePlaybackResponse),
+ getDownload: (id: string) =>
+ request(`/api/mobile/caps/${id}/download`, Mobile.MobileDownloadResponse),
+ createComment: (
+ id: string,
+ input: { content: string; timestamp: number | null },
+ ) =>
+ request(`/api/mobile/caps/${id}/comments`, Mobile.MobileComment, {
+ method: "POST",
+ body: input,
+ }),
+ deleteComment: (id: string) =>
+ request(`/api/mobile/comments/${id}`, Mobile.MobileSuccessResponse, {
+ method: "DELETE",
+ }),
+ createReaction: (
+ id: string,
+ input: { content: string; timestamp: number | null },
+ ) =>
+ request(`/api/mobile/caps/${id}/reactions`, Mobile.MobileComment, {
+ method: "POST",
+ body: input,
+ }),
+ createUpload: (input: MobileUploadCreateInput) =>
+ request("/api/mobile/uploads", Mobile.MobileUploadCreateResponse, {
+ method: "POST",
+ body: input,
+ }),
+ updateUploadProgress: (
+ id: string,
+ input: { uploaded: number; total: number },
+ ) =>
+ request(
+ `/api/mobile/uploads/${id}/progress`,
+ Mobile.MobileSuccessResponse,
+ {
+ method: "POST",
+ body: input,
+ },
+ ),
+ completeUpload: (
+ id: string,
+ input: { rawFileKey: string; contentLength?: number },
+ ) =>
+ request(
+ `/api/mobile/uploads/${id}/complete`,
+ Mobile.MobileSuccessResponse,
+ {
+ method: "POST",
+ body: input,
+ },
+ ),
+ revokeSession: () =>
+ request("/api/mobile/session/revoke", Mobile.MobileSuccessResponse, {
+ method: "POST",
+ }),
+ };
+};
+
+const targetHeaders = (headers: Record<string, string>) => {
+ const result = new Headers();
+ for (const [key, value] of Object.entries(headers)) {
+ result.set(key, value);
+ }
+ return result;
+};
+
+const isNativeUploadUri = (uri: string) =>
+ uri.startsWith("file://") || uri.startsWith("content://");
+
+const getLocalFileSize = async (file: UploadFile) => {
+ if (typeof file.size === "number" && file.size > 0) return file.size;
+
+ const info = await FileSystem.getInfoAsync(file.uri);
+ if (!info.exists || info.isDirectory) return 0;
+ return info.size;
+};
+
+const uploadNativeFile = async (
+ method: "POST" | "PUT",
+ url: string,
+ file: UploadFile,
+ options: FileSystem.FileSystemUploadOptions,
+ onProgress?: (progress: UploadProgress) => void,
+) => {
+ const task = FileSystem.createUploadTask(
+ url,
+ file.uri,
+ {
+ ...options,
+ httpMethod: method,
+ },
+ (data) => {
+ onProgress?.({
+ loaded: data.totalBytesSent,
+ total: data.totalBytesExpectedToSend,
+ });
+ },
+ );
+ const response = await task.uploadAsync();
+ if (!response || response.status < 200 || response.status >= 300) {
+ throw new MobileApiError(
+ "Upload target rejected the file",
+ response?.status ?? 0,
+ response?.body ?? null,
+ );
+ }
+};
+
+const uploadWithXhr = (
+ method: "POST" | "PUT",
+ url: string,
+ headers: Headers,
+ body: FormData | Blob,
+ onProgress?: (progress: UploadProgress) => void,
+) =>
+ new Promise<void>((resolve, reject) => {
+ const xhr = new XMLHttpRequest();
+ xhr.open(method, url);
+ headers.forEach((value, key) => {
+ xhr.setRequestHeader(key, value);
+ });
+ xhr.upload.onprogress = (event) => {
+ onProgress?.({
+ loaded: event.loaded,
+ total: event.lengthComputable ? event.total : 0,
+ });
+ };
+ xhr.onload = () => {
+ if (xhr.status >= 200 && xhr.status < 300) {
+ resolve();
+ return;
+ }
+ reject(
+ new MobileApiError(
+ "Upload target rejected the file",
+ xhr.status,
+ xhr.responseText,
+ ),
+ );
+ };
+ xhr.onerror = () => {
+ reject(new Error("Upload failed"));
+ };
+ xhr.send(body);
+ });
+
+const fileBlob = async (file: UploadFile) => {
+ const response = await fetch(file.uri);
+ return response.blob();
+};
+
+export const uploadToTarget = async (
+ target: Storage.UploadTarget,
+ file: UploadFile,
+ onProgress?: (progress: UploadProgress) => void,
+) => {
+ if (target.type === "s3Post") {
+ if (isNativeUploadUri(file.uri)) {
+ await uploadNativeFile(
+ "POST",
+ target.url,
+ file,
+ {
+ fieldName: "file",
+ mimeType: file.type,
+ parameters: target.fields,
+ uploadType: FileSystem.FileSystemUploadType.MULTIPART,
+ },
+ onProgress,
+ );
+ return;
+ }
+
+ const formData = new FormData();
+ for (const [key, value] of Object.entries(target.fields)) {
+ formData.append(key, value);
+ }
+ formData.append("file", {
+ uri: file.uri,
+ name: file.name,
+ type: file.type,
+ } as unknown as Blob);
+ await uploadWithXhr(
+ "POST",
+ target.url,
+ new Headers(),
+ formData,
+ onProgress,
+ );
+ return;
+ }
+
+ const headers = { ...target.headers };
+ let size = file.size;
+ if (
+ target.type === "driveResumable" &&
+ typeof size === "number" &&
+ size > 0
+ ) {
+ headers["Content-Range"] = `bytes 0-${size - 1}/${size}`;
+ }
+
+ if (isNativeUploadUri(file.uri)) {
+ if (target.type === "driveResumable" && !size) {
+ size = await getLocalFileSize(file);
+ if (size > 0) {
+ headers["Content-Range"] = `bytes 0-${size - 1}/${size}`;
+ }
+ }
+
+ await uploadNativeFile(
+ "PUT",
+ target.url,
+ file,
+ {
+ headers,
+ uploadType: FileSystem.FileSystemUploadType.BINARY_CONTENT,
+ },
+ onProgress,
+ );
+ return;
+ }
+
+ const blob = await fileBlob(file);
+ if (target.type === "driveResumable" && !size && blob.size > 0) {
+ headers["Content-Range"] = `bytes 0-${blob.size - 1}/${blob.size}`;
+ }
+ await uploadWithXhr(
+ "PUT",
+ target.url,
+ targetHeaders(headers),
+ blob,
+ onProgress,
+ );
+};