0.0.6 • Published 6 months ago

mharj-etag-tools v0.0.6

Weekly downloads
6
License
LGPL-2.1-or-later
Repository
github
Last release
6 months ago

mharj-etag-tools

Simple ETag object setup to store and handle both data and ETag value.

Install

npm i mharj-etag-tools

Example

Cache validation example with state (i.e. redux)

const todo = getState().todo; // IEtagObject<PlaceholderObjectType>
const headers = new Headers();
if (haveETag(todo)) { // Checks if todo is IWithEtagObject type (has actual ETag value)
	headers.set('if-none-match', getETag(todo)); // Attaches ETag header from last state
}
const res = await fetch('https://jsonplaceholder.typicode.com/todos/1', {headers});
if (res.status === 200) {
    const data = await res.json();
    const todoEtagObject = wrapEtag(data, getEtagHeader(res));
    dispatch(storeTodo(todoEtagObject)); // updates IEtagObject<PlaceholderObjectType> to state
}
if(res.status === 304) {
    // do nothing as state is already up to date
}
0.0.5

6 months ago

0.0.6

6 months ago

0.0.4

2 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago