(data: unknown)
| 24 | import type { ChatGptOAuthCredentials } from '@codebuff/sdk' |
| 25 | |
| 26 | function parseOAuthTokenResponse(data: unknown): { |
| 27 | accessToken: string |
| 28 | refreshToken: string |
| 29 | expiresInMs: number |
| 30 | } { |
| 31 | if (!data || typeof data !== 'object') { |
| 32 | throw new Error('Invalid token response format from ChatGPT OAuth.') |
| 33 | } |
| 34 | |
| 35 | const tokenData = data as { |
| 36 | access_token?: unknown |
| 37 | refresh_token?: unknown |
| 38 | expires_in?: unknown |
| 39 | } |
| 40 | |
| 41 | if ( |
| 42 | typeof tokenData.access_token !== 'string' || |
| 43 | tokenData.access_token.trim().length === 0 |
| 44 | ) { |
| 45 | throw new Error('Token exchange did not return a valid access token.') |
| 46 | } |
| 47 | |
| 48 | const refreshToken = |
| 49 | typeof tokenData.refresh_token === 'string' ? tokenData.refresh_token : '' |
| 50 | const expiresInMs = |
| 51 | typeof tokenData.expires_in === 'number' && |
| 52 | Number.isFinite(tokenData.expires_in) && |
| 53 | tokenData.expires_in > 0 |
| 54 | ? tokenData.expires_in * 1000 |
| 55 | : 3600 * 1000 |
| 56 | |
| 57 | return { |
| 58 | accessToken: tokenData.access_token, |
| 59 | refreshToken, |
| 60 | expiresInMs, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | function toBase64Url(buffer: Buffer): string { |
| 65 | return buffer |
no outgoing calls
no test coverage detected