memofetch

1.1.1 • Public • Published

memoFetch

fetch API with memoization

Install

npm i memofetch

Usage

GET

import { memoFetch, setExpirationTime, setMaxMemo } from 'memoFetch';

(async () => {
  await setMaxMemo(10);
  await setExpirationTime(5000);

  const { data } = await memoFetch(
    'https://naveropenapi.apigw.ntruss.com/map-geocode/v2/geocode?query=' +
      encodeURIComponent('양천구'),
    {
      filter: ({ addresses }) => {
        return {
          x: addresses[0].x,
          y: addresses[0].y,
        };
      },
      headers: {
        'X-NCP-APIGW-API-KEY-ID': 'sample',
        'X-NCP-APIGW-API-KEY': 'sample',
      },
    },
  );

  console.log(data);
})();

Save in JSON format

{
  "memo": {
    "gBWWpZc5vilNlknBafd7yqi+nAo=": {
      "value": {
        "headers": {
          "server": "nginx",
          "date": "Sun, 19 Jan 2020 14:24:55 GMT",
          "content-type": "application/json;charset=UTF-8",
          "content-length": "867",
          "connection": "close",
          "x-ncp-trace-id": "36d31ckp3edpj38phnccqmcoph"
        },
        "type": "object",
        "data": {
          "x": "129.3112381",
          "y": "35.5396493"
        }
      },
      "time": 1579443896001
    }
  }
}

Data structure of the return value

const { headers, type, data } = await memoFetch(...);
  • headers : Headers of the response
  • type : Type of the return data ["string" | "object"]
  • data : Data

POST

(async () => {
  await setMaxMemo(10);
  await setExpirationTime(5000);
  const { data } = await memoFetch('https://postman-echo.com/post', {
    headers: {
      'Content-Type': 'application/json',
    },

    method: 'POST',
    body: { b: 4 },
  });

  console.log(data);
})();

APIs

memoFetch

import { memoFetch, setExpirationTime, setMaxMemo } from 'memofetch';

const main = async () => {
  await setMaxMemo(30);
  await setExpirationTime(5000);

  const { data } = await memoFetch(
    'https://postman-echo.com/get?foo1=bar1&foo2=bar2',
  );

  console.log(data);
};

Options

The second argument can be an option value.

import { memoFetch, setExpirationTime, setMaxMemo } from 'memofetch';

const main = async () => {
  const { data } = await memoFetch('url...', {
    headers: {
      id: 1,
    },
    filter: v => v,
    method: 'POST',
    body: { name: 'youngjae' },
    useMemo: true,
  });

  console.log(data);
};
  • agent : a Instance of http.Agent | boolean (Default : http.globalAgent)
  • headers : Headers of the request (Default : null)
  • method : HTTP request methods (Default : GET)
  • filter : Filter Function for Response Data (Default : v => v)
  • body : HTTP Message Body (Default : '')
  • useMemo : if useMemo is true, memoFetch works in memoization mode. (Default : true)

setConfigPath

Set the path where memo data will be saved.

  • Default : process.cwd()
import { setConfigPath } from 'memoFetch';

setConfigPath('./config/memo.json');

setExpirationTime

Set the expiration time for memo data.

  • Unit : ms
  • Default : 5000
import { setExpirationTime } from 'memoFetch';

setExpirationTime(3600 * 1000); // set to 1hr

setMaxMemo

When the size of the memo data becomes larger than the set value, everything is initialized.

  • Default : 10
import { setMaxMemo } from 'memoFetch';

setMaxMemo(50);

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i memofetch

      Weekly Downloads

      1

      Version

      1.1.1

      License

      MIT

      Unpacked Size

      13.9 kB

      Total Files

      4

      Last publish

      Collaborators

      • bityoungjae