Skip to content

Commit 359d8f7

Browse files
committed
Parse the gist responses instead of assuming they are valid
Also adding missing `adaptFetchError` calls.
1 parent 8dd53f1 commit 359d8f7

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

ui/frontend/reducers/output/gist.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Draft, PayloadAction, createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2+
import * as z from 'zod';
23

3-
import { jsonGet, jsonPost, routes } from '../../actions';
4+
import { adaptFetchError, jsonGet, jsonPost, routes } from '../../actions';
45
import { baseUrlSelector, codeSelector } from '../../selectors';
56
import RootState from '../../state';
67
import { Channel, Edition, Mode } from '../../types';
@@ -39,11 +40,12 @@ type PerformGistLoadProps = Pick<
3940
Exclude<keyof SuccessProps, 'url' | 'code' | 'stdout' | 'stderr'>
4041
>;
4142

42-
interface GistResponseBody {
43-
id: string;
44-
url: string;
45-
code: string;
46-
}
43+
const GistResponseBody = z.object({
44+
id: z.string(),
45+
url: z.string(),
46+
code: z.string(),
47+
});
48+
type GistResponseBody = z.infer<typeof GistResponseBody>;
4749

4850
export const performGistLoad = createAsyncThunk<
4951
SuccessProps,
@@ -55,8 +57,9 @@ export const performGistLoad = createAsyncThunk<
5557
const gistUrl = new URL(routes.meta.gistLoad, baseUrl);
5658
const u = new URL(id, gistUrl);
5759

58-
const gist = await jsonGet(u);
59-
return { channel, mode, edition, ...gist };
60+
const d = await adaptFetchError(() => jsonGet(u));
61+
const gist = await GistResponseBody.parseAsync(d);
62+
return { ...gist, channel, mode, edition, stdout: '', stderr: '' };
6063
});
6164

6265
export const performGistSave = createAsyncThunk<SuccessProps, void, { state: RootState }>(
@@ -71,8 +74,9 @@ export const performGistSave = createAsyncThunk<SuccessProps, void, { state: Roo
7174
},
7275
} = state;
7376

74-
const json = await jsonPost<GistResponseBody>(routes.meta.gistSave, { code });
75-
return { ...json, code, stdout, stderr, channel, mode, edition };
77+
const d = await adaptFetchError(() => jsonPost(routes.meta.gistSave, { code }));
78+
const gist = await GistResponseBody.parseAsync(d);
79+
return { ...gist, code, stdout, stderr, channel, mode, edition };
7680
},
7781
);
7882

0 commit comments

Comments
 (0)