npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

idempotency-client

v1.0.0

Published

Generic idempotency key manager with IndexedDB storage for frontend apps

Readme

Idempotency Store Frontend Guide

This document explains how to use the IdempotencyStore class in the frontend. This class is designed to help manage and persist idempotency keys and responses locally (e.g., in IndexedDB) to avoid duplicate submissions or calls.


Languages Supported


What is Idempotency?

Idempotency means that repeating the same action multiple times will always result in the same outcome. In frontend applications, this ensures that retrying a request (like submitting a form) won’t cause duplication.

This utility is useful for cases like:

  • Payment or checkout submissions
  • Form submissions
  • Offline caching of server responses

Why Use Idempotency?

  • Avoiding duplicate requests
  • Ensuring idempotent responses
  • Optimizing performance
  • Enhancing user experience

Backend Compatibility ?

This library is compatible with the backend Idempotency Middleware, which ensures that the same request is processed only once and returns the same result. Read the Backend Guide


Installation

npm install idempotency-client
  • UUID() - generates a unique identifier

Core Methods

CreateKey(requestId: string, ttlMs?: number): Promise<string>

Creates a new idempotency key for a request, with optional TTL.

GetKey(requestId: string): Promise<string | null>

Returns the stored key for the given request if it's not expired.

SaveResponse(requestId: string, responseData: any): Promise<void>

Saves the response data associated with the requestId.

GetResponse(requestId: string): Promise<any>

Retrieves cached response data if it exists.

Clear(requestId: string): Promise<void>

Deletes the stored key and response for the given requestId.

ExportStore(): Promise<string>

Exports all saved data as JSON (useful for backup).

ImportStore(jsonData: string): Promise<void>

Restores data from a JSON string.

AutoCleanup(thresholdMs?: number): Promise<void>

Removes expired entries automatically (default is 7 days).


Example Usage

import IdempotencyStore from 'idempotency-client';

async function handleFormSubmit() {
  const requestId = 'submit::formA';
  const key = await IdempotencyStore.CreateKey(requestId, 60000);
  const existing = await IdempotencyStore.GetResponse(requestId);
  if (existing) {
    renderFromCache(existing);
    return;
  }
  const response = await fetch('/api/submit', {
    method: 'POST',
    headers: { 'Idempotency-Key': key },
    body: JSON.stringify(data)
  });
  const result = await response.json();
  await IdempotencyStore.SaveResponse(requestId, result);
  renderResponse(result);
}

Notes

  • Ensure the server supports idempotency via headers.
  • requestId should uniquely represent the logical request.
  • Run AutoCleanup() periodically to prevent storage bloat.

Summary

This store is a reliable, frontend-safe mechanism for caching request outcomes and safely retrying operations without duplication.