Advertisement
crutch12

TypedStorage

Aug 17th, 2023 (edited)
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // implementation
  2.  
  3. class TypedStorage {
  4.     constructor({ prefix }) {
  5.  
  6.     }
  7.  
  8.     addSchema(key, schema, fallback: (parsed) => typeof schema) {
  9.  
  10.     }
  11.  
  12.     // invalidate if value doesn't pass validation
  13.     getItem(key) {
  14.  
  15.     }
  16.  
  17.     setItem(key, fallback) {
  18.  
  19.     }
  20. }
  21.  
  22. // usage
  23.  
  24. const UserSchema = zod.object({ name: zod.string().required(), age: zod.number() })
  25.  
  26. const typedStorage = new TypedStorage({ prefix: 'myStore' })
  27.     // key, schema, fallback
  28.     .addSchema('users', zod.array(UserSchema), () => [])
  29.  
  30. const users: typeof UserSchema = [
  31.     {
  32.         name: 'asdasd',
  33.         age: 12,
  34.     }
  35. ]
  36.  
  37. typedStorage.setItem('users', users)
  38.  
  39. const parsedUsers = typedStorage.getItem('users') // typeof UserSchema[]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement