@outfitter/state 0.2.3 → 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.
@@ -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,11 +1,25 @@
1
1
  {
2
2
  "name": "@outfitter/state",
3
+ "version": "0.2.5",
3
4
  "description": "Pagination cursor persistence and state management for Outfitter",
4
- "version": "0.2.3",
5
- "type": "module",
5
+ "keywords": [
6
+ "cursor",
7
+ "outfitter",
8
+ "pagination",
9
+ "state",
10
+ "typescript"
11
+ ],
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/outfitter-dev/outfitter.git",
16
+ "directory": "packages/state"
17
+ },
6
18
  "files": [
7
19
  "dist"
8
20
  ],
21
+ "type": "module",
22
+ "sideEffects": false,
9
23
  "module": "./dist/index.js",
10
24
  "types": "./dist/index.d.ts",
11
25
  "exports": {
@@ -17,38 +31,24 @@
17
31
  },
18
32
  "./package.json": "./package.json"
19
33
  },
20
- "sideEffects": false,
34
+ "publishConfig": {
35
+ "access": "public"
36
+ },
21
37
  "scripts": {
22
- "build": "bunup --filter @outfitter/state",
23
- "lint": "biome lint ./src",
24
- "lint:fix": "biome lint --write ./src",
38
+ "build": "cd ../.. && bash ./scripts/run-bunup-with-lock.sh bunup --filter @outfitter/state",
39
+ "lint": "oxlint ./src",
40
+ "lint:fix": "oxlint --fix ./src",
25
41
  "test": "bun test",
26
42
  "typecheck": "tsc --noEmit",
27
43
  "clean": "rm -rf dist",
28
44
  "prepublishOnly": "bun ../../scripts/check-publish-manifest.ts"
29
45
  },
30
46
  "dependencies": {
31
- "@outfitter/contracts": "0.4.1",
32
- "@outfitter/types": "0.2.3"
47
+ "@outfitter/contracts": "0.5.0",
48
+ "@outfitter/types": "0.2.5"
33
49
  },
34
50
  "devDependencies": {
35
- "@types/bun": "latest",
36
- "typescript": "^5.8.0"
37
- },
38
- "keywords": [
39
- "outfitter",
40
- "state",
41
- "pagination",
42
- "cursor",
43
- "typescript"
44
- ],
45
- "license": "MIT",
46
- "repository": {
47
- "type": "git",
48
- "url": "https://github.com/outfitter-dev/outfitter.git",
49
- "directory": "packages/state"
50
- },
51
- "publishConfig": {
52
- "access": "public"
51
+ "@types/bun": "^1.3.9",
52
+ "typescript": "^5.9.3"
53
53
  }
54
54
  }