Skip to content

Commit 8a42568

Browse files
authored
Process custom fails (#101)
* zod parse custom command handler outputs to ensure they satisfy expected output * 1.6.9
1 parent 7fe833e commit 8a42568

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "smartblocks",
3-
"version": "1.6.8",
3+
"version": "1.6.9",
44
"description": "Create custom and programmable templates from within Roam!",
55
"main": "./build/main.js",
66
"scripts": {

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import saveDailyConfig from "./utils/saveDailyConfig";
4444
import DailyConfigComponent from "./components/DailyConfigComponent";
4545
import { runDaily } from "./utils/scheduleNextDailyRun";
4646
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
47+
import { zCommandOutput } from "./utils/zodTypes";
4748

4849
const getLegacy42Setting = (name: string) => {
4950
const settings = Object.fromEntries(
@@ -280,7 +281,7 @@ export default runExtension(async ({ extensionAPI }) => {
280281
proccessBlockText,
281282
processBlock: proccessBlockWithSmartness,
282283
})(...args);
283-
return result;
284+
return zCommandOutput.parse(result);
284285
} catch (e) {
285286
console.error(e);
286287
return `Custom Command ${command} Failed: ${(e as Error).message}`;

src/utils/core.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ import getDailyConfig from "./getDailyConfig";
6868
import scheduleNextDailyRun from "./scheduleNextDailyRun";
6969
import apiPost from "roamjs-components/util/apiPost";
7070
import deleteBlock from "roamjs-components/writes/deleteBlock";
71+
import { zCommandOutput } from "./zodTypes";
72+
import { z } from "zod";
7173

7274
type FormDialogProps = Parameters<typeof FormDialog>[0];
7375
const renderFormDialog = createOverlayRender<FormDialogProps>(
@@ -415,7 +417,7 @@ const outputTodoBlocks = (
415417
.map(getFormatter(format, createTagRegex("TODO")));
416418
};
417419

418-
type CommandOutput = string | string[] | InputTextNode[];
420+
type CommandOutput = z.infer<typeof zCommandOutput>;
419421
export type CommandHandler = (
420422
...args: string[]
421423
) => CommandOutput | Promise<CommandOutput>;

src/utils/zodTypes.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { InputTextNode } from "roamjs-components/types";
2+
import { z } from "zod";
3+
4+
const zInputTextNodeBase = z.object({
5+
text: z.string(),
6+
uid: z.string().optional(),
7+
heading: z.number().optional(),
8+
textAlign: z.enum(["left", "center", "right", "justify"]).optional(),
9+
viewType: z.enum(["document", "bullet", "numbered"]).optional(),
10+
open: z.boolean().optional(),
11+
props: z.record(z.unknown()).optional(),
12+
});
13+
14+
// TODO: This should live in roamjs-components
15+
const zInputTextNode: z.ZodType<InputTextNode> = z.lazy(() =>
16+
zInputTextNodeBase.and(
17+
z.object({ children: z.array(zInputTextNode).optional() })
18+
)
19+
);
20+
export const zCommandOutput = z
21+
.string()
22+
.or(z.string().array())
23+
.or(zInputTextNode.array());

0 commit comments

Comments
 (0)