setStorageItemAsync
This function is designed to abstract away the complexities of environment-specific storage management, allowing for consistent storage operations across various platforms.
API
import { setStorageItemAsync } from "@flexnative/authentication";
Definition
An asynchronous function named setStorageItemAsync that manages storage operations depending on the environment (web or non-web) and the provided value.
setStorageItemAsync
Type: async function setStorageItemAsync(key: string, value: string | null): Promise<void>
Parameters
Name | Type | Required | Default Value | Description |
---|---|---|---|---|
key | string | true | undefined | Representing the identifier for the storage item. |
value | string | false | null | Representing the data to be stored or a command to remove the item if null |
Returns
A Promise<void>
, indicating the asynchronous nature of the function with no return value upon resolution.
Function Body
-
Environment Check:
- The function begins by checking if the current execution environment is the web.
-
Web Environment Logic:
- Try-Catch Block:
A
try-catch
block is used to safely handle any potential errors during local storage operations. - Value Check:
- If
value
isnull
, the function attempts to remove the item associated with thekey
fromlocalStorage
usinglocalStorage.removeItem(key)
. - If
value
is notnull
, it stores thekey-value
pair inlocalStorage
usinglocalStorage.setItem(key, value)
.
- If
- Error Handling:
- If an error occurs during these operations, it logs an error message to the console indicating that local storage is unavailable.
- Try-Catch Block:
A
-
Non-Web Environment Logic:
- For environments other than the web, it uses a secure storage mechanism, likely for mobile or server environments, represented by
SecureStore
. - Value Check:
- If
value
isnull
, it removes the item associated withkey
usingawait SecureStore.deleteItemAsync(key)
. - If
value
is notnull
, it saves thekey-value
pair usingawait SecureStore.setItemAsync(key, value)
.
- If
- For environments other than the web, it uses a secure storage mechanism, likely for mobile or server environments, represented by