|
| 1 | +import { type ShareVisibility, type ShareResponse, shareResponseSchema } from "@roo-code/types" |
| 2 | + |
| 3 | +import { getRooCodeApiUrl } from "./config" |
| 4 | +import type { AuthService } from "./auth" |
| 5 | +import { getUserAgent } from "./utils" |
| 6 | +import { AuthenticationError, CloudAPIError, NetworkError, TaskNotFoundError } from "./errors" |
| 7 | + |
| 8 | +interface CloudAPIRequestOptions extends Omit<RequestInit, "headers"> { |
| 9 | + timeout?: number |
| 10 | + headers?: Record<string, string> |
| 11 | +} |
| 12 | + |
| 13 | +export class CloudAPI { |
| 14 | + private authService: AuthService |
| 15 | + private log: (...args: unknown[]) => void |
| 16 | + private baseUrl: string |
| 17 | + |
| 18 | + constructor(authService: AuthService, log?: (...args: unknown[]) => void) { |
| 19 | + this.authService = authService |
| 20 | + this.log = log || console.log |
| 21 | + this.baseUrl = getRooCodeApiUrl() |
| 22 | + } |
| 23 | + |
| 24 | + private async request<T>( |
| 25 | + endpoint: string, |
| 26 | + options: CloudAPIRequestOptions & { |
| 27 | + parseResponse?: (data: unknown) => T |
| 28 | + } = {}, |
| 29 | + ): Promise<T> { |
| 30 | + const { timeout = 10000, parseResponse, headers = {}, ...fetchOptions } = options |
| 31 | + |
| 32 | + const sessionToken = this.authService.getSessionToken() |
| 33 | + |
| 34 | + if (!sessionToken) { |
| 35 | + throw new AuthenticationError() |
| 36 | + } |
| 37 | + |
| 38 | + const url = `${this.baseUrl}${endpoint}` |
| 39 | + |
| 40 | + const requestHeaders = { |
| 41 | + "Content-Type": "application/json", |
| 42 | + Authorization: `Bearer ${sessionToken}`, |
| 43 | + "User-Agent": getUserAgent(), |
| 44 | + ...headers, |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + const response = await fetch(url, { |
| 49 | + ...fetchOptions, |
| 50 | + headers: requestHeaders, |
| 51 | + signal: AbortSignal.timeout(timeout), |
| 52 | + }) |
| 53 | + |
| 54 | + if (!response.ok) { |
| 55 | + await this.handleErrorResponse(response, endpoint) |
| 56 | + } |
| 57 | + |
| 58 | + const data = await response.json() |
| 59 | + |
| 60 | + if (parseResponse) { |
| 61 | + return parseResponse(data) |
| 62 | + } |
| 63 | + |
| 64 | + return data as T |
| 65 | + } catch (error) { |
| 66 | + if (error instanceof TypeError && error.message.includes("fetch")) { |
| 67 | + throw new NetworkError(`Network error while calling ${endpoint}`) |
| 68 | + } |
| 69 | + |
| 70 | + if (error instanceof CloudAPIError) { |
| 71 | + throw error |
| 72 | + } |
| 73 | + |
| 74 | + if (error instanceof Error && error.name === "AbortError") { |
| 75 | + throw new CloudAPIError(`Request to ${endpoint} timed out`, undefined, undefined) |
| 76 | + } |
| 77 | + |
| 78 | + throw new CloudAPIError( |
| 79 | + `Unexpected error while calling ${endpoint}: ${error instanceof Error ? error.message : String(error)}`, |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private async handleErrorResponse(response: Response, endpoint: string): Promise<never> { |
| 85 | + let responseBody: unknown |
| 86 | + |
| 87 | + try { |
| 88 | + responseBody = await response.json() |
| 89 | + } catch { |
| 90 | + responseBody = await response.text() |
| 91 | + } |
| 92 | + |
| 93 | + switch (response.status) { |
| 94 | + case 401: |
| 95 | + throw new AuthenticationError() |
| 96 | + case 404: |
| 97 | + if (endpoint.includes("/share")) { |
| 98 | + throw new TaskNotFoundError() |
| 99 | + } |
| 100 | + throw new CloudAPIError(`Resource not found: ${endpoint}`, 404, responseBody) |
| 101 | + default: |
| 102 | + throw new CloudAPIError( |
| 103 | + `HTTP ${response.status}: ${response.statusText}`, |
| 104 | + response.status, |
| 105 | + responseBody, |
| 106 | + ) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + async shareTask(taskId: string, visibility: ShareVisibility = "organization"): Promise<ShareResponse> { |
| 111 | + this.log(`[CloudAPI] Sharing task ${taskId} with visibility: ${visibility}`) |
| 112 | + |
| 113 | + const response = await this.request("/api/extension/share", { |
| 114 | + method: "POST", |
| 115 | + body: JSON.stringify({ taskId, visibility }), |
| 116 | + parseResponse: (data) => shareResponseSchema.parse(data), |
| 117 | + }) |
| 118 | + |
| 119 | + this.log("[CloudAPI] Share response:", response) |
| 120 | + return response |
| 121 | + } |
| 122 | +} |
0 commit comments