Skip to content

Commit

Permalink
feat: add esm build
Browse files Browse the repository at this point in the history
Also:
- docs: adds demo
  • Loading branch information
brettz9 committed Jun 21, 2024
1 parent ad9764d commit fa8aaef
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 15 deletions.
27 changes: 27 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Zodex Demo</title>
<style>
textarea { width: 300px; height: 400px; }
</style>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" />
<script type="importmap">
{
"imports": {
"zod": "../node_modules/zod/lib/index.mjs",
"react": "./react.mjs"
}
}
</script>
<script type="module" src="index.mjs"></script>
</head>
<body>
<textarea id="js" placeholder="Put arbitrary JavaScript object here (e.g., `{a: 5}`)">{a: 5}</textarea>
<textarea id="zod" placeholder="Put Zod here (e.g., `z.object({a: z.number()})`">z.object({a: z.number()})</textarea>
<button id="zerialize">Zerialize</button>
<textarea id="zodexJSON" placeholder="Put Zodex JSON here (or zerialize a Zod expression)"></textarea>
<button id="dezerialize">Dezerialize and validate</button>
</body>
</html>
66 changes: 66 additions & 0 deletions demo/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {z} from 'zod';
import {zerialize, dezerialize} from '../dist/esm/index.js';

const $ = (sel) => {
return document.querySelector(sel);
};

$('#dezerialize').addEventListener('click', () => {
let js;
try {
js = eval(`(${$('#js').value})`);
} catch (err) {
alert('Error evaluating JavaScript');
return;
}

let json;
try {
json = JSON.parse($('#zodexJSON').value);
} catch (err) {
alert('Error evaluating JSON');
return;
}

let dez;
try {
dez = dezerialize(json);
} catch (err) {
alert('Error dezerializing JSON');
return;
}

let parseObj;
try {
parseObj = dez.safeParse(js);
} catch (err) {
alert('Error parsing JavaScript');
return;
}

alert(JSON.stringify(parseObj, null, 2));
});

$('#zerialize').addEventListener('click', () => {
let zod;
try {
zod = eval(`(${$('#zod').value})`);
} catch (err) {
alert('Error evaluating Zod');
return;
}
let zer;
try {
zer = zerialize(zod);
} catch (err) {
alert('Error zerializing Zod');
return;
}

try {
$('#zodexJSON').value = JSON.stringify(zer, null, 2);
} catch (err) {
alert('Error stringifying Zod JSON object');
return;
}
});
1 change: 1 addition & 0 deletions demo/react.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "zodex",
"version": "0.0.0-dev",
"description": "Type-safe (de)serialization for Zod",
"type": "commonjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"keywords": [
Expand All @@ -13,11 +14,12 @@
"author": "Gregor Weber<[email protected]>",
"license": "MIT",
"scripts": {
"start": "static",
"prepare": "husky install",
"check-style": "prettier --check src",
"lint": "eslint src/**",
"test": "vitest --coverage --silent=false",
"build": "rm -rf dist && pnpm tsc",
"build": "rm -rf dist && pnpm tsc --module esnext --moduleResolution bundler --outDir dist/esm && pnpm tsc",
"prepublish": "pnpm run build"
},
"peerDependencies": {
Expand All @@ -31,6 +33,7 @@
"zod": "^3.23.8"
},
"devDependencies": {
"@brettz9/node-static": "^0.1.1",
"@commitlint/cli": "19.3.0",
"@commitlint/config-conventional": "19.2.2",
"@types/react": "18.3.3",
Expand Down
41 changes: 41 additions & 0 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions src/dezerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import {
SzUndefined,
SzUnknown,
SzVoid,
} from "./types";
import { ZodTypes } from "./zod-types";
} from "./types.js";
import { ZodTypes } from "./zod-types.js";

type DezerializerOptions =
| {
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test } from "vitest";
import { z } from "zod";

import { dezerialize, SzType, zerialize, Zerialize } from "./index";
import { dezerialize, SzType, zerialize, Zerialize } from "./index.js";

const p = <
Schema extends z.ZodFirstPartySchemaTypes,
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { SzType } from "./types";
import { SzType } from "./types.js";

export * from "./dezerialize";
export * from "./zerialize";
export * from "./dezerialize.js";
export * from "./zerialize.js";

export * from "./types";
export { mapTypesToViews } from "./ui";
export * from "./types.js";
export { mapTypesToViews } from "./ui.js";

type KeysOfUnion<T> = T extends T ? keyof T : never;
export type SzPropertyKeysOf<T extends SzType> = KeysOfUnion<
Expand Down
2 changes: 1 addition & 1 deletion src/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
SzFunction,
SzEnum,
SzPromise,
} from "./types";
} from "./types.js";

type PrimitiveTypes = {
string: string;
Expand Down
4 changes: 2 additions & 2 deletions src/ui.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

import { SzInfer } from "./infer";
import { SzType } from "./types";
import { SzInfer } from "./infer.js";
import { SzType } from "./types.js";

type ShapeValueProps<Value> = {
value: Value;
Expand Down
4 changes: 2 additions & 2 deletions src/zerialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {
SzType,
SzUnknown,
STRING_KINDS,
} from "./types";
import { ZodTypes, ZTypeName } from "./zod-types";
} from "./types.js";
import { ZodTypes, ZTypeName } from "./zod-types.js";

export const PRIMITIVES = {
ZodString: "string",
Expand Down
2 changes: 1 addition & 1 deletion vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
coverage: {
exclude: ["*.js", "node_modules"]
exclude: ["*.js", "demo", "node_modules"]
}
},
});

0 comments on commit fa8aaef

Please sign in to comment.