{"id":17031178,"url":"https://github.com/shashwatah/mastak","last_synced_at":"2026-02-23T02:39:03.401Z","repository":{"id":42711997,"uuid":"284126621","full_name":"shashwatah/mastak","owner":"shashwatah","description":"NPM package for automated, in-memory API caching.","archived":false,"fork":false,"pushed_at":"2025-07-22T04:26:15.000Z","size":198,"stargazers_count":16,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-04T03:54:44.941Z","etag":null,"topics":["api","automated","cache","in-memory","node-cache","npm","request","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/mastak","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shashwatah.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-07-31T20:47:34.000Z","updated_at":"2025-04-09T10:16:35.000Z","dependencies_parsed_at":"2025-04-12T12:33:41.826Z","dependency_job_id":"298ac5fd-c0b0-4d26-9f7d-a4b36db26dd7","html_url":"https://github.com/shashwatah/mastak","commit_stats":null,"previous_names":["shashwatah/mastak","araekiel/mastak"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/shashwatah/mastak","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shashwatah%2Fmastak","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shashwatah%2Fmastak/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shashwatah%2Fmastak/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shashwatah%2Fmastak/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shashwatah","download_url":"https://codeload.github.com/shashwatah/mastak/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shashwatah%2Fmastak/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29735759,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-23T02:24:00.660Z","status":"ssl_error","status_checked_at":"2026-02-23T02:22:56.087Z","response_time":90,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","automated","cache","in-memory","node-cache","npm","request","typescript"],"created_at":"2024-10-14T08:10:44.065Z","updated_at":"2026-02-23T02:39:03.372Z","avatar_url":"https://github.com/shashwatah.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eMastak\u003c/h1\u003e\n\n\u003ch4 align=\"center\"\u003e\nAn \u003ca href=\"https://www.npmjs.com/\"\u003enpm\u003c/a\u003e package for automated, in-memory API caching.\n\u003cbr/\u003e\nBuilt with \u003ca href=\"https://www.typescriptlang.org/\"\u003eTypeScript\u003c/a\u003e.\n\u003c/h4\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"LICENSE\"\u003e\u003cimg alt=\"Release\" src=\"https://img.shields.io/badge/license-MIT-green\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://github.com/shashwatah/mastak/releases/tag/v1.1.4\"\u003e\u003cimg alt=\"Github Release\" src=\"https://img.shields.io/badge/release-v1.1.4-blue\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"#about\"\u003eAbout\u003c/a\u003e •\n  \u003ca href=\"#installation\"\u003eInstallation\u003c/a\u003e •\n  \u003ca href=\"#initialization\"\u003eInitialization\u003c/a\u003e •\n  \u003ca href=\"#types\"\u003eTypes\u003c/a\u003e •\n  \u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e •\n  \u003ca href=\"#license\"\u003eLicense\u003c/a\u003e\n\u003c/p\u003e\n\n## About\n\nAn \u003ca href=\"https://www.npmjs.com/\"\u003enpm\u003c/a\u003e module to automate the regular processing and caching of responses from APIs. With a caching mechanism inspired by [node-cache](https://www.npmjs.com/package/node-cache), this module has all the standard interface methods to interact with the in-memory cache.\u003cbr\u003e\nMastak makes requests using [node-fetch](https://www.npmjs.com/package/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_).\n\n## Installation\n\n```bash\n$ npm install mastak --save\n```\n\n## Initialization\n\n```js\nconst Mastak = require(\"mastak\");\nconst cache = new Mastak();\n```\n\n\u003e To import the module in TypeScript, _esModuleInterop_ needs to be set to true in your _tsconfig.json_.\n\n### Options\n\n- `stdTTL`: _(default: `0`)_ - the standard timeout(in seconds) for each element of the cache, `0` = infinite.\n- `autoUpdate`: _(default: `true`)_ - boolean flag that states if each element in the cache has to be regularly updated or not.\n- `updateInterval`: _(default: `3600(1 hr in secs)`)_ - the standard interval(in seconds) at which each element in the cache has to be updated.\n- `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.\n\n#### Example\n\n```js\nconst Mastak = require(\"mastak\");\nconst cache = new Mastak({\n  stdTTL: 1800,\n  updateInterval: 7200,\n});\n```\n\n## Types\n\nThere are 3 types/interfaces that a user has to take into account when using Mastak, i.e. `Request`, `CacheInput` \u0026 `CacheUnit`.\u003cbr\u003e\n`Request` \u0026 `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.\n\n### Request\n\n`Request` defines the data needed to form a valid request that can be sent using `node-fetch`.\n\n```ts\ninterface Request {\n  url: string; // url for the api\n  method: string; // http method to be used\n  body?: {\n    [key: string]: any; // body for the request\n  };\n  headers?: {\n    [key: string]: string; // headers\n  };\n}\n```\n\n### CacheInput\n\n`CacheInput` defines all the data that needs to be input to set or update an API.\n\n```ts\ninterface CacheInput {\n  request: Request;\n  resProcessor?: any; // a function that processes the response recieved\n  updateInterval?: number; // the interval over which the API needs to be updated\n  ttl?: number; // the timeout for the API\n}\n```\n\n### CacheUnit\n\n`CacheUnit` defines the format in which an API is stored in the cache. It extends `CacheInput` i.e. it inherits all its properties.\n\n```ts\ninterface CacheUnit extends CacheInput {\n  setTime: number; // the time at which this API/CacheUnit was set\n  lastUpdate: number; // the time at which the value was last updated\n  value: any; // the processed response from the API\n}\n```\n\n\u003e ? - field is not required.\n\nTake a look at [src/types/main.interfaces.ts](https://github.com/shashwatah/mastak/blob/master/src/types/main.interfaces.ts) to see all the defined interfaces.\n\n## Usage\n\n### set()\n\nSet an API or `CacheUnit` in the cache with the key provided.\u003cbr\u003eReturns a promise that resolves with the entire `CacheUnit` stored against a key or rejects an error.\n\n```js\nMastak.set((key: string), (api: CacheInput));\n```\n\n#### Example\n\n```js\nconst request = {\n  url: \"https://jsonplaceholder.typicode.com/posts\",\n  method: \"POST\",\n  body: {\n    title: \"foo\",\n    body: \"bar\",\n    userId: 1,\n  },\n  headers: {\n    \"Content-type\": \"application/json; charset=UTF-8\",\n  },\n};\n\nconst api = {\n  request: request,\n  ttl: 1800,\n};\n\nconst foo = async () =\u003e {\n  try {\n    let response = await cache.set(\"JSONPlaceholder\", api);\n    console.log(\"set()\", response);\n  } catch (err) {\n    console.warn(err.message);\n  }\n};\n\nfoo();\n```\n\n#### Output\n\n```js\nset() { setTime: 1621113414640,\nlastUpdate: 1621113414640,\nvalue: { title: 'foo', body: 'bar', userId: 1, id: 101 },\nrequest:\n{ url: 'https://jsonplaceholder.typicode.com/posts',\nmethod: 'POST',\nbody: { title: 'foo', body: 'bar', userId: 1 },\nheaders: { 'Content-type': 'application/json; charset=UTF-8' } } }\n```\n\n\u003chr\u003e\n\n### get()\n\nGet the currently stored value for an API with the key.\u003cbr\u003eReturns the \"value\" for the `CacheUnit` or throws a `BadKey` error.\n\n```js\nMastak.get((key: string));\n```\n\n#### Example\n\n```js\ntry {\n  let response = await cache.get(\"JSONPlaceholder\");\n  console.log(\"get()\", response);\n} catch (err) {\n  console.warn(err.message);\n}\n```\n\n#### Output\n\n```js\nget() { title: 'foo', body: 'bar', userId: 1, id: 101 }\n```\n\n\u003chr\u003e\n\n### update()\n\nUpdate the data of a `CacheUnit` and update its value if `updateNow` argument is _true_.\u003cbr\u003e\nReturns a promise that resolves with the updated `CacheUnit` or rejects an error.\n\n```js\nMastak.update((key: string), (api: CacheInput), (updateNow: boolean));\n```\n\n#### Example\n\n```js\nconst request2 = {\n  url: \"https://jsonplaceholder.typicode.com/posts/2\",\n  method: \"PATCH\",\n  body: {\n    title: \"foo\",\n  },\n};\n\nconst resProcessor2 = (data) =\u003e {\n  return data.userId;\n};\n\nconst api2 = {\n  request: request2,\n  resProcessor: resProcessor2,\n};\n\nconst foo = async () =\u003e {\n  try {\n    response = await cache.update(\"JSONPlaceholder\", api2, true);\n    console.log(\"update()\", response);\n  } catch (err) {\n    console.warn(err.message);\n  }\n};\n\nfoo();\n```\n\n#### Output\n\n```js\nupdate() { setTime: 1621113648549,\nlastUpdate: 1621113649233,\nvalue: 1,\nrequest:\n{ url: 'https://jsonplaceholder.typicode.com/posts/2',\n    method: 'PATCH',\n    body: { title: 'foo' } },\nresProcessor: [Function: something2] }\n```\n\n\u003chr\u003e\n\n### delete()\n\nDelete a `CacheUnit` with the key.\u003cbr\u003e\nReturns _boolean_ - _true_ if successful or throws a `BadKey` error\n\n```js\nMastak.delete((key: string));\n```\n\n#### Example\n\n```js\ntry {\n  let response = await cache.delete(\"JSONPlaceholder\");\n  console.log(\"delete()\", response);\n} catch (err) {\n  console.warn(err.message);\n}\n```\n\n#### Output\n\n```js\ndelete() true\n```\n\n\u003chr\u003e\n\n### setMulti()\n\nSet multiple APIs or `CacheUnit`s in the cache with arrays of keys and `CacheInput`s.\u003cbr\u003e\nReturns a promise that resolves with an array of proccessed `CacheUnit`s or rejects an error.\n\n```js\nMastak.setMulti((keys: Array\u003cstring\u003e), (apis: Array\u003cCacheInput\u003e));\n```\n\n#### Example\n\n```js\nconst request = {\n  url: \"https://jsonplaceholder.typicode.com/posts\",\n  method: \"POST\",\n  body: {\n    title: \"foo\",\n    body: \"bar\",\n    userId: 1,\n  },\n  headers: {\n    \"Content-type\": \"application/json; charset=UTF-8\",\n  },\n};\n\nconst request2 = {\n  url: \"https://jsonplaceholder.typicode.com/posts/2\",\n  method: \"PATCH\",\n  body: {\n    title: \"foo\",\n  },\n};\n\nconst resProcessor2 = (data) =\u003e {\n  return data.userId;\n};\n\nconst api: CacheInput = {\n  request: request,\n  ttl: 1800,\n};\n\nconst api2 = {\n  request: request2,\n  resProcessor: resProcessor2,\n};\n\nconst foo = async () =\u003e {\n  try {\n    let response = await cache.setMulti(\n      [\"JSONPlaceholder\", \"JSONPlaceholder2\"],\n      [api, api2]\n    );\n    console.log(\"setMulti()\", response);\n  } catch (err) {\n    console.warn(err.message);\n  }\n};\n\nfoo();\n```\n\n#### Output\n\n```js\nsetMulti()[\n  ({\n    setTime: 1621113734595,\n    lastUpdate: 1621113734595,\n    value: { title: \"foo\", body: \"bar\", userId: 1, id: 101 },\n    request: {\n      url: \"https://jsonplaceholder.typicode.com/posts\",\n      method: \"POST\",\n      body: [Object],\n      headers: [Object],\n    },\n  },\n  {\n    setTime: 1621113735169,\n    lastUpdate: 1621113735169,\n    value: 1,\n    request: {\n      url: \"https://jsonplaceholder.typicode.com/posts/2\",\n      method: \"PATCH\",\n      body: [Object],\n    },\n    resProcessor: [(Function: something2)],\n  })\n];\n```\n\n\u003chr\u003e\n\n### getMulti()\n\nGet current value of multiple `CacheUnit`s with an array of keys.\u003cbr\u003e\nReturns an array of values or throws a `BadKey` error.\n\n```js\nMastak.getMulti((keys: Array\u003cstring\u003e));\n```\n\n#### Example\n\n```js\ntry {\n  let response = cache.getMulti([\"JSONPlaceholder\", \"JSONPlaceholder2\"]);\n  console.log(\"getMulti()\", response);\n} catch (err) {\n  console.warn(err.message);\n}\n```\n\n#### Output\n\n```js\ngetMulti() { JSONPlaceholder: { title: 'foo', body: 'bar', userId: 1, id: 101 },\n  JSONPlaceholder2: 1 }\n```\n\n\u003chr\u003e\n\n### has()\n\nChecks if the cache contains a key or not.\u003cbr\u003e\nReturns _boolean_ - _true_ or _false_\n\n```js\nMastak.has((key: string));\n```\n\n#### Example\n\n```js\nlet response = cache.has(\"JSONPlaceholder\");\nconsole.log(\"has()\", response);\n```\n\n#### Output\n\n```js\nhas() true\n```\n\n\u003chr\u003e\n\n### keys()\n\nGet all the keys currently stored in the cache.\u003cbr\u003e\nReturns an array of _strings(keys)_.\n\n```js\nMastak.keys();\n```\n\n#### Example\n\n```js\nlet response = cache.keys();\nconsole.log(\"keys()\", response);\n```\n\n#### Output\n\n```js\nkeys()[(\"JSONPlaceholder\", \"JSONPlaceholder2\")];\n```\n\n\u003chr\u003e\n\n### deleteMulti()\n\nDelete multiple `CacheUnit`s with an array of keys.\u003cbr\u003e\nReturns _boolean_ - _true_ if successful or throws a `BadKey` error.\n\n```js\nMastak.deleteMulti((keys: Array\u003cstring\u003e));\n```\n\n#### Example\n\n```js\ntry {\n  let response = cache.deleteMulti([\"JSONPlaceholder\", \"JSONPlaceholder2\"]);\n  console.log(\"deleteMulti()\", response);\n} catch (err) {\n  console.warn(err.message);\n}\n```\n\n#### Output\n\n```js\ndeleteMulti() true\n```\n\n\u003chr\u003e\n\n### take()\n\nDelete a `CacheUnit` and return its value.\u003cbr\u003e\nReturns the deleted `CacheUnit` or throws a `BadKey` error.\n\n```js\nMastak.take((key: string));\n```\n\n#### Example\n\n```js\ntry {\n  let response = cache.take(\"JSONPlaceholder\");\n  console.log(\"take()\", response);\n} catch (err) {\n  console.warn(err.message);\n}\n```\n\n#### Output\n\n```js\ntake() { setTime: 1621113915875,\nlastUpdate: 1621113915875,\nvalue: { title: 'foo', body: 'bar', userId: 1, id: 101 },\nrequest:\n{ url: 'https://jsonplaceholder.typicode.com/posts',\n    method: 'POST',\n    body: { title: 'foo', body: 'bar', userId: 1 },\n    headers: { 'Content-type': 'application/json; charset=UTF-8' } } }\n```\n\n\u003chr\u003e\n\n### flush()\n\nDelete all the data in the cache.\u003cbr\u003e\nReturns _boolean_ - _true_.\n\n```js\nMastak.flush();\n```\n\n#### Example\n\n```js\nlet response = cache.flush();\nconsole.log(\"flush()\", response);\n```\n\n#### Output\n\n```js\nflush() true\n```\n\n\u003chr\u003e\n\n## License\n\n[MIT License](https://github.com/shashwatah/mastak/blob/main/LICENSE) | Copyright (c) 2024 Kumar Shashwat\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshashwatah%2Fmastak","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshashwatah%2Fmastak","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshashwatah%2Fmastak/lists"}