Storage
The Storage API provides key–value local data storage.
Try it online
Import
ts
import woo from 'mini-sdk'
// 或
import { storage } from 'mini-sdk'API reference
setStorage
Sets a key–value pair.
ts
woo.setStorage(options: SetStorageOptions): Promise<void>Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Key name |
data | unknown | Yes | Value (any serializable data) |
ts
// 存储字符串
await woo.setStorage({ key: 'username', data: 'Alice' })
// 存储对象
await woo.setStorage({
key: 'userProfile',
data: { name: 'Alice', age: 25, tags: ['dev', 'design'] },
})
// 存储数组
await woo.setStorage({
key: 'recentSearches',
data: ['mini-sdk', 'bridge', 'router'],
})getStorage
Reads the value for a key.
ts
woo.getStorage<T>(options: GetStorageOptions<T>): Promise<GetStorageResult<T>>Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Key name |
Return value:
ts
interface GetStorageResult<T = unknown> {
data: T // 存储的值
}ts
// 读取字符串
const { data: username } = await woo.getStorage({ key: 'username' })
console.log(username) // "Alice"
// 带泛型的类型安全读取
interface UserProfile {
name: string
age: number
tags: string[]
}
const { data: profile } = await woo.getStorage<UserProfile>({
key: 'userProfile',
})
console.log(profile.name) // "Alice"removeStorage
Removes a key.
ts
woo.removeStorage(options: RemoveStorageOptions): Promise<void>Parameters:
| Field | Type | Required | Description |
|---|---|---|---|
key | string | Yes | Key to remove |
ts
await woo.removeStorage({ key: 'username' })clearStorage
Clears all stored data.
ts
woo.clearStorage(options?: ClearStorageOptions): Promise<void>ts
await woo.clearStorage()WARNING
clearStorage removes all storage for the mini program. Use with care.
End-to-end example: token management
vue
<script setup>
import { ref, onMounted } from 'vue'
import woo from 'mini-sdk'
const TOKEN_KEY = 'auth_token'
const USER_KEY = 'user_info'
const user = ref(null)
async function saveAuth(token, userInfo) {
await Promise.all([
woo.setStorage({ key: TOKEN_KEY, data: token }),
woo.setStorage({ key: USER_KEY, data: userInfo }),
])
}
async function loadUser() {
try {
const { data } = await woo.getStorage({ key: USER_KEY })
user.value = data
} catch {
user.value = null
}
}
async function logout() {
await Promise.all([
woo.removeStorage({ key: TOKEN_KEY }),
woo.removeStorage({ key: USER_KEY }),
])
user.value = null
}
onMounted(loadUser)
</script>
<template>
<div v-if="user">{{ user.name }}</div>
<button v-else @click="saveAuth('token', { name: 'Alice' })">登录</button>
<button @click="logout">退出</button>
</template>tsx
import { useState, useEffect } from 'react'
import woo from 'mini-sdk'
const TOKEN_KEY = 'auth_token'
const USER_KEY = 'user_info'
function AuthManager() {
const [user, setUser] = useState(null)
useEffect(() => {
woo.getStorage({ key: USER_KEY })
.then(({ data }) => setUser(data))
.catch(() => setUser(null))
}, [])
async function login() {
await Promise.all([
woo.setStorage({ key: TOKEN_KEY, data: 'token' }),
woo.setStorage({ key: USER_KEY, data: { name: 'Alice' } }),
])
setUser({ name: 'Alice' })
}
async function logout() {
await Promise.all([
woo.removeStorage({ key: TOKEN_KEY }),
woo.removeStorage({ key: USER_KEY }),
])
setUser(null)
}
return (
<>
{user ? <div>{user.name}</div> : <button onClick={login}>登录</button>}
<button onClick={logout}>退出</button>
</>
)
}Type definitions
ts
interface SetStorageOptions {
key: string
data: unknown
success?: () => void
fail?: (err: BridgeError) => void
complete?: () => void
}
interface GetStorageOptions<T = unknown> {
key: string
success?: (res: GetStorageResult<T>) => void
fail?: (err: BridgeError) => void
complete?: () => void
}
interface GetStorageResult<T = unknown> {
data: T
}
interface RemoveStorageOptions {
key: string
success?: () => void
fail?: (err: BridgeError) => void
complete?: () => void
}
interface ClearStorageOptions {
success?: () => void
fail?: (err: BridgeError) => void
complete?: () => void
}API quick reference
| Method | Description | Signature |
|---|---|---|
setStorage | Store data | ({ key, data }) → Promise<void> |
getStorage | Read data | ({ key }) → Promise<{ data }> |
removeStorage | Remove a key | ({ key }) → Promise<void> |
clearStorage | Clear all data | () → Promise<void> |
getStorageInfo | Get storage info | () → Promise<{ keys, currentSize, limitSize }> |
setStorageSync | Synchronous set | (key, data) → void |
getStorageSync | Synchronous get | (key) → any |
removeStorageSync | Synchronous remove | (key) → void |
clearStorageSync | Synchronous clear | () → void |