@node-idempotency/core
Advanced tools
Comparing version 1.0.4 to 1.0.5
{ | ||
"name": "@node-idempotency/core", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "A Race-Condition free Node.js library that ensures idempotency for requests, preventing unintended duplicate operations.", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mahendraHegde/node-idempotency.git" | ||
}, | ||
"files": [ | ||
"dist/" | ||
], | ||
"scripts": { | ||
"prepublishOnly": "pnpm build", | ||
"test:e2e": "jest --config ./tests/jest-e2e.json --runInBand --forceExit", | ||
"build": "tsc -p ./tsconfig.build.json", | ||
"typecheck": "tsc -b", | ||
"docs:core": "typedoc --commentStyle block --excludePrivate --plugin typedoc-plugin-markdown --out docs ./src/index.ts" | ||
}, | ||
"keywords": [ | ||
@@ -29,12 +26,18 @@ "node-idempotency", | ||
"author": "Mahendra Hegde", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@node-idempotency/storage-adapter-memory": "1.0.0" | ||
"@node-idempotency/storage-adapter-memory": "1.0.1" | ||
}, | ||
"dependencies": { | ||
"@node-idempotency/storage": "1.0.1" | ||
"@node-idempotency/storage": "1.0.2" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"test:e2e": "jest --config ./tests/jest-e2e.json --runInBand --forceExit", | ||
"build": "tsc -p ./tsconfig.build.json", | ||
"typecheck": "tsc -b", | ||
"docs:core": "typedoc --commentStyle block --excludePrivate --plugin typedoc-plugin-markdown --out docs ./src/index.ts" | ||
} | ||
} | ||
} |
#### @node-idempotency/core | ||
Core package that, makes requests idempotent and powers | ||
Core package that is | ||
- <i>Race Condition free.</i> | ||
- <i>Modular, you can attach your storage or plug in core implementation into your implemtation.</i> | ||
- <i>[RFC](https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/) compliant. </i> | ||
and powers | ||
- [`@node-idempotency/nestjs`](https://www.npmjs.com/package/@node-idempotency/nestjs) - Plug and Play `nestjs` wrapper for `@node-idempotency/core` | ||
@@ -30,9 +36,11 @@ | ||
```ts | ||
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 | ||
import { Idempotency } from "@node-idempotency/core"; | ||
import { MemoryStorageAdapter } from "@node-idempotency/storage-adapter-memory"; | ||
const idempotency = new Idempotency(new MemoryStorageAdapter(), {...idempotencyOptions}); | ||
// Create an Idempotency instance using a MemoryStorageAdapter | ||
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 | ||
// On receiving a request, call `onRequest` to validate idempotency | ||
try { | ||
@@ -44,19 +52,20 @@ const response = await idempotency.onRequest({ | ||
path: "/charge", | ||
options: { ...idempotencyOptions } //use options here override idempotencyOptions per request level | ||
options: { ...idempotencyOptions }, // Optional request-level overrides | ||
}); | ||
if (!response) { | ||
//request is new, allow it to proceed | ||
// New request, allow it to proceed | ||
return; | ||
} | ||
// its a duplicate, dont process again, return previous response | ||
// ex: res.status(response.additional.status).send(response.body) | ||
// Duplicate request, return previous response | ||
// Example: 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. | ||
// Handle idempotency errors (conflict, in-progress, fingerprint mismatch, etc.) | ||
// Refer to API documentation for specific error codes | ||
} | ||
// make sure to intercept the response so that the cycle is complete | ||
const response = await idempotency.onResponse( | ||
{ | ||
// Intercept response to complete the idempotency cycle | ||
const response = await idempotency.onResponse( | ||
{ | ||
method: "POST", | ||
@@ -66,11 +75,11 @@ headers: { "idempotency-key": "123" }, | ||
path: "/charge", | ||
options: { ...idempotencyOptions } //use options here override idempotencyOptions per request level | ||
options: { ...idempotencyOptions }, // Optional request-level overrides | ||
}, | ||
{ | ||
body:{ charge:"success" } //or error: your_error, | ||
additional:{ status: 201 } | ||
}); | ||
body: { charge: "success" }, // or error: your_error | ||
additional: { status: 201 }, | ||
}, | ||
); | ||
``` | ||
check details about the api [here](https://github.com/mahendraHegde/node-idempotency/blob/main/packages/core/docs/classes/Idempotency.md) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
24356
23
83
0
+ Added@node-idempotency/storage@1.0.2(transitive)
- Removed@node-idempotency/storage@1.0.1(transitive)