|
| 1 | +import { LoomExportData } from "../types/loom"; |
| 2 | +import { getApiBaseUrl } from "../utils/urls"; |
| 3 | + |
| 4 | +interface ApiResponse<T> { |
| 5 | + data: T; |
| 6 | + error?: string; |
| 7 | +} |
| 8 | + |
| 9 | +interface UserResponse { |
| 10 | + user: User; |
| 11 | + expires: string; |
| 12 | +} |
| 13 | +interface User { |
| 14 | + name: string; |
| 15 | + email: string; |
| 16 | + image: string; |
| 17 | + id: string; |
| 18 | +} |
| 19 | + |
| 20 | +interface LoomImportResponse { |
| 21 | + success: boolean; |
| 22 | + message: string; |
| 23 | +} |
| 24 | + |
| 25 | +interface Workspace { |
| 26 | + id: string; |
| 27 | + name: string; |
| 28 | + ownerId: string; |
| 29 | + metadata: null; |
| 30 | + allowedEmailDomain: null; |
| 31 | + customDomain: null; |
| 32 | + domainVerified: null; |
| 33 | + createdAt: string; |
| 34 | + updatedAt: string; |
| 35 | + workosOrganizationId: null; |
| 36 | + workosConnectionId: null; |
| 37 | +} |
| 38 | + |
| 39 | +interface WorkspaceResponse { |
| 40 | + workspaces: Workspace[]; |
| 41 | +} |
| 42 | + |
| 43 | +export class CapApi { |
| 44 | + private baseUrl = getApiBaseUrl(); |
| 45 | + private headers: HeadersInit = { |
| 46 | + accept: "*/*", |
| 47 | + "content-type": "application/json", |
| 48 | + }; |
| 49 | + |
| 50 | + private async getAuthToken(): Promise<string | null> { |
| 51 | + return new Promise((resolve) => { |
| 52 | + chrome.runtime.sendMessage({ action: "getAuthStatus" }, (response) => { |
| 53 | + resolve(response?.token || null); |
| 54 | + }); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + private async getHeaders(): Promise<HeadersInit> { |
| 59 | + const token = await this.getAuthToken(); |
| 60 | + return { |
| 61 | + ...this.headers, |
| 62 | + ...(token ? { Authorization: `Bearer ${token}` } : {}), |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + private async request<T>( |
| 67 | + endpoint: string, |
| 68 | + options: RequestInit = {} |
| 69 | + ): Promise<ApiResponse<T>> { |
| 70 | + try { |
| 71 | + const headers = await this.getHeaders(); |
| 72 | + const response = await fetch(`${this.baseUrl}${endpoint}`, { |
| 73 | + ...options, |
| 74 | + headers: { |
| 75 | + ...headers, |
| 76 | + ...options.headers, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + const data = await response.json(); |
| 81 | + |
| 82 | + if (!response.ok) { |
| 83 | + throw new Error(data.error || "API request failed"); |
| 84 | + } |
| 85 | + |
| 86 | + return { data: data as T }; |
| 87 | + } catch (error) { |
| 88 | + return { |
| 89 | + data: {} as T, |
| 90 | + error: |
| 91 | + error instanceof Error ? error.message : "Unknown error occurred", |
| 92 | + }; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public async getUser(): Promise<UserResponse> { |
| 97 | + const response = await this.request<UserResponse>("/auth/session"); |
| 98 | + return response.data; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sends imported Loom data to Cap.so |
| 103 | + * @param loomData The exported Loom data to import into Cap.so |
| 104 | + * @returns Response with import status |
| 105 | + */ |
| 106 | + public async sendLoomData( |
| 107 | + loomData: LoomExportData |
| 108 | + ): Promise<LoomImportResponse> { |
| 109 | + const response = await this.request<LoomImportResponse>("/import/loom", { |
| 110 | + method: "POST", |
| 111 | + body: JSON.stringify(loomData), |
| 112 | + }); |
| 113 | + |
| 114 | + if (response.error) { |
| 115 | + return { |
| 116 | + success: false, |
| 117 | + message: response.error, |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + return response.data; |
| 122 | + } |
| 123 | + |
| 124 | + public async getWorkspaceDetails(): Promise<WorkspaceResponse> { |
| 125 | + const response = await this.request<WorkspaceResponse>( |
| 126 | + "/settings/workspace/details" |
| 127 | + ); |
| 128 | + return response.data; |
| 129 | + } |
| 130 | +} |
0 commit comments