From 742d49658a2477011e3ebc177a673ba7bd4a8897 Mon Sep 17 00:00:00 2001 From: Michael Sweeney Date: Wed, 4 Sep 2024 18:32:45 -0700 Subject: [PATCH] pretiier --- examples/react/src/App.tsx | 66 ++++++------- src/index.ts | 192 ++++++++++++++++++------------------- 2 files changed, 129 insertions(+), 129 deletions(-) diff --git a/examples/react/src/App.tsx b/examples/react/src/App.tsx index 3fbabc4..5625799 100644 --- a/examples/react/src/App.tsx +++ b/examples/react/src/App.tsx @@ -1,6 +1,6 @@ -import './App.css' -import { z } from 'zod' -import { schema, useSnapshot } from '../../../src/index' +import './App.css'; +import { z } from 'zod'; +import { schema, useSnapshot } from '../../../src/index'; const userSchema = z.object({ username: z.string(), @@ -10,10 +10,10 @@ const userSchema = z.object({ lastName: z.string(), address: z.object({ city: z.string(), - country: z.string() - }) - }) -}) + country: z.string(), + }), + }), +}); const userState = schema(userSchema).proxy( { @@ -24,75 +24,75 @@ const userState = schema(userSchema).proxy( lastName: 'Smith', address: { city: 'Wonderland', - country: 'Fantasy' - } - } + country: 'Fantasy', + }, + }, }, - { safeParse: true, errorHandler: (e) => console.log(e.message) } -) + { safeParse: true, errorHandler: (e) => console.log(e.message) }, +); function App() { - const user = useSnapshot(userState) + const user = useSnapshot(userState); return (

Vite + React

- + (userState.username = e.target.value)} />

Username: {user.username}

- + (userState.age = Number(e.target.value))} />

Age: {user.age}

- + (userState.profile.firstName = e.target.value)} />

First Name: {user.profile.firstName}

- + (userState.profile.lastName = e.target.value)} />

Last Name: {user.profile.lastName}

- + (userState.profile.address.city = e.target.value)} />

City: {user.profile.address.city}

- + (userState.profile.address.country = e.target.value)} />

Last Name: {user.profile.address.country}

- ) + ); } -export default App +export default App; diff --git a/src/index.ts b/src/index.ts index 1dafa3a..bb6dac5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,85 +1,85 @@ /* eslint-disable */ -import { z, ZodType } from 'zod' -import { proxy as vproxy, useSnapshot as vsnap, getVersion } from 'valtio' -import _ from 'lodash' +import { z, ZodType } from 'zod'; +import { proxy as vproxy, useSnapshot as vsnap, getVersion } from 'valtio'; +import _ from 'lodash'; type ValtioProxy = { - [P in keyof T]: T[P] -} + [P in keyof T]: T[P]; +}; type SchemaConfig = { - parseAsync?: boolean - safeParse?: boolean - errorHandler?: (error: unknown) => void -} + parseAsync?: boolean; + safeParse?: boolean; + errorHandler?: (error: unknown) => void; +}; const defaultConfig = { parseAsync: false, safeParse: false, - errorHandler: (error: unknown) => console.error(error) -} + errorHandler: (error: unknown) => console.error(error), +}; export const vzGlobalConfig = { safeParse: false, - errorHandler: (error: unknown) => console.error(error) -} + errorHandler: (error: unknown) => console.error(error), +}; const isObject = (x: unknown): x is object => - typeof x === 'object' && x !== null + typeof x === 'object' && x !== null; -type MergedConfig = Required +type MergedConfig = Required; type SchemaMeta = SchemaConfig & { - initialState: unknown -} + initialState: unknown; +}; -type PropType = string | number | symbol -const schemaMeta = new WeakMap, SchemaMeta>() -const pathList = new WeakMap<{}, PropType[]>() -const isProxySymbol = Symbol('isProxy') +type PropType = string | number | symbol; +const schemaMeta = new WeakMap, SchemaMeta>(); +const pathList = new WeakMap<{}, PropType[]>(); +const isProxySymbol = Symbol('isProxy'); type SchemaReturn> = { proxy: { - (initialState: any, config?: SchemaConfig): ValtioProxy> - } -} + (initialState: any, config?: SchemaConfig): ValtioProxy>; + }; +}; -const valtioStoreSymbol = Symbol('valtioStore') +const valtioStoreSymbol = Symbol('valtioStore'); export const useSnapshot = (store: any) => { - const valtioStore = store[valtioStoreSymbol] - console.log('valtioStore', valtioStore) - return vsnap(valtioStore[valtioStoreSymbol]) -} + const valtioStore = store[valtioStoreSymbol]; + console.log('valtioStore', valtioStore); + return vsnap(valtioStore[valtioStoreSymbol]); +}; export const schema = >( - zodSchema: T + zodSchema: T, ): SchemaReturn => { const proxy = ( initialState: z.infer, - config: SchemaConfig = {} + config: SchemaConfig = {}, ): ValtioProxy> => { if (!isObject(initialState)) { - throw new Error('object required') + throw new Error('object required'); } - const mergedConfig: MergedConfig = { ...defaultConfig, ...config } + const mergedConfig: MergedConfig = { ...defaultConfig, ...config }; - const parseAsync = mergedConfig.parseAsync - const safeParse = mergedConfig.safeParse - const errorHandler = mergedConfig.errorHandler + const parseAsync = mergedConfig.parseAsync; + const safeParse = mergedConfig.safeParse; + const errorHandler = mergedConfig.errorHandler; // before proxying, validate the initial state - console.log('in schema') + console.log('in schema'); if (parseAsync) { zodSchema.parseAsync(initialState).catch((e) => { - throw e - }) + throw e; + }); } else { - zodSchema.parse(initialState) + zodSchema.parse(initialState); } - const valtioProxy = vproxy(initialState) + const valtioProxy = vproxy(initialState); const createProxy = (target: any, parentPath: PropType[] = []): any => { if (!schemaMeta.has(zodSchema)) { @@ -87,104 +87,104 @@ export const schema = >( safeParse, parseAsync, errorHandler, - initialState - }) + initialState, + }); } return new Proxy(target, { get(target, prop, receiver) { - const value = Reflect.get(target, prop, receiver) + const value = Reflect.get(target, prop, receiver); if (isObject(value)) { if ((value as any)[isProxySymbol]) { - return value + return value; } else { - const newPath = parentPath.concat(prop) - pathList.set(value, newPath) - const proxyObj = createProxy(value, newPath) - proxyObj[isProxySymbol] = true - return proxyObj + const newPath = parentPath.concat(prop); + pathList.set(value, newPath); + const proxyObj = createProxy(value, newPath); + proxyObj[isProxySymbol] = true; + return proxyObj; } } else { - const pathToGet = [...(pathList.get(target) || []), prop] - return _.get(valtioProxy, pathToGet, value) + const pathToGet = [...(pathList.get(target) || []), prop]; + return _.get(valtioProxy, pathToGet, value); } }, set(target, prop, value, receiver) { const originalObject = schemaMeta.get(zodSchema)! - .initialState as z.infer + .initialState as z.infer; - const objectToValidate = _.cloneDeep(originalObject) - const pathToSet = [...(pathList.get(target) || []), prop] + const objectToValidate = _.cloneDeep(originalObject); + const pathToSet = [...(pathList.get(target) || []), prop]; - _.set(objectToValidate, pathToSet, value) - console.log(getVersion(valtioProxy)) + _.set(objectToValidate, pathToSet, value); + console.log(getVersion(valtioProxy)); const handleAsyncParse = async () => { try { - const parsedValue = await zodSchema.parseAsync(objectToValidate) - _.set(valtioProxy, pathToSet, value) - console.log(getVersion(valtioProxy)) - Reflect.set(target, prop, value, receiver) - return true + const parsedValue = await zodSchema.parseAsync(objectToValidate); + _.set(valtioProxy, pathToSet, value); + console.log(getVersion(valtioProxy)); + Reflect.set(target, prop, value, receiver); + return true; } catch (error) { - errorHandler(error) - console.log('safepafse in error', safeParse) + errorHandler(error); + console.log('safepafse in error', safeParse); if (!safeParse) { - throw error + throw error; } - return true + return true; } - } + }; const handleSyncParse = () => { - console.log('safeParse', safeParse) + console.log('safeParse', safeParse); try { if (safeParse) { - const result = zodSchema.safeParse(objectToValidate) + const result = zodSchema.safeParse(objectToValidate); if (result.success) { - _.set(valtioProxy, pathToSet, value) - Reflect.set(target, prop, value, receiver) - return true + _.set(valtioProxy, pathToSet, value); + Reflect.set(target, prop, value, receiver); + return true; } else { - errorHandler(result.error) + errorHandler(result.error); // need to return true here to prevent an error from being thrown // ifrom the proxy not updating the value - return true + return true; } } else { - const parsedValue = zodSchema.parse(objectToValidate) - _.set(valtioProxy, pathToSet, value) - Reflect.set(target, prop, value, receiver) - return true + const parsedValue = zodSchema.parse(objectToValidate); + _.set(valtioProxy, pathToSet, value); + Reflect.set(target, prop, value, receiver); + return true; } } catch (error) { - errorHandler(error) + errorHandler(error); if (!safeParse) { - throw error + throw error; } - return false + return false; } - } + }; if (parseAsync) { handleAsyncParse().catch((error) => { - errorHandler(error) + errorHandler(error); if (!safeParse) { - throw error + throw error; } - }) - return true + }); + return true; } else { - return handleSyncParse() + return handleSyncParse(); } - } - }) - } + }, + }); + }; - const store = createProxy(valtioProxy) - store[valtioStoreSymbol] = valtioProxy + const store = createProxy(valtioProxy); + store[valtioStoreSymbol] = valtioProxy; - return store - } - return { proxy } -} + return store; + }; + return { proxy }; +};