Custom session storage

The SDK comes with a built-in set of Storage providers: MemoryStorage and LocalStorage. In NodeJS environments (server side), only Memory is available.

To override the default storage for your platform, pass it in the Session initialization:

import Bitcapital, { MemoryStorage, Session, Storage1Util } from "@bitcapital/core-sdk";

// Overriding storage driver with built-in Memory Storage
// The name used here is used for logging purposes only
const driver = new StorageUtil('session', new MemoryStorage());

// Initialize a custom session with desired storage
const session = Session.initialize({
  storage: driver,
  oauth: {
    baseURL: config.baseURL,
    clientId: config.clientId,
    clientSecret: config.clientSecret,
  },
  http: {
    baseURL: config.baseURL,
    clientId: config.clientId,
    clientSecret: config.clientSecret,
  }
});

// Initialize bitcapital service with specified credentials
const bitcapital = Bitcapital.initialize({
  // Pass your custom session instance
  session,
  // Other initialization configs...
  baseURL: config.baseURL,
  clientId: config.clientId,
  clientSecret: config.clientSecret,
});

Using a custom storage

To implement another Storage mechanism, extend the StorageUtilEngine interface and register it in the Session initialization as shown before.

import { StorageUtilEngine } from "@bitcapital/core-sdk";

export default class MemoryStorage implements StorageUtilEngine {
  protected data: any = {};

  async setItem(key: string, value: string): Promise<any> {
    this.data[key] = value;
    return value;
  }
  async getItem(key: string): Promise<any> {
    return this.data[key];
  }
  async removeItem(key: string): Promise<void> {
    delete this.data[key];
  }
  async clear(): Promise<void> {
    this.data = {};
  }
}

Did this page help you?