From 6f663936924b9c16a808a706c7d26d41177dfaea Mon Sep 17 00:00:00 2001 From: Michael Sweeney Date: Sat, 31 Aug 2024 22:33:01 -0700 Subject: [PATCH] ran prettier --- src/index.ts | 186 +++++++++++++++++++++++++-------------------------- 1 file changed, 93 insertions(+), 93 deletions(-) diff --git a/src/index.ts b/src/index.ts index d56636b..6732cbb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,99 +1,99 @@ /* eslint-disable */ -import { z, ZodType } from 'zod' -import { proxy as vproxy, useSnapshot as vsnap } from 'valtio' -import _ from 'lodash' +import { z, ZodType } from 'zod'; +import { proxy as vproxy, useSnapshot as vsnap } 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[]>() +type PropType = string | number | symbol; +const schemaMeta = new WeakMap, SchemaMeta>(); +const pathList = new WeakMap<{}, PropType[]>(); type SchemaReturn> = { proxy: { - (initialState: any, config?: SchemaConfig): ValtioProxy> - } -} + (initialState: any, config?: SchemaConfig): ValtioProxy>; + }; +}; function updateObjectAtPath(obj: any, path: PropType[], newValue: any) { - let stack = [...path] - let object = obj + let stack = [...path]; + let object = obj; while (stack.length > 1) { - const key = stack.shift() - if (key === undefined) return + const key = stack.shift(); + if (key === undefined) return; if (!object[key] || typeof object[key] !== 'object') { - object[key] = {} + object[key] = {}; } - object = object[key] + object = object[key]; } - const lastKey = stack.shift() - if (lastKey !== undefined) object[lastKey] = newValue + const lastKey = stack.shift(); + if (lastKey !== undefined) object[lastKey] = newValue; } -const valtioStoreSymbol = Symbol('valtioStore') +const valtioStoreSymbol = Symbol('valtioStore'); export const useSnapshot = (store: any) => { - return vsnap(store[valtioStoreSymbol]) -} + return vsnap(store[valtioStoreSymbol]); +}; export const schema = >( - zodSchema: T + zodSchema: T, ): SchemaReturn => { - let valtioProxy: any + let valtioProxy: any; 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 if (parseAsync) { zodSchema.parseAsync(initialState).catch((e) => { - throw e - }) + throw e; + }); } else { - zodSchema.parse(initialState) + zodSchema.parse(initialState); } - valtioProxy = vproxy(initialState) + valtioProxy = vproxy(initialState); const createProxy = (target: any, parentPath: PropType[] = []): any => { if (!schemaMeta.has(zodSchema)) { @@ -101,91 +101,91 @@ 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)) { - const newPath = parentPath.concat(prop) - pathList.set(value, newPath) - return createProxy(value, newPath) + const newPath = parentPath.concat(prop); + pathList.set(value, newPath); + return createProxy(value, newPath); } else { - return value + return value; } }, set(target, prop, value, receiver) { const originalObject = schemaMeta.get(zodSchema)! - .initialState as z.infer + .initialState as z.infer; - const objectToValidate = JSON.parse(JSON.stringify(originalObject)) - const path = (pathList.get(target) || []).concat(prop) + const objectToValidate = JSON.parse(JSON.stringify(originalObject)); + const path = (pathList.get(target) || []).concat(prop); - updateObjectAtPath(objectToValidate, path, value) + updateObjectAtPath(objectToValidate, path, value); const handleAsyncParse = async () => { try { - const parsedValue = await zodSchema.parseAsync(objectToValidate) - _.set(valtioProxy, value, path) - Reflect.set(target, prop, value, receiver) - return true + const parsedValue = await zodSchema.parseAsync(objectToValidate); + _.set(valtioProxy, value, path); + Reflect.set(target, prop, value, receiver); + return true; } catch (error) { - errorHandler(error) + errorHandler(error); if (!safeParse) { - throw error + throw error; } - return false + return false; } - } + }; const handleSyncParse = () => { try { if (safeParse) { - const result = zodSchema.safeParse(objectToValidate) + const result = zodSchema.safeParse(objectToValidate); if (result.success) { - valtioProxy[prop] = value - Reflect.set(target, prop, value, receiver) - return true + valtioProxy[prop] = value; + Reflect.set(target, prop, value, receiver); + return true; } else { - errorHandler(result.error) - return false + errorHandler(result.error); + return false; } } else { - const parsedValue = zodSchema.parse(objectToValidate) - Reflect.set(target, prop, value, receiver) - valtioProxy[prop] = value - return true + const parsedValue = zodSchema.parse(objectToValidate); + Reflect.set(target, prop, value, receiver); + valtioProxy[prop] = value; + 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 }; +};