@choosemycompany/lib 0.10.1 → 1.0.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.
- package/README.md +2 -3
- package/dist/http/types.d.ts +5 -0
- package/dist/index.d.ts +8 -3
- package/dist/index.js +667 -318
- package/dist/store/MissingItemIdError.d.ts +20 -0
- package/dist/store/StoreResetError.d.ts +18 -0
- package/dist/store/types.d.ts +239 -14
- package/dist/store/useStore.d.ts +88 -32
- package/package.json +2 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown when the store receives an item in which `getItemId` — the one given
|
|
3
|
+
* in the store options, or the default that reads `item.id` — finds no id,
|
|
4
|
+
* and no id to fall back on is known. The item breaks the type the API
|
|
5
|
+
* promises: a contract error of the API, not a case the store plans for.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* try {
|
|
10
|
+
* return await create(payload)
|
|
11
|
+
* }
|
|
12
|
+
* catch (error) {
|
|
13
|
+
* if (error instanceof MissingItemIdError) throw error // the API answered without an id
|
|
14
|
+
* registerValidationErrors.value = extractApiErrors(error)
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare class MissingItemIdError extends Error {
|
|
19
|
+
constructor(hasCustomGetItemId: boolean);
|
|
20
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rejection a `queue` task of `withPending` or `mutate` receives when the
|
|
3
|
+
* store is reset before its turn comes: the task never starts.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* try {
|
|
8
|
+
* await withPending(scope, () => upsertDependency(questionId, draft), { mode: 'queue' })
|
|
9
|
+
* }
|
|
10
|
+
* catch (error) {
|
|
11
|
+
* if (error instanceof StoreResetError) return
|
|
12
|
+
* throw error
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class StoreResetError extends Error {
|
|
17
|
+
constructor();
|
|
18
|
+
}
|
package/dist/store/types.d.ts
CHANGED
|
@@ -25,13 +25,6 @@ export type View<Item> = ViewKeyParams & {
|
|
|
25
25
|
/** `true` if more pages exist after the current one. */
|
|
26
26
|
hasMore: boolean;
|
|
27
27
|
};
|
|
28
|
-
/**
|
|
29
|
-
* Reserved for "infinite scroll vs. paginated" switching. Not read by the
|
|
30
|
-
* implementation — declared here to lock the public surface.
|
|
31
|
-
*/
|
|
32
|
-
export type ViewOptions = {
|
|
33
|
-
infinite?: boolean;
|
|
34
|
-
};
|
|
35
28
|
/**
|
|
36
29
|
* Permitted shape for an item identifier. Restricting to `string | number`
|
|
37
30
|
* keeps `Record<Id, Item>` indexing tractable on TypeScript ≥ 5 strict mode.
|
|
@@ -49,12 +42,11 @@ export type IdKey = string | number;
|
|
|
49
42
|
* @typeParam UpdatePatch - Shape passed to `updateMutation` (defaults to `Partial<Item>`).
|
|
50
43
|
*/
|
|
51
44
|
export type StoreOptions<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePayload = Item, UpdatePatch = Partial<Item>> = {
|
|
52
|
-
/** Default query parameters. Held for reference, never read by the store itself. */
|
|
53
|
-
params?: Params;
|
|
54
45
|
/**
|
|
55
|
-
* Computes the cache key for an item. Defaults to `(item) => item.id
|
|
56
|
-
*
|
|
57
|
-
*
|
|
46
|
+
* Computes the cache key for an item. Defaults to `(item) => item.id`;
|
|
47
|
+
* provide it when items use a non-standard identifier (`uuid`, `slug`,
|
|
48
|
+
* composite key, …). An item in which it finds no id breaks the type the API
|
|
49
|
+
* promises, and the store raises a `MissingItemIdError`.
|
|
58
50
|
*/
|
|
59
51
|
getItemId?: (item: Item) => Id;
|
|
60
52
|
/** Fetches a single item by id. Required to call `retrieve()`. */
|
|
@@ -62,10 +54,54 @@ export type StoreOptions<Id extends IdKey, Item, Params extends ViewKeyParams, C
|
|
|
62
54
|
/** Fetches one page. Required to call `list()`. */
|
|
63
55
|
fetchPage?: (params: Params) => Promise<View<Item>>;
|
|
64
56
|
/**
|
|
65
|
-
* Transformation applied to every item before it lands in `items
|
|
66
|
-
*
|
|
57
|
+
* Transformation applied to every item before it lands in `items`: those a
|
|
58
|
+
* fetch brings in, those `setItem` receives, and those `createMutation`,
|
|
59
|
+
* `updateMutation` and `mutate` return. Identity by default.
|
|
67
60
|
*/
|
|
68
61
|
enrichItem?: (item: Item) => Item;
|
|
62
|
+
/**
|
|
63
|
+
* Combines the cached item with an incoming one, on every cache write —
|
|
64
|
+
* `list`, `retrieve`, `setItem`, `create`, `update`, `mutate` — after `enrichItem` and
|
|
65
|
+
* `getItemId`. `cached` is `undefined` the first time an id is written.
|
|
66
|
+
*
|
|
67
|
+
* Needed when one entity comes back in several shapes: a list endpoint that
|
|
68
|
+
* returns fewer fields than the detail one would otherwise overwrite the full
|
|
69
|
+
* item with its partial copy. Absent, the incoming item replaces the cached
|
|
70
|
+
* one.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* mergeItem: (cached, incoming) => ({ ...cached, ...incoming })
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
mergeItem?: (cached: Item | undefined, incoming: Item) => Item;
|
|
78
|
+
/**
|
|
79
|
+
* Tells whether a cached item holds everything `retrieve` promises. When it
|
|
80
|
+
* returns `false`, `retrieve(id)` fetches again instead of serving the cache
|
|
81
|
+
* — the partial item a list brought in is not what a detail page asked for.
|
|
82
|
+
* Absent, any cached item is served.
|
|
83
|
+
*
|
|
84
|
+
* `hasData({ kind: 'item', id })` does **not** consult it: a partial item is
|
|
85
|
+
* still data, and showing it while the full one loads is the point.
|
|
86
|
+
*/
|
|
87
|
+
isComplete?: (item: Item) => boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Tells whether a `retrieve` failure means the item **no longer exists** for
|
|
90
|
+
* this user. When it returns `true`, `retrieve` drops the item from the cache
|
|
91
|
+
* and from every view (as `removeItem` does), keeps the error under
|
|
92
|
+
* `{ kind: 'item', id }`, and resolves with `undefined`.
|
|
93
|
+
*
|
|
94
|
+
* Defaults to an HTTP status of 404, 410 or 403 on `error.response`, read
|
|
95
|
+
* defensively: an error without a `response` (network failure, abort,
|
|
96
|
+
* timeout) is never "gone". Any other failure keeps the cached item.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* // Only a 404 means "gone" for this entity — a 403 is a transient right
|
|
101
|
+
* isGone: error => (error as MandeError).response?.status === 404
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
isGone?: (error: unknown) => boolean;
|
|
69
105
|
/** Persists a new item server-side. Required to call `create()`. */
|
|
70
106
|
createMutation?: (payload: CreatePayload) => Promise<Item>;
|
|
71
107
|
/** Patches an existing item server-side. Required to call `update()`. */
|
|
@@ -73,6 +109,18 @@ export type StoreOptions<Id extends IdKey, Item, Params extends ViewKeyParams, C
|
|
|
73
109
|
/** Deletes an item server-side. Required to call `remove()`. */
|
|
74
110
|
removeMutation?: (id: Id) => Promise<void>;
|
|
75
111
|
};
|
|
112
|
+
/**
|
|
113
|
+
* Options accepted by `removeItem()`.
|
|
114
|
+
*/
|
|
115
|
+
export type RemoveItemOptions = {
|
|
116
|
+
/**
|
|
117
|
+
* Also clear the errors of that item — its failed `retrieve`, and its failed
|
|
118
|
+
* `update` and `remove`. Defaults to `true`: an entity that is gone cannot
|
|
119
|
+
* still be "impossible to load", and a kept error would surface on the next
|
|
120
|
+
* entity to reuse the id.
|
|
121
|
+
*/
|
|
122
|
+
clearError?: boolean;
|
|
123
|
+
};
|
|
76
124
|
/**
|
|
77
125
|
* Options accepted by `retrieve()`.
|
|
78
126
|
*/
|
|
@@ -80,6 +128,15 @@ export type RetrieveOptions = {
|
|
|
80
128
|
/** When `true`, bypass the cache and fetch even if the item is already known. */
|
|
81
129
|
force?: boolean;
|
|
82
130
|
};
|
|
131
|
+
/**
|
|
132
|
+
* Options accepted by `create()`.
|
|
133
|
+
*/
|
|
134
|
+
export type CreateOptions = {
|
|
135
|
+
/** The entity the creation belongs to (the model a dimension is created in), read by `isMutating({ name: 'create', id })`. */
|
|
136
|
+
id?: IdKey;
|
|
137
|
+
/** A local id the caller mints, to tell two concurrent creations apart. */
|
|
138
|
+
lid?: string;
|
|
139
|
+
};
|
|
83
140
|
/**
|
|
84
141
|
* Options accepted by `list()`.
|
|
85
142
|
*/
|
|
@@ -87,3 +144,171 @@ export type ListOptions = {
|
|
|
87
144
|
/** When `true`, bypass the cache and fetch even if the view is already known. */
|
|
88
145
|
force?: boolean;
|
|
89
146
|
};
|
|
147
|
+
/**
|
|
148
|
+
* The four kinds of address a store operation can occupy. `item` and `view`
|
|
149
|
+
* are the store's own reads (`retrieve` / `list`); `query` is a read of the
|
|
150
|
+
* store's own making that is neither — every page of a list loaded in one
|
|
151
|
+
* go, a count, an export; `mutation` covers every write, whether it is one of
|
|
152
|
+
* the built-in CRUD methods, a `mutate`, or a bespoke operation handed to
|
|
153
|
+
* `withPending`.
|
|
154
|
+
*/
|
|
155
|
+
export type PendingKind = 'item' | 'view' | 'query' | 'mutation';
|
|
156
|
+
/**
|
|
157
|
+
* The **address** of an operation — never its identity. Several operations may
|
|
158
|
+
* share one address (four concurrent `remove` calls on the same id, say); each
|
|
159
|
+
* still gets its own entry, so none of them erases another's.
|
|
160
|
+
*
|
|
161
|
+
* A mutation carries an optional `id` for the entity it targets — which is not
|
|
162
|
+
* always an item of *this* store, hence `IdKey` rather than `Id` — and an
|
|
163
|
+
* optional `lid` (client-side local id) for the caller that needs to tell two
|
|
164
|
+
* concurrent creations apart. Both absent means "the collection": the
|
|
165
|
+
* `{ kind: 'mutation', name: 'create' }` sentinel answers "a creation is
|
|
166
|
+
* running". A query carries a `name` and
|
|
167
|
+
* an optional `id` the same way, without `lid`.
|
|
168
|
+
*
|
|
169
|
+
* @typeParam Id - Item identifier type of the owning store.
|
|
170
|
+
* @typeParam Params - Paginated query shape of the owning store.
|
|
171
|
+
*/
|
|
172
|
+
export type PendingScope<Id extends IdKey, Params extends ViewKeyParams> = {
|
|
173
|
+
kind: 'item';
|
|
174
|
+
id: Id;
|
|
175
|
+
} | {
|
|
176
|
+
kind: 'view';
|
|
177
|
+
params: Params;
|
|
178
|
+
} | {
|
|
179
|
+
kind: 'query';
|
|
180
|
+
name: string;
|
|
181
|
+
id?: IdKey;
|
|
182
|
+
} | {
|
|
183
|
+
kind: 'mutation';
|
|
184
|
+
name: MutationName;
|
|
185
|
+
id?: IdKey;
|
|
186
|
+
lid?: string;
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* The name of a mutation: one of the three the store runs itself, or any name
|
|
190
|
+
* a store gives its own writes (`'publish'`, `'register'`, …). The editor
|
|
191
|
+
* offers the three; any other string is accepted.
|
|
192
|
+
*/
|
|
193
|
+
export type MutationName = 'create' | 'update' | 'remove' | (string & {});
|
|
194
|
+
/**
|
|
195
|
+
* Partial filter over {@link PendingScope}. **Every absent field is a
|
|
196
|
+
* wildcard**, so `{}` (or no argument at all) matches anything and
|
|
197
|
+
* `{ kind: 'mutation', name: 'create' }` matches every creation regardless of
|
|
198
|
+
* target. `params` matches partially as well: a view matches when its params
|
|
199
|
+
* contain every field of the filter's, compared through the serialisation of
|
|
200
|
+
* the view key, so `{ kind: 'view', params: { q: 'x' } }` matches every page
|
|
201
|
+
* listed with `q: 'x'`, whatever its other params.
|
|
202
|
+
*
|
|
203
|
+
* `exact: true` turns the `params` match exact: only the page whose view key is
|
|
204
|
+
* the key of `params` matches, as with the `exact` option of TanStack Query's
|
|
205
|
+
* filters. It changes nothing without `params`.
|
|
206
|
+
*
|
|
207
|
+
* `predicate` is the escape hatch for what the grammar cannot express; it runs
|
|
208
|
+
* last, on scopes that already passed the declared fields.
|
|
209
|
+
*/
|
|
210
|
+
export type PendingFilter<Id extends IdKey, Params extends ViewKeyParams> = Partial<{
|
|
211
|
+
kind: PendingKind;
|
|
212
|
+
id: IdKey;
|
|
213
|
+
params: Partial<Params>;
|
|
214
|
+
exact: boolean;
|
|
215
|
+
name: MutationName;
|
|
216
|
+
lid: string;
|
|
217
|
+
predicate: (scope: PendingScope<Id, Params>) => boolean;
|
|
218
|
+
}>;
|
|
219
|
+
/**
|
|
220
|
+
* Filter accepted by `isMutating()`: the fields of a mutation scope, each one a
|
|
221
|
+
* wildcard when absent. `{ name: 'create', lid }` reads one creation,
|
|
222
|
+
* `{ id }` every write on that target, `{}` any mutation at all.
|
|
223
|
+
*/
|
|
224
|
+
export type MutatingFilter = Partial<{
|
|
225
|
+
name: MutationName;
|
|
226
|
+
id: IdKey;
|
|
227
|
+
lid: string;
|
|
228
|
+
}>;
|
|
229
|
+
/**
|
|
230
|
+
* Filter accepted by `isQuerying()`: the fields of a query scope, each one a
|
|
231
|
+
* wildcard when absent.
|
|
232
|
+
*/
|
|
233
|
+
export type QueryingFilter = Partial<{
|
|
234
|
+
name: string;
|
|
235
|
+
id: IdKey;
|
|
236
|
+
}>;
|
|
237
|
+
/**
|
|
238
|
+
* The scope `mutate()` runs its work under, as a mutation: `name` says what
|
|
239
|
+
* the write does, `id` the entity it targets, `lid` a local id to tell two
|
|
240
|
+
* concurrent calls apart.
|
|
241
|
+
*/
|
|
242
|
+
export type MutateScope = {
|
|
243
|
+
name: MutationName;
|
|
244
|
+
id?: IdKey;
|
|
245
|
+
lid?: string;
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* Concurrency policy applied to a `withPending` call, resolved against the
|
|
249
|
+
* operation's **concurrency key** (its address, or the `channel` override).
|
|
250
|
+
*
|
|
251
|
+
* - `parallel` (default) — run now, alongside anything already in flight.
|
|
252
|
+
* - `dedupe` — if an operation is already in flight on that key, **share** its
|
|
253
|
+
* promise instead of starting a second one. Never abandons: a caller always
|
|
254
|
+
* receives the value, never a bare `undefined`.
|
|
255
|
+
* - `queue` — chain after whatever is already in flight on that key. The work
|
|
256
|
+
* is only invoked when its turn comes, which defers the **read** as well as
|
|
257
|
+
* the write — the point when a payload is rebuilt from the cache.
|
|
258
|
+
*
|
|
259
|
+
* `dedupe` and `queue` therefore require a thunk: neither can de-duplicate nor
|
|
260
|
+
* postpone work that has already been started.
|
|
261
|
+
*/
|
|
262
|
+
export type PendingMode = 'parallel' | 'dedupe' | 'queue';
|
|
263
|
+
/**
|
|
264
|
+
* Options accepted by `mutate()`: the policies of `withPending` that run the
|
|
265
|
+
* caller's own work. `dedupe` is not one of them — it would hand the caller
|
|
266
|
+
* whatever the operation it joins returns, which need not be an item.
|
|
267
|
+
*/
|
|
268
|
+
export type MutateOptions = {
|
|
269
|
+
/** Concurrency policy. Defaults to `parallel`. */
|
|
270
|
+
mode?: Exclude<PendingMode, 'dedupe'>;
|
|
271
|
+
/** Overrides the concurrency key `queue` resolves against — see {@link WithPendingOptions}. */
|
|
272
|
+
channel?: string;
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Options accepted by `withPending()`.
|
|
276
|
+
*/
|
|
277
|
+
export type WithPendingOptions = {
|
|
278
|
+
/** Concurrency policy. Defaults to `parallel`. */
|
|
279
|
+
mode?: PendingMode;
|
|
280
|
+
/**
|
|
281
|
+
* Overrides the concurrency key `dedupe` and `queue` resolve against.
|
|
282
|
+
* Needed whenever operations with *different* names must share one queue —
|
|
283
|
+
* an `upsertDependency` and a `removeDependency` on the same question, for
|
|
284
|
+
* instance. Errors stay on each operation's own address: a successful
|
|
285
|
+
* `removeDependency` on the channel leaves the failure of an
|
|
286
|
+
* `upsertDependency` in place. Ignored by `parallel`.
|
|
287
|
+
*/
|
|
288
|
+
channel?: string;
|
|
289
|
+
};
|
|
290
|
+
/**
|
|
291
|
+
* An error held by the store, paired with the address that produced it. Exposed
|
|
292
|
+
* as a list rather than a map so that reading an error never requires
|
|
293
|
+
* rebuilding a string key.
|
|
294
|
+
*/
|
|
295
|
+
export type StoreError<Id extends IdKey, Params extends ViewKeyParams> = {
|
|
296
|
+
scope: PendingScope<Id, Params>;
|
|
297
|
+
error: unknown;
|
|
298
|
+
};
|
|
299
|
+
/**
|
|
300
|
+
* The addresses that can hold **data**, which is the subset of
|
|
301
|
+
* {@link PendingScope} `hasData()` accepts — a mutation has no cache entry of
|
|
302
|
+
* its own, so asking whether it has data means nothing.
|
|
303
|
+
*
|
|
304
|
+
* Unlike a {@link PendingFilter} this is not a filter: there is no wildcard.
|
|
305
|
+
* "Is at least one address holding data" answers no real question, whereas
|
|
306
|
+
* "has *this* page ever loaded" is asked on every screen.
|
|
307
|
+
*/
|
|
308
|
+
export type DataScope<Id extends IdKey, Params extends ViewKeyParams> = {
|
|
309
|
+
kind: 'item';
|
|
310
|
+
id: Id;
|
|
311
|
+
} | {
|
|
312
|
+
kind: 'view';
|
|
313
|
+
params: Params;
|
|
314
|
+
};
|
package/dist/store/useStore.d.ts
CHANGED
|
@@ -1,35 +1,46 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IdKey, ListOptions, RetrieveOptions, StoreOptions, View, ViewKeyParams } from './types';
|
|
1
|
+
import { CreateOptions, DataScope, IdKey, ListOptions, MutateOptions, MutateScope, MutatingFilter, PendingFilter, PendingScope, QueryingFilter, RemoveItemOptions, RetrieveOptions, StoreError, StoreOptions, View, ViewKeyParams, WithPendingOptions } from './types';
|
|
3
2
|
/** Deterministic string built from a `ViewKeyParams` object — see {@link getViewKey}. */
|
|
4
3
|
type ViewKey = string;
|
|
5
4
|
/**
|
|
6
5
|
* Internal shape of a cached view. Keeps only `itemIds[]` so that `items` remains
|
|
7
6
|
* the single source of truth for item data, avoiding the duplication issue
|
|
8
7
|
* where a view's `items[]` could drift from `items.value[id]` after a re-fetch.
|
|
9
|
-
*
|
|
10
|
-
* `revision` is the store's revision when the page was requested: a `create()`,
|
|
11
|
-
* an `update()` or a `markViewsStale()` landing after that request makes the
|
|
12
|
-
* view stale, and the next `list()` on it refetches the page.
|
|
13
8
|
*/
|
|
14
9
|
type StoredView<Id extends IdKey> = ViewKeyParams & {
|
|
15
10
|
itemIds: Id[];
|
|
16
11
|
total: number;
|
|
17
12
|
hasMore: boolean;
|
|
18
|
-
revision: number;
|
|
19
13
|
};
|
|
20
14
|
type StoreItems<Id extends IdKey, Item> = Record<Id, Item>;
|
|
21
15
|
type StoreViews<Id extends IdKey> = Record<ViewKey, StoredView<Id>>;
|
|
22
|
-
|
|
16
|
+
/**
|
|
17
|
+
* The cache as Pinia state: what a Pinia setup store must return so that it
|
|
18
|
+
* becomes the store's state — hydrated from the server render, shown in the
|
|
19
|
+
* devtools, seen by `$subscribe`, `$patch` and HMR. Read-only at the type
|
|
20
|
+
* level: the accessors are the only way in — see `Store.state`.
|
|
21
|
+
*/
|
|
22
|
+
export type StoreState<Id extends IdKey, Item> = {
|
|
23
|
+
readonly items: Readonly<StoreItems<Id, Item>>;
|
|
24
|
+
readonly views: Readonly<StoreViews<Id>>;
|
|
25
|
+
};
|
|
23
26
|
/**
|
|
24
27
|
* Builds a deterministic string key from a `ViewKeyParams` (or any superset) so
|
|
25
28
|
* that two semantically equal queries always hash to the same view, regardless
|
|
26
29
|
* of property order.
|
|
27
30
|
*
|
|
31
|
+
* Fields are sorted by code point, whatever the locale of the runtime, and
|
|
32
|
+
* fields set to `undefined` are left out. A string, an array or an object is
|
|
33
|
+
* written as JSON, object keys sorted at every depth; a `Date` as its ISO string,
|
|
34
|
+
* inside JSON too; a number, a boolean or `null` as `String(value)`. Quoting the
|
|
35
|
+
* strings keeps a value holding `|` or `=` from reading as two fields.
|
|
36
|
+
*
|
|
28
37
|
* @example
|
|
29
38
|
* ```ts
|
|
30
|
-
* getViewKey({ page: 1, limit: 10 })
|
|
31
|
-
* getViewKey({ limit: 10, page: 1 })
|
|
32
|
-
* getViewKey({ page: 1, limit: 10, q: 'x' })
|
|
39
|
+
* getViewKey({ page: 1, limit: 10 }) // "limit=10|page=1"
|
|
40
|
+
* getViewKey({ limit: 10, page: 1 }) // "limit=10|page=1" (same)
|
|
41
|
+
* getViewKey({ page: 1, limit: 10, q: 'x' }) // 'limit=10|page=1|q="x"'
|
|
42
|
+
* getViewKey({ page: 1, limit: 10, q: undefined }) // "limit=10|page=1"
|
|
43
|
+
* getViewKey({ page: 1, limit: 10, ids: [3, 1] }) // "ids=[3,1]|limit=10|page=1"
|
|
33
44
|
* ```
|
|
34
45
|
*/
|
|
35
46
|
export declare function getViewKey(params: ViewKeyParams): ViewKey;
|
|
@@ -44,32 +55,73 @@ export declare function getViewKey(params: ViewKeyParams): ViewKey;
|
|
|
44
55
|
* @typeParam UpdatePatch - Patch shape passed to `update()` (defaults to `Partial<Item>`).
|
|
45
56
|
*/
|
|
46
57
|
export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePayload = Item, UpdatePatch = Partial<Item>> = {
|
|
47
|
-
/**
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
/**
|
|
59
|
+
* The cache as **Pinia state**. Return it from the Pinia setup store
|
|
60
|
+
* (`return { state, … }`): that is what makes it the store's state, so Pinia
|
|
61
|
+
* serialises it on the server and hydrates it on the client, and the
|
|
62
|
+
* devtools, `$subscribe`, `$patch` and HMR all see it. Read it through the
|
|
63
|
+
* accessors below (`getItem`, `getView`, `hasData`, …), which resolve views
|
|
64
|
+
* and stay reactive; its type is read-only so no code writes the cache
|
|
65
|
+
* behind their back.
|
|
66
|
+
*/
|
|
67
|
+
state: StoreState<Id, Item>;
|
|
68
|
+
/** The cached item for `id`, or `undefined`. Reactive: safe inside a `computed`. */
|
|
69
|
+
getItem: (id: Id) => Item | undefined;
|
|
70
|
+
/** Everything the item cache currently holds. **Not** a collection — see the JSDoc. */
|
|
71
|
+
getItems: () => Item[];
|
|
72
|
+
/** Writes an item into the cache (after `enrichItem`, through `mergeItem`), touching no view and marking none stale. */
|
|
73
|
+
setItem: (item: Item) => void;
|
|
74
|
+
/** Drops an item from the cache, from every view holding it, and (by default) its errors. */
|
|
75
|
+
removeItem: (id: Id, removeItemOptions?: RemoveItemOptions) => void;
|
|
76
|
+
/**
|
|
77
|
+
* Marks the cached views stale — every one without `filter`, otherwise those whose params contain it.
|
|
78
|
+
* `getView()` keeps serving them, the next `list()` on each refetches the page.
|
|
79
|
+
*/
|
|
80
|
+
markViewsStale: (filter?: Partial<Params>) => void;
|
|
81
|
+
/** `true` once this page has loaded, or while this item is in the cache — never loaded vs. loaded and empty. */
|
|
82
|
+
hasData: (scope: DataScope<Id, Params>) => boolean;
|
|
83
|
+
/** Every error matching `filter`, each paired with the address that produced it. */
|
|
84
|
+
getErrors: (filter?: PendingFilter<Id, Params>) => StoreError<Id, Params>[];
|
|
85
|
+
/** `true` when at least one in-flight operation matches `filter` (absent fields are wildcards). */
|
|
86
|
+
isPending: (filter?: PendingFilter<Id, Params>) => boolean;
|
|
87
|
+
/** How many in-flight operations match `filter`. */
|
|
88
|
+
pendingCount: (filter?: PendingFilter<Id, Params>) => number;
|
|
89
|
+
/** `true` while `retrieve(id)` is in flight. Sugar over `isPending({ kind: 'item', id })`. */
|
|
90
|
+
isRetrieving: (id: Id) => boolean;
|
|
91
|
+
/**
|
|
92
|
+
* `true` while a `list()` is in flight on a page whose params contain `params` — on any page without it.
|
|
93
|
+
* With `exact: true`, only on the page of exactly these params. Sugar over `isPending({ kind: 'view', params, exact })`.
|
|
94
|
+
*/
|
|
95
|
+
isListing: (params?: Partial<Params>, listingOptions?: {
|
|
96
|
+
exact?: boolean;
|
|
97
|
+
}) => boolean;
|
|
98
|
+
/** `true` while a mutation is in flight, narrowed by `name`, target `id` and `lid` when given. Sugar over `isPending({ kind: 'mutation', … })`. */
|
|
99
|
+
isMutating: (filter?: MutatingFilter) => boolean;
|
|
100
|
+
/** `true` while a query is in flight, narrowed by `name` and `id` when given. Sugar over `isPending({ kind: 'query', … })`. */
|
|
101
|
+
isQuerying: (filter?: QueryingFilter) => boolean;
|
|
102
|
+
/** The first error matching `filter`, or `undefined`. */
|
|
103
|
+
getError: (filter?: PendingFilter<Id, Params>) => unknown;
|
|
104
|
+
/** Drops the errors matching `filter` — all of them when called with no argument. */
|
|
105
|
+
clearErrors: (filter?: PendingFilter<Id, Params>) => void;
|
|
106
|
+
/** Folds arbitrary work into the store's pending + error lifecycle under `scope`. */
|
|
107
|
+
withPending: <T>(scope: PendingScope<Id, Params>, work: Promise<T> | (() => Promise<T>), withPendingOptions?: WithPendingOptions) => Promise<T>;
|
|
108
|
+
/**
|
|
109
|
+
* Fetches one item by id (cache-first unless `force: true`, which always starts a new request). On failure:
|
|
110
|
+
* drops the item when `isGone`, keeps the cache otherwise, and resolves `undefined` — an abort excepted, which serves the cached item if complete.
|
|
111
|
+
*/
|
|
56
112
|
retrieve: (id: Id, retrieveOptions?: RetrieveOptions) => Promise<Item | undefined>;
|
|
57
|
-
/** Fetches one page (cache-first unless `force: true
|
|
113
|
+
/** Fetches one page (cache-first unless `force: true`, which always starts a new request, or the view is stale). Returns the resolved `View<Item>`. */
|
|
58
114
|
list: (params: Params, listOptions?: ListOptions) => Promise<View<Item> | undefined>;
|
|
59
115
|
/** Synchronous lookup: rebuilds a `View<Item>` from the cache without fetching. */
|
|
60
116
|
getView: (params: Params) => View<Item> | undefined;
|
|
61
|
-
/**
|
|
62
|
-
|
|
63
|
-
/** Calls `
|
|
64
|
-
create: (payload: CreatePayload, key?: ViewKey | Id) => Promise<Item>;
|
|
65
|
-
/** Calls `updateMutation` (wrapped in `withPending(id)`), replaces `items[id]`, and marks every view stale. */
|
|
117
|
+
/** Calls `createMutation` under `{ kind: 'mutation', name: 'create', id?, lid? }`, inserts the item into `items`, and marks every view stale. */
|
|
118
|
+
create: (payload: CreatePayload, createOptions?: CreateOptions) => Promise<Item>;
|
|
119
|
+
/** Calls `updateMutation` under `{ kind: 'mutation', name: 'update', id }`, replaces `items[id]`, and marks every view stale. */
|
|
66
120
|
update: (id: Id, patch: UpdatePatch) => Promise<Item>;
|
|
67
|
-
/**
|
|
68
|
-
|
|
69
|
-
/** Calls `removeMutation
|
|
121
|
+
/** Runs a write of the store's own under `{ kind: 'mutation', … }`, then caches the item it returns and marks every view stale. */
|
|
122
|
+
mutate: (scope: MutateScope, work: () => Promise<Item>, mutateOptions?: MutateOptions) => Promise<Item>;
|
|
123
|
+
/** Calls `removeMutation`, deletes `items[id]`, removes id from every view, and marks every view stale. */
|
|
70
124
|
remove: (id: Id) => Promise<void>;
|
|
71
|
-
/** Clears every entry from `errors`. */
|
|
72
|
-
clearErrors: () => void;
|
|
73
125
|
/** Resets the store back to its initial empty state. */
|
|
74
126
|
reset: () => void;
|
|
75
127
|
};
|
|
@@ -90,8 +142,12 @@ export type Store<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePa
|
|
|
90
142
|
*
|
|
91
143
|
* await store.list({ page: 1, limit: 20 })
|
|
92
144
|
* await store.update('org-42', { name: 'Acme Inc.' })
|
|
145
|
+
*
|
|
146
|
+
* store.isListing({ page: 1, limit: 20 }, { exact: true }) // that page is loading
|
|
147
|
+
* store.isListing({ q: 'acme' }) // a page searched for 'acme' is loading
|
|
148
|
+
* store.isMutating({ name: 'update', id: 'org-42' }) // that item is being written
|
|
149
|
+
* store.isPending() // anything at all is running
|
|
93
150
|
* ```
|
|
94
151
|
*/
|
|
95
|
-
export declare const CREATE_PENDING_KEY = "create";
|
|
96
152
|
export declare function useStore<Id extends IdKey, Item, Params extends ViewKeyParams, CreatePayload = Item, UpdatePatch = Partial<Item>>(options: StoreOptions<Id, Item, Params, CreatePayload, UpdatePatch>): Store<Id, Item, Params, CreatePayload, UpdatePatch>;
|
|
97
153
|
export {};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@choosemycompany/lib",
|
|
3
3
|
"description": "Shared composables, stores and types for ChooseMyCompany front apps",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"packageManager": "pnpm@10.17.0",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"@types/node": "^24.5.2",
|
|
42
42
|
"@vitejs/plugin-vue": "^5.1.4",
|
|
43
43
|
"@vue/test-utils": "^2.4.6",
|
|
44
|
+
"devalue": "^5.7.1",
|
|
44
45
|
"eslint": "^9.36.0",
|
|
45
46
|
"globals": "^16.4.0",
|
|
46
47
|
"h3": "^1.13.0",
|