Key-Value State Operations
The State provider has key-value operations for storing state.
Set a Value
Using a key, you can store a value for retrieval later.
Method Signature
set<T>(key: string, value: T)
Setting a Value
const session = vcr.createSession();
const state = new State(session);
await state.set("obj", { "foo": bar });
Increment a Value
If you have stored an integer you can increment it by a specified amount.
Method Signature
increment<T>(key: string, count: number)
Incrementing a Value
await state.increment("count", 1);
Decrement a Value
If you have stored an integer you can decrement it by a specified amount.
Method Signature
decrement<T>(key: string, count: number)
Decrementing a Value
await state.decrement("count", 1);
Get a Value
Using a key, you can fetch a value previously stored.
Method Signature
get<T>(key: string)
Getting a Value
const object = await state.get("obj");
Expire a Value
Using a key, you can expire a value previously stored after a specified number of seconds.
Method Signature
expire(key: string, seconds: number, option?: EXPIRE_OPTION)
Types
expire supports a set of options(EXPIRE_OPTION):
NX: Set expiry only when the key has no expiryXX: Set expiry only when the key has an existing expiryGT: Set expiry only when the new expiry is greater than the current oneLT: Set expiry only when the new expiry is less than the current one
Expiring a Value
await state.expire("obj", 200);
Delete a Value
Using a key, you can delete a value previously stored.
Method Signature
delete<T>(key: string)
Deleting a Value
await state.delete("obj");