(body: string)
| 23 | * Supports: Pull protocol `{ sessions, page? }`, ChatLab API `{ success, data }`, and plain array. |
| 24 | */ |
| 25 | export function parseRemoteSessionsResponse(body: string): RemoteSessionDiscoveryResult { |
| 26 | const parsed = JSON.parse(body) |
| 27 | |
| 28 | let sessions: RemoteSession[] |
| 29 | let pageSource: Record<string, unknown> | undefined |
| 30 | |
| 31 | if (Array.isArray(parsed)) { |
| 32 | sessions = parsed |
| 33 | } else if (parsed && typeof parsed === 'object') { |
| 34 | sessions = parsed.sessions ?? parsed.data?.sessions ?? parsed.data ?? [] |
| 35 | if (!Array.isArray(sessions)) sessions = [] |
| 36 | pageSource = parsed.page ?? parsed.data?.page |
| 37 | } else { |
| 38 | sessions = [] |
| 39 | } |
| 40 | |
| 41 | return { |
| 42 | sessions, |
| 43 | page: |
| 44 | pageSource && typeof pageSource === 'object' |
| 45 | ? { |
| 46 | hasMore: Boolean(pageSource.hasMore), |
| 47 | nextCursor: typeof pageSource.nextCursor === 'string' ? pageSource.nextCursor : undefined, |
| 48 | } |
| 49 | : undefined, |
| 50 | } |
| 51 | } |
no test coverage detected