Session Storage Agent - JS/TS
Read and write value in session storage.
Logic Type | Available |
---|---|
Generic logic | ✅ |
Aggregator logic | ✅ |
Session Storage
For each executed task, a session storage is created in LOC runtime and shared by all logic in the same data process. It is the primary approach for a task to pass data from one lofic to others. The session storage will be purged after the task complete execution.
Import and Usage
- JavaScript
- TypeScript
import {
SessionStorageAgent,
} from "@fstnetwork/loc-logic-sdk";
export async function run(ctx) {
const data = await SessionStorageAgent.get("data");
}
export async function handleError(ctx, error) {
// ... same
}
import {
GenericContext,
RailwayError,
SessionStorageAgent,
} from "@fstnetwork/loc-logic-sdk";
export async function run(ctx: GenericContext) {
const data = await SessionStorageAgent.get("data");
}
export async function handleError(ctx: GenericContext, error: RailwayError) {
// ... same
}
Class Reference
Type
SessionStorageAgent
Method: Get Session Value
async get(key: string): Promise<string | number | object | Uint8Array>
Parameter | Description |
---|---|
key | Key of session data |
Returns a value from session storage (null
if not exist).
The return type depends on how it was written into the session storage. If the value is a single number (which is valid JSON) and stored using putJson
, it would be returned as a number.
Methods: Write Session Value
- JSON
- String
- Uint8Array
async putJson(key: string, value: any): Promise<boolean>
Parameter | Description |
---|---|
key | Key of session data |
value | JavaScript JSON object |
Write a JSON value into session storage. Overwrite if already exists.
Runtime will throw a JSON parsing error if the object cannot be serialised properly to JSON.
You can try using JSON.parse(JSON.stringify(object)
to transform an object with methods into a proper JSON object, although some fields may be different or discarded.
async putString(key: string, value: string): Promise<boolean>
Parameter | Description |
---|---|
key | Key of session data |
value | string data |
Write a string value into session storage. Overwrite if already exists.
async putByteArray(key: string, value: Uint8Array | string): Promise<boolean>
Parameter | Description |
---|---|
key | Key of session data |
value | Uint8Array or string data |
Write a Uint8Array or string value into session storage. Overwrite if already exists.
Method: Delete Session Value
async delete(key: string): Promise<void>
async remove(key: string): Promise<void>
Parameter | Description |
---|---|
key | Key of session data |
Delete a value in session storage. Do nothing if not exist.
Examples
Write String Data Into Session Storage
await SessionStorageAgent.putString(
"data",
"so long, and thanks for all the fish",
);
Write JSON Data Into Session Storage
await SessionStorageAgent.putJson("data", {
name: "Arthur Dent",
age: 42,
job: {
title: "Sandwich-maker",
salary: 0,
},
quotes: [
"Is there any tea on this spaceship?",
"This must be Thursday. I never could get the hang of Thursdays.",
"Would it save you a lot of time if I just gave up and went mad now?",
],
});
A single number is valid Json thus can be saved as a JSON session data:
await SessionStorageAgent.putJson("number", 42);
It will be read as a number from SessionStorageAgent.get
.
If the object contains methods, you can force convert it to a JSON object (although non-JSON values like undefined
would be dropped):
await SessionStorageAgent.putJson(
"data",
JSON.parse(JSON.stringify(objectWithMethods)),
);
Read String Data from Session Storage
const data = await SessionStorageAgent.get("data");
Set a default value if the local data does not exist (returns null
):
const data = await SessionStorageAgent.get("data") || "default value";
Read Json Data from Session Storage
const data = await SessionStorageAgent.get("data");
// get values (undefined if not found)
const name = data?.name;
const age = data?.age;
const title = data?.job?.title;
const quotes = data?.quotes;
const firstQuote = quotes ? quotes[0] : null;
for (const quote of quotes) {
// ...
}