* Throws unless `apiUrl` serves the CLI API. Asked by `login` before it opens * a browser: on an instance without the API, /cli/authorize lands on the * dashboard's login page and never calls back, and the CLI used to wait out * its full five minutes. An unauthenticated GET /me answers a co
()
| 111 | * — an HTML page, a redirect, an older `{ error: "..." }` — means no API. |
| 112 | */ |
| 113 | async assertCliApi(): Promise<void> { |
| 114 | const path = `/api/v1${operations.me.path}`; |
| 115 | let response: Response; |
| 116 | try { |
| 117 | response = await fetch(`${this.apiUrl}${path}`, { |
| 118 | headers: { |
| 119 | accept: 'application/json', |
| 120 | 'user-agent': `decap-cli/${CLI_VERSION} (${this.client})`, |
| 121 | 'x-decap-client': this.client, |
| 122 | }, |
| 123 | redirect: 'manual', |
| 124 | }); |
| 125 | } catch (err) { |
| 126 | throw new ApiError( |
| 127 | 0, |
| 128 | 'network_error', |
| 129 | `Could not reach ${this.apiUrl}: ${(err as Error).message}`, |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | let error: { code?: unknown; message?: unknown } | undefined; |
| 134 | try { |
| 135 | error = JSON.parse(await response.text())?.error; |
| 136 | } catch { |
| 137 | // Not JSON: not the API. |
| 138 | } |
| 139 | if (typeof error?.code === 'string' && typeof error?.message === 'string') return; |
| 140 | throw new ApiError( |
| 141 | response.status, |
| 142 | 'cli_unsupported', |
| 143 | `${this.apiUrl} does not support the decap CLI yet: GET ${path} answered ${response.status} ` + |
| 144 | 'without the API’s error shape. Pass --api-url (or set DECAP_API_URL) to sign in to a ' + |
| 145 | 'Turbo instance that does.', |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | export { operations }; |