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

@twitter-api-v2/plugin-cache-core

v1.0.0

Published

Core package to make request caching plugins for twitter-api-v2

Downloads

98

Readme

@twitter-api-v2/plugin-cache-core

Provide core utils to create a request cache plugin for twitter-api-v2

This package is meant to be used by other plugin packages, and can't be used alone.

Usage

import { TwitterApiCachePluginCore } from '@twitter-api-v2/plugin-cache-core';

class MyRequestCachePlugin extends TwitterApiCachePluginCore {
  // You must write those methods with the following signatures:
  protected hasKey(key: string): boolean | Promise<boolean>;

  protected getKey(key: string): TwitterResponse<any> | undefined | Promise<TwitterResponse<any> | undefined>;

  protected setKey(key: string, response: TwitterResponse<any>): void | Promise<void>;
}

then, use your plugin into twitter-api-v2:

import { TwitterApi } from 'twitter-api-v2';
import { MyRequestCachePlugin } from './your-plugin';

const client = new TwitterApi(yourKeys, { plugins: [new MyRequestCachePlugin()] });

Defaults and customization

By default

  • a key is generated from a request using request URL and parameters
  • cached requests are ONLY requests for GET method
  • request key is not scoped by user/token, so if you're using a shared key/value storage (like file system or Redis), cache will be shared across all client instances

Defaut settings customization

Key generation

If you just want to add a prefix to the key, you can customize this by overriding the .getKeyPrefix() method:

class MyRequestCachePlugin extends TwitterApiCachePluginCore {
  protected getKeyPrefix() {
    return 'prefix-';
  }
}

If you want to generate key by yourself, override the .getRequestKey() method:

class MyRequestCachePlugin extends TwitterApiCachePluginCore {
  protected getRequestKey({ url, params }: ITwitterApiBeforeRequestConfigHookArgs) {
    const method = params.method.toUpperCase();
    // Ignore request params/query, just use URL + method
    return this.getHashOfString(`${method} ${url.toString()}`);
  }
}

Cached requests are ONLY requests for GET method

You can customize this by overriding the .isRequestCacheable() method. If you accept other methods than GET, you might need to rewrite the .getRequestKey() method, because the base one ignores a possible request body.

class MyRequestCachePlugin extends TwitterApiCachePluginCore {
  protected isRequestCacheable(args: ITwitterApiBeforeRequestConfigHookArgs) {
    const method = args.params.method.toUpperCase();
    // Accept GET and DELETE
    return method === 'GET' || method === 'DELETE';
  }
}

Request key is not scoped by user/token

You can customize this, for example, by overriding the .getKeyPrefix() method and using a user ID as key prefix:

class UserScopedRequestCachePlugin extends TwitterApiCachePluginCore {
  constructor(protected userId: string) {
    super();
  }

  protected getKeyPrefix() {
    return this.userId;
  }
}

const client = new TwitterApi(user.twitterKeys, { plugins: [new UserScopedRequestCachePlugin(user.id)] });

If you want to have unique cache per plugin instance, but your storage is shared, you can generate a unique prefix in constructor:

import * as crypto from 'crypto';

class UniqueRequestCachePlugin extends TwitterApiCachePluginCore {
  protected uniqueId: string;

  constructor() {
    super();
    this.uniqueId = crypto.randomBytes(16).toString('hex');
  }

  protected getKeyPrefix() {
    return this.uniqueId;
  }
}

const client = new TwitterApi(yourKeys, { plugins: [new UniqueRequestCachePlugin()] });