All files / webview-app/src/schemas messages.ts

76.92% Statements 10/13
0% Branches 0/6
0% Functions 0/2
76.92% Lines 10/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91        3x                       3x         3x         3x         3x                                       3x         3x         3x               3x           3x                              
import { z } from "zod";
 
import { CanvasStateSchema, ConfigDefaultsSchema } from "@/schemas/canvas";
 
export const ToolbarCommandSchema = z.enum([
  "newWindow",
  "open",
  "save",
  "generate",
  "initConfig",
  "undo",
  "redo",
  "delete",
  "previewCode",
]);
 
export const StateChangedMessageSchema = z.object({
  type: z.literal("stateChanged"),
  state: CanvasStateSchema,
});
 
export const ToolbarCommandMessageSchema = z.object({
  type: z.literal("toolbarCommand"),
  command: ToolbarCommandSchema,
});
 
export const LoadStateMessageSchema = z.object({
  type: z.literal("loadState"),
  state: CanvasStateSchema,
});
 
export const ConfigDefaultsMessageSchema = z
  .object({
    type: z.literal("configDefaults"),
    defaults: ConfigDefaultsSchema.optional(),
    config: ConfigDefaultsSchema.optional(),
  })
  .superRefine((message, ctx) => {
    if (!message.defaults && !message.config) {
      ctx.addIssue({
        code: z.ZodIssueCode.custom,
        message: "configDefaults message must include defaults or config",
        path: ["defaults"],
      });
    }
  })
  .transform((message) => ({
    type: message.type,
    defaults: message.defaults ?? message.config,
  }));
 
export const PreviewCodeFileSchema = z.object({
  fileName: z.string(),
  content: z.string(),
});
 
export const PreviewCodeResponseMessageSchema = z.object({
  type: z.literal("previewCodeResponse"),
  files: z.array(PreviewCodeFileSchema),
});
 
export const MessageSchema = z.discriminatedUnion("type", [
  StateChangedMessageSchema,
  ToolbarCommandMessageSchema,
  LoadStateMessageSchema,
  ConfigDefaultsMessageSchema,
  PreviewCodeResponseMessageSchema,
]);
 
export const IncomingExtensionMessageSchema = z.discriminatedUnion("type", [
  LoadStateMessageSchema,
  ConfigDefaultsMessageSchema,
  PreviewCodeResponseMessageSchema,
]);
 
export const OutgoingExtensionMessageSchema = z.discriminatedUnion("type", [
  StateChangedMessageSchema,
  ToolbarCommandMessageSchema,
]);
 
export type ToolbarCommand = z.infer<typeof ToolbarCommandSchema>;
export type StateChangedMessage = z.infer<typeof StateChangedMessageSchema>;
export type ToolbarCommandMessage = z.infer<typeof ToolbarCommandMessageSchema>;
export type LoadStateMessage = z.infer<typeof LoadStateMessageSchema>;
export type ConfigDefaultsMessage = z.infer<typeof ConfigDefaultsMessageSchema>;
export type PreviewCodeFile = z.infer<typeof PreviewCodeFileSchema>;
export type PreviewCodeResponseMessage = z.infer<typeof PreviewCodeResponseMessageSchema>;
export type ExtensionMessage = z.infer<typeof MessageSchema>;
export type IncomingExtensionMessage = z.infer<typeof IncomingExtensionMessageSchema>;
export type OutgoingExtensionMessage = z.infer<typeof OutgoingExtensionMessageSchema>;