Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(mobile): extend mobile API client

Richie McIlroyba208ab06ba1
diff --git a/apps/mobile/src/api/mobile.ts b/apps/mobile/src/api/mobile.ts
index 4f979b5f0..4c44f2c63 100644
--- a/apps/mobile/src/api/mobile.ts
+++ b/apps/mobile/src/api/mobile.ts
@@ -1,5 +1,4 @@
-import { Mobile, type Storage } from "@cap/web-domain";
-import { Schema } from "effect";
+import type { Mobile, Storage } from "@cap/web-domain";
import * as FileSystem from "expo-file-system/legacy";
export type MobileApiKeyResponse = typeof Mobile.MobileApiKeyResponse.Type;
@@ -8,28 +7,58 @@ export type MobileAuthConfigResponse =
typeof Mobile.MobileAuthConfigResponse.Type;
export type MobileBootstrapResponse =
typeof Mobile.MobileBootstrapResponse.Type;
+export type MobileUser = typeof Mobile.MobileUser.Type;
+export type MobileProfileInput = typeof Mobile.MobileProfileInput.Type;
+export type MobileProfileImageInput =
+ typeof Mobile.MobileProfileImageInput.Type;
export type MobileCapsListResponse = typeof Mobile.MobileCapsListResponse.Type;
+export type MobileCapStatusesResponse =
+ typeof Mobile.MobileCapStatusesResponse.Type;
export type MobileCapSummary = typeof Mobile.MobileCapSummary.Type;
export type MobileFolder = typeof Mobile.MobileFolder.Type;
+export type MobileSpace = typeof Mobile.MobileSpace.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 MobileAnalyticsRange = typeof Mobile.MobileAnalyticsRange.Type;
+export type MobileAnalyticsData = typeof Mobile.MobileAnalyticsData.Type;
+export type MobileAnalyticsResponse =
+ typeof Mobile.MobileAnalyticsResponse.Type;
+export type MobileOrganizationSettings =
+ typeof Mobile.MobileOrganizationSettings.Type;
+export type MobileOrganizationSettingsInput =
+ typeof Mobile.MobileOrganizationSettingsInput.Type;
+export type MobileOrganizationIconInput =
+ typeof Mobile.MobileOrganizationIconInput.Type;
+export type MobileLoomImportResponse =
+ typeof Mobile.MobileLoomImportResponse.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 =
+type ContractMobileUploadCreateInput =
typeof Mobile.MobileUploadCreateInput.Type;
+export type MobileUploadCreateInput = Omit<
+ ContractMobileUploadCreateInput,
+ "organizationId" | "folderId"
+> & {
+ organizationId?: string;
+ folderId?: string;
+};
export type MobileUploadCreateResponse =
typeof Mobile.MobileUploadCreateResponse.Type;
export type MobileRecordingCreateInput =
typeof Mobile.MobileRecordingCreateInput.Type;
export type MobileRecordingCreateResponse =
typeof Mobile.MobileRecordingCreateResponse.Type;
+export type MobileRecordingUploadTargetsResponse =
+ typeof Mobile.MobileRecordingUploadTargetsResponse.Type;
export type MobileRecordingCompleteInput =
typeof Mobile.MobileRecordingCompleteInput.Type;
+export type MobileContentReportReason =
+ (typeof Mobile.MobileContentReportInput.Type)["reason"];
export type MobileApiClient = ReturnType<typeof createMobileApiClient>;
@@ -54,7 +83,7 @@ type ClientOptions = {
};
type RequestOptions = {
- method?: "GET" | "POST" | "PATCH" | "DELETE";
+ method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
query?: Record<string, string | number | null | undefined>;
body?: unknown;
};
@@ -72,10 +101,16 @@ export class MobileApiError extends Error {
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 responseObject = <A>(value: unknown): A => {
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
+ throw new MobileApiError(
+ "Mobile API returned an invalid response",
+ 502,
+ value,
+ );
+ }
+ return value as A;
+};
const appendQuery = (
url: URL,
@@ -102,7 +137,7 @@ const parseJson = async (response: Response) => {
export const createSessionRequestUrl = (
baseUrl: string,
redirectUri: string,
- provider?: "google" | "workos",
+ provider?: "apple" | "google" | "workos",
organizationId?: string,
) => {
const url = new URL("/api/mobile/session/request", trimBaseUrl(baseUrl));
@@ -115,9 +150,8 @@ export const createSessionRequestUrl = (
export const createMobileApiClient = ({ baseUrl, getToken }: ClientOptions) => {
const origin = trimBaseUrl(baseUrl);
- const request = async <A, I>(
+ const request = async <A>(
path: string,
- schema: Schema.Schema<A, I, never>,
options: RequestOptions = {},
): Promise<A> => {
const token = await getToken();
@@ -149,12 +183,11 @@ export const createMobileApiClient = ({ baseUrl, getToken }: ClientOptions) => {
payload,
);
}
- return decode(schema, payload);
+ return responseObject<A>(payload);
};
- const publicRequest = async <A, I>(
+ const publicRequest = async <A>(
path: string,
- schema: Schema.Schema<A, I, never>,
options: Omit<RequestOptions, "query"> = {},
): Promise<A> => {
const url = new URL(path, origin);
@@ -178,104 +211,184 @@ export const createMobileApiClient = ({ baseUrl, getToken }: ClientOptions) => {
payload,
);
}
- return decode(schema, payload);
+ return responseObject<A>(payload);
};
return {
getAuthConfig: () =>
- publicRequest(
- "/api/mobile/session/config",
- Mobile.MobileAuthConfigResponse,
- ),
+ publicRequest<MobileAuthConfigResponse>("/api/mobile/session/config"),
requestEmailCode: (email: string) =>
- publicRequest(
+ publicRequest<MobileSuccessResponse>(
"/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),
+ publicRequest<MobileApiKeyResponse>("/api/mobile/session/email/verify", {
+ method: "POST",
+ body: input,
+ }),
+ bootstrap: () => request<MobileBootstrapResponse>("/api/mobile/bootstrap"),
+ requestAccountDeletion: () =>
+ request<MobileSuccessResponse>("/api/mobile/user/account-deletion", {
+ method: "POST",
+ body: { confirmation: "DELETE" },
+ }),
+ blockUser: (userId: string) =>
+ request<MobileSuccessResponse>("/api/mobile/user/blocks", {
+ method: "POST",
+ body: { userId },
+ }),
setActiveOrganization: (organizationId: string) =>
- request(
- "/api/mobile/user/active-organization",
- Mobile.MobileBootstrapResponse,
- {
- method: "PATCH",
- body: { organizationId },
- },
- ),
+ request<MobileBootstrapResponse>("/api/mobile/user/active-organization", {
+ method: "PATCH",
+ body: { organizationId },
+ }),
+ updateProfile: (input: MobileProfileInput) =>
+ request<MobileUser>("/api/mobile/user/profile", {
+ method: "PATCH",
+ body: input,
+ }),
+ updateProfileImage: (input: MobileProfileImageInput) =>
+ request<MobileUser>("/api/mobile/user/profile/image", {
+ method: "PUT",
+ body: input,
+ }),
+ removeProfileImage: () =>
+ request<MobileUser>("/api/mobile/user/profile/image", {
+ method: "DELETE",
+ }),
listCaps: (params: {
folderId?: string | null;
+ spaceId?: string | null;
page?: number;
limit?: number;
}) =>
- request("/api/mobile/caps", Mobile.MobileCapsListResponse, {
+ request<MobileCapsListResponse>("/api/mobile/caps", {
query: params,
}),
+ getCapStatuses: (ids: readonly string[]) =>
+ request<MobileCapStatusesResponse>("/api/mobile/caps/statuses", {
+ method: "POST",
+ body: { ids },
+ }),
createFolder: (input: MobileFolderCreateInput) =>
- request("/api/mobile/folders", Mobile.MobileFolder, {
+ request<MobileFolder>("/api/mobile/folders", {
method: "POST",
body: input,
}),
getCap: (id: string) =>
- request(`/api/mobile/caps/${id}`, Mobile.MobileCapDetail),
+ request<MobileCapDetail>(`/api/mobile/caps/${encodeURIComponent(id)}`),
+ reportCap: (id: string, reason: MobileContentReportReason) =>
+ request<MobileSuccessResponse>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/report`,
+ {
+ method: "POST",
+ body: { reason },
+ },
+ ),
updateCapSharing: (id: string, input: MobileCapSharingInput) =>
- request(`/api/mobile/caps/${id}/sharing`, Mobile.MobileCapSummary, {
- method: "PATCH",
- body: input,
- }),
+ request<MobileCapSummary>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/sharing`,
+ {
+ method: "PATCH",
+ body: input,
+ },
+ ),
updateCapTitle: (id: string, input: MobileCapTitleInput) =>
- request(`/api/mobile/caps/${id}/title`, Mobile.MobileCapSummary, {
- method: "PATCH",
- body: input,
- }),
+ request<MobileCapSummary>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/title`,
+ {
+ method: "PATCH",
+ body: input,
+ },
+ ),
updateCapPassword: (id: string, input: MobileCapPasswordInput) =>
- request(`/api/mobile/caps/${id}/password`, Mobile.MobileCapSummary, {
+ request<MobileCapSummary>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/password`,
+ {
+ method: "PATCH",
+ body: input,
+ },
+ ),
+ deleteCap: (id: string) =>
+ request<MobileSuccessResponse>(
+ `/api/mobile/caps/${encodeURIComponent(id)}`,
+ {
+ method: "DELETE",
+ },
+ ),
+ getPlayback: (id: string) =>
+ request<MobilePlaybackResponse>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/playback`,
+ ),
+ getDownload: (id: string) =>
+ request<MobileDownloadResponse>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/download`,
+ ),
+ getCapAnalytics: (id: string, range: MobileAnalyticsRange) =>
+ request<MobileAnalyticsResponse>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/analytics`,
+ { query: { range } },
+ ),
+ getOrganizationSettings: () =>
+ request<MobileOrganizationSettings>("/api/mobile/organization/settings"),
+ updateOrganizationSettings: (input: MobileOrganizationSettingsInput) =>
+ request<MobileOrganizationSettings>("/api/mobile/organization/settings", {
method: "PATCH",
body: input,
}),
- deleteCap: (id: string) =>
- request(`/api/mobile/caps/${id}`, Mobile.MobileSuccessResponse, {
- method: "DELETE",
+ updateOrganizationIcon: (input: MobileOrganizationIconInput) =>
+ request<MobileOrganizationSettings>(
+ "/api/mobile/organization/settings/icon",
+ {
+ method: "PUT",
+ body: input,
+ },
+ ),
+ removeOrganizationIcon: () =>
+ request<MobileOrganizationSettings>(
+ "/api/mobile/organization/settings/icon",
+ { method: "DELETE" },
+ ),
+ importLoom: (loomUrl: string) =>
+ request<MobileLoomImportResponse>("/api/mobile/imports/loom", {
+ method: "POST",
+ body: { loomUrl },
}),
- 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,
- }),
+ request<MobileComment>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/comments`,
+ {
+ method: "POST",
+ body: input,
+ },
+ ),
deleteComment: (id: string) =>
- request(`/api/mobile/comments/${id}`, Mobile.MobileSuccessResponse, {
- method: "DELETE",
- }),
+ request<MobileSuccessResponse>(
+ `/api/mobile/comments/${encodeURIComponent(id)}`,
+ {
+ method: "DELETE",
+ },
+ ),
createReaction: (
id: string,
input: { content: string; timestamp: number | null },
) =>
- request(`/api/mobile/caps/${id}/reactions`, Mobile.MobileComment, {
- method: "POST",
- body: input,
- }),
+ request<MobileComment>(
+ `/api/mobile/caps/${encodeURIComponent(id)}/reactions`,
+ {
+ method: "POST",
+ body: input,
+ },
+ ),
createUpload: (input: MobileUploadCreateInput) =>
- request("/api/mobile/uploads", Mobile.MobileUploadCreateResponse, {
+ request<MobileUploadCreateResponse>("/api/mobile/uploads", {
method: "POST",
body: input,
}),
@@ -283,9 +396,8 @@ export const createMobileApiClient = ({ baseUrl, getToken }: ClientOptions) => {
id: string,
input: { uploaded: number; total: number },
) =>
- request(
- `/api/mobile/uploads/${id}/progress`,
- Mobile.MobileSuccessResponse,
+ request<MobileSuccessResponse>(
+ `/api/mobile/uploads/${encodeURIComponent(id)}/progress`,
{
method: "POST",
body: input,
@@ -295,39 +407,36 @@ export const createMobileApiClient = ({ baseUrl, getToken }: ClientOptions) => {
id: string,
input: { rawFileKey: string; contentLength?: number },
) =>
- request(
- `/api/mobile/uploads/${id}/complete`,
- Mobile.MobileSuccessResponse,
+ request<MobileSuccessResponse>(
+ `/api/mobile/uploads/${encodeURIComponent(id)}/complete`,
{
method: "POST",
body: input,
},
),
createRecording: (input: MobileRecordingCreateInput) =>
- request("/api/mobile/recordings", Mobile.MobileRecordingCreateResponse, {
+ request<MobileRecordingCreateResponse>("/api/mobile/recordings", {
method: "POST",
body: input,
}),
createRecordingUploadTargets: (id: string, subpaths: string[]) =>
- request(
- `/api/mobile/recordings/${id}/segments/targets`,
- Mobile.MobileRecordingUploadTargetsResponse,
+ request<MobileRecordingUploadTargetsResponse>(
+ `/api/mobile/recordings/${encodeURIComponent(id)}/segments/targets`,
{
method: "POST",
body: { subpaths },
},
),
completeRecording: (id: string, input: MobileRecordingCompleteInput) =>
- request(
- `/api/mobile/recordings/${id}/complete`,
- Mobile.MobileSuccessResponse,
+ request<MobileSuccessResponse>(
+ `/api/mobile/recordings/${encodeURIComponent(id)}/complete`,
{
method: "POST",
body: input,
},
),
revokeSession: () =>
- request("/api/mobile/session/revoke", Mobile.MobileSuccessResponse, {
+ request<MobileSuccessResponse>("/api/mobile/session/revoke", {
method: "POST",
}),
};
@@ -359,6 +468,10 @@ const uploadNativeFile = async (
options: FileSystem.FileSystemUploadOptions,
onProgress?: (progress: UploadProgress) => void,
) => {
+ const info = await FileSystem.getInfoAsync(file.uri);
+ if (!info.exists || info.isDirectory) {
+ throw new Error("The recording file is no longer available.");
+ }
const task = FileSystem.createUploadTask(
url,
file.uri,