@outfitter/state 0.2.4 → 0.2.5
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/dist/index.d.ts +4 -619
- package/dist/index.js +3 -409
- package/dist/internal/cursor.d.ts +3 -0
- package/dist/internal/cursor.js +15 -0
- package/dist/internal/pagination.d.ts +3 -0
- package/dist/internal/pagination.js +59 -0
- package/dist/internal/stores.d.ts +3 -0
- package/dist/internal/stores.js +208 -0
- package/dist/internal/types.d.ts +2 -0
- package/dist/internal/types.js +1 -0
- package/dist/shared/@outfitter/state-3n2wh1yq.js +154 -0
- package/dist/shared/@outfitter/state-6h22x3e6.d.ts +264 -0
- package/dist/shared/@outfitter/state-9q2yv8dy.d.ts +133 -0
- package/dist/shared/@outfitter/state-cgc92xms.d.ts +139 -0
- package/dist/shared/@outfitter/state-y5wh3wpn.d.ts +139 -0
- package/package.json +4 -4
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { Cursor, PaginationResult, PaginationStore } from "./state-6h22x3e6.js";
|
|
2
|
+
import { Result, StorageError } from "@outfitter/contracts";
|
|
3
|
+
/**
|
|
4
|
+
* Default page size when limit is not specified in cursor metadata.
|
|
5
|
+
*/
|
|
6
|
+
declare const DEFAULT_PAGE_LIMIT = 25;
|
|
7
|
+
/**
|
|
8
|
+
* Get the default pagination store (module-level singleton).
|
|
9
|
+
*
|
|
10
|
+
* The default store is lazily initialized on first access.
|
|
11
|
+
* Use this when you want cursors to persist across multiple
|
|
12
|
+
* load/save calls within the same process.
|
|
13
|
+
*
|
|
14
|
+
* @returns The default pagination store instance
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const store = getDefaultPaginationStore();
|
|
19
|
+
* store.set("cursor-1", cursor);
|
|
20
|
+
*
|
|
21
|
+
* // Later, same store is returned
|
|
22
|
+
* const sameStore = getDefaultPaginationStore();
|
|
23
|
+
* sameStore.get("cursor-1"); // Returns the cursor
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare function getDefaultPaginationStore(): PaginationStore;
|
|
27
|
+
/**
|
|
28
|
+
* Create an in-memory pagination store.
|
|
29
|
+
*
|
|
30
|
+
* This is a simple Map-backed store for cursor persistence.
|
|
31
|
+
* Unlike {@link createCursorStore}, this store does not handle
|
|
32
|
+
* TTL/expiration - it's designed for simple pagination use cases.
|
|
33
|
+
*
|
|
34
|
+
* @returns A new pagination store instance
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const store = createPaginationStore();
|
|
39
|
+
*
|
|
40
|
+
* const cursor = createCursor({ position: 0, metadata: { limit: 25 } });
|
|
41
|
+
* if (cursor.isOk()) {
|
|
42
|
+
* store.set(cursor.value.id, cursor.value);
|
|
43
|
+
* const retrieved = store.get(cursor.value.id);
|
|
44
|
+
* }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare function createPaginationStore(): PaginationStore;
|
|
48
|
+
/**
|
|
49
|
+
* Extract a page of items based on cursor position.
|
|
50
|
+
*
|
|
51
|
+
* Uses `cursor.position` as the offset and `cursor.metadata.limit` as
|
|
52
|
+
* the page size (defaults to {@link DEFAULT_PAGE_LIMIT} if not specified).
|
|
53
|
+
*
|
|
54
|
+
* Returns a `nextCursor` for fetching the next page, or `null` if there
|
|
55
|
+
* are no more items (i.e., this is the last page).
|
|
56
|
+
*
|
|
57
|
+
* @typeParam T - The type of items being paginated
|
|
58
|
+
* @param items - The full array of items to paginate
|
|
59
|
+
* @param cursor - Cursor containing position (offset) and optionally limit in metadata
|
|
60
|
+
* @returns Object containing the page slice and next cursor (or null)
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
65
|
+
* const cursor = createCursor({
|
|
66
|
+
* position: 0,
|
|
67
|
+
* metadata: { limit: 3 },
|
|
68
|
+
* });
|
|
69
|
+
*
|
|
70
|
+
* if (cursor.isOk()) {
|
|
71
|
+
* const { page, nextCursor } = paginate(items, cursor.value);
|
|
72
|
+
* console.log(page); // [1, 2, 3]
|
|
73
|
+
* console.log(nextCursor?.position); // 3
|
|
74
|
+
*
|
|
75
|
+
* if (nextCursor) {
|
|
76
|
+
* const { page: page2 } = paginate(items, nextCursor);
|
|
77
|
+
* console.log(page2); // [4, 5, 6]
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function paginate<T>(items: T[], cursor: Cursor): PaginationResult<T>;
|
|
83
|
+
/**
|
|
84
|
+
* Load a cursor from a pagination store.
|
|
85
|
+
*
|
|
86
|
+
* Returns `Ok(null)` if the cursor is not found (not an error).
|
|
87
|
+
* This differs from {@link CursorStore.get} which returns a `NotFoundError`.
|
|
88
|
+
*
|
|
89
|
+
* @param id - The cursor ID to load
|
|
90
|
+
* @param store - Optional store to load from (defaults to module-level store)
|
|
91
|
+
* @returns Result containing the cursor or null if not found
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* // Using default store
|
|
96
|
+
* const result = loadCursor("my-cursor");
|
|
97
|
+
* if (result.isOk()) {
|
|
98
|
+
* if (result.value) {
|
|
99
|
+
* console.log(`Found cursor at position ${result.value.position}`);
|
|
100
|
+
* } else {
|
|
101
|
+
* console.log("Cursor not found, starting fresh");
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
*
|
|
105
|
+
* // Using custom store
|
|
106
|
+
* const store = createPaginationStore();
|
|
107
|
+
* const result = loadCursor("my-cursor", store);
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare function loadCursor(id: string, store?: PaginationStore): Result<Cursor | null, StorageError>;
|
|
111
|
+
/**
|
|
112
|
+
* Save a cursor to a pagination store.
|
|
113
|
+
*
|
|
114
|
+
* The cursor is stored by its `id` property.
|
|
115
|
+
*
|
|
116
|
+
* @param cursor - The cursor to save
|
|
117
|
+
* @param store - Optional store to save to (defaults to module-level store)
|
|
118
|
+
* @returns Result indicating success or storage error
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const cursor = createCursor({
|
|
123
|
+
* id: "search-results",
|
|
124
|
+
* position: 50,
|
|
125
|
+
* metadata: { limit: 25, query: "status:open" },
|
|
126
|
+
* });
|
|
127
|
+
*
|
|
128
|
+
* if (cursor.isOk()) {
|
|
129
|
+
* // Save to default store
|
|
130
|
+
* saveCursor(cursor.value);
|
|
131
|
+
*
|
|
132
|
+
* // Or save to custom store
|
|
133
|
+
* const store = createPaginationStore();
|
|
134
|
+
* saveCursor(cursor.value, store);
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
declare function saveCursor(cursor: Cursor, store?: PaginationStore): Result<void, StorageError>;
|
|
139
|
+
export { DEFAULT_PAGE_LIMIT, getDefaultPaginationStore, createPaginationStore, paginate, loadCursor, saveCursor };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@outfitter/state",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "Pagination cursor persistence and state management for Outfitter",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cursor",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"access": "public"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
|
-
"build": "cd ../.. && bunup --filter @outfitter/state",
|
|
38
|
+
"build": "cd ../.. && bash ./scripts/run-bunup-with-lock.sh bunup --filter @outfitter/state",
|
|
39
39
|
"lint": "oxlint ./src",
|
|
40
40
|
"lint:fix": "oxlint --fix ./src",
|
|
41
41
|
"test": "bun test",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"prepublishOnly": "bun ../../scripts/check-publish-manifest.ts"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@outfitter/contracts": "0.
|
|
48
|
-
"@outfitter/types": "0.2.
|
|
47
|
+
"@outfitter/contracts": "0.5.0",
|
|
48
|
+
"@outfitter/types": "0.2.5"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@types/bun": "^1.3.9",
|