CloudStorage
Implements functionality related to Telegram Mini Apps cloud storage.
Initialization
Component constructor accepts Telegram Mini Apps version, function to generate request identifiers for called Telegram Mini Apps methods and optional function to call them.
import { CloudStorage, postEvent } from '@tma.js/sdk';
const cloudStorage = new CloudStorage(
'6.10',
() => Math.random().toString(),
postEvent,
);
import { CloudStorage, postEvent } from '@tma.js/sdk';
const cloudStorage = new CloudStorage(
'6.10',
() => Math.random().toString(),
postEvent,
);
Setting items
To save a value in the cloud storage, it is required to use the set
method:
cloudStorage
.set('my-key', 'my-value')
.then(() => console.log('Item saved'));
cloudStorage
.set('my-key', 'my-value')
.then(() => console.log('Item saved'));
Getting items
To get values by keys, it is required to use get
method, which acquires both single string value and array of string values:
cloudStorage
.get('my-key')
.then((value) => {
console.log(value);
// Output: 'my-value'
});
cloudStorage
.get('non-existent')
.then((value) => {
console.log(value);
// Output: ''
});
cloudStorage
.get('my-key')
.then((value) => {
console.log(value);
// Output: 'my-value'
});
cloudStorage
.get('non-existent')
.then((value) => {
console.log(value);
// Output: ''
});
cloudStorage
.get(['my-key', 'non-existent'])
.then((result) => {
console.log('Result is', result);
// Output:
// {
// 'my-key': 'my-value',
// 'non-existent': ''
// }
});
cloudStorage
.get(['my-key', 'non-existent'])
.then((result) => {
console.log('Result is', result);
// Output:
// {
// 'my-key': 'my-value',
// 'non-existent': ''
// }
});
This method returns empty strings for those keys, which don't exist in the cloud storage.
Getting keys
To retrieve all registered keys in the cloud storage, it is required to use the getKeys
method:
cloudStorage
.getKeys()
.then((keys) => {
// Will be ['my-key'].
console.log('Keys are', keys);
})
cloudStorage
.getKeys()
.then((keys) => {
// Will be ['my-key'].
console.log('Keys are', keys);
})
Deleting items
To delete items in the cloud storage, it is required to use delete
method. This method allows deleting both single and multiple items:
cloudStorage
.delete('my-key')
.then(() => console.log('Key was deleted'));
cloudStorage
.delete('my-key')
.then(() => console.log('Key was deleted'));
cloudStorage
.delete(['my-key', 'another-key'])
.then(() => console.log('Keys were deleted'));
cloudStorage
.delete(['my-key', 'another-key'])
.then(() => console.log('Keys were deleted'));
Methods support
List of methods, which could be used in supports
component instance method:
delete
get
getKeys
set
import { CloudStorage } from '@tma.js/sdk';
const cloudStorage = new CloudStorage(...);
cloudStorage.supports('delete');
cloudStorage.supports('get');
cloudStorage.supports('getKeys');
cloudStorage.supports('set');
import { CloudStorage } from '@tma.js/sdk';
const cloudStorage = new CloudStorage(...);
cloudStorage.supports('delete');
cloudStorage.supports('get');
cloudStorage.supports('getKeys');
cloudStorage.supports('set');