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