()
| 155 | } |
| 156 | |
| 157 | async function handleConnect() { |
| 158 | try { |
| 159 | setConnectionStatus('connecting'); |
| 160 | let proxy = 'ext'; |
| 161 | if (selectedEnv) { |
| 162 | proxy = selectedEnv.proxy; |
| 163 | } |
| 164 | let newWsId: string | null = null; |
| 165 | let interpolatedRequest = { ...currentRequest }; |
| 166 | if (selectedEnvData) { |
| 167 | const collection = findCollection(collections, currentRequest.collectionId); |
| 168 | if (!collection) { |
| 169 | throw Error('Collection not found for id: ' + currentRequest.collectionId); |
| 170 | } |
| 171 | const interpolateResult = interpolate(currentRequest, selectedEnvData); |
| 172 | interpolatedRequest = interpolateResult.result; |
| 173 | } |
| 174 | switch (proxy) { |
| 175 | case 'server': |
| 176 | newWsId = await serverConnect(interpolatedRequest); |
| 177 | break; |
| 178 | case 'ext': |
| 179 | if (!isExtInitialized.current) { |
| 180 | openExtModal(); |
| 181 | throw Error('Extension not initialized'); |
| 182 | } |
| 183 | if (getMinorVersion(extVersion.current) < 10) { |
| 184 | throw Error( |
| 185 | 'Upgrade to the latest version of the extension to use websocket requests.', |
| 186 | ); |
| 187 | } |
| 188 | newWsId = await extensionConnect(interpolatedRequest); |
| 189 | break; |
| 190 | default: |
| 191 | throw Error('Unknown proxy'); |
| 192 | } |
| 193 | setConnectionStatus('connected'); |
| 194 | wsId.current = newWsId; |
| 195 | dispatchCurrentRequest({ |
| 196 | type: CurrentRequestActionType.PATCH_DATA, |
| 197 | data: { |
| 198 | ...currentRequest.data, |
| 199 | response: { |
| 200 | ...currentRequest.data.response, |
| 201 | messages: [], |
| 202 | }, |
| 203 | }, |
| 204 | }); |
| 205 | } catch (err) { |
| 206 | setConnectionStatus('disconnected'); |
| 207 | console.error(err); |
| 208 | errorToast('Failed to connect: ' + err, toast); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | async function extensionDisconnect() { |
| 213 | return await sendMessageToExtension(currentRequest.id, { |
nothing calls this directly
no test coverage detected