Skip to content

Commit 1828325

Browse files
committed
Parse the compile response instead of assuming it's valid
1 parent 359d8f7 commit 1828325

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

ui/frontend/compileActions.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AsyncThunk, createAsyncThunk } from '@reduxjs/toolkit';
2+
import * as z from 'zod';
23

34
import { SimpleThunkAction, adaptFetchError, jsonPost, routes } from './actions';
45
import { compileRequestPayloadSelector } from './selectors';
@@ -17,11 +18,12 @@ interface CompileRequestBody {
1718
processAssembly: string;
1819
}
1920

20-
interface CompileResponseBody {
21-
code: string;
22-
stdout: string;
23-
stderr: string;
24-
}
21+
const CompileResponseBody = z.object({
22+
code: z.string(),
23+
stdout: z.string(),
24+
stderr: z.string(),
25+
});
26+
type CompileResponseBody = z.infer<typeof CompileResponseBody>;
2527

2628
interface Props {
2729
sliceName: string;
@@ -34,9 +36,10 @@ interface CompileActions {
3436
}
3537

3638
export const makeCompileActions = ({ sliceName, target }: Props): CompileActions => {
37-
const action = createAsyncThunk(sliceName, async (payload: CompileRequestBody) =>
38-
adaptFetchError(() => jsonPost<CompileResponseBody>(routes.compile, payload)),
39-
);
39+
const action = createAsyncThunk(sliceName, async (payload: CompileRequestBody) => {
40+
const d = await adaptFetchError(() => jsonPost(routes.compile, payload));
41+
return CompileResponseBody.parseAsync(d);
42+
});
4043

4144
const performCompile = (): SimpleThunkAction => (dispatch, getState) => {
4245
const state = getState();

0 commit comments

Comments
 (0)