-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Krzysztof Borowy
authored
Nov 22, 2023
1 parent
bd4c170
commit 622d505
Showing
126 changed files
with
1,451 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { StorageExtension } from "../src"; | ||
|
||
export interface MyExampleExtension extends StorageExtension { | ||
double: (num: number) => Promise<number>; | ||
|
||
uppercase: (text: string) => string; | ||
|
||
key: string; | ||
} | ||
|
||
export class ExampleExtension implements MyExampleExtension { | ||
key = "my-example-storage"; | ||
|
||
double = async (num: number) => num * 2; | ||
|
||
uppercase = (text: string): string => text.toUpperCase(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { AsyncStorage, StorageKeys, StorageModel } from "../src"; | ||
import { ExampleExtension, MyExampleExtension } from "./ExampleExtension"; | ||
|
||
type MyModel = StorageModel<{ | ||
age: number; | ||
name: string; | ||
likes: boolean[]; | ||
}>; | ||
|
||
// @ts-ignore | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
class ExampleStorage implements AsyncStorage<MyModel, MyExampleExtension> { | ||
private storage: MyModel = { | ||
age: null, | ||
name: null, | ||
likes: null, | ||
}; | ||
|
||
getItem = async <K extends keyof MyModel>(key: K): Promise<MyModel[K]> => { | ||
return this.storage[key]; | ||
}; | ||
|
||
setItem = async <K extends StorageKeys<MyModel>>( | ||
key: K, | ||
value: MyModel[K] | ||
): Promise<void> => { | ||
this.storage[key] = value; | ||
}; | ||
|
||
removeItem = async <K extends StorageKeys<MyModel>>( | ||
key: K | ||
): Promise<void> => { | ||
this.storage[key] = null; | ||
}; | ||
|
||
getMany = async <K extends StorageKeys<MyModel>>( | ||
keys: K[] | ||
): Promise<{ [k in K]: MyModel[k] }> => { | ||
return keys.reduce((entries, key) => { | ||
entries[key] = this.storage[key] ?? null; | ||
return entries; | ||
}, {} as { [k in K]: MyModel[k] }); | ||
}; | ||
|
||
setMany = async <K extends StorageKeys<MyModel>>(entries: { | ||
[k in K]: MyModel[k]; | ||
}): Promise<void> => { | ||
Object.entries(entries).forEach((entry) => { | ||
const key = entry[0] as K; | ||
this.storage[key] = entry[1] as MyModel[K]; | ||
}); | ||
}; | ||
|
||
removeMany = async <K extends StorageKeys<MyModel>>( | ||
keys: K[] | ||
): Promise<void> => { | ||
keys.forEach((k) => { | ||
this.storage[k] = null; | ||
}); | ||
}; | ||
|
||
clear = async (): Promise<void> => { | ||
this.storage.age = null; | ||
this.storage.name = null; | ||
this.storage.likes = null; | ||
}; | ||
|
||
ext: MyExampleExtension = new ExampleExtension(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "@react-native-async-storage/core", | ||
"name": "@react-native-async-storage/api", | ||
"version": "0.0.0", | ||
"description": "Core API of Async Storage", | ||
"source": "src/index.ts", | ||
|
@@ -17,7 +17,8 @@ | |
"prepack": "yarn build", | ||
"build": "bob build", | ||
"test:lint": "eslint src/**", | ||
"test:ts": "tsc --noEmit" | ||
"test:ts": "tsc --noEmit", | ||
"test:jest": "jest" | ||
}, | ||
"keywords": [ | ||
"react-native", | ||
|
@@ -30,8 +31,11 @@ | |
"author": "Krzysztof Borowy <[email protected]>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/jest": "29.5.4", | ||
"eslint": "8.26.0", | ||
"jest": "29.5.0", | ||
"react-native-builder-bob": "0.20.0", | ||
"ts-jest": "29.1.1", | ||
"typescript": "4.9.5" | ||
}, | ||
"react-native-builder-bob": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { StorageKeys, StorageModel } from "./StorageModel"; | ||
import type { StorageExtension } from "./StorageExtension"; | ||
|
||
/** | ||
* AsyncStorage Interface | ||
* Provides methods for managing asynchronous storage operations. | ||
* @typeParam S - type of the storage model. | ||
* @typeParam E - type of the storage extension, or unknown, if no extension is provided. | ||
*/ | ||
export interface AsyncStorage< | ||
S extends StorageModel, | ||
E extends StorageExtension | unknown = unknown | ||
> { | ||
/** | ||
* Retrieves a single item from storage based on the provided key. | ||
* @param key - The key to identify the item within the storage. | ||
* @returns Promise resolving to the value associated with the key, | ||
* or null if the key does not exist. | ||
*/ | ||
getItem<K extends StorageKeys<S>>(key: K): Promise<S[K]>; | ||
|
||
/** | ||
* Sets the value of the specified item in the storage. | ||
* @param key - The key under which the value should be stored. | ||
* @param value - The value to be stored. | ||
* @returns Promise that resolves when the operation is completed. | ||
*/ | ||
setItem<K extends StorageKeys<S>>(key: K, value: S[K]): Promise<void>; | ||
|
||
/** | ||
* Removes the item from storage identified by the provided key. | ||
* @param key - The key of the item to be removed. | ||
* @returns Promise that resolves when the operation is completed. | ||
*/ | ||
removeItem<K extends StorageKeys<S>>(key: K): Promise<void>; | ||
|
||
/** | ||
* Retrieves multiple items from storage based on the provided keys. | ||
* @param keys - An array of keys to identify the items to be retrieved. | ||
* @returns Promise resolving to an object with key-value pairs, | ||
* where the values are associated with the keys, | ||
* or null if a key does not exist. | ||
*/ | ||
getMany<K extends StorageKeys<S>>(keys: K[]): Promise<{ [k in K]: S[k] }>; | ||
|
||
/** | ||
* Sets multiple items in the storage. | ||
* @param entries - An object containing key-value pairs to be stored. | ||
* @returns Promise that resolves when the operation is completed. | ||
*/ | ||
setMany<K extends StorageKeys<S>>(entries: { | ||
[k in K]: S[k]; | ||
}): Promise<void>; | ||
|
||
/** | ||
* Removes multiple items from storage based on the provided keys. | ||
* @param keys - An array of keys identifying the items to be removed. | ||
* @returns Promise that resolves when the operation is completed. | ||
*/ | ||
removeMany<K extends StorageKeys<S>>(keys: K[]): Promise<void>; | ||
|
||
/** | ||
* Clears all the data from the storage. | ||
* @returns Promise that resolves when the operation is completed. | ||
*/ | ||
clear(): Promise<void>; | ||
|
||
/** | ||
* Represents the extension for providing additional functionality | ||
* beyond the standard storage interface. | ||
* See {@link StorageExtension} for more details. | ||
*/ | ||
ext: E; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* The StorageExtension type serves as a means to extend the functionalities of the | ||
* core interface beyond its operations. It acts as a placeholder for implementing | ||
* additional methods. | ||
*/ | ||
export type StorageExtension = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* A type used to define the structure and shape of the data to be stored. | ||
*/ | ||
export type StorageModel< | ||
T extends Record<string, unknown> | unknown = unknown | ||
> = { | ||
[K in keyof T]: NonNullable<T[K]> | null; | ||
}; | ||
|
||
/** | ||
* A utility type to extract key. | ||
*/ | ||
export type StorageKeys<T> = keyof T; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { AsyncStorage } from "./AsyncStorage"; | ||
export type { StorageExtension } from "./StorageExtension"; | ||
export type { StorageKeys, StorageModel } from "./StorageModel"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../.config/tsconfig.base.json", | ||
"include": ["./src/**/*", "./example/**/*"], | ||
"compilerOptions": { | ||
"types": ["jest"] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ build/ | |
.gradle | ||
local.properties | ||
*.iml | ||
!android/gradlew* | ||
|
||
# Visual Studio | ||
# | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.