@oncely/core 0.2.0

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.
@@ -0,0 +1,119 @@
1
+ import { M as MemoryStorage, A as AcquireResult, f as StoredResponse, g as OncelyOptions, a as Oncely, O as OncelyConfig } from './errors-BUehgS6t.cjs';
2
+ export { C as ConflictError, I as IdempotencyError, d as MismatchError, b as MissingKeyError, R as RunOptions, h as RunResult, S as StorageAdapter, e as StorageError } from './errors-BUehgS6t.cjs';
3
+
4
+ /**
5
+ * Mock storage for testing.
6
+ * Same as MemoryStorage but with additional test utilities.
7
+ */
8
+ declare class MockStorage extends MemoryStorage {
9
+ /** Track all operations for assertions */
10
+ readonly operations: Array<{
11
+ type: string;
12
+ key: string;
13
+ timestamp: number;
14
+ }>;
15
+ /** Simulated conflict keys */
16
+ private simulatedConflicts;
17
+ /** Simulated mismatch keys */
18
+ private simulatedMismatches;
19
+ acquire(key: string, hash: string | null, ttl: number): Promise<AcquireResult>;
20
+ save(key: string, response: StoredResponse): Promise<void>;
21
+ release(key: string): Promise<void>;
22
+ delete(key: string): Promise<void>;
23
+ clear(): Promise<void>;
24
+ /**
25
+ * Get operations for a specific key.
26
+ */
27
+ getOperationsForKey(key: string): {
28
+ type: string;
29
+ key: string;
30
+ timestamp: number;
31
+ }[];
32
+ /**
33
+ * Assert that a key was acquired.
34
+ */
35
+ assertAcquired(key: string): void;
36
+ /**
37
+ * Assert that a key was saved (cached).
38
+ */
39
+ assertCached(key: string): void;
40
+ /**
41
+ * Assert that a key was NOT saved (not cached).
42
+ */
43
+ assertNotCached(key: string): void;
44
+ /**
45
+ * Assert that a key was released.
46
+ */
47
+ assertReleased(key: string): void;
48
+ /**
49
+ * Simulate a conflict error for a specific key.
50
+ * The next acquire for this key will return conflict status.
51
+ */
52
+ simulateConflict(key: string): void;
53
+ /**
54
+ * Simulate a mismatch error for a specific key.
55
+ * The next acquire for this key will return mismatch status.
56
+ */
57
+ simulateMismatch(key: string, existingHash?: string, providedHash?: string): void;
58
+ /**
59
+ * Clear all simulations.
60
+ */
61
+ clearSimulations(): void;
62
+ }
63
+ /**
64
+ * Create an oncely instance configured for testing.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * import { createTestInstance, MockStorage } from '@oncely/core/testing';
69
+ *
70
+ * describe('my api', () => {
71
+ * const storage = new MockStorage();
72
+ * const idempotency = createTestInstance({ storage });
73
+ *
74
+ * beforeEach(() => storage.clear());
75
+ *
76
+ * it('handles duplicate requests', async () => {
77
+ * const handler = vi.fn().mockResolvedValue({ id: 1 });
78
+ *
79
+ * await idempotency.run({ key: 'test', handler });
80
+ * await idempotency.run({ key: 'test', handler });
81
+ *
82
+ * expect(handler).toHaveBeenCalledTimes(1);
83
+ * storage.assertCached('test');
84
+ * });
85
+ * });
86
+ * ```
87
+ */
88
+ declare function createTestInstance(options?: Partial<OncelyOptions>): Oncely;
89
+ /**
90
+ * @deprecated Use createTestInstance instead.
91
+ */
92
+ declare const createTestOncely: typeof createTestInstance;
93
+ /**
94
+ * Set up oncely for testing with auto-reset between tests.
95
+ * Returns utilities for testing assertions.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * import { setupTest } from '@oncely/core/testing';
100
+ *
101
+ * describe('my api', () => {
102
+ * const { storage, instance, reset } = setupTest();
103
+ *
104
+ * beforeEach(() => reset());
105
+ *
106
+ * it('caches responses', async () => {
107
+ * await instance.run({ key: 'test', handler: () => ({ ok: true }) });
108
+ * storage.assertCached('test');
109
+ * });
110
+ * });
111
+ * ```
112
+ */
113
+ declare function setupTest(config?: OncelyConfig): {
114
+ storage: MockStorage;
115
+ instance: Oncely;
116
+ reset: () => void;
117
+ };
118
+
119
+ export { AcquireResult, MockStorage, OncelyConfig, OncelyOptions, StoredResponse, createTestInstance, createTestOncely, setupTest };
@@ -0,0 +1,119 @@
1
+ import { M as MemoryStorage, A as AcquireResult, f as StoredResponse, g as OncelyOptions, a as Oncely, O as OncelyConfig } from './errors-BUehgS6t.js';
2
+ export { C as ConflictError, I as IdempotencyError, d as MismatchError, b as MissingKeyError, R as RunOptions, h as RunResult, S as StorageAdapter, e as StorageError } from './errors-BUehgS6t.js';
3
+
4
+ /**
5
+ * Mock storage for testing.
6
+ * Same as MemoryStorage but with additional test utilities.
7
+ */
8
+ declare class MockStorage extends MemoryStorage {
9
+ /** Track all operations for assertions */
10
+ readonly operations: Array<{
11
+ type: string;
12
+ key: string;
13
+ timestamp: number;
14
+ }>;
15
+ /** Simulated conflict keys */
16
+ private simulatedConflicts;
17
+ /** Simulated mismatch keys */
18
+ private simulatedMismatches;
19
+ acquire(key: string, hash: string | null, ttl: number): Promise<AcquireResult>;
20
+ save(key: string, response: StoredResponse): Promise<void>;
21
+ release(key: string): Promise<void>;
22
+ delete(key: string): Promise<void>;
23
+ clear(): Promise<void>;
24
+ /**
25
+ * Get operations for a specific key.
26
+ */
27
+ getOperationsForKey(key: string): {
28
+ type: string;
29
+ key: string;
30
+ timestamp: number;
31
+ }[];
32
+ /**
33
+ * Assert that a key was acquired.
34
+ */
35
+ assertAcquired(key: string): void;
36
+ /**
37
+ * Assert that a key was saved (cached).
38
+ */
39
+ assertCached(key: string): void;
40
+ /**
41
+ * Assert that a key was NOT saved (not cached).
42
+ */
43
+ assertNotCached(key: string): void;
44
+ /**
45
+ * Assert that a key was released.
46
+ */
47
+ assertReleased(key: string): void;
48
+ /**
49
+ * Simulate a conflict error for a specific key.
50
+ * The next acquire for this key will return conflict status.
51
+ */
52
+ simulateConflict(key: string): void;
53
+ /**
54
+ * Simulate a mismatch error for a specific key.
55
+ * The next acquire for this key will return mismatch status.
56
+ */
57
+ simulateMismatch(key: string, existingHash?: string, providedHash?: string): void;
58
+ /**
59
+ * Clear all simulations.
60
+ */
61
+ clearSimulations(): void;
62
+ }
63
+ /**
64
+ * Create an oncely instance configured for testing.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * import { createTestInstance, MockStorage } from '@oncely/core/testing';
69
+ *
70
+ * describe('my api', () => {
71
+ * const storage = new MockStorage();
72
+ * const idempotency = createTestInstance({ storage });
73
+ *
74
+ * beforeEach(() => storage.clear());
75
+ *
76
+ * it('handles duplicate requests', async () => {
77
+ * const handler = vi.fn().mockResolvedValue({ id: 1 });
78
+ *
79
+ * await idempotency.run({ key: 'test', handler });
80
+ * await idempotency.run({ key: 'test', handler });
81
+ *
82
+ * expect(handler).toHaveBeenCalledTimes(1);
83
+ * storage.assertCached('test');
84
+ * });
85
+ * });
86
+ * ```
87
+ */
88
+ declare function createTestInstance(options?: Partial<OncelyOptions>): Oncely;
89
+ /**
90
+ * @deprecated Use createTestInstance instead.
91
+ */
92
+ declare const createTestOncely: typeof createTestInstance;
93
+ /**
94
+ * Set up oncely for testing with auto-reset between tests.
95
+ * Returns utilities for testing assertions.
96
+ *
97
+ * @example
98
+ * ```typescript
99
+ * import { setupTest } from '@oncely/core/testing';
100
+ *
101
+ * describe('my api', () => {
102
+ * const { storage, instance, reset } = setupTest();
103
+ *
104
+ * beforeEach(() => reset());
105
+ *
106
+ * it('caches responses', async () => {
107
+ * await instance.run({ key: 'test', handler: () => ({ ok: true }) });
108
+ * storage.assertCached('test');
109
+ * });
110
+ * });
111
+ * ```
112
+ */
113
+ declare function setupTest(config?: OncelyConfig): {
114
+ storage: MockStorage;
115
+ instance: Oncely;
116
+ reset: () => void;
117
+ };
118
+
119
+ export { AcquireResult, MockStorage, OncelyConfig, OncelyOptions, StoredResponse, createTestInstance, createTestOncely, setupTest };