@igniter-js/caller 0.1.3 → 0.1.4
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.
- package/AGENTS.md +2275 -172
- package/CHANGELOG.md +13 -0
- package/README.md +108 -14
- package/dist/adapters/index.d.mts +1 -0
- package/dist/adapters/index.d.ts +1 -0
- package/dist/adapters/index.js +91 -0
- package/dist/adapters/index.js.map +1 -0
- package/dist/adapters/index.mjs +89 -0
- package/dist/adapters/index.mjs.map +1 -0
- package/dist/index-COZVROi_.d.mts +121 -0
- package/dist/index-COZVROi_.d.ts +121 -0
- package/dist/index.d.mts +754 -280
- package/dist/index.d.ts +754 -280
- package/dist/index.js +992 -241
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +989 -242
- package/dist/index.mjs.map +1 -1
- package/dist/telemetry/index.d.mts +100 -0
- package/dist/telemetry/index.d.ts +100 -0
- package/dist/telemetry/index.js +54 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/telemetry/index.mjs +52 -0
- package/dist/telemetry/index.mjs.map +1 -0
- package/package.json +24 -4
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this package will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## 0.1.3
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- Telemetry integration via `@igniter-js/telemetry` with a dedicated subpath export.
|
|
10
|
+
- Mock store adapter in `@igniter-js/caller/adapters` for testing.
|
|
11
|
+
- Expanded test coverage for core, builders, adapters, and utils.
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
|
|
15
|
+
- Standardized builders/core structure and public entrypoint pattern.
|
|
16
|
+
- Updated documentation to match the current API and telemetry events.
|
|
17
|
+
|
|
5
18
|
## 0.1.1
|
|
6
19
|
|
|
7
20
|
### New Features
|
package/README.md
CHANGED
|
@@ -14,7 +14,8 @@ Type-safe HTTP client for Igniter.js apps. Built on top of `fetch`, it gives you
|
|
|
14
14
|
- ✅ **Retries** - linear or exponential backoff + status-based retry
|
|
15
15
|
- ✅ **Caching** - in-memory cache + optional persistent store adapter
|
|
16
16
|
- ✅ **Schema Validation** - validate request/response using `StandardSchemaV1`
|
|
17
|
-
- ✅ **
|
|
17
|
+
- ✅ **StandardSchema Support** - Zod or any library that implements `StandardSchemaV1`
|
|
18
|
+
- ✅ **Telemetry-ready** - optional integration with `@igniter-js/telemetry`
|
|
18
19
|
- ✅ **Global Events** - observe responses for logging/telemetry/cache invalidation
|
|
19
20
|
- ✅ **Auto query encoding** - body in GET requests converts to query params
|
|
20
21
|
|
|
@@ -22,19 +23,29 @@ Type-safe HTTP client for Igniter.js apps. Built on top of `fetch`, it gives you
|
|
|
22
23
|
|
|
23
24
|
```bash
|
|
24
25
|
# npm
|
|
25
|
-
npm install @igniter-js/caller @igniter-js/core
|
|
26
|
+
npm install @igniter-js/caller @igniter-js/core
|
|
26
27
|
|
|
27
28
|
# pnpm
|
|
28
|
-
pnpm add @igniter-js/caller @igniter-js/core
|
|
29
|
+
pnpm add @igniter-js/caller @igniter-js/core
|
|
29
30
|
|
|
30
31
|
# yarn
|
|
31
|
-
yarn add @igniter-js/caller @igniter-js/core
|
|
32
|
+
yarn add @igniter-js/caller @igniter-js/core
|
|
32
33
|
|
|
33
34
|
# bun
|
|
34
|
-
bun add @igniter-js/caller @igniter-js/core
|
|
35
|
+
bun add @igniter-js/caller @igniter-js/core
|
|
35
36
|
```
|
|
36
37
|
|
|
37
|
-
|
|
38
|
+
Optional dependencies:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# Telemetry (optional)
|
|
42
|
+
npm install @igniter-js/telemetry
|
|
43
|
+
|
|
44
|
+
# Schema validation (optional - Zod or any StandardSchemaV1-compatible lib)
|
|
45
|
+
npm install zod
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
> `@igniter-js/core` is required. `@igniter-js/telemetry` and `zod` are optional peer dependencies.
|
|
38
49
|
|
|
39
50
|
## Quick Start
|
|
40
51
|
|
|
@@ -230,9 +241,66 @@ const api = IgniterCaller.create()
|
|
|
230
241
|
.build()
|
|
231
242
|
```
|
|
232
243
|
|
|
244
|
+
## Adapters
|
|
245
|
+
|
|
246
|
+
The package ships a mock store adapter for tests and local development:
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
import { MockCallerStoreAdapter } from '@igniter-js/caller/adapters'
|
|
250
|
+
import { IgniterCaller } from '@igniter-js/caller'
|
|
251
|
+
|
|
252
|
+
const store = MockCallerStoreAdapter.create()
|
|
253
|
+
|
|
254
|
+
const api = IgniterCaller.create()
|
|
255
|
+
.withStore(store)
|
|
256
|
+
.build()
|
|
257
|
+
```
|
|
258
|
+
|
|
233
259
|
## Schema Validation (StandardSchemaV1)
|
|
234
260
|
|
|
235
261
|
If you already use schemas in your Igniter.js app, you can validate requests and responses automatically.
|
|
262
|
+
Schemas must implement `StandardSchemaV1` (Zod is supported, and any compatible library works).
|
|
263
|
+
|
|
264
|
+
### Preferred: IgniterCallerSchema builder
|
|
265
|
+
|
|
266
|
+
```ts
|
|
267
|
+
import { IgniterCaller, IgniterCallerSchema } from '@igniter-js/caller'
|
|
268
|
+
import { z } from 'zod'
|
|
269
|
+
|
|
270
|
+
const UserSchema = z.object({ id: z.string(), name: z.string() })
|
|
271
|
+
const ErrorSchema = z.object({ message: z.string() })
|
|
272
|
+
|
|
273
|
+
const callerSchemas = IgniterCallerSchema.create()
|
|
274
|
+
.schema('User', UserSchema)
|
|
275
|
+
.schema('Error', ErrorSchema)
|
|
276
|
+
.path('/users/:id', (path) =>
|
|
277
|
+
path.get({
|
|
278
|
+
responses: {
|
|
279
|
+
200: path.ref('User').schema,
|
|
280
|
+
404: path.ref('Error').schema,
|
|
281
|
+
},
|
|
282
|
+
doc: 'Get user by id',
|
|
283
|
+
tags: ['users'],
|
|
284
|
+
operationId: 'users.get',
|
|
285
|
+
}),
|
|
286
|
+
)
|
|
287
|
+
.build()
|
|
288
|
+
|
|
289
|
+
const api = IgniterCaller.create()
|
|
290
|
+
.withBaseUrl('https://api.example.com')
|
|
291
|
+
.withSchemas(callerSchemas, { mode: 'strict' })
|
|
292
|
+
.build()
|
|
293
|
+
|
|
294
|
+
type UserResponse = ReturnType<
|
|
295
|
+
typeof callerSchemas.$Infer.Response<'/users/:id', 'GET', 200>
|
|
296
|
+
>
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
`callerSchemas.get` exposes runtime helpers (`path`, `endpoint`, `request`, `response`, `schema`) and
|
|
300
|
+
`callerSchemas.$Infer` provides type inference without extra imports. `path.ref()` helpers use Zod
|
|
301
|
+
wrappers; when using a different StandardSchema implementation, use `ref().schema` directly.
|
|
302
|
+
|
|
303
|
+
### Manual object literal (still supported)
|
|
236
304
|
|
|
237
305
|
```ts
|
|
238
306
|
import { IgniterCaller } from '@igniter-js/caller'
|
|
@@ -270,10 +338,17 @@ By default this outputs `src/callers/<hostname>/schema.ts` and `index.ts`:
|
|
|
270
338
|
|
|
271
339
|
```ts
|
|
272
340
|
import { facebookCaller } from './src/callers/api.example.com'
|
|
341
|
+
import { facebookCallerSchemas } from './src/callers/api.example.com/schema'
|
|
273
342
|
|
|
274
343
|
const result = await facebookCaller.get('/products').execute()
|
|
344
|
+
type ProductsResponse = ReturnType<
|
|
345
|
+
typeof facebookCallerSchemas.$Infer.Response<'/products', 'GET', 200>
|
|
346
|
+
>
|
|
275
347
|
```
|
|
276
348
|
|
|
349
|
+
The generated `schema.ts` uses `IgniterCallerSchema` (path-first builder), registers reusable
|
|
350
|
+
schemas, and includes derived type aliases for each endpoint.
|
|
351
|
+
|
|
277
352
|
## `responseType()` for Typing and Validation
|
|
278
353
|
|
|
279
354
|
Use `responseType()` to:
|
|
@@ -296,12 +371,12 @@ const result = await api.get('/file').responseType<Blob>().execute()
|
|
|
296
371
|
|
|
297
372
|
## Global Events
|
|
298
373
|
|
|
299
|
-
You can observe responses globally using `
|
|
374
|
+
You can observe responses globally using `IgniterCallerManager.on()`:
|
|
300
375
|
|
|
301
376
|
```ts
|
|
302
|
-
import {
|
|
377
|
+
import { IgniterCallerManager } from '@igniter-js/caller'
|
|
303
378
|
|
|
304
|
-
const unsubscribe =
|
|
379
|
+
const unsubscribe = IgniterCallerManager.on(/^\/users/, (result, ctx) => {
|
|
305
380
|
console.log(`[${ctx.method}] ${ctx.url}`, {
|
|
306
381
|
ok: !result.error,
|
|
307
382
|
status: result.status,
|
|
@@ -312,6 +387,24 @@ const unsubscribe = IgniterCaller.on(/^\/users/, (result, ctx) => {
|
|
|
312
387
|
unsubscribe()
|
|
313
388
|
```
|
|
314
389
|
|
|
390
|
+
## Observability (Telemetry)
|
|
391
|
+
|
|
392
|
+
```ts
|
|
393
|
+
import { IgniterTelemetry } from '@igniter-js/telemetry'
|
|
394
|
+
import { IgniterCaller } from '@igniter-js/caller'
|
|
395
|
+
import { IgniterCallerTelemetryEvents } from '@igniter-js/caller/telemetry'
|
|
396
|
+
|
|
397
|
+
const telemetry = IgniterTelemetry.create()
|
|
398
|
+
.withService('my-api')
|
|
399
|
+
.addEvents(IgniterCallerTelemetryEvents)
|
|
400
|
+
.build()
|
|
401
|
+
|
|
402
|
+
const api = IgniterCaller.create()
|
|
403
|
+
.withBaseUrl('https://api.example.com')
|
|
404
|
+
.withTelemetry(telemetry)
|
|
405
|
+
.build()
|
|
406
|
+
```
|
|
407
|
+
|
|
315
408
|
## Error Handling
|
|
316
409
|
|
|
317
410
|
All predictable failures return an `IgniterCallerError` with stable error codes.
|
|
@@ -351,6 +444,7 @@ Creates a new caller builder.
|
|
|
351
444
|
| `.withResponseInterceptor(fn)` | Adds a response interceptor |
|
|
352
445
|
| `.withStore(store, options)` | Configures a persistent store |
|
|
353
446
|
| `.withSchemas(schemas, options)` | Configures schema validation |
|
|
447
|
+
| `.withTelemetry(telemetry)` | Attaches telemetry manager |
|
|
354
448
|
| `.build()` | Builds the caller instance |
|
|
355
449
|
|
|
356
450
|
### Request Methods
|
|
@@ -385,11 +479,11 @@ Creates a new caller builder.
|
|
|
385
479
|
|
|
386
480
|
| Method | Description |
|
|
387
481
|
|--------|-------------|
|
|
388
|
-
| `
|
|
389
|
-
| `
|
|
390
|
-
| `
|
|
391
|
-
| `
|
|
392
|
-
| `
|
|
482
|
+
| `IgniterCallerManager.on(pattern, callback)` | Registers event listener |
|
|
483
|
+
| `IgniterCallerManager.off(pattern, callback?)` | Removes event listener |
|
|
484
|
+
| `IgniterCallerManager.invalidate(key)` | Invalidates cache entry |
|
|
485
|
+
| `IgniterCallerManager.invalidatePattern(pattern)` | Invalidates cache by pattern |
|
|
486
|
+
| `IgniterCallerManager.batch(requests)` | Executes requests in parallel |
|
|
393
487
|
|
|
394
488
|
## Contributing
|
|
395
489
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { M as MockCallerStoreAdapter } from '../index-COZVROi_.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { M as MockCallerStoreAdapter } from '../index-COZVROi_.js';
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/adapters/mock.adapter.ts
|
|
4
|
+
var MockCallerStoreAdapter = class _MockCallerStoreAdapter {
|
|
5
|
+
constructor() {
|
|
6
|
+
/** Underlying in-memory store. */
|
|
7
|
+
this.client = /* @__PURE__ */ new Map();
|
|
8
|
+
/** Tracks all calls for assertions. */
|
|
9
|
+
this.calls = {
|
|
10
|
+
get: 0,
|
|
11
|
+
set: 0,
|
|
12
|
+
delete: 0,
|
|
13
|
+
has: 0
|
|
14
|
+
};
|
|
15
|
+
/** Captures recent operations. */
|
|
16
|
+
this.history = {
|
|
17
|
+
get: [],
|
|
18
|
+
set: [],
|
|
19
|
+
delete: [],
|
|
20
|
+
has: []
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Creates a new mock adapter instance. */
|
|
24
|
+
static create() {
|
|
25
|
+
return new _MockCallerStoreAdapter();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Retrieves a cached value by key.
|
|
29
|
+
*
|
|
30
|
+
* @param key - Cache key (without prefix).
|
|
31
|
+
* @returns Cached value or null.
|
|
32
|
+
*/
|
|
33
|
+
async get(key) {
|
|
34
|
+
this.calls.get += 1;
|
|
35
|
+
this.history.get.push(key);
|
|
36
|
+
return this.client.has(key) ? this.client.get(key) : null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Stores a cached value.
|
|
40
|
+
*
|
|
41
|
+
* @param key - Cache key (without prefix).
|
|
42
|
+
* @param value - Value to store.
|
|
43
|
+
* @param options - Cache options (ttl, etc).
|
|
44
|
+
*/
|
|
45
|
+
async set(key, value, options) {
|
|
46
|
+
this.calls.set += 1;
|
|
47
|
+
this.history.set.push({ key, value, options });
|
|
48
|
+
this.client.set(key, value);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Removes a cached value.
|
|
52
|
+
*
|
|
53
|
+
* @param key - Cache key (without prefix).
|
|
54
|
+
*/
|
|
55
|
+
async delete(key) {
|
|
56
|
+
this.calls.delete += 1;
|
|
57
|
+
this.history.delete.push(key);
|
|
58
|
+
this.client.delete(key);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Checks if a cached value exists.
|
|
62
|
+
*
|
|
63
|
+
* @param key - Cache key (without prefix).
|
|
64
|
+
* @returns True when the key exists.
|
|
65
|
+
*/
|
|
66
|
+
async has(key) {
|
|
67
|
+
this.calls.has += 1;
|
|
68
|
+
this.history.has.push(key);
|
|
69
|
+
return this.client.has(key);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Clears all tracked state.
|
|
73
|
+
*
|
|
74
|
+
* @returns Nothing.
|
|
75
|
+
*/
|
|
76
|
+
clear() {
|
|
77
|
+
this.client.clear();
|
|
78
|
+
this.calls.get = 0;
|
|
79
|
+
this.calls.set = 0;
|
|
80
|
+
this.calls.delete = 0;
|
|
81
|
+
this.calls.has = 0;
|
|
82
|
+
this.history.get = [];
|
|
83
|
+
this.history.set = [];
|
|
84
|
+
this.history.delete = [];
|
|
85
|
+
this.history.has = [];
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
exports.MockCallerStoreAdapter = MockCallerStoreAdapter;
|
|
90
|
+
//# sourceMappingURL=index.js.map
|
|
91
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/mock.adapter.ts"],"names":[],"mappings":";;;AAUO,IAAM,sBAAA,GAAN,MAAM,uBAAA,CAEb;AAAA,EAFO,WAAA,GAAA;AASL;AAAA,IAAA,IAAA,CAAS,MAAA,uBAAa,GAAA,EAAqB;AAG3C;AAAA,IAAA,IAAA,CAAS,KAAA,GAAQ;AAAA,MACf,GAAA,EAAK,CAAA;AAAA,MACL,GAAA,EAAK,CAAA;AAAA,MACL,MAAA,EAAQ,CAAA;AAAA,MACR,GAAA,EAAK;AAAA,KACP;AAGA;AAAA,IAAA,IAAA,CAAS,OAAA,GAAU;AAAA,MACjB,KAAK,EAAC;AAAA,MACN,KAAK,EAAC;AAAA,MACN,QAAQ,EAAC;AAAA,MACT,KAAK;AAAC,KACR;AAAA,EAAA;AAAA;AAAA,EArBA,OAAO,MAAA,GAAiC;AACtC,IAAA,OAAO,IAAI,uBAAA,EAAuB;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA2BA,MAAM,IAAa,GAAA,EAAgC;AACjD,IAAA,IAAA,CAAK,MAAM,GAAA,IAAO,CAAA;AAClB,IAAA,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAI,IAAA,CAAK,GAAG,CAAA;AACzB,IAAA,OAAQ,IAAA,CAAK,OAAO,GAAA,CAAI,GAAG,IAAK,IAAA,CAAK,MAAA,CAAO,GAAA,CAAI,GAAG,CAAA,GAAU,IAAA;AAAA,EAC/D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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AA,CAAK,OAAA,CAAQ,SAAS,EAAC;AACvB,IAAA,IAAA,CAAK,OAAA,CAAQ,MAAM,EAAC;AAAA,EACtB;AACF","file":"index.js","sourcesContent":["/**\n * @fileoverview In-memory mock store adapter for @igniter-js/caller.\n * @module @igniter-js/caller/adapters/mock\n */\n\nimport type { IgniterCallerStoreAdapter, IgniterCallerStoreOptions } from \"../types/store\";\n\n/**\n * In-memory mock adapter for request caching in tests.\n */\nexport class MockCallerStoreAdapter\n implements IgniterCallerStoreAdapter<Map<string, unknown>>\n{\n /** Creates a new mock adapter instance. */\n static create(): MockCallerStoreAdapter {\n return new MockCallerStoreAdapter();\n }\n\n /** Underlying in-memory store. */\n readonly client = new Map<string, unknown>();\n\n /** Tracks all calls for assertions. */\n readonly calls = {\n get: 0,\n set: 0,\n delete: 0,\n has: 0,\n };\n\n /** Captures recent operations. */\n readonly history = {\n get: [] as string[],\n set: [] as Array<{ key: string; value: unknown; options?: IgniterCallerStoreOptions }>,\n delete: [] as string[],\n has: [] as string[],\n };\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - Cache key (without prefix).\n * @returns Cached value or null.\n */\n async get<T = any>(key: string): Promise<T | null> {\n this.calls.get += 1;\n this.history.get.push(key);\n return (this.client.has(key) ? (this.client.get(key) as T) : null);\n }\n\n /**\n * Stores a cached value.\n *\n * @param key - Cache key (without prefix).\n * @param value - Value to store.\n * @param options - Cache options (ttl, etc).\n */\n async set(\n key: string,\n value: any,\n options?: IgniterCallerStoreOptions,\n ): Promise<void> {\n this.calls.set += 1;\n this.history.set.push({ key, value, options });\n this.client.set(key, value);\n }\n\n /**\n * Removes a cached value.\n *\n * @param key - Cache key (without prefix).\n */\n async delete(key: string): Promise<void> {\n this.calls.delete += 1;\n this.history.delete.push(key);\n this.client.delete(key);\n }\n\n /**\n * Checks if a cached value exists.\n *\n * @param key - Cache key (without prefix).\n * @returns True when the key exists.\n */\n async has(key: string): Promise<boolean> {\n this.calls.has += 1;\n this.history.has.push(key);\n return this.client.has(key);\n }\n\n /**\n * Clears all tracked state.\n *\n * @returns Nothing.\n */\n clear(): void {\n this.client.clear();\n this.calls.get = 0;\n this.calls.set = 0;\n this.calls.delete = 0;\n this.calls.has = 0;\n this.history.get = [];\n this.history.set = [];\n this.history.delete = [];\n this.history.has = [];\n }\n}\n"]}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/adapters/mock.adapter.ts
|
|
2
|
+
var MockCallerStoreAdapter = class _MockCallerStoreAdapter {
|
|
3
|
+
constructor() {
|
|
4
|
+
/** Underlying in-memory store. */
|
|
5
|
+
this.client = /* @__PURE__ */ new Map();
|
|
6
|
+
/** Tracks all calls for assertions. */
|
|
7
|
+
this.calls = {
|
|
8
|
+
get: 0,
|
|
9
|
+
set: 0,
|
|
10
|
+
delete: 0,
|
|
11
|
+
has: 0
|
|
12
|
+
};
|
|
13
|
+
/** Captures recent operations. */
|
|
14
|
+
this.history = {
|
|
15
|
+
get: [],
|
|
16
|
+
set: [],
|
|
17
|
+
delete: [],
|
|
18
|
+
has: []
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** Creates a new mock adapter instance. */
|
|
22
|
+
static create() {
|
|
23
|
+
return new _MockCallerStoreAdapter();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Retrieves a cached value by key.
|
|
27
|
+
*
|
|
28
|
+
* @param key - Cache key (without prefix).
|
|
29
|
+
* @returns Cached value or null.
|
|
30
|
+
*/
|
|
31
|
+
async get(key) {
|
|
32
|
+
this.calls.get += 1;
|
|
33
|
+
this.history.get.push(key);
|
|
34
|
+
return this.client.has(key) ? this.client.get(key) : null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Stores a cached value.
|
|
38
|
+
*
|
|
39
|
+
* @param key - Cache key (without prefix).
|
|
40
|
+
* @param value - Value to store.
|
|
41
|
+
* @param options - Cache options (ttl, etc).
|
|
42
|
+
*/
|
|
43
|
+
async set(key, value, options) {
|
|
44
|
+
this.calls.set += 1;
|
|
45
|
+
this.history.set.push({ key, value, options });
|
|
46
|
+
this.client.set(key, value);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Removes a cached value.
|
|
50
|
+
*
|
|
51
|
+
* @param key - Cache key (without prefix).
|
|
52
|
+
*/
|
|
53
|
+
async delete(key) {
|
|
54
|
+
this.calls.delete += 1;
|
|
55
|
+
this.history.delete.push(key);
|
|
56
|
+
this.client.delete(key);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Checks if a cached value exists.
|
|
60
|
+
*
|
|
61
|
+
* @param key - Cache key (without prefix).
|
|
62
|
+
* @returns True when the key exists.
|
|
63
|
+
*/
|
|
64
|
+
async has(key) {
|
|
65
|
+
this.calls.has += 1;
|
|
66
|
+
this.history.has.push(key);
|
|
67
|
+
return this.client.has(key);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Clears all tracked state.
|
|
71
|
+
*
|
|
72
|
+
* @returns Nothing.
|
|
73
|
+
*/
|
|
74
|
+
clear() {
|
|
75
|
+
this.client.clear();
|
|
76
|
+
this.calls.get = 0;
|
|
77
|
+
this.calls.set = 0;
|
|
78
|
+
this.calls.delete = 0;
|
|
79
|
+
this.calls.has = 0;
|
|
80
|
+
this.history.get = [];
|
|
81
|
+
this.history.set = [];
|
|
82
|
+
this.history.delete = [];
|
|
83
|
+
this.history.has = [];
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export { MockCallerStoreAdapter };
|
|
88
|
+
//# sourceMappingURL=index.mjs.map
|
|
89
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/mock.adapter.ts"],"names":[],"mappings":";AAUO,IAAM,sBAAA,GAAN,MAAM,uBAAA,CAEb;AAAA,EAFO,WAAA,GAAA;AASL;AAAA,IAAA,IAAA,CAAS,MAAA,uBAAa,GAAA,EAAqB;AAG3C;AAA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CAAK,GAAG,CAAA;AAC5B,IAAA,IAAA,CAAK,MAAA,CAAO,OAAO,GAAG,CAAA;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,IAAI,GAAA,EAA+B;AACvC,IAAA,IAAA,CAAK,MAAM,GAAA,IAAO,CAAA;AAClB,IAAA,IAAA,CAAK,OAAA,CAAQ,GAAA,CAAI,IAAA,CAAK,GAAG,CAAA;AACzB,IAAA,OAAO,IAAA,CAAK,MAAA,CAAO,GAAA,CAAI,GAAG,CAAA;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,KAAA,GAAc;AACZ,IAAA,IAAA,CAAK,OAAO,KAAA,EAAM;AAClB,IAAA,IAAA,CAAK,MAAM,GAAA,GAAM,CAAA;AACjB,IAAA,IAAA,CAAK,MAAM,GAAA,GAAM,CAAA;AACjB,IAAA,IAAA,CAAK,MAAM,MAAA,GAAS,CAAA;AACpB,IAAA,IAAA,CAAK,MAAM,GAAA,GAAM,CAAA;AACjB,IAAA,IAAA,CAAK,OAAA,CAAQ,MAAM,EAAC;AACpB,IAAA,IAAA,CAAK,OAAA,CAAQ,MAAM,EAAC;AACpB,IAAA,IAAA,CAAK,OAAA,CAAQ,SAAS,EAAC;AACvB,IAAA,IAAA,CAAK,OAAA,CAAQ,MAAM,EAAC;AAAA,EACtB;AACF","file":"index.mjs","sourcesContent":["/**\n * @fileoverview In-memory mock store adapter for @igniter-js/caller.\n * @module @igniter-js/caller/adapters/mock\n */\n\nimport type { IgniterCallerStoreAdapter, IgniterCallerStoreOptions } from \"../types/store\";\n\n/**\n * In-memory mock adapter for request caching in tests.\n */\nexport class MockCallerStoreAdapter\n implements IgniterCallerStoreAdapter<Map<string, unknown>>\n{\n /** Creates a new mock adapter instance. */\n static create(): MockCallerStoreAdapter {\n return new MockCallerStoreAdapter();\n }\n\n /** Underlying in-memory store. */\n readonly client = new Map<string, unknown>();\n\n /** Tracks all calls for assertions. */\n readonly calls = {\n get: 0,\n set: 0,\n delete: 0,\n has: 0,\n };\n\n /** Captures recent operations. */\n readonly history = {\n get: [] as string[],\n set: [] as Array<{ key: string; value: unknown; options?: IgniterCallerStoreOptions }>,\n delete: [] as string[],\n has: [] as string[],\n };\n\n /**\n * Retrieves a cached value by key.\n *\n * @param key - Cache key (without prefix).\n * @returns Cached value or null.\n */\n async get<T = any>(key: string): Promise<T | null> {\n this.calls.get += 1;\n this.history.get.push(key);\n return (this.client.has(key) ? (this.client.get(key) as T) : null);\n }\n\n /**\n * Stores a cached value.\n *\n * @param key - Cache key (without prefix).\n * @param value - Value to store.\n * @param options - Cache options (ttl, etc).\n */\n async set(\n key: string,\n value: any,\n options?: IgniterCallerStoreOptions,\n ): Promise<void> {\n this.calls.set += 1;\n this.history.set.push({ key, value, options });\n this.client.set(key, value);\n }\n\n /**\n * Removes a cached value.\n *\n * @param key - Cache key (without prefix).\n */\n async delete(key: string): Promise<void> {\n this.calls.delete += 1;\n this.history.delete.push(key);\n this.client.delete(key);\n }\n\n /**\n * Checks if a cached value exists.\n *\n * @param key - Cache key (without prefix).\n * @returns True when the key exists.\n */\n async has(key: string): Promise<boolean> {\n this.calls.has += 1;\n this.history.has.push(key);\n return this.client.has(key);\n }\n\n /**\n * Clears all tracked state.\n *\n * @returns Nothing.\n */\n clear(): void {\n this.client.clear();\n this.calls.get = 0;\n this.calls.set = 0;\n this.calls.delete = 0;\n this.calls.has = 0;\n this.history.get = [];\n this.history.set = [];\n this.history.delete = [];\n this.history.has = [];\n }\n}\n"]}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Store adapter interface compatible with Igniter.js Store.
|
|
3
|
+
*
|
|
4
|
+
* This allows IgniterCaller to use any store implementation (Redis, in-memory, etc.)
|
|
5
|
+
* for persistent caching across requests and deployments.
|
|
6
|
+
*/
|
|
7
|
+
interface IgniterCallerStoreAdapter<TClient = any> {
|
|
8
|
+
/** The underlying client instance (e.g., Redis client). */
|
|
9
|
+
readonly client: TClient;
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves a value from the store by its key.
|
|
12
|
+
*
|
|
13
|
+
* @param key - The key to retrieve.
|
|
14
|
+
* @returns The value if found (auto-deserialized), otherwise null.
|
|
15
|
+
*/
|
|
16
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
17
|
+
/**
|
|
18
|
+
* Stores a value in the store.
|
|
19
|
+
*
|
|
20
|
+
* @param key - The key to store the value under.
|
|
21
|
+
* @param value - The value to store (will be auto-serialized).
|
|
22
|
+
* @param options - Configuration options, such as TTL.
|
|
23
|
+
*/
|
|
24
|
+
set(key: string, value: any, options?: {
|
|
25
|
+
ttl?: number;
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Deletes a key from the store.
|
|
30
|
+
*
|
|
31
|
+
* @param key - The key to delete.
|
|
32
|
+
*/
|
|
33
|
+
delete(key: string): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Checks if a key exists in the store.
|
|
36
|
+
*
|
|
37
|
+
* @param key - The key to check.
|
|
38
|
+
* @returns `true` if the key exists, otherwise `false`.
|
|
39
|
+
*/
|
|
40
|
+
has(key: string): Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration options for store-based caching.
|
|
44
|
+
*/
|
|
45
|
+
interface IgniterCallerStoreOptions {
|
|
46
|
+
/** Default TTL in seconds for cached entries (default: 3600 = 1 hour). */
|
|
47
|
+
ttl?: number;
|
|
48
|
+
/** Prefix for all cache keys (default: 'igniter:caller:'). */
|
|
49
|
+
keyPrefix?: string;
|
|
50
|
+
/** Whether to fallback to fetch cache when store is unavailable (default: true). */
|
|
51
|
+
fallbackToFetch?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @fileoverview In-memory mock store adapter for @igniter-js/caller.
|
|
56
|
+
* @module @igniter-js/caller/adapters/mock
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* In-memory mock adapter for request caching in tests.
|
|
61
|
+
*/
|
|
62
|
+
declare class MockCallerStoreAdapter implements IgniterCallerStoreAdapter<Map<string, unknown>> {
|
|
63
|
+
/** Creates a new mock adapter instance. */
|
|
64
|
+
static create(): MockCallerStoreAdapter;
|
|
65
|
+
/** Underlying in-memory store. */
|
|
66
|
+
readonly client: Map<string, unknown>;
|
|
67
|
+
/** Tracks all calls for assertions. */
|
|
68
|
+
readonly calls: {
|
|
69
|
+
get: number;
|
|
70
|
+
set: number;
|
|
71
|
+
delete: number;
|
|
72
|
+
has: number;
|
|
73
|
+
};
|
|
74
|
+
/** Captures recent operations. */
|
|
75
|
+
readonly history: {
|
|
76
|
+
get: string[];
|
|
77
|
+
set: Array<{
|
|
78
|
+
key: string;
|
|
79
|
+
value: unknown;
|
|
80
|
+
options?: IgniterCallerStoreOptions;
|
|
81
|
+
}>;
|
|
82
|
+
delete: string[];
|
|
83
|
+
has: string[];
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Retrieves a cached value by key.
|
|
87
|
+
*
|
|
88
|
+
* @param key - Cache key (without prefix).
|
|
89
|
+
* @returns Cached value or null.
|
|
90
|
+
*/
|
|
91
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
92
|
+
/**
|
|
93
|
+
* Stores a cached value.
|
|
94
|
+
*
|
|
95
|
+
* @param key - Cache key (without prefix).
|
|
96
|
+
* @param value - Value to store.
|
|
97
|
+
* @param options - Cache options (ttl, etc).
|
|
98
|
+
*/
|
|
99
|
+
set(key: string, value: any, options?: IgniterCallerStoreOptions): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Removes a cached value.
|
|
102
|
+
*
|
|
103
|
+
* @param key - Cache key (without prefix).
|
|
104
|
+
*/
|
|
105
|
+
delete(key: string): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Checks if a cached value exists.
|
|
108
|
+
*
|
|
109
|
+
* @param key - Cache key (without prefix).
|
|
110
|
+
* @returns True when the key exists.
|
|
111
|
+
*/
|
|
112
|
+
has(key: string): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* Clears all tracked state.
|
|
115
|
+
*
|
|
116
|
+
* @returns Nothing.
|
|
117
|
+
*/
|
|
118
|
+
clear(): void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { type IgniterCallerStoreAdapter as I, MockCallerStoreAdapter as M, type IgniterCallerStoreOptions as a };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Store adapter interface compatible with Igniter.js Store.
|
|
3
|
+
*
|
|
4
|
+
* This allows IgniterCaller to use any store implementation (Redis, in-memory, etc.)
|
|
5
|
+
* for persistent caching across requests and deployments.
|
|
6
|
+
*/
|
|
7
|
+
interface IgniterCallerStoreAdapter<TClient = any> {
|
|
8
|
+
/** The underlying client instance (e.g., Redis client). */
|
|
9
|
+
readonly client: TClient;
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves a value from the store by its key.
|
|
12
|
+
*
|
|
13
|
+
* @param key - The key to retrieve.
|
|
14
|
+
* @returns The value if found (auto-deserialized), otherwise null.
|
|
15
|
+
*/
|
|
16
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
17
|
+
/**
|
|
18
|
+
* Stores a value in the store.
|
|
19
|
+
*
|
|
20
|
+
* @param key - The key to store the value under.
|
|
21
|
+
* @param value - The value to store (will be auto-serialized).
|
|
22
|
+
* @param options - Configuration options, such as TTL.
|
|
23
|
+
*/
|
|
24
|
+
set(key: string, value: any, options?: {
|
|
25
|
+
ttl?: number;
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Deletes a key from the store.
|
|
30
|
+
*
|
|
31
|
+
* @param key - The key to delete.
|
|
32
|
+
*/
|
|
33
|
+
delete(key: string): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Checks if a key exists in the store.
|
|
36
|
+
*
|
|
37
|
+
* @param key - The key to check.
|
|
38
|
+
* @returns `true` if the key exists, otherwise `false`.
|
|
39
|
+
*/
|
|
40
|
+
has(key: string): Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration options for store-based caching.
|
|
44
|
+
*/
|
|
45
|
+
interface IgniterCallerStoreOptions {
|
|
46
|
+
/** Default TTL in seconds for cached entries (default: 3600 = 1 hour). */
|
|
47
|
+
ttl?: number;
|
|
48
|
+
/** Prefix for all cache keys (default: 'igniter:caller:'). */
|
|
49
|
+
keyPrefix?: string;
|
|
50
|
+
/** Whether to fallback to fetch cache when store is unavailable (default: true). */
|
|
51
|
+
fallbackToFetch?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @fileoverview In-memory mock store adapter for @igniter-js/caller.
|
|
56
|
+
* @module @igniter-js/caller/adapters/mock
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* In-memory mock adapter for request caching in tests.
|
|
61
|
+
*/
|
|
62
|
+
declare class MockCallerStoreAdapter implements IgniterCallerStoreAdapter<Map<string, unknown>> {
|
|
63
|
+
/** Creates a new mock adapter instance. */
|
|
64
|
+
static create(): MockCallerStoreAdapter;
|
|
65
|
+
/** Underlying in-memory store. */
|
|
66
|
+
readonly client: Map<string, unknown>;
|
|
67
|
+
/** Tracks all calls for assertions. */
|
|
68
|
+
readonly calls: {
|
|
69
|
+
get: number;
|
|
70
|
+
set: number;
|
|
71
|
+
delete: number;
|
|
72
|
+
has: number;
|
|
73
|
+
};
|
|
74
|
+
/** Captures recent operations. */
|
|
75
|
+
readonly history: {
|
|
76
|
+
get: string[];
|
|
77
|
+
set: Array<{
|
|
78
|
+
key: string;
|
|
79
|
+
value: unknown;
|
|
80
|
+
options?: IgniterCallerStoreOptions;
|
|
81
|
+
}>;
|
|
82
|
+
delete: string[];
|
|
83
|
+
has: string[];
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Retrieves a cached value by key.
|
|
87
|
+
*
|
|
88
|
+
* @param key - Cache key (without prefix).
|
|
89
|
+
* @returns Cached value or null.
|
|
90
|
+
*/
|
|
91
|
+
get<T = any>(key: string): Promise<T | null>;
|
|
92
|
+
/**
|
|
93
|
+
* Stores a cached value.
|
|
94
|
+
*
|
|
95
|
+
* @param key - Cache key (without prefix).
|
|
96
|
+
* @param value - Value to store.
|
|
97
|
+
* @param options - Cache options (ttl, etc).
|
|
98
|
+
*/
|
|
99
|
+
set(key: string, value: any, options?: IgniterCallerStoreOptions): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Removes a cached value.
|
|
102
|
+
*
|
|
103
|
+
* @param key - Cache key (without prefix).
|
|
104
|
+
*/
|
|
105
|
+
delete(key: string): Promise<void>;
|
|
106
|
+
/**
|
|
107
|
+
* Checks if a cached value exists.
|
|
108
|
+
*
|
|
109
|
+
* @param key - Cache key (without prefix).
|
|
110
|
+
* @returns True when the key exists.
|
|
111
|
+
*/
|
|
112
|
+
has(key: string): Promise<boolean>;
|
|
113
|
+
/**
|
|
114
|
+
* Clears all tracked state.
|
|
115
|
+
*
|
|
116
|
+
* @returns Nothing.
|
|
117
|
+
*/
|
|
118
|
+
clear(): void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { type IgniterCallerStoreAdapter as I, MockCallerStoreAdapter as M, type IgniterCallerStoreOptions as a };
|