The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 6 | createNotificationInputSchema, |
| 7 | emitAmbientEventsInputSchema, |
| 8 | appPermissionSchema, |
| 9 | type AppPermission, |
| 10 | type BatchDeleteAppIndexDocumentsInput, |
| 11 | type BatchUpsertAppIndexDocumentsInput, |
| 12 | type ColineAppRegistrationPayload, |
| 13 | type CreateNotificationInputInput, |
| 14 | type EmitAmbientEventsInputInput, |
| 15 | } from "./manifest"; |
| 16 | import { ColineApiError } from "./errors"; |
| 17 |
| 18 | interface ApiErrorPayload { |
| 19 | error?: { |
| 20 | code?: string; |
| 21 | message?: string; |
| 22 | }; |
| 23 | } |
| 24 |
| 25 | async function parseJsonResponse<T>(response: Response): Promise<T> { |
| 26 | const payload = (await response.json().catch(() => null)) as T | null; |
| 27 |
| 28 | if (!response.ok) { |
| 29 | const errorMessage = |
| 30 | payload && |
| 31 | typeof payload === "object" && |
| 32 | "error_description" in payload && |
| 33 | typeof (payload as Record<string, unknown>)["error_description"] === "string" |
| 34 | ? ((payload as Record<string, unknown>)["error_description"] as string) |
| 35 | : `Request failed (${response.status}).`; |
| 36 |
| 37 | throw new ColineApiError({ |
| 38 | message: errorMessage, |
| 39 | type: "oauth", |
| 40 | status: response.status, |
| 41 | code: |
| 42 | payload && |
| 43 | typeof payload === "object" && |
| 44 | "error" in payload && |
| 45 | typeof (payload as Record<string, unknown>)["error"] === "string" |
| 46 | ? ((payload as Record<string, unknown>)["error"] as string) |
| 47 | : null, |
| 48 | }); |
| 49 | } |
| 50 |
| 51 | if (!payload) { |
| 52 | throw new ColineApiError({ |
| 53 | message: "Invalid JSON response.", |
| 54 | type: "oauth", |
| 55 | status: response.status, |
| 56 | }); |
| 57 | } |
| 58 |
| 59 | return payload; |
| 60 | } |
| 61 |
| 62 | async function parseApiResponse<T>(response: Response): Promise<T> { |
| 63 | const payload = (await response.json().catch(() => null)) as |
| 64 | | { data?: T; error?: ApiErrorPayload["error"] } |
| 65 | | null; |
| 66 |
| 67 | if (!response.ok) { |
| 68 | throw new ColineApiError({ |
| 69 | message: payload?.error?.message ?? `Request failed (${response.status}).`, |
| 70 | type: "api", |
| 71 | status: response.status, |
| 72 | code: payload?.error?.code ?? null, |
| 73 | }); |
| 74 | } |
| 75 |
| 76 | if (!payload || !("data" in payload)) { |
| 77 | throw new ColineApiError({ |
| 78 | message: "Invalid API response.", |
| 79 | type: "api", |
| 80 | status: response.status, |
| 81 | }); |
| 82 | } |
| 83 |
| 84 | return payload.data as T; |
| 85 | } |
| 86 |
| 87 | export interface ColineApiClientOptions { |
| 88 | baseUrl: string; |
| 89 | apiKey?: string; |
| 90 | fetch?: typeof fetch; |
| 91 | headers?: HeadersInit; |
| 92 | } |
| 93 |
| 94 | export interface RegisteredAppRecord { |
| 95 | appId: string; |
| 96 | appKey: string; |
| 97 | name: string; |
| 98 | description: string | null; |
| 99 | iconUrl: string | null; |
| 100 | status: "draft" | "active" | "disabled" | "archived"; |
| 101 | ownerUserId: string | null; |
| 102 | storeListed: boolean; |
| 103 | latestPublishedVersion: string | null; |
| 104 | } |
| 105 |
| 106 | export interface RegisteredAppDetail extends RegisteredAppRecord { |
| 107 | latestPublishedVersionId: string | null; |
| 108 | } |
| 109 |
| 110 | export interface RegisteredAppVersionSummary { |
| 111 | versionId: string; |
| 112 | version: string; |
| 113 | status: "draft" | "published" | "deprecated"; |
| 114 | reviewStatus: "draft" | "submitted" | "approved" | "rejected"; |
| 115 | apiBaseUrl: string; |
| 116 | permissions: string[]; |
| 117 | notificationChannels: Array<{ |
| 118 | key: string; |
| 119 | name: string; |
| 120 | description?: string; |
| 121 | defaultDeliveries: string[]; |
| 122 | defaultPriority: "low" | "default" | "high"; |
| 123 | }>; |
| 124 | kairo: ColineKairoIntegration | null; |
| 125 | fileTypes: Array<{ |
| 126 | typeKey: string; |
| 127 | name: string; |
| 128 | description: string | null; |
| 129 | }>; |
| 130 | submittedAt: string | null; |
| 131 | reviewedAt: string | null; |
| 132 | reviewNotes: string | null; |
| 133 | publishedAt: string | null; |
| 134 | createdAt: string; |
| 135 | updatedAt: string; |
| 136 | } |
| 137 |
| 138 | export interface WorkspaceInstalledApp { |
| 139 | installId: string; |
| 140 | appId: string; |
| 141 | appKey: string; |
| 142 | name: string; |
| 143 | description: string | null; |
| 144 | iconUrl: string | null; |
| 145 | status: "active" | "suspended" | "revoked"; |
| 146 | installScope: "workspace" | "user"; |
| 147 | scopeSubjectId: string; |
| 148 | version: string | null; |
| 149 | } |
| 150 |
| 151 | export interface WorkspaceAppCatalogItem { |
| 152 | appId: string; |
| 153 | appKey: string; |
| 154 | name: string; |
| 155 | description: string | null; |
| 156 | iconUrl: string | null; |
| 157 | status: "draft" | "active" | "disabled" | "archived"; |
| 158 | latestPublishedVersion: string | null; |
| 159 | latestPublishedVersionId: string | null; |
| 160 | isInstalled: boolean; |
| 161 | } |
| 162 |
| 163 | export interface WorkspaceAppsResponse { |
| 164 | installedApps: WorkspaceInstalledApp[]; |
| 165 | catalog: WorkspaceAppCatalogItem[]; |
| 166 | permissions: { |
| 167 | canManageApps: boolean; |
| 168 | }; |
| 169 | } |
| 170 |
| 171 | export interface InstallWorkspaceAppInput { |
| 172 | appId: string; |
| 173 | pinnedVersionId?: string; |
| 174 | grantedPermissions?: AppPermission[]; |
| 175 | } |
| 176 |
| 177 | export interface InstallWorkspaceAppResponse { |
| 178 | installId: string; |
| 179 | appId: string; |
| 180 | appKey: string; |
| 181 | version: string; |
| 182 | grantedPermissions: AppPermission[]; |
| 183 | } |
| 184 |
| 185 | export interface SubmitAppVersionForReviewInput { |
| 186 | storeListed?: boolean; |
| 187 | } |
| 188 |
| 189 | export interface SubmitAppVersionForReviewResponse { |
| 190 | appId: string; |
| 191 | appKey: string; |
| 192 | versionId: string; |
| 193 | version: string; |
| 194 | reviewStatus: "submitted"; |
| 195 | storeListed: boolean; |
| 196 | } |
| 197 |
| 198 | export interface WorkspaceAppPermissionsResponse { |
| 199 | install: WorkspaceInstalledApp; |
| 200 | availablePermissions: string[]; |
| 201 | grantedPermissions: string[]; |
| 202 | permissions: { |
| 203 | canManageApps: boolean; |
| 204 | }; |
| 205 | } |
| 206 |
| 207 | export interface WorkspaceInstalledAppResponse { |
| 208 | install: WorkspaceInstalledApp; |
| 209 | permissions: { |
| 210 | canManageApps: boolean; |
| 211 | }; |
| 212 | } |
| 213 |
| 214 | export interface AppFileDocumentResponse { |
| 215 | file: { |
| 216 | id: string; |
| 217 | name: string; |
| 218 | fileType: string; |
| 219 | appKey: string; |
| 220 | appName: string; |
| 221 | typeLabel: string; |
| 222 | openHref: string; |
| 223 | updatedAt: string; |
| 224 | }; |
| 225 | document: Record<string, unknown>; |
| 226 | version: number; |
| 227 | capabilities: { |
| 228 | canRead: boolean; |
| 229 | canWrite: boolean; |
| 230 | canManage: boolean; |
| 231 | }; |
| 232 | } |
| 233 |
| 234 | export interface TabModelRecord { |
| 235 | id: string; |
| 236 | object: "model"; |
| 237 | created: number; |
| 238 | owned_by: string; |
| 239 | providerModelId: string; |
| 240 | pricing: { |
| 241 | inputPerMillionUsd: number; |
| 242 | outputPerMillionUsd: number; |
| 243 | }; |
| 244 | capabilities: { |
| 245 | streaming: boolean; |
| 246 | surfaces: string[]; |
| 247 | }; |
| 248 | } |
| 249 |
| 250 | export interface OAuthTokenResponse { |
| 251 | access_token: string; |
| 252 | token_type: "Bearer"; |
| 253 | expires_in: number; |
| 254 | refresh_token: string; |
| 255 | scope: string; |
| 256 | } |
| 257 |
| 258 | export interface OAuthUserInfoResponse { |
| 259 | sub: string; |
| 260 | email: string; |
| 261 | email_verified: boolean; |
| 262 | name: string; |
| 263 | given_name: string | null; |
| 264 | family_name: string | null; |
| 265 | picture: string | null; |
| 266 | preferred_username: string | null; |
| 267 | scope: string; |
| 268 | } |
| 269 |
| 270 | export interface UpdateWorkspaceAppPermissionsInput { |
| 271 | grantedPermissions: AppPermission[]; |
| 272 | } |
| 273 |
| 274 | function joinUrl(baseUrl: string, path: string): string { |
| 275 | return `${baseUrl.replace(/\/+$/, "")}${path}`; |
| 276 | } |
| 277 |
| 278 | export class ColineApiClient { |
| 279 | private readonly baseUrl: string; |
| 280 | private readonly apiKey: string | undefined; |
| 281 | private readonly fetchImpl: typeof fetch; |
| 282 | private readonly defaultHeaders: HeadersInit | undefined; |
| 283 |
| 284 | constructor(options: ColineApiClientOptions) { |
| 285 | this.baseUrl = options.baseUrl; |
| 286 | this.apiKey = options.apiKey; |
| 287 | this.fetchImpl = options.fetch ?? fetch; |
| 288 | this.defaultHeaders = options.headers; |
| 289 | } |
| 290 |
| 291 | private async request<T>(path: string, init?: RequestInit): Promise<T> { |
| 292 | const headers = new Headers(this.defaultHeaders); |
| 293 | const requestHeaders = new Headers(init?.headers); |
| 294 | requestHeaders.forEach((value, key) => headers.set(key, value)); |
| 295 |
| 296 | if (!headers.has("Content-Type") && init?.body) { |
| 297 | headers.set("Content-Type", "application/json"); |
| 298 | } |
| 299 |
| 300 | if (this.apiKey && !headers.has("Authorization")) { |
| 301 | headers.set("Authorization", `Bearer ${this.apiKey}`); |
| 302 | } |
| 303 |
| 304 | const response = await this.fetchImpl(joinUrl(this.baseUrl, path), { |
| 305 | ...init, |
| 306 | headers, |
| 307 | }); |
| 308 |
| 309 | return parseApiResponse<T>(response); |
| 310 | } |
| 311 |
| 312 | withApiKey(apiKey: string): ColineApiClient { |
| 313 | const options: ColineApiClientOptions = { |
| 314 | baseUrl: this.baseUrl, |
| 315 | apiKey, |
| 316 | fetch: this.fetchImpl, |
| 317 | }; |
| 318 |
| 319 | if (this.defaultHeaders !== undefined) { |
| 320 | options.headers = this.defaultHeaders; |
| 321 | } |
| 322 |
| 323 | return new ColineApiClient(options); |
| 324 | } |
| 325 |
| 326 | withHeaders(headers: HeadersInit): ColineApiClient { |
| 327 | const mergedHeaders = new Headers(this.defaultHeaders); |
| 328 | new Headers(headers).forEach((value, key) => mergedHeaders.set(key, value)); |
| 329 |
| 330 | const options: ColineApiClientOptions = { |
| 331 | baseUrl: this.baseUrl, |
| 332 | fetch: this.fetchImpl, |
| 333 | headers: mergedHeaders, |
| 334 | }; |
| 335 |
| 336 | if (this.apiKey !== undefined) { |
| 337 | options.apiKey = this.apiKey; |
| 338 | } |
| 339 |
| 340 | return new ColineApiClient(options); |
| 341 | } |
| 342 |
| 343 | buildLoginWithColineAuthorizeUrl(input: { |
| 344 | clientId: string; |
| 345 | redirectUri: string; |
| 346 | scope?: string; |
| 347 | state?: string; |
| 348 | codeChallenge?: string; |
| 349 | codeChallengeMethod?: "S256" | "plain"; |
| 350 | }): string { |
| 351 | const url = new URL("/api/v1/oauth/authorize", this.baseUrl); |
| 352 | url.searchParams.set("response_type", "code"); |
| 353 | url.searchParams.set("client_id", input.clientId); |
| 354 | url.searchParams.set("redirect_uri", input.redirectUri); |
| 355 | if (input.scope) { |
| 356 | url.searchParams.set("scope", input.scope); |
| 357 | } |
| 358 | if (input.state) { |
| 359 | url.searchParams.set("state", input.state); |
| 360 | } |
| 361 | if (input.codeChallenge) { |
| 362 | url.searchParams.set("code_challenge", input.codeChallenge); |
| 363 | } |
| 364 | if (input.codeChallengeMethod) { |
| 365 | url.searchParams.set("code_challenge_method", input.codeChallengeMethod); |
| 366 | } |
| 367 | return url.toString(); |
| 368 | } |
| 369 |
| 370 | async listApps(): Promise<{ apps: RegisteredAppRecord[] }> { |
| 371 | return this.request<{ apps: RegisteredAppRecord[] }>("/api/v1/apps"); |
| 372 | } |
| 373 |
| 374 | async getApp(appKey: string): Promise<{ app: RegisteredAppDetail }> { |
| 375 | return this.request<{ app: RegisteredAppDetail }>(`/api/v1/apps/${appKey}`); |
| 376 | } |
| 377 |
| 378 | async listAppVersions( |
| 379 | appKey: string, |
| 380 | ): Promise<{ versions: RegisteredAppVersionSummary[] }> { |
| 381 | return this.request<{ versions: RegisteredAppVersionSummary[] }>( |
| 382 | `/api/v1/apps/${appKey}/versions`, |
| 383 | ); |
| 384 | } |
| 385 |
| 386 | async registerApp( |
| 387 | payload: ColineAppRegistrationPayload, |
| 388 | ): Promise<{ appId: string; appKey: string; versionId: string; version: string }> { |
| 389 | const body = colineAppRegistrationPayloadSchema.parse(payload); |
| 390 |
| 391 | return this.request<{ |
| 392 | appId: string; |
| 393 | appKey: string; |
| 394 | versionId: string; |
| 395 | version: string; |
| 396 | }>("/api/apps", { |
| 397 | method: "POST", |
| 398 | body: JSON.stringify(body), |
| 399 | }); |
| 400 | } |
| 401 |
| 402 | async submitAppVersionForReview( |
| 403 | appKey: string, |
| 404 | versionId: string, |
| 405 | input: SubmitAppVersionForReviewInput = {}, |
| 406 | ): Promise<SubmitAppVersionForReviewResponse> { |
| 407 | return this.request<SubmitAppVersionForReviewResponse>( |
| 408 | `/api/apps/${appKey}/versions/${versionId}/submit`, |
| 409 | { |
| 410 | method: "POST", |
| 411 | body: JSON.stringify({ |
| 412 | ...(input.storeListed !== undefined |
| 413 | ? { storeListed: input.storeListed } |
| 414 | : {}), |
| 415 | }), |
| 416 | }, |
| 417 | ); |
| 418 | } |
| 419 |
| 420 | async listWorkspaceApps(workspaceSlug: string): Promise<WorkspaceAppsResponse> { |
| 421 | return this.request<WorkspaceAppsResponse>( |
| 422 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 423 | ); |
| 424 | } |
| 425 |
| 426 | async installWorkspaceApp( |
| 427 | workspaceSlug: string, |
| 428 | input: InstallWorkspaceAppInput, |
| 429 | ): Promise<InstallWorkspaceAppResponse> { |
| 430 | const grantedPermissions = input.grantedPermissions |
| 431 | ? input.grantedPermissions.map((permission) => |
| 432 | appPermissionSchema.parse(permission), |
| 433 | ) |
| 434 | : undefined; |
| 435 |
| 436 | return this.request<InstallWorkspaceAppResponse>( |
| 437 | `/api/v1/workspaces/${workspaceSlug}/apps`, |
| 438 | { |
| 439 | method: "POST", |
| 440 | body: JSON.stringify({ |
| 441 | appId: input.appId, |
| 442 | ...(input.pinnedVersionId |
| 443 | ? { pinnedVersionId: input.pinnedVersionId } |
| 444 | : {}), |
| 445 | ...(grantedPermissions ? { grantedPermissions } : {}), |
| 446 | }), |
| 447 | }, |
| 448 | ); |
| 449 | } |
| 450 |
| 451 | async getWorkspaceAppPermissions( |
| 452 | workspaceSlug: string, |
| 453 | appKey: string, |
| 454 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 455 | return this.request<WorkspaceAppPermissionsResponse>( |
| 456 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 457 | ); |
| 458 | } |
| 459 |
| 460 | async getWorkspaceInstalledApp( |
| 461 | workspaceSlug: string, |
| 462 | appKey: string, |
| 463 | ): Promise<WorkspaceInstalledAppResponse> { |
| 464 | return this.request<WorkspaceInstalledAppResponse>( |
| 465 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 466 | ); |
| 467 | } |
| 468 |
| 469 | async updateWorkspaceAppPermissions( |
| 470 | workspaceSlug: string, |
| 471 | appKey: string, |
| 472 | input: UpdateWorkspaceAppPermissionsInput, |
| 473 | ): Promise<WorkspaceAppPermissionsResponse> { |
| 474 | return this.request<WorkspaceAppPermissionsResponse>( |
| 475 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/permissions`, |
| 476 | { |
| 477 | method: "PUT", |
| 478 | body: JSON.stringify({ |
| 479 | grantedPermissions: input.grantedPermissions.map((permission) => |
| 480 | appPermissionSchema.parse(permission), |
| 481 | ), |
| 482 | }), |
| 483 | }, |
| 484 | ); |
| 485 | } |
| 486 |
| 487 | async uninstallWorkspaceApp( |
| 488 | workspaceSlug: string, |
| 489 | appKey: string, |
| 490 | ): Promise<{ ok: true }> { |
| 491 | return this.request<{ ok: true }>( |
| 492 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}`, |
| 493 | { |
| 494 | method: "DELETE", |
| 495 | }, |
| 496 | ); |
| 497 | } |
| 498 |
| 499 | async emitAppAmbientEvents( |
| 500 | workspaceSlug: string, |
| 501 | appKey: string, |
| 502 | input: EmitAmbientEventsInputInput, |
| 503 | ): Promise<{ events: Array<{ id: string; dedupeKey: string }> }> { |
| 504 | const body = emitAmbientEventsInputSchema.parse(input); |
| 505 |
| 506 | return this.request<{ events: Array<{ id: string; dedupeKey: string }> }>( |
| 507 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/ambient/events`, |
| 508 | { |
| 509 | method: "POST", |
| 510 | body: JSON.stringify(body), |
| 511 | }, |
| 512 | ); |
| 513 | } |
| 514 |
| 515 | async createAppNotification( |
| 516 | workspaceSlug: string, |
| 517 | appKey: string, |
| 518 | input: CreateNotificationInputInput, |
| 519 | ): Promise<{ ok: true }> { |
| 520 | const body = createNotificationInputSchema.parse(input); |
| 521 |
| 522 | return this.request<{ ok: true }>( |
| 523 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/notifications`, |
| 524 | { |
| 525 | method: "POST", |
| 526 | body: JSON.stringify(body), |
| 527 | }, |
| 528 | ); |
| 529 | } |
| 530 |
| 531 | async upsertAppIndexDocuments( |
| 532 | workspaceSlug: string, |
| 533 | appKey: string, |
| 534 | input: BatchUpsertAppIndexDocumentsInput, |
| 535 | ): Promise<{ documents: Array<{ id: string; documentKey: string; visibilityMode: string }> }> { |
| 536 | const body = batchUpsertAppIndexDocumentsSchema.parse(input); |
| 537 |
| 538 | return this.request<{ |
| 539 | documents: Array<{ id: string; documentKey: string; visibilityMode: string }>; |
| 540 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, { |
| 541 | method: "POST", |
| 542 | body: JSON.stringify(body), |
| 543 | }); |
| 544 | } |
| 545 |
| 546 | async deleteAppIndexDocuments( |
| 547 | workspaceSlug: string, |
| 548 | appKey: string, |
| 549 | input: BatchDeleteAppIndexDocumentsInput, |
| 550 | ): Promise<{ deleted: Array<{ id: string; documentKey: string }> }> { |
| 551 | const body = batchDeleteAppIndexDocumentsSchema.parse(input); |
| 552 |
| 553 | return this.request<{ deleted: Array<{ id: string; documentKey: string }> }>( |
| 554 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/index-documents`, |
| 555 | { |
| 556 | method: "DELETE", |
| 557 | body: JSON.stringify(body), |
| 558 | }, |
| 559 | ); |
| 560 | } |
| 561 |
| 562 | async getAppFileDocument( |
| 563 | workspaceSlug: string, |
| 564 | appKey: string, |
| 565 | fileId: string, |
| 566 | ): Promise<AppFileDocumentResponse> { |
| 567 | return this.request<AppFileDocumentResponse>( |
| 568 | `/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, |
| 569 | ); |
| 570 | } |
| 571 |
| 572 | async updateAppFileDocument( |
| 573 | workspaceSlug: string, |
| 574 | appKey: string, |
| 575 | fileId: string, |
| 576 | document: Record<string, unknown>, |
| 577 | ): Promise<{ document: Record<string, unknown>; version: number; updatedAt: string }> { |
| 578 | return this.request<{ |
| 579 | document: Record<string, unknown>; |
| 580 | version: number; |
| 581 | updatedAt: string; |
| 582 | }>(`/api/v1/workspaces/${workspaceSlug}/apps/${appKey}/files/${fileId}`, { |
| 583 | method: "PATCH", |
| 584 | body: JSON.stringify({ document }), |
| 585 | }); |
| 586 | } |
| 587 |
| 588 | async listTabModels(): Promise<{ object: "list"; data: TabModelRecord[] }> { |
| 589 | return this.request<{ object: "list"; data: TabModelRecord[] }>( |
| 590 | "/api/v1/tab/models", |
| 591 | ); |
| 592 | } |
| 593 |
| 594 | async createTabChatCompletion(input: { |
| 595 | model: string; |
| 596 | stream?: boolean; |
| 597 | tab_context: Record<string, unknown>; |
| 598 | messages?: Array<{ role?: string; content?: string }>; |
| 599 | }): Promise<{ |
| 600 | id: string; |
| 601 | object: "chat.completion"; |
| 602 | created: number; |
| 603 | model: string; |
| 604 | choices: Array<{ |
| 605 | index: number; |
| 606 | message: { role: "assistant"; content: string }; |
| 607 | finish_reason: "stop"; |
| 608 | }>; |
| 609 | usage: { |
| 610 | prompt_tokens: number; |
| 611 | completion_tokens: number; |
| 612 | total_tokens: number; |
| 613 | }; |
| 614 | }> { |
| 615 | return this.request("/api/v1/tab/chat/completions", { |
| 616 | method: "POST", |
| 617 | body: JSON.stringify(input), |
| 618 | }); |
| 619 | } |
| 620 |
| 621 | async exchangeOAuthCode(input: { |
| 622 | clientId: string; |
| 623 | clientSecret: string; |
| 624 | code: string; |
| 625 | redirectUri: string; |
| 626 | codeVerifier?: string; |
| 627 | }): Promise<OAuthTokenResponse> { |
| 628 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/token"), { |
| 629 | method: "POST", |
| 630 | headers: { |
| 631 | "Content-Type": "application/json", |
| 632 | }, |
| 633 | body: JSON.stringify({ |
| 634 | grant_type: "authorization_code", |
| 635 | client_id: input.clientId, |
| 636 | client_secret: input.clientSecret, |
| 637 | code: input.code, |
| 638 | redirect_uri: input.redirectUri, |
| 639 | ...(input.codeVerifier ? { code_verifier: input.codeVerifier } : {}), |
| 640 | }), |
| 641 | }); |
| 642 |
| 643 | return parseJsonResponse<OAuthTokenResponse>(response); |
| 644 | } |
| 645 |
| 646 | async getOAuthUserInfo(accessToken: string): Promise<OAuthUserInfoResponse> { |
| 647 | const response = await this.fetchImpl(joinUrl(this.baseUrl, "/api/v1/oauth/userinfo"), { |
| 648 | method: "GET", |
| 649 | headers: { |
| 650 | Authorization: `Bearer ${accessToken}`, |
| 651 | }, |
| 652 | }); |
| 653 |
| 654 | return parseJsonResponse<OAuthUserInfoResponse>(response); |
| 655 | } |
| 656 | } |
| 657 |