Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@lion/ajax

Package Overview
Dependencies
Maintainers
2
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lion/ajax

Thin wrapper around fetch with support for interceptors.

  • 1.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
59
increased by37.21%
Maintainers
2
Weekly downloads
 
Created
Source

Tools >> Ajax >> Overview ||10

Ajax is a small wrapper around fetch which:

  • Allows globally registering request and response interceptors
  • Throws on 4xx and 5xx status codes
  • Supports caching, so a request can be prevented from reaching to network, by returning the cached response.
  • Supports JSON with ajax.fetchJSON by automatically serializing request body and deserializing response payload as JSON, and adding the correct Content-Type and Accept headers.
  • Adds accept-language header to requests based on application language
  • Adds XSRF header to request if the cookie is present

Installation

npm i --save @lion/ajax

Relation to fetch

Ajax delegates all requests to fetch. ajax.fetch and ajax.fetchJson have the same function signature as window.fetch, you can use any online resource to learn more about fetch. MDN is a great start.

Ajax class options

PropertyTypeDefault ValueDescription
addAcceptLanguagebooleantrueWhether to add the Accept-Language header from the data-localize-lang document property
addCachingbooleanfalseWhether to add the cache interceptor and start storing responses in the cache, even if cacheOptions.useCache is false
xsrfCookieNamestring"XSRF-TOKEN"The name for the Cross Site Request Forgery cookie
xsrfHeaderNamestring"X-XSRF-TOKEN"The name for the Cross Site Request Forgery header
jsonPrefixstring""The prefix to add to add to responses for the .fetchJson functions
cacheOptions.useCachebooleanfalseWhether to use the default cache interceptors to cache requests
cacheOptions.getCacheIdentifierfunctiona function returning the string _defaultA function to determine the cache that should be used for each request; used to make sure responses for one session are not used in the next
cacheOptions.methodsstring[]["get"]The HTTP methods to cache reponses for. Any other method will invalidate the cache for this request, see "Invalidating cache", below
cacheOptions.maxAgenumber360000The time to keep a response in the cache before invalidating it automatically
cacheOptions.invalidateUrlsstring[]undefinedUrls to invalidate each time a method not in cacheOptions.methods is encountered, see "Invalidating cache", below
cacheOptions.invalidateUrlsRegexregexundefinedRegular expression matching urls to invalidate each time a method not in cacheOptions.methods is encountered, see "Invalidating cache", below
cacheOptions.requestIdFunctionfunctiona function returning the base url and serialized search parametersFunction to determine what defines a unique URL
cacheOptions.contentTypesstring[]undefinedWhitelist of content types that will be stored to or retrieved from the cache
cacheOptions.maxResponseSizenumberundefinedThe maximum response size in bytes that will be stored to or retrieved from the cache
cacheOptions.maxCacheSizenumberundefinedThe maxiumum total size in bytes of the cache; when the cache gets larger it is truncated

Usage

import { ajax, createCacheInterceptors } from '@lion/ajax';

const getCacheIdentifier = () => {
  let userId = localStorage.getItem('lion-ajax-cache-demo-user-id');
  if (!userId) {
    localStorage.setItem('lion-ajax-cache-demo-user-id', '1');
    userId = '1';
  }
  return userId;
};

const TEN_MINUTES = 1000 * 60 * 10; // in milliseconds

const cacheOptions = {
  useCache: true,
  maxAge: TEN_MINUTES,
};

const [cacheRequestInterceptor, cacheResponseInterceptor] = createCacheInterceptors(
  getCacheIdentifier,
  cacheOptions,
);

ajax.addRequestInterceptor(cacheRequestInterceptor);
ajax.addResponseInterceptor(cacheResponseInterceptor);

Or use a custom cache object and add the cache config to the constructor:

import { Ajax } from '@lion/ajax';

const storeButDontRetrieveByDefaultConfig = {
  addCaching: true,
  cacheOptions: {
    getCacheIdentifier,
    useCache: false,
    maxAge: TEN_MINUTES,
  },
};

const customAjax = new Ajax(storeButDontRetrieveByDefaultConfig);

Invalidating the cache

Invalidating the cache, or cache busting, can be done in multiple ways:

  • Going past the maxAge of the cache object
  • Changing cache identifier (e.g. user session or active profile changes)
  • Doing a non GET request to the cached endpoint
    • Invalidates the cache of that endpoint
    • Invalidates the cache of all other endpoints matching invalidatesUrls and invalidateUrlsRegex

Restricting what to cache

The library has a number of options available to restrict what should be cached. They include:

By content type

cacheOptions.contentTypes

If this option is set, it is interpreted as a whitelist for which content types to cache. The content types of a given response is derived from its Content-Type header. If this option is set, responses that do not have a Content-Type header are never added to or retrieved from the cache.

By response size

cacheOptions.maxResponseSize

This option sets a maximum size (in bytes) for a single response to be cached. The size of the response is determined first by looking at the Content-Length header; if this header is not available, the response is inspected (through the blob() function) and its size retrieved.

Limiting the cache size

cacheOptions.maxCacheSize

This option sets a maximum size (in bytes) for the whole cache. The size of a response is determined first by looking at the Content-Length header; if this header is not available, the response is inspected (through the blob() function) and its size retrieved.

If the cache grows larger than the maxCacheSize option, the cache is truncated according to a First-In-First-Out (FIFO) algorithm that simply removes the oldest entries until the cache is smaller than options.maxCacheSize.

Keywords

FAQs

Package last updated on 09 Nov 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc