cc-haha/desktop/src/api/hahaOpenAIOAuth.ts
程序员阿江(Relakkes) 83ee4c09ec Add desktop ChatGPT OAuth controls
The desktop app now has a dedicated OpenAI OAuth API client and store mirroring the existing Claude Official flow. The component uses the existing browser-open plus polling pattern and never exposes token material to the UI.

Constraint: OAuth status responses must not return token bodies

Rejected: Reuse Claude OAuth store | the status shape and endpoint differ

Confidence: high

Scope-risk: narrow

Tested: cd desktop && bun run test -- src/stores/hahaOpenAIOAuthStore.test.ts

Tested: git diff --check
2026-05-18 23:53:11 +08:00

39 lines
916 B
TypeScript

// desktop/src/api/hahaOpenAIOAuth.ts
import { api, getBaseUrl } from './client'
export type HahaOpenAIOAuthStatus =
| { loggedIn: false }
| {
loggedIn: true
expiresAt: number | null
email: string | null
accountId: string | null
}
function currentServerPort(): number {
const port = new URL(getBaseUrl()).port
const parsed = Number.parseInt(port, 10)
if (!Number.isFinite(parsed) || parsed <= 0) {
throw new Error(`Cannot determine server port from baseUrl: ${getBaseUrl()}`)
}
return parsed
}
export const hahaOpenAIOAuthApi = {
start() {
return api.post<{ authorizeUrl: string; state: string }>(
'/api/haha-openai-oauth/start',
{ serverPort: currentServerPort() },
)
},
status() {
return api.get<HahaOpenAIOAuthStatus>('/api/haha-openai-oauth')
},
logout() {
return api.delete<{ ok: true }>('/api/haha-openai-oauth')
},
}