npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@coolgk/token

v2.0.8

Published

an expirable, revocable, renewable token with data storage

Downloads

43

Readme

@coolgk/token

a javascript / typescript module

npm install @coolgk/token

an expirable, revocable, renewable token with data storage

Report bugs here: https://github.com/coolgk/node-utils/issues

Examples

import { Token } from '@coolgk/token';
import { createClient } from 'redis';
// OR
// const { Token } = require('@coolgk/token');
// const createClient = require('redis').createClient;

(async () => {

    const redisClient = createClient({
        host: 'localhost',
        port: 6379,
        password: '----'
    });

    const token = new Token({
        redisClient: redisClient,
        expiry: 5,
        token: 'abcde'
    });

    console.log(
        await token.verify()
    ) // false

    await token.renew();

    console.log(
        await token.verify()
    ) // true

    console.log(
        await token.get('var1');
    ); // null

    console.log(
        await token.getAll()
    ); // {}

    await token.set('var1', {a: 'var1', b: false});

    console.log(
        await token.get('var1');
    ); // {a: 'var1', b: false}

    await token.set('var2', 'string var 2');

    console.log(
        await token.getAll()
    ); // { var1: { a: 'var1', b: false }, var2: 'string var 2' }

    await token.delete('var2');

    console.log(
        await token.get('var2');
    ); // null

    console.log(
        await token.getAll()
    ); // { var1: { a: 'var1', b: false } }

    await token.destroy();

    console.log(
        await token.verify()
    ) // false

    console.log(
        await token.get('var1');
    ); // null

    console.log(
        await token.getAll()
    ); // {}

    redisClient.quit();
})()

Classes

Constants

Token

Kind: global class

new Token(options)

| Param | Type | Default | Description | | --- | --- | --- | --- | | options | object | | | | options.token | string | | token string for creating a token object | | options.redisClient | object | | redis client from redis.createClient() | | [options.prefix] | string | "'token'" | prefix used in redis e.g. token:[TOKEN_STRING...] | | [options.expiry] | number | 0 | in seconds. 0 = never expire |

token.renew([expiry]) ⇒ promise

Kind: instance method of Token

| Param | Type | Description | | --- | --- | --- | | [expiry] | number | in seconds |

token.set(name, value) ⇒ promise

set a data field value

Kind: instance method of Token

| Param | Type | Description | | --- | --- | --- | | name | string | field name | | value | * | anything can be JSON.stringify'ed |

token.verify() ⇒ promise.<boolean>

verify if token has expired

Kind: instance method of Token

token.get(name) ⇒ promise

get the value of a data field

Kind: instance method of Token

| Param | Type | Description | | --- | --- | --- | | name | string | data field name |

token.destroy() ⇒ promise

delete the token

Kind: instance method of Token

token.delete(name) ⇒ promise

delete a data field in the token

Kind: instance method of Token

| Param | Type | Description | | --- | --- | --- | | name | string | data field name |

token.getAll() ⇒ promise.<{}>

get the values of all data fields in the token

Kind: instance method of Token

token.setToken(token)

set a new token string

Kind: instance method of Token

| Param | Type | Description | | --- | --- | --- | | token | string | new token string |

TokenError : object

Error Codes

Kind: global constant
Properties

| Name | Type | Description | | --- | --- | --- | | INVALID_TOKEN | string | invalid token string | | RESERVED_NAME | string | reserved names are used when setting token variables e.g. _timestamp | | EXPIRED_TOKEN | string | token expired or renew() has not been called |