|
1 | | -import fetch from 'isomorphic-fetch'; |
2 | 1 | import { ThunkAction as ReduxThunkAction, AnyAction } from '@reduxjs/toolkit'; |
3 | 2 |
|
4 | 3 | import { |
@@ -35,21 +34,6 @@ import { performCompileToLlvmIrOnly } from './reducers/output/llvmIr'; |
35 | 34 | import { performCompileToMirOnly } from './reducers/output/mir'; |
36 | 35 | import { performCompileToWasmOnly } from './reducers/output/wasm'; |
37 | 36 |
|
38 | | -export const routes = { |
39 | | - compile: '/compile', |
40 | | - execute: '/execute', |
41 | | - format: '/format', |
42 | | - clippy: '/clippy', |
43 | | - miri: '/miri', |
44 | | - macroExpansion: '/macro-expansion', |
45 | | - meta: { |
46 | | - crates: '/meta/crates', |
47 | | - versions: '/meta/versions', |
48 | | - gistSave: '/meta/gist', |
49 | | - gistLoad: '/meta/gist/id', |
50 | | - }, |
51 | | -}; |
52 | | - |
53 | 37 | export type ThunkAction<T = void> = ReduxThunkAction<T, State, {}, Action>; |
54 | 38 | export type SimpleThunkAction<T = void> = ReduxThunkAction<T, State, {}, AnyAction>; |
55 | 39 |
|
@@ -148,87 +132,6 @@ export const reExecuteWithBacktrace = (): ThunkAction => dispatch => { |
148 | 132 | dispatch(performExecuteOnly()); |
149 | 133 | }; |
150 | 134 |
|
151 | | -type FetchArg = Parameters<typeof fetch>[0]; |
152 | | - |
153 | | -export function jsonGet(url: FetchArg): Promise<unknown> { |
154 | | - return fetchJson(url, { |
155 | | - method: 'get', |
156 | | - }); |
157 | | -} |
158 | | - |
159 | | -export function jsonPost(url: FetchArg, body: Record<string, any>): Promise<unknown> { |
160 | | - return fetchJson(url, { |
161 | | - method: 'post', |
162 | | - body: JSON.stringify(body), |
163 | | - }); |
164 | | -} |
165 | | - |
166 | | -async function fetchJson(url: FetchArg, args: RequestInit) { |
167 | | - const headers = new Headers(args.headers); |
168 | | - headers.set('Content-Type', 'application/json'); |
169 | | - |
170 | | - let response; |
171 | | - try { |
172 | | - response = await fetch(url, { ...args, headers }); |
173 | | - } catch (networkError) { |
174 | | - // e.g. server unreachable |
175 | | - if (networkError instanceof Error) { |
176 | | - throw ({ |
177 | | - error: `Network error: ${networkError.toString()}`, |
178 | | - }); |
179 | | - } else { |
180 | | - throw ({ |
181 | | - error: 'Unknown error while fetching JSON', |
182 | | - }); |
183 | | - } |
184 | | - } |
185 | | - |
186 | | - let body; |
187 | | - try { |
188 | | - body = await response.json(); |
189 | | - } catch (convertError) { |
190 | | - if (convertError instanceof Error) { |
191 | | - throw ({ |
192 | | - error: `Response was not JSON: ${convertError.toString()}`, |
193 | | - }); |
194 | | - } else { |
195 | | - throw ({ |
196 | | - error: 'Unknown error while converting JSON', |
197 | | - }); |
198 | | - } |
199 | | - } |
200 | | - |
201 | | - if (response.ok) { |
202 | | - // HTTP 2xx |
203 | | - return body; |
204 | | - } else { |
205 | | - // HTTP 4xx, 5xx (e.g. malformed JSON request) |
206 | | - throw body; |
207 | | - } |
208 | | -} |
209 | | - |
210 | | -// We made some strange decisions with how the `fetchJson` function |
211 | | -// communicates errors, so we untwist those here to fit better with |
212 | | -// redux-toolkit's ideas. |
213 | | -export const adaptFetchError = async <R>(cb: () => Promise<R>): Promise<R> => { |
214 | | - let result; |
215 | | - |
216 | | - try { |
217 | | - result = await cb(); |
218 | | - } catch (e) { |
219 | | - if (e && typeof e === 'object' && 'error' in e && typeof e.error === 'string') { |
220 | | - throw new Error(e.error); |
221 | | - } else { |
222 | | - throw new Error('An unknown error occurred'); |
223 | | - } |
224 | | - } |
225 | | - |
226 | | - if (result && typeof result === 'object' && 'error' in result && typeof result.error === 'string') { |
227 | | - throw new Error(result.error); |
228 | | - } |
229 | | - |
230 | | - return result; |
231 | | -} |
232 | 135 |
|
233 | 136 | function performAutoOnly(): ThunkAction { |
234 | 137 | return function(dispatch, getState) { |
|
0 commit comments