Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
@twitter-api-v2/plugin-cache-core
Advanced tools
Core package to make request caching plugins for twitter-api-v2
Weekly downloads
Readme
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.
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()] });
key
is generated from a request using request URL and parametersIf 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()}`);
}
}
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';
}
}
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()] });
FAQs
Core package to make request caching plugins for twitter-api-v2
The npm package @twitter-api-v2/plugin-cache-core receives a total of 4 weekly downloads. As such, @twitter-api-v2/plugin-cache-core popularity was classified as not popular.
We found that @twitter-api-v2/plugin-cache-core demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.