@hile/redis-idempotency 2.1.2 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +17 -14
  2. package/SKILL.md +2 -2
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -170,26 +170,29 @@ await pipeline.dispatch(ctx)
170
170
  console.log(ctx.state.result)
171
171
  ```
172
172
 
173
- Current `@hile/model@2.1.1` `defineModel()` returns a local `result` variable from its terminal middleware. That means a short-circuited middleware cannot make `model.handler()` return `ctx.state.result`. Until `@hile/model` returns `ctx.state.result`, prefer function-level `withIdempotency()` inside `main()`:
173
+ With `@hile/model@3.0.0+`, `defineModel()` returns `ctx.state.result` after the pipeline finishes, so `idempotent()` can be used directly as model middleware when the middleware is created with an already available Redis client. A cached idempotency hit can short-circuit the pipeline and still become the model result:
174
174
 
175
175
  ```typescript
176
- const debitModel = defineModel({
177
- services: [redisService],
178
- async main([redis], input: DebitInput) {
179
- return withIdempotency(
180
- redis,
181
- `idem:prod:wallet:debit:${input.tenantId}:${input.requestId}`,
182
- () => performDebit(input),
183
- {
176
+ function createDebitModel(redis: Redis) {
177
+ return defineModel({
178
+ pipelines: [
179
+ idempotent({
180
+ redis,
181
+ key: (input: DebitInput) => `idem:prod:wallet:debit:${input.tenantId}:${input.requestId}`,
182
+ fingerprint: stableHash,
184
183
  lockTtl: 60_000,
185
184
  resultTtl: 86_400_000,
186
- fingerprint: stableHash(input),
187
- },
188
- )
189
- },
190
- })
185
+ }),
186
+ ],
187
+ async main(input: DebitInput) {
188
+ return performDebit(input, redis)
189
+ },
190
+ })
191
+ }
191
192
  ```
192
193
 
194
+ For normal Hile models that load Redis through `services: [redisService]`, or for code that needs a narrower critical section than the whole model, use function-level `withIdempotency()` inside `main()` instead.
195
+
193
196
  ## Error Types
194
197
 
195
198
  | Error | When it happens | Recommended handling |
package/SKILL.md CHANGED
@@ -44,9 +44,9 @@ Build `stableHash()` fingerprints from plain DTOs. It rejects unsupported object
44
44
 
45
45
  Default cached results must be plain JSON values. If the wrapped function returns `Date`, `BigInt`, class instances, `Map` / `Set`, or any other rich type, pass `resultCodec` so cache hits deserialize to the same shape as the first call.
46
46
 
47
- ## Current Model Guidance
47
+ ## Model Guidance
48
48
 
49
- Prefer function-level `withIdempotency()` inside `defineModel().main`. Current `@hile/model@2.1.1` does not return `ctx.state.result` on middleware short-circuit, so `idempotent()` is safe for raw `Pipeline` usage and future model versions that return `ctx.state.result`, but not for current `defineModel` cache hits.
49
+ With `@hile/model@3.0.0+`, `defineModel()` returns `ctx.state.result` after the pipeline finishes, so `idempotent()` can short-circuit model execution when the middleware is created with an already available Redis client. Prefer function-level `withIdempotency()` inside `defineModel().main` when Redis is loaded through model `services`, or when only a smaller part of `main()` needs duplicate-execution protection.
50
50
 
51
51
  ## Failure Policy
52
52
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hile/redis-idempotency",
3
- "version": "2.1.2",
3
+ "version": "3.0.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -22,7 +22,7 @@
22
22
  "vitest": "^4.0.18"
23
23
  },
24
24
  "dependencies": {
25
- "@hile/model": "^2.1.1"
25
+ "@hile/model": "^3.0.1"
26
26
  },
27
- "gitHead": "93da114ef293e48682e7df94d89418dfbf9a0226"
27
+ "gitHead": "ffc9f14ed2591075290c77c1bcc0afc67ecf1794"
28
28
  }