From 71f687e34572b5b5d5256bc6b0e8267ecde2a171 Mon Sep 17 00:00:00 2001 From: redstonekasi Date: Tue, 17 Jan 2023 00:52:20 +0100 Subject: [PATCH] [Types] Type MMKVManager --- src/def.d.ts | 34 ++++++++++++++++++++++++++++++++++ src/lib/storage.ts | 8 ++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/def.d.ts b/src/def.d.ts index 5a9d10e..44671df 100644 --- a/src/def.d.ts +++ b/src/def.d.ts @@ -81,6 +81,40 @@ interface RNConstants extends _RN.PlatformConstants { systemName: string; } +/** + * A key-value storage based upon `SharedPreferences` on Android. + * + * These types are based on Android though everything should be the same between + * platforms. + */ +interface MMKVManager { + /** + * Get the value for the given `key`, or null + * @param key The key to fetch + */ + getItem: (key: string) => Promise; + /** + * Deletes the value for the given `key` + * @param key The key to delete + */ + removeItem: (key: string) => void; + /** + * Sets the value of `key` to `value` + */ + setItem: (key: string, value: string) => void; + /** + * Goes through every item in storage and returns it, excluding the + * keys specified in `exclude`. + * @param exclude A list of items to exclude from result + */ + refresh: (exclude: string[]) => Promise>; + /** + * You will be murdered if you use this function. + * Clears ALL of Discord's settings. + */ + clear: () => void; +} + type Indexable = { [index: string]: Type } interface VendettaObject { diff --git a/src/lib/storage.ts b/src/lib/storage.ts index 3980092..8094f55 100644 --- a/src/lib/storage.ts +++ b/src/lib/storage.ts @@ -1,9 +1,9 @@ -import { Indexable } from "@types"; +import { Indexable, MMKVManager } from "@types"; import { ReactNative as RN } from "@metro/hoist"; // Discord's custom special storage sauce // TODO: Type this -const MMKVManager = RN.NativeModules.MMKVManager; +const MMKVManager = RN.NativeModules.MMKVManager as MMKVManager; // TODO: React hook? // TODO: Clean up types, if necessary @@ -34,7 +34,7 @@ export default function createStorage(storeName: string, onRestore?: (parsed: } } - MMKVManager.getItem(storeName).then(async function (v: any) { + MMKVManager.getItem(storeName).then(async function (v) { if (!v) return; const parsed: T & Indexable = JSON.parse(v); @@ -46,4 +46,4 @@ export default function createStorage(storeName: string, onRestore?: (parsed: }) return new Proxy(internalStore, proxyValidator) as T; -} \ No newline at end of file +}