Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@lion/ajax
Advanced tools
Ajax
is a small wrapper around fetch
which:
ajax.fetchJSON
by automatically serializing request body and deserializing response payload as JSON, and adding the correct Content-Type and Accept headers.npm i --save @lion/ajax
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.
Property | Type | Default Value | Description |
---|---|---|---|
addAcceptLanguage | boolean | true | Whether to add the Accept-Language header from the data-localize-lang document property |
addCaching | boolean | false | Whether to add the cache interceptor and start storing responses in the cache, even if cacheOptions.useCache is false |
xsrfCookieName | string | "XSRF-TOKEN" | The name for the Cross Site Request Forgery cookie |
xsrfHeaderName | string | "X-XSRF-TOKEN" | The name for the Cross Site Request Forgery header |
jsonPrefix | string | "" | The prefix to add to add to responses for the .fetchJson functions |
cacheOptions.useCache | boolean | false | Whether to use the default cache interceptors to cache requests |
cacheOptions.getCacheIdentifier | function | a function returning the string _default | A 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.methods | string[] | ["get"] | The HTTP methods to cache reponses for. Any other method will invalidate the cache for this request, see "Invalidating cache", below |
cacheOptions.maxAge | number | 360000 | The time to keep a response in the cache before invalidating it automatically |
cacheOptions.invalidateUrls | string[] | undefined | Urls to invalidate each time a method not in cacheOptions.methods is encountered, see "Invalidating cache", below |
cacheOptions.invalidateUrlsRegex | regex | undefined | Regular expression matching urls to invalidate each time a method not in cacheOptions.methods is encountered, see "Invalidating cache", below |
cacheOptions.requestIdFunction | function | a function returning the base url and serialized search parameters | Function to determine what defines a unique URL |
cacheOptions.contentTypes | string[] | undefined | Whitelist of content types that will be stored to or retrieved from the cache |
cacheOptions.maxResponseSize | number | undefined | The maximum response size in bytes that will be stored to or retrieved from the cache |
cacheOptions.maxCacheSize | number | undefined | The maxiumum total size in bytes of the cache; when the cache gets larger it is truncated |
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, or cache busting, can be done in multiple ways:
maxAge
of the cache objectinvalidatesUrls
and invalidateUrlsRegex
The library has a number of options available to restrict what should be cached. They include:
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.
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.
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
.
FAQs
Thin wrapper around fetch with support for interceptors.
The npm package @lion/ajax receives a total of 59 weekly downloads. As such, @lion/ajax popularity was classified as not popular.
We found that @lion/ajax demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.