diff --git a/.husky/commit-msg b/.husky/commit-msg index 86890fd..e65df50 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - npx commitlint --edit ${1} diff --git a/.husky/pre-commit b/.husky/pre-commit index da01059..7e00fd1 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,5 +1,2 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - pnpm lint-staged -pnpm install --frozen-lockfile \ No newline at end of file +pnpm install --frozen-lockfile diff --git a/commitlint.config.mjs b/commitlint.config.js similarity index 100% rename from commitlint.config.mjs rename to commitlint.config.js diff --git a/demo/index.html b/demo/index.html new file mode 100644 index 0000000..3517a0d --- /dev/null +++ b/demo/index.html @@ -0,0 +1,26 @@ + + + + + Zodex Demo + + + + + + + + + + + + + diff --git a/demo/index.js b/demo/index.js new file mode 100644 index 0000000..5c7c95e --- /dev/null +++ b/demo/index.js @@ -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; + } +}); diff --git a/eslint.config.mjs b/eslint.config.js similarity index 77% rename from eslint.config.mjs rename to eslint.config.js index 4a8a1ba..60ab9fe 100644 --- a/eslint.config.mjs +++ b/eslint.config.js @@ -5,22 +5,11 @@ import eslintConfigPrettier from "eslint-config-prettier"; export default tseslint.config( { - ignores: [ - "src/schema.zodex.json", - "dist/", - ".idea", - "coverage", - ], + ignores: ["src/schema.zodex.json", "dist/", ".idea", "coverage"], }, eslint.configs.recommended, ...tseslint.configs.recommended, eslintConfigPrettier, - { - files: ['*.cjs'], - languageOptions: { - globals: globals.node - } - }, { files: ["demo/**/*.js"], languageOptions: { @@ -40,5 +29,5 @@ export default tseslint.config( "@typescript-eslint/no-explicit-any": "off", "@typescript-eslint/no-unused-vars": "off", }, - }, + } ); diff --git a/package.json b/package.json index afa7224..1bc0f7a 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,16 @@ "name": "zodex", "version": "0.0.0-dev", "description": "Type-safe (de)serialization for Zod", + "type": "module", "main": "dist/index.js", "types": "dist/index.d.ts", + "exports": { + ".": { + "import": "./dist/esm/index.js", + "require": "./dist/index.js" + }, + "./dist/schema.zodex.json": "./dist/schema.zodex.json" + }, "keywords": [ "zod", "serialization", @@ -13,11 +21,12 @@ "author": "Gregor Weber", "license": "MIT", "scripts": { + "start": "vite serve demo", "prepare": "husky", "check-style": "prettier --check src", "lint": "eslint --no-warn-ignored .", "test": "vitest --coverage --silent=false --reporter=basic", - "build": "rm -rf dist && pnpm tsc && cp src/schema.zodex.json dist/schema.zodex.json", + "build": "rm -rf dist && pnpm tsc --module esnext --moduleResolution bundler --outDir dist/esm && pnpm tsc && echo '{\"type\": \"commonjs\"}' > dist/package.json && echo '{\"type\": \"module\"}' > dist/esm/package.json && cp src/schema.zodex.json dist/schema.zodex.json", "prepublish": "pnpm run build" }, "peerDependencies": { @@ -40,6 +49,7 @@ "prettier": "2.8.8", "typescript": "5.5.4", "typescript-eslint": "8.4.0", + "vite": "^5.4.3", "vitest": "2.0.5" }, "packageManager": "pnpm@9.9.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0d69257..35059a8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -51,6 +51,9 @@ importers: typescript-eslint: specifier: 8.4.0 version: 8.4.0(eslint@9.9.1(jiti@1.21.6))(typescript@5.5.4) + vite: + specifier: ^5.4.3 + version: 5.4.3(@types/node@22.5.4) vitest: specifier: 2.0.5 version: 2.0.5(@types/node@22.5.4) diff --git a/src/dezerialize.ts b/src/dezerialize.ts index 9a38b19..e191d15 100644 --- a/src/dezerialize.ts +++ b/src/dezerialize.ts @@ -35,8 +35,8 @@ import { SzUnknown, SzVoid, SzRef, -} from "./types"; -import { ZodTypes } from "./zod-types"; +} from "./types.js"; +import { ZodTypes } from "./zod-types.js"; type DezerializerOptions = { superRefinements?: { diff --git a/src/index.test.ts b/src/index.test.ts index ea62a96..b819351 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -3,7 +3,7 @@ import { expect, test } from "vitest"; import { z } from "zod"; import { SzCatch } from "./types"; -import { dezerialize, SzType, zerialize, Zerialize } from "./index"; +import { dezerialize, SzType, zerialize, Zerialize } from "./index.js"; const zodexSchemaJSON = JSON.parse( fs.readFileSync("./src/schema.zodex.json", "utf-8") diff --git a/src/index.ts b/src/index.ts index 22d9b69..6d751da 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ -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 * from "./types.js"; type KeysOfUnion = T extends T ? keyof T : never; export type SzPropertyKeysOf = KeysOfUnion< diff --git a/src/zerialize.ts b/src/zerialize.ts index 76adfd6..8496553 100644 --- a/src/zerialize.ts +++ b/src/zerialize.ts @@ -25,8 +25,8 @@ import { SzUnknown, STRING_KINDS, SzRef, -} from "./types"; -import { ZodTypes, ZTypeName } from "./zod-types"; +} from "./types.js"; +import { ZodTypes, ZTypeName } from "./zod-types.js"; export const PRIMITIVES = { ZodString: "string", diff --git a/tsconfig.json b/tsconfig.json index d707dc4..ed4ac2f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,5 +9,5 @@ "declaration": true, "outDir": "dist" }, - "exclude": ["vite.config.mts", "dist", "node_modules", "**/*.test.ts"] + "exclude": ["vite.config.ts", "dist", "node_modules", "**/*.test.ts"] } diff --git a/vite.config.mts b/vite.config.mts deleted file mode 100644 index 5b597f3..0000000 --- a/vite.config.mts +++ /dev/null @@ -1,12 +0,0 @@ -import { defineConfig } from 'vitest/config'; - -export default defineConfig({ - test: { - coverage: { - exclude: [ - ".idea", "dist", "*.config.mjs", "*.config.mts", - "*.js", "node_modules" - ] - } - }, -}); diff --git a/vite.config.ts b/vite.config.ts new file mode 100644 index 0000000..b56c302 --- /dev/null +++ b/vite.config.ts @@ -0,0 +1,17 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + coverage: { + exclude: [ + "demo", + ".idea", + "dist", + "*.config.js", + "*.config.ts", + "node_modules", + "src/zod-types.ts", + ], + }, + }, +});