An npm package for automated, in-memory API caching.
Built with TypeScript.
About • Installation • Initialization • Types • Usage • License
An npm module to automate the regular processing and caching of responses from APIs. With a caching mechanism inspired by node-cache, this module has all the standard interface methods to interact with the in-memory cache.
Mastak makes requests using node-fetch and processes the response based on the resProcessor()
function provided by the user. Each key gets a timeout(ttl)
and an updateInterval
(if autoUpdate
is true).
$ npm install mastak --save
const Mastak = require("mastak");
const cache = new Mastak();
To import the module in TypeScript, esModuleInterop needs to be set to true in your tsconfig.json.
stdTTL
: (default:0
) - the standard timeout(in seconds) for each element of the cache,0
= infinite.autoUpdate
: (default:true
) - boolean flag that states if each element in the cache has to be regularly updated or not.updateInterval
: (default:3600(1 hr in secs)
) - the standard interval(in seconds) at which each element in the cache has to be updated.checkPeriod
: (default:600(10 min in secs)
) - the regular interval(in seconds) at which the internal checkData() method will check each element for timeout and autoUpdate.
const Mastak = require("mastak");
const cache = new Mastak({
stdTTL: 1800,
updateInterval: 7200,
});
There are 3 types/interfaces that a user has to take into account when using Mastak, i.e. Request
, CacheInput
& CacheUnit
.
Request
& CacheInput
define the format of input that is expected from the user while CacheUnit
defines the format in which an API and its value is stored within the cache.
Request
defines the data needed to form a valid request that can be sent using node-fetch
.
interface Request {
url: string; // url for the api
method: string; // http method to be used
body?: {
[key: string]: any; // body for the request
};
headers?: {
[key: string]: string; // headers
};
}
CacheInput
defines all the data that needs to be input to set or update an API.
interface CacheInput {
request: Request;
resProcessor?: any; // a function that processes the response recieved
updateInterval?: number; // the interval over which the API needs to be updated
ttl?: number; // the timeout for the API
}
CacheUnit
defines the format in which an API is stored in the cache. It extends CacheInput
i.e. it inherits all its properties.
interface CacheUnit extends CacheInput {
setTime: number; // the time at which this API/CacheUnit was set
lastUpdate: number; // the time at which the value was last updated
value: any; // the processed response from the API
}
? - field is not required.
Take a look at src/types/main.interfaces.ts to see all the defined interfaces.
Set an API or CacheUnit
in the cache with the key provided.
Returns a promise that resolves with the entire CacheUnit
stored against a key or rejects an error.
Mastak.set((key: string), (api: CacheInput));
const request = {
url: "https://jsonplaceholder.typicode.com/posts",
method: "POST",
body: {
title: "foo",
body: "bar",
userId: 1,
},
headers: {
"Content-type": "application/json; charset=UTF-8",
},
};
const api = {
request: request,
ttl: 1800,
};
const foo = async () => {
try {
let response = await cache.set("JSONPlaceholder", api);
console.log("set()", response);
} catch (err) {
console.warn(err.message);
}
};
foo();
set() { setTime: 1621113414640,
lastUpdate: 1621113414640,
value: { title: 'foo', body: 'bar', userId: 1, id: 101 },
request:
{ url: 'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
body: { title: 'foo', body: 'bar', userId: 1 },
headers: { 'Content-type': 'application/json; charset=UTF-8' } } }
Get the currently stored value for an API with the key.
Returns the "value" for the CacheUnit
or throws a BadKey
error.
Mastak.get((key: string));
try {
let response = await cache.get("JSONPlaceholder");
console.log("get()", response);
} catch (err) {
console.warn(err.message);
}
get() { title: 'foo', body: 'bar', userId: 1, id: 101 }
Update the data of a CacheUnit
and update its value if updateNow
argument is true.
Returns a promise that resolves with the updated CacheUnit
or rejects an error.
Mastak.update((key: string), (api: CacheInput), (updateNow: boolean));
const request2 = {
url: "https://jsonplaceholder.typicode.com/posts/2",
method: "PATCH",
body: {
title: "foo",
},
};
const resProcessor2 = (data) => {
return data.userId;
};
const api2 = {
request: request2,
resProcessor: resProcessor2,
};
const foo = async () => {
try {
response = await cache.update("JSONPlaceholder", api2, true);
console.log("update()", response);
} catch (err) {
console.warn(err.message);
}
};
foo();
update() { setTime: 1621113648549,
lastUpdate: 1621113649233,
value: 1,
request:
{ url: 'https://jsonplaceholder.typicode.com/posts/2',
method: 'PATCH',
body: { title: 'foo' } },
resProcessor: [Function: something2] }
Delete a CacheUnit
with the key.
Returns boolean - true if successful or throws a BadKey
error
Mastak.delete((key: string));
try {
let response = await cache.delete("JSONPlaceholder");
console.log("delete()", response);
} catch (err) {
console.warn(err.message);
}
delete() true
Set multiple APIs or CacheUnit
s in the cache with arrays of keys and CacheInput
s.
Returns a promise that resolves with an array of proccessed CacheUnit
s or rejects an error.
Mastak.setMulti((keys: Array<string>), (apis: Array<CacheInput>));
const request = {
url: "https://jsonplaceholder.typicode.com/posts",
method: "POST",
body: {
title: "foo",
body: "bar",
userId: 1,
},
headers: {
"Content-type": "application/json; charset=UTF-8",
},
};
const request2 = {
url: "https://jsonplaceholder.typicode.com/posts/2",
method: "PATCH",
body: {
title: "foo",
},
};
const resProcessor2 = (data) => {
return data.userId;
};
const api: CacheInput = {
request: request,
ttl: 1800,
};
const api2 = {
request: request2,
resProcessor: resProcessor2,
};
const foo = async () => {
try {
let response = await cache.setMulti(
["JSONPlaceholder", "JSONPlaceholder2"],
[api, api2]
);
console.log("setMulti()", response);
} catch (err) {
console.warn(err.message);
}
};
foo();
setMulti()[
({
setTime: 1621113734595,
lastUpdate: 1621113734595,
value: { title: "foo", body: "bar", userId: 1, id: 101 },
request: {
url: "https://jsonplaceholder.typicode.com/posts",
method: "POST",
body: [Object],
headers: [Object],
},
},
{
setTime: 1621113735169,
lastUpdate: 1621113735169,
value: 1,
request: {
url: "https://jsonplaceholder.typicode.com/posts/2",
method: "PATCH",
body: [Object],
},
resProcessor: [(Function: something2)],
})
];
Get current value of multiple CacheUnit
s with an array of keys.
Returns an array of values or throws a BadKey
error.
Mastak.getMulti((keys: Array<string>));
try {
let response = cache.getMulti(["JSONPlaceholder", "JSONPlaceholder2"]);
console.log("getMulti()", response);
} catch (err) {
console.warn(err.message);
}
getMulti() { JSONPlaceholder: { title: 'foo', body: 'bar', userId: 1, id: 101 },
JSONPlaceholder2: 1 }
Checks if the cache contains a key or not.
Returns boolean - true or false
Mastak.has((key: string));
let response = cache.has("JSONPlaceholder");
console.log("has()", response);
has() true
Get all the keys currently stored in the cache.
Returns an array of strings(keys).
Mastak.keys();
let response = cache.keys();
console.log("keys()", response);
keys()[("JSONPlaceholder", "JSONPlaceholder2")];
Delete multiple CacheUnit
s with an array of keys.
Returns boolean - true if successful or throws a BadKey
error.
Mastak.deleteMulti((keys: Array<string>));
try {
let response = cache.deleteMulti(["JSONPlaceholder", "JSONPlaceholder2"]);
console.log("deleteMulti()", response);
} catch (err) {
console.warn(err.message);
}
deleteMulti() true
Delete a CacheUnit
and return its value.
Returns the deleted CacheUnit
or throws a BadKey
error.
Mastak.take((key: string));
try {
let response = cache.take("JSONPlaceholder");
console.log("take()", response);
} catch (err) {
console.warn(err.message);
}
take() { setTime: 1621113915875,
lastUpdate: 1621113915875,
value: { title: 'foo', body: 'bar', userId: 1, id: 101 },
request:
{ url: 'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
body: { title: 'foo', body: 'bar', userId: 1 },
headers: { 'Content-type': 'application/json; charset=UTF-8' } } }
Delete all the data in the cache.
Returns boolean - true.
Mastak.flush();
let response = cache.flush();
console.log("flush()", response);
flush() true
MIT License | Copyright (c) 2024 Kumar Shashwat