Skip to content

Commit fdca26e

Browse files
author
Krzysztof Borowy
committed
api
1 parent 1e195df commit fdca26e

12 files changed

+2000
-46
lines changed

.config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"strict": true,
1313
"moduleResolution": "node",
1414
"resolveJsonModule": true,
15+
"strictNullChecks": true,
1516
"allowJs": true,
1617
"checkJs": true,
1718
"allowSyntheticDefaultImports": true,
File renamed without changes.

packages/api/jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

packages/core/package.json renamed to packages/api/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@react-native-async-storage/core",
2+
"name": "@react-native-async-storage/api",
33
"version": "0.0.0",
44
"description": "Core API of Async Storage",
55
"source": "src/index.ts",
@@ -17,7 +17,8 @@
1717
"prepack": "yarn build",
1818
"build": "bob build",
1919
"test:lint": "eslint src/**",
20-
"test:ts": "tsc --noEmit"
20+
"test:ts": "tsc --noEmit",
21+
"test:jest": "jest"
2122
},
2223
"keywords": [
2324
"react-native",
@@ -30,8 +31,11 @@
3031
"author": "Krzysztof Borowy <[email protected]>",
3132
"license": "MIT",
3233
"devDependencies": {
34+
"@types/jest": "29.5.4",
3335
"eslint": "8.26.0",
36+
"jest": "29.5.0",
3437
"react-native-builder-bob": "0.20.0",
38+
"ts-jest": "29.1.1",
3539
"typescript": "4.9.5"
3640
},
3741
"react-native-builder-bob": {

packages/api/src/AsyncStorage.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { StorageKeys, StorageModel } from "./StorageModel";
2+
import { StorageExtension } from "./StorageExtension";
3+
4+
/**
5+
* AsyncStorage Interface
6+
* Provides methods for managing asynchronous storage operations.
7+
* @typeParam S - type of the storage model.
8+
* @typeParam E - type of the storage extension, or unknown, if no extension is provided.
9+
*/
10+
export interface AsyncStorage<
11+
S extends StorageModel,
12+
E extends StorageExtension | unknown = unknown
13+
> {
14+
/**
15+
* Retrieves a single item from storage based on the provided key.
16+
* @param key - The key to identify the item within the storage.
17+
* @returns Promise resolving to the value associated with the key,
18+
* or null if the key does not exist.
19+
*/
20+
getItem<K extends StorageKeys<S>>(key: K): Promise<S[K]>;
21+
22+
/**
23+
* Sets the value of the specified item in the storage.
24+
* @param key - The key under which the value should be stored.
25+
* @param value - The value to be stored.
26+
* @returns Promise that resolves when the operation is completed.
27+
*/
28+
setItem<K extends StorageKeys<S>>(key: K, value: S[K]): Promise<void>;
29+
30+
/**
31+
* Removes the item from storage identified by the provided key.
32+
* @param key - The key of the item to be removed.
33+
* @returns Promise that resolves when the operation is completed.
34+
*/
35+
removeItem<K extends StorageKeys<S>>(key: K): Promise<void>;
36+
37+
/**
38+
* Retrieves multiple items from storage based on the provided keys.
39+
* @param keys - An array of keys to identify the items to be retrieved.
40+
* @returns Promise resolving to an object with key-value pairs,
41+
* where the values are associated with the keys,
42+
* or null if a key does not exist.
43+
*/
44+
getMany<K extends StorageKeys<S>>(keys: K[]): Promise<{ [k in K]: S[k] }>;
45+
46+
/**
47+
* Sets multiple items in the storage.
48+
* @param entries - An object containing key-value pairs to be stored.
49+
* @returns Promise that resolves when the operation is completed.
50+
*/
51+
setMany<K extends StorageKeys<S>>(entries: {
52+
[k in K]: S[k];
53+
}): Promise<void>;
54+
55+
/**
56+
* Removes multiple items from storage based on the provided keys.
57+
* @param keys - An array of keys identifying the items to be removed.
58+
* @returns Promise that resolves when the operation is completed.
59+
*/
60+
removeMany<K extends StorageKeys<S>>(keys: K[]): Promise<void>;
61+
62+
/**
63+
* Clears all the data from the storage.
64+
* @returns Promise that resolves when the operation is completed.
65+
*/
66+
clear(): Promise<void>;
67+
68+
/**
69+
* Represents the extension for providing additional functionality
70+
* beyond the standard storage interface.
71+
* See {@link StorageExtension} for more details.
72+
*/
73+
ext: E;
74+
}

packages/api/src/StorageExtension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* The StorageExtension type serves as a means to extend the functionalities of the
3+
* core interface beyond its operations. It acts as a placeholder for implementing
4+
* additional methods.
5+
*/
6+
export type StorageExtension = {};

packages/api/src/StorageModel.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* A type used to define the structure and shape of the data to be stored.
3+
*/
4+
export type StorageModel<
5+
T extends Record<string, unknown> | unknown = unknown
6+
> = {
7+
[K in keyof T]: NonNullable<T[K]> | null;
8+
};
9+
10+
/**
11+
* A utility type to extract key.
12+
*/
13+
export type StorageKeys<T> = keyof T;

packages/api/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { AsyncStorage } from "./AsyncStorage";
2+
export { StorageExtension } from "./StorageExtension";
3+
export * from "./StorageModel";

packages/api/tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "../../.config/tsconfig.base.json",
3+
"include": ["./src/**/*", "./example/**/*"],
4+
"compilerOptions": {
5+
"types": ["jest"]
6+
}
7+
}

packages/core/src/index.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/core/tsconfig.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)