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

@node-idempotency/core

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@node-idempotency/core

A Race-Condition free Node.js library that ensures idempotency for requests, preventing unintended duplicate operations.

  • 1.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
55
decreased by-68.93%
Maintainers
1
Weekly downloads
 
Created
Source
@node-idempotency/core

Core package that, makes requests idempotent and powers

if above packages dont meet your needs, you can utilise the core package directly to tweek it as per your needs.

install
npm i @node-idempotency/core
usage

The flow for idempotency is simple, you call the onRequest handler, when you receieve the request from clients before it reaches your business logic/controller.

onRequest handler validates request for conflicts, figerprint missmatch, no idempotency-key(when idempotency is enforced) and gives back the response if the key is already seen, you typically give back the "cached" response to the client.

if its a new request, it marks the request as progress generates fingerprint using body (so that it can validate conflicts for duplicate requests and figure out fingerprint missmatch), and returns undefined, you are responsible here to pass the request to your controller/business logic.

onResponse handler is called by you when your business logic completes for the first time, so that the response can be stored and the request can be marked as complete.

import { Idempotency } from '@node-idempotency/core'
import { MemoryStorageAdapter } from "@node-idempotency/storage-adapter-memory"; //or any other storage adapter of your choice which meets @node-idempotency/storage interface

const idempotency = new Idempotency(new MemoryStorageAdapter(), {...idempotencyOptions});

// on receiving the request call `onRequest`
// it validate the request based on `idempotencyOptions` and throws eror if the request is concurrent, sends different body for the same key or doesnt sent idempotency-key when idempotency is enforced
try {
  const response = await idempotency.onRequest({
    method: "POST",
    headers: { "idempotency-key": "123" },
    body: { pay: 100 },
    path: "/charge",
    options: { ...idempotencyOptions } //use options here override idempotencyOptions per request level
  });

  if (!response) {
    //request is new, allow it to proceed
    return;
  }
  // its a duplicate, dont process again, return previous response
  // ex: res.status(response.additional.status).send(response.body)
} catch (err) {
  //handle idempotency error here(conflict, in-progress, figerprint missmatch etc).
  //check api details for defined error codes.
}

// make sure to intercept the response so that the cycle is complete
  const response = await idempotency.onResponse(
    {
    method: "POST",
    headers: { "idempotency-key": "123" },
    body: { pay: 100 },
    path: "/charge",
    options: { ...idempotencyOptions } //use options here override idempotencyOptions per request level
  },
  {
    body:{ charge:"success" } //or error: your_error,
    additional:{ status: 201 }
  });

check details about the api here

Keywords

FAQs

Package last updated on 26 Mar 2024

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