🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@oncely/core

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

@oncely/core

Idempotency engine for HTTP APIs - ensures operations execute exactly once

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

@oncely/core

Core idempotency library with memory storage. The foundation for all oncely packages.

npm version

Installation

npm install @oncely/core

Quick Start

import { MemoryStorage, hashObject, parseTtl } from '@oncely/core';

const storage = new MemoryStorage();

// Acquire a lock for a request
const result = await storage.acquire('request-key', hashObject(body), parseTtl('1h'));

if (result.status === 'acquired') {
  // Execute the operation
  const response = await processRequest(body);

  // Save the response
  await storage.save('request-key', {
    data: response,
    createdAt: Date.now(),
    hash: hashObject(body),
  });
}

if (result.status === 'hit') {
  // Return cached response
  return result.response.data;
}

Storage Adapter

MemoryStorage is the default adapter, ideal for:

  • Development and testing
  • Single-instance deployments
  • Prototyping

For production multi-instance deployments, use:

StorageAdapter Interface

interface StorageAdapter {
  acquire(key: string, hash: string | null, ttl: number): Promise<AcquireResult>;
  save(key: string, response: StoredResponse): Promise<void>;
  release(key: string): Promise<void>;
  delete(key: string): Promise<void>;
  clear(): Promise<void>;
}

type AcquireResult =
  | { status: 'acquired' }
  | { status: 'hit'; response: StoredResponse }
  | { status: 'conflict'; startedAt: number }
  | { status: 'mismatch'; existingHash: string; providedHash: string };

Utilities

Hash Objects

import { hashObject } from '@oncely/core';

const hash = hashObject({ amount: 100, currency: 'USD' });
// => 'a1b2c3d4...'

Parse TTL

import { parseTtl } from '@oncely/core';

parseTtl('1h'); // 3600000
parseTtl('30m'); // 1800000
parseTtl('5s'); // 5000
parseTtl('1d'); // 86400000
parseTtl(60000); // 60000

Error Classes

import {
  IdempotencyError,
  MissingKeyError, // 400 - Key required but missing
  ConflictError, // 409 - Request in progress
  MismatchError, // 422 - Key reused with different body
} from '@oncely/core';

All errors extend IdempotencyError and include:

  • statusCode - HTTP status code
  • toProblemDetails() - RFC 7807 Problem Details

Constants

import { IDEMPOTENCY_KEY_HEADER, IDEMPOTENCY_REPLAY_HEADER } from '@oncely/core';

IDEMPOTENCY_KEY_HEADER; // 'Idempotency-Key'
IDEMPOTENCY_REPLAY_HEADER; // 'Idempotency-Replay'

Testing Utilities

import { createTestStorage, MockStorage } from '@oncely/core/testing';

// Create a test storage with optional pre-seeded data
const storage = createTestStorage({
  'existing-key': { data: { id: 1 }, createdAt: Date.now(), hash: null },
});

// Or use MockStorage for fine-grained control
const mock = new MockStorage();
mock.simulateConflict('key');
mock.simulateError(new Error('Connection failed'));

License

MIT

Keywords

idempotency

FAQs

Package last updated on 25 Jan 2026

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