Skip to content

Commit

Permalink
Process custom fails (#101)
Browse files Browse the repository at this point in the history
* zod parse custom command handler outputs to ensure they satisfy expected output

* 1.6.9
  • Loading branch information
dvargas92495 authored Oct 10, 2023
1 parent 7fe833e commit 8a42568
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "smartblocks",
"version": "1.6.8",
"version": "1.6.9",
"description": "Create custom and programmable templates from within Roam!",
"main": "./build/main.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import saveDailyConfig from "./utils/saveDailyConfig";
import DailyConfigComponent from "./components/DailyConfigComponent";
import { runDaily } from "./utils/scheduleNextDailyRun";
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
import { zCommandOutput } from "./utils/zodTypes";

const getLegacy42Setting = (name: string) => {
const settings = Object.fromEntries(
Expand Down Expand Up @@ -280,7 +281,7 @@ export default runExtension(async ({ extensionAPI }) => {
proccessBlockText,
processBlock: proccessBlockWithSmartness,
})(...args);
return result;
return zCommandOutput.parse(result);
} catch (e) {
console.error(e);
return `Custom Command ${command} Failed: ${(e as Error).message}`;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import getDailyConfig from "./getDailyConfig";
import scheduleNextDailyRun from "./scheduleNextDailyRun";
import apiPost from "roamjs-components/util/apiPost";
import deleteBlock from "roamjs-components/writes/deleteBlock";
import { zCommandOutput } from "./zodTypes";
import { z } from "zod";

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

type CommandOutput = string | string[] | InputTextNode[];
type CommandOutput = z.infer<typeof zCommandOutput>;
export type CommandHandler = (
...args: string[]
) => CommandOutput | Promise<CommandOutput>;
Expand Down
23 changes: 23 additions & 0 deletions src/utils/zodTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { InputTextNode } from "roamjs-components/types";
import { z } from "zod";

const zInputTextNodeBase = z.object({
text: z.string(),
uid: z.string().optional(),
heading: z.number().optional(),
textAlign: z.enum(["left", "center", "right", "justify"]).optional(),
viewType: z.enum(["document", "bullet", "numbered"]).optional(),
open: z.boolean().optional(),
props: z.record(z.unknown()).optional(),
});

// TODO: This should live in roamjs-components
const zInputTextNode: z.ZodType<InputTextNode> = z.lazy(() =>
zInputTextNodeBase.and(
z.object({ children: z.array(zInputTextNode).optional() })
)
);
export const zCommandOutput = z
.string()
.or(z.string().array())
.or(zInputTextNode.array());

0 comments on commit 8a42568

Please sign in to comment.