Standard interface to manage tokens

// Example of typescript with TokenStore and redis
import * as wppconnect from '@wppconnect-team/wppconnect';
import * as redis from 'redis';

const redisClient = redis.createClient(6379, '127.0.0.1');

// Base of interface:
const myTokenStore: wppconnect.tokenStore.TokenStore = {
getToken: (sessionName: string) => {
// Your logic here
}),
setToken: (sessionName: string, tokenData: wppconnect.tokenStore.SessionToken) => {
// Your logic here
}),
removeToken: (sessionName: string) => {
// Your logic here
}),
listTokens: () => {
// Your logic here
}),
};

// Example with redis
const myTokenStore: wppconnect.tokenStore.TokenStore = {
getToken: (sessionName: string) =>
new Promise((resolve, reject) => {
redisClient.get(sessionName, (err, reply) => {
if (err) {
return reject(err);
}
resolve(JSON.parse(reply));
});
}),
setToken: (sessionName: string, tokenData: wppconnect.tokenStore.SessionToken) =>
new Promise((resolve) => {
redisClient.set(sessionName, JSON.stringify(tokenData), (err, reply) => {
return resolve(err ? false : true);
});
}),
removeToken: (sessionName: string) =>
new Promise((resolve) => {
redisClient.del(sessionName, (err) => {
return resolve(err ? false : true);
});
}),
listTokens: () =>
new Promise((resolve) => {
redisClient.keys('*', (err, keys) => {
if (err) {
return resolve([]);
}
return resolve(keys);
});
}),
};

wppconnect.create({
session: 'mySession',
tokenStore: myTokenStore,
});

wppconnect.create({
session: 'otherSession',
tokenStore: myTokenStore,
});
interface TokenStore<T> {
    getToken(sessionName: string): T | Promise<T>;
    listTokens(): string[] | Promise<string[]>;
    removeToken(sessionName: string): boolean | Promise<boolean>;
    setToken(sessionName: string, tokenData: T): boolean | Promise<boolean>;
}

Type Parameters

Implemented by

Methods

  • Return the session data if exists

    Parameters

    • sessionName: string

      Name of session

    Returns T | Promise<T>

  • A liste of avaliable sessions

    Returns string[] | Promise<string[]>

  • Remove the session

    Parameters

    • sessionName: string

      Name of session

    Returns boolean | Promise<boolean>

    Token was removed

  • Store the session token data

    Parameters

    • sessionName: string

      Name of session

    • tokenData: T

      Session token data

    Returns boolean | Promise<boolean>