@warp-drive/json-api 5.9.0-alpha.9 → 5.9.0-beta.1

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.
@@ -1,454 +0,0 @@
1
- import type { CacheCapabilitiesManager } from "@warp-drive/core/types";
2
- import type { Cache, ChangedAttributesHash, RelationshipDiff } from "@warp-drive/core/types/cache";
3
- import type { Change } from "@warp-drive/core/types/cache/change";
4
- import type { Operation } from "@warp-drive/core/types/cache/operations";
5
- import type { CollectionRelationship, ResourceRelationship } from "@warp-drive/core/types/cache/relationship";
6
- import type { LocalRelationshipOperation } from "@warp-drive/core/types/graph";
7
- import type { RequestKey, ResourceKey } from "@warp-drive/core/types/identifier";
8
- import type { Value } from "@warp-drive/core/types/json/raw";
9
- import type { RequestContext, StructuredDataDocument, StructuredDocument, StructuredErrorDocument } from "@warp-drive/core/types/request";
10
- import type { CollectionResourceDataDocument, ResourceDocument, ResourceErrorDocument, ResourceMetaDocument, SingleResourceDataDocument } from "@warp-drive/core/types/spec/document";
11
- import type { ApiError } from "@warp-drive/core/types/spec/error";
12
- import type { CollectionResourceDocument, ExistingResourceObject, ResourceObject, SingleResourceDocument } from "@warp-drive/core/types/spec/json-api-raw";
13
- /**
14
- * ```ts
15
- * import { JSONAPICache } from '@warp-drive/json-api';
16
- * ```
17
- *
18
- * A {@link Cache} implementation tuned for [{json:api}](https://jsonapi.org/)
19
- *
20
- * @categoryDescription Cache Management
21
- * APIs for primary cache management functionality
22
- * @categoryDescription Cache Forking
23
- * APIs that support Cache Forking
24
- * @categoryDescription SSR Support
25
- * APIs that support SSR functionality
26
- * @categoryDescription Resource Lifecycle
27
- * APIs that support management of resource data
28
- * @categoryDescription Resource Data
29
- * APIs that support granular field level management of resource data
30
- * @categoryDescription Resource State
31
- * APIs that support managing Resource states
32
- *
33
- * @public
34
- */
35
- export declare class JSONAPICache implements Cache {
36
- /**
37
- * The Cache Version that this implementation implements.
38
- *
39
- * @public
40
- */
41
- version: "2";
42
- constructor(capabilities: CacheCapabilitiesManager);
43
- /**
44
- * Cache the response to a request
45
- *
46
- * Implements `Cache.put`.
47
- *
48
- * Expects a StructuredDocument whose `content` member is a JsonApiDocument.
49
- *
50
- * ```js
51
- * cache.put({
52
- * request: { url: 'https://api.example.com/v1/user/1' },
53
- * content: {
54
- * data: {
55
- * type: 'user',
56
- * id: '1',
57
- * attributes: {
58
- * name: 'Chris'
59
- * }
60
- * }
61
- * }
62
- * })
63
- * ```
64
- *
65
- * > **Note**
66
- * > The nested `content` and `data` members are not a mistake. This is because
67
- * > there are two separate concepts involved here, the `StructuredDocument` which contains
68
- * > the context of a given Request that has been issued with the returned contents as its
69
- * > `content` property, and a `JSON:API Document` which is the json contents returned by
70
- * > this endpoint and which uses its `data` property to signify which resources are the
71
- * > primary resources associated with the request.
72
- *
73
- * StructuredDocument's with urls will be cached as full documents with
74
- * associated resource membership order and contents preserved but linked
75
- * into the cache.
76
- *
77
- * @category Cache Management
78
- * @public
79
- */
80
- put<T extends SingleResourceDocument>(doc: StructuredDataDocument<T>): SingleResourceDataDocument;
81
- put<T extends CollectionResourceDocument>(doc: StructuredDataDocument<T>): CollectionResourceDataDocument;
82
- put<T extends ResourceErrorDocument>(doc: StructuredErrorDocument<T>): ResourceErrorDocument;
83
- put<T extends ResourceMetaDocument>(doc: StructuredDataDocument<T>): ResourceMetaDocument;
84
- /**
85
- * Update the "remote" or "canonical" (persisted) state of the Cache
86
- * by merging new information into the existing state.
87
- *
88
- * @category Cache Management
89
- * @public
90
- * @param op the operation or list of operations to perform
91
- */
92
- patch(op: Operation | Operation[]): void;
93
- /**
94
- * Update the "local" or "current" (unpersisted) state of the Cache
95
- *
96
- * @category Cache Management
97
- * @public
98
- */
99
- mutate(mutation: LocalRelationshipOperation): void;
100
- /**
101
- * Peek resource data from the Cache.
102
- *
103
- * In development, if the return value
104
- * is JSON the return value
105
- * will be deep-cloned and deep-frozen
106
- * to prevent mutation thereby enforcing cache
107
- * Immutability.
108
- *
109
- * This form of peek is useful for implementations
110
- * that want to feed raw-data from cache to the UI
111
- * or which want to interact with a blob of data
112
- * directly from the presentation cache.
113
- *
114
- * An implementation might want to do this because
115
- * de-referencing records which read from their own
116
- * blob is generally safer because the record does
117
- * not require retainining connections to the Store
118
- * and Cache to present data on a per-field basis.
119
- *
120
- * This generally takes the place of `getAttr` as
121
- * an API and may even take the place of `getRelationship`
122
- * depending on implementation specifics, though this
123
- * latter usage is less recommended due to the advantages
124
- * of the Graph handling necessary entanglements and
125
- * notifications for relational data.
126
- *
127
- * :::warning
128
- * It is not recommended to use the return value as
129
- * a serialized representation of the resource both
130
- * due to it containing local mutations and because
131
- * it may contain additional fields not recognized
132
- * by the {json:api} API implementation such as `lid` and
133
- * the various internal WarpDrive bookkeeping fields.
134
- * :::
135
- *
136
- * @category Cache Management
137
- * @public
138
- */
139
- peek(identifier: ResourceKey): ResourceObject | null;
140
- peek(identifier: RequestKey): ResourceDocument | null;
141
- /**
142
- * Peek the remote resource data from the Cache.
143
- *
144
- * @category Cache Management
145
- * @public
146
- */
147
- peekRemoteState(identifier: ResourceKey): ResourceObject | null;
148
- peekRemoteState(identifier: RequestKey): ResourceDocument | null;
149
- /**
150
- * Peek the Cache for the existing request data associated with
151
- * a cacheable request.
152
- *
153
- * This is effectively the reverse of `put` for a request in
154
- * that it will return the the request, response, and content
155
- * whereas `peek` will return just the `content`.
156
- *
157
- * @category Cache Management
158
- * @public
159
- */
160
- peekRequest(identifier: RequestKey): StructuredDocument<ResourceDocument> | null;
161
- /**
162
- * Push resource data from a remote source into the cache for this identifier
163
- *
164
- * @category Cache Management
165
- * @public
166
- * @return if `calculateChanges` is true then calculated key changes should be returned
167
- */
168
- upsert(identifier: ResourceKey, data: ExistingResourceObject, calculateChanges?: boolean): void | string[];
169
- /**
170
- * Create a fork of the cache from the current state.
171
- *
172
- * Applications should typically not call this method themselves,
173
- * preferring instead to fork at the Store level, which will
174
- * utilize this method to fork the cache.
175
- *
176
- * @category Cache Forking
177
- * @private
178
- */
179
- fork(): Promise<Cache>;
180
- /**
181
- * Merge a fork back into a parent Cache.
182
- *
183
- * Applications should typically not call this method themselves,
184
- * preferring instead to merge at the Store level, which will
185
- * utilize this method to merge the caches.
186
- *
187
- * @category Cache Forking
188
- * @private
189
- */
190
- merge(_cache: Cache): Promise<void>;
191
- /**
192
- * Generate the list of changes applied to all
193
- * record in the store.
194
- *
195
- * Each individual resource or document that has
196
- * been mutated should be described as an individual
197
- * `Change` entry in the returned array.
198
- *
199
- * A `Change` is described by an object containing up to
200
- * three properties: (1) the `identifier` of the entity that
201
- * changed; (2) the `op` code of that change being one of
202
- * `upsert` or `remove`, and if the op is `upsert` a `patch`
203
- * containing the data to merge into the cache for the given
204
- * entity.
205
- *
206
- * This `patch` is opaque to the Store but should be understood
207
- * by the Cache and may expect to be utilized by an Adapter
208
- * when generating data during a `save` operation.
209
- *
210
- * It is generally recommended that the `patch` contain only
211
- * the updated state, ignoring fields that are unchanged
212
- *
213
- * ```ts
214
- * interface Change {
215
- * identifier: ResourceKey | RequestKey;
216
- * op: 'upsert' | 'remove';
217
- * patch?: unknown;
218
- * }
219
- * ```
220
- *
221
- * @category Cache Forking
222
- * @private
223
- */
224
- diff(): Promise<Change[]>;
225
- /**
226
- * Serialize the entire contents of the Cache into a Stream
227
- * which may be fed back into a new instance of the same Cache
228
- * via `cache.hydrate`.
229
- *
230
- * @category SSR Support
231
- * @private
232
- */
233
- dump(): Promise<ReadableStream<unknown>>;
234
- /**
235
- * hydrate a Cache from a Stream with content previously serialized
236
- * from another instance of the same Cache, resolving when hydration
237
- * is complete.
238
- *
239
- * This method should expect to be called both in the context of restoring
240
- * the Cache during application rehydration after SSR **AND** at unknown
241
- * times during the lifetime of an already booted application when it is
242
- * desired to bulk-load additional information into the cache. This latter
243
- * behavior supports optimizing pre/fetching of data for route transitions
244
- * via data-only SSR modes.
245
- *
246
- * @category SSR Support
247
- * @private
248
- */
249
- hydrate(stream: ReadableStream<unknown>): Promise<void>;
250
- /**
251
- * [LIFECYCLE] Signal to the cache that a new record has been instantiated on the client
252
- *
253
- * It returns properties from options that should be set on the record during the create
254
- * process. This return value behavior is deprecated.
255
- *
256
- * @category Resource Lifecycle
257
- * @public
258
- */
259
- clientDidCreate(identifier: ResourceKey, options?: Record<string, Value>): Record<string, unknown>;
260
- /**
261
- * [LIFECYCLE] Signals to the cache that a resource
262
- * will be part of a save transaction.
263
- *
264
- * @category Resource Lifecycle
265
- * @public
266
- */
267
- willCommit(identifier: ResourceKey | ResourceKey[], _context: RequestContext | null): void;
268
- /**
269
- * [LIFECYCLE] Signals to the cache that a resource
270
- * was successfully updated as part of a save transaction.
271
- *
272
- * @category Resource Lifecycle
273
- * @public
274
- */
275
- didCommit(committedIdentifier: ResourceKey, result: StructuredDataDocument<SingleResourceDataDocument> | null): SingleResourceDataDocument;
276
- didCommit(committedIdentifier: ResourceKey[], result: StructuredDataDocument<SingleResourceDataDocument> | null): SingleResourceDataDocument;
277
- didCommit(committedIdentifier: ResourceKey[], result: StructuredDataDocument<CollectionResourceDataDocument> | null): CollectionResourceDataDocument;
278
- /**
279
- * [LIFECYCLE] Signals to the cache that a resource
280
- * was update via a save transaction failed.
281
- *
282
- * @category Resource Lifecycle
283
- * @public
284
- */
285
- commitWasRejected(identifier: ResourceKey | ResourceKey[], errors?: ApiError[]): void;
286
- /**
287
- * [LIFECYCLE] Signals to the cache that all data for a resource
288
- * should be cleared.
289
- *
290
- * This method is a candidate to become a mutation
291
- *
292
- * @category Resource Lifecycle
293
- * @public
294
- */
295
- unloadRecord(identifier: ResourceKey): void;
296
- /**
297
- * Retrieve the data for an attribute from the cache
298
- * with local mutations applied.
299
- *
300
- * @category Resource Data
301
- * @public
302
- */
303
- getAttr(identifier: ResourceKey, attr: string | string[]): Value | undefined;
304
- /**
305
- * Retrieve the remote data for an attribute from the cache
306
- *
307
- * @category Resource Data
308
- * @public
309
- */
310
- getRemoteAttr(identifier: ResourceKey, attr: string | string[]): Value | undefined;
311
- /**
312
- * Mutate the data for an attribute in the cache
313
- *
314
- * This method is a candidate to become a mutation
315
- *
316
- * @category Resource Data
317
- * @public
318
- */
319
- setAttr(identifier: ResourceKey, attr: string | string[], value: Value): void;
320
- /**
321
- * Query the cache for the changed attributes of a resource.
322
- *
323
- * @category Resource Data
324
- * @public
325
- * @return `{ '<field>': ['<old>', '<new>'] }`
326
- */
327
- changedAttrs(identifier: ResourceKey): ChangedAttributesHash;
328
- /**
329
- * Query the cache for whether any mutated attributes exist
330
- *
331
- * @category Resource Data
332
- * @public
333
- */
334
- hasChangedAttrs(identifier: ResourceKey): boolean;
335
- /**
336
- * Tell the cache to discard any uncommitted mutations to attributes
337
- *
338
- * This method is a candidate to become a mutation
339
- *
340
- * @category Resource Data
341
- * @public
342
- * @return the names of fields that were restored
343
- */
344
- rollbackAttrs(identifier: ResourceKey): string[];
345
- /**
346
- * Query the cache for the changes to relationships of a resource.
347
- *
348
- * Returns a map of relationship names to RelationshipDiff objects.
349
- *
350
- * ```ts
351
- * type RelationshipDiff =
352
- | {
353
- kind: 'collection';
354
- remoteState: ResourceKey[];
355
- additions: Set<ResourceKey>;
356
- removals: Set<ResourceKey>;
357
- localState: ResourceKey[];
358
- reordered: boolean;
359
- }
360
- | {
361
- kind: 'resource';
362
- remoteState: ResourceKey | null;
363
- localState: ResourceKey | null;
364
- };
365
- ```
366
- *
367
- * @category Resource Data
368
- * @public
369
- */
370
- changedRelationships(identifier: ResourceKey): Map<string, RelationshipDiff>;
371
- /**
372
- * Query the cache for whether any mutated relationships exist
373
- *
374
- * @category Resource Data
375
- * @public
376
- */
377
- hasChangedRelationships(identifier: ResourceKey): boolean;
378
- /**
379
- * Tell the cache to discard any uncommitted mutations to relationships.
380
- *
381
- * This will also discard the change on any appropriate inverses.
382
- *
383
- * This method is a candidate to become a mutation
384
- *
385
- * @category Resource Data
386
- * @public
387
- * @return the names of relationships that were restored
388
- */
389
- rollbackRelationships(identifier: ResourceKey): string[];
390
- /**
391
- * Query the cache for the current state of a relationship property
392
- *
393
- * @category Resource Data
394
- * @public
395
- * @return resource relationship object
396
- */
397
- getRelationship(identifier: ResourceKey, field: string): ResourceRelationship | CollectionRelationship;
398
- /**
399
- * Query the cache for the remote state of a relationship property
400
- *
401
- * @category Resource Data
402
- * @public
403
- * @return resource relationship object
404
- */
405
- getRemoteRelationship(identifier: ResourceKey, field: string): ResourceRelationship | CollectionRelationship;
406
- /**
407
- * Update the cache state for the given resource to be marked
408
- * as locally deleted, or remove such a mark.
409
- *
410
- * This method is a candidate to become a mutation
411
- *
412
- * @category Resource State
413
- * @public
414
- */
415
- setIsDeleted(identifier: ResourceKey, isDeleted: boolean): void;
416
- /**
417
- * Query the cache for any validation errors applicable to the given resource.
418
- *
419
- * @category Resource State
420
- * @public
421
- */
422
- getErrors(identifier: ResourceKey): ApiError[];
423
- /**
424
- * Query the cache for whether a given resource has any available data
425
- *
426
- * @category Resource State
427
- * @public
428
- */
429
- isEmpty(identifier: ResourceKey): boolean;
430
- /**
431
- * Query the cache for whether a given resource was created locally and not
432
- * yet persisted.
433
- *
434
- * @category Resource State
435
- * @public
436
- */
437
- isNew(identifier: ResourceKey): boolean;
438
- /**
439
- * Query the cache for whether a given resource is marked as deleted (but not
440
- * necessarily persisted yet).
441
- *
442
- * @category Resource State
443
- * @public
444
- */
445
- isDeleted(identifier: ResourceKey): boolean;
446
- /**
447
- * Query the cache for whether a given resource has been deleted and that deletion
448
- * has also been persisted.
449
- *
450
- * @category Resource State
451
- * @public
452
- */
453
- isDeletionCommitted(identifier: ResourceKey): boolean;
454
- }
@@ -1,3 +0,0 @@
1
- import type { SchemaService } from "@warp-drive/core/types";
2
- import type { CollectionResourceDocument, SingleResourceDocument } from "@warp-drive/core/types/spec/json-api-raw";
3
- export declare function validateDocumentFields(schema: SchemaService, jsonApiDoc: SingleResourceDocument | CollectionResourceDocument): void;
@@ -1,18 +0,0 @@
1
- import type { ResourceDocument } from "@warp-drive/core/types/spec/document";
2
- import { type Reporter } from "../utils.js";
3
- /**
4
- * Validates that all `data` members of relationships have a matching
5
- * resource object in the `included` or `data` section of the document.
6
- *
7
- * Optionally: validates that all resource objects in `included` are reachable
8
- * from at least one relationship `data` member in the primary data. This is a
9
- * spec requirement but has allowed caveats such as for sparse-fields wherein
10
- * related resources may be present with the linkage property omitted.
11
- * This setting is on by default but can be disabled by setting `strict.enforceReachable`
12
- * to false.
13
- *
14
- * Version: 1.1
15
- * Section: 7.4
16
- * Link: https://jsonapi.org/format/#document-compound-documents
17
- */
18
- export declare function validateFullLinkage(reporter: Reporter, doc: ResourceDocument): void;
@@ -1,12 +0,0 @@
1
- import type { ResourceDocument } from "@warp-drive/core/types/spec/document";
2
- import type { Reporter } from "../utils.js";
3
- /**
4
- * A common mistake, especially during development when mocking responses, is to
5
- * return the same resource multiple times, sometimes with differing attributes or
6
- * relationships.
7
- *
8
- * Version: 1.1
9
- * Section: 7.4
10
- * Link: https://jsonapi.org/format/#document-compound-documents
11
- */
12
- export declare function validateNoDuplicateResources(reporter: Reporter, doc: ResourceDocument): void;
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- import type { CacheCapabilitiesManager } from "@warp-drive/core/types";
2
- import type { StructuredDocument } from "@warp-drive/core/types/request";
3
- import type { ResourceDocument } from "@warp-drive/core/types/spec/document";
4
- export declare function validateDocument(capabilities: CacheCapabilitiesManager, doc: StructuredDocument<ResourceDocument>): void;
@@ -1,100 +0,0 @@
1
- import type { FuseResult } from "fuse.js";
2
- import Fuse from "fuse.js";
3
- import jsonToAst from "json-to-ast";
4
- import type { CacheCapabilitiesManager, SchemaService } from "@warp-drive/core/types";
5
- import type { StructuredDataDocument, StructuredDocument, StructuredErrorDocument } from "@warp-drive/core/types/request";
6
- import type { FieldSchema } from "@warp-drive/core/types/schema/fields";
7
- import type { ResourceDataDocument, ResourceDocument, ResourceErrorDocument, ResourceMetaDocument } from "@warp-drive/core/types/spec/document";
8
- import type { ResourceObject } from "@warp-drive/core/types/spec/json-api-raw";
9
- export declare function inspectType(obj: unknown): string;
10
- export declare function isSimpleObject(obj: unknown): obj is Record<string, unknown>;
11
- export declare const RELATIONSHIP_FIELD_KINDS: string[];
12
- export type PathLike = Array<string | number>;
13
- interface ErrorReport {
14
- path: PathLike;
15
- message: string;
16
- loc: {
17
- start: {
18
- line: number;
19
- column: number;
20
- offset: number;
21
- };
22
- end: {
23
- line: number;
24
- column: number;
25
- offset: number;
26
- };
27
- };
28
- type: "error" | "warning" | "info";
29
- kind: "key" | "value";
30
- }
31
- export interface ResourcePresence {
32
- data: Map<string, Map<string, ResourceInfo[]>>;
33
- included: Map<string, Map<string, ResourceInfo[]>>;
34
- all: Map<string, Map<string, ResourceInfo[]>>;
35
- }
36
- export interface ResourceInfo {
37
- data: ResourceObject;
38
- /**
39
- * null if the only primary data member
40
- */
41
- index: number | null;
42
- location: "data" | "included";
43
- }
44
- export declare class Reporter {
45
- capabilities: CacheCapabilitiesManager;
46
- contextDocument: StructuredDocument<ResourceDocument>;
47
- errors: ErrorReport[];
48
- ast: ReturnType<typeof jsonToAst>;
49
- jsonStr: string;
50
- strict: {
51
- linkage: boolean;
52
- unknownType: boolean;
53
- unknownAttribute: boolean;
54
- unknownRelationship: boolean;
55
- enforceReachable: boolean;
56
- };
57
- _presence: ResourcePresence | null;
58
- get presence(): ResourcePresence;
59
- constructor(capabilities: CacheCapabilitiesManager, doc: StructuredDocument<ResourceDocument>);
60
- _typeFilter: Fuse<string> | undefined;
61
- searchTypes(type: string): FuseResult<string>[];
62
- _fieldFilters: Map<string, Fuse<string>>;
63
- searchFields(type: string, field: string): FuseResult<string>[];
64
- get schema(): SchemaService;
65
- getLocation(path: PathLike, kind: "key" | "value"): {
66
- start: {
67
- line: number;
68
- column: number;
69
- offset: number;
70
- };
71
- end: {
72
- line: number;
73
- column: number;
74
- offset: number;
75
- };
76
- };
77
- error(path: PathLike, message: string, kind?: "key" | "value"): void;
78
- warn(path: PathLike, message: string, kind?: "key" | "value"): void;
79
- info(path: PathLike, message: string, kind?: "key" | "value"): void;
80
- hasExtension(extensionName: string): boolean;
81
- getExtension(extensionName: string): ReporterFn | undefined;
82
- report(colorize?: boolean): void;
83
- }
84
- type ReporterFn = (reporter: Reporter, path: PathLike) => void;
85
- export declare function isMetaDocument(doc: StructuredDocument<ResourceDocument>): doc is StructuredDataDocument<ResourceMetaDocument>;
86
- export declare function isErrorDocument(doc: StructuredDocument<ResourceDocument>): doc is StructuredErrorDocument<ResourceErrorDocument>;
87
- export declare function isPushedDocument(doc: unknown): doc is {
88
- content: ResourceDataDocument;
89
- };
90
- export declare function logPotentialMatches(matches: FuseResult<string>[], kind: string): string;
91
- export declare function getRemoteField(fields: Map<string, FieldSchema>, key: string): FieldSchema | undefined;
92
- export declare function checkResourceInMap(map: Map<string, Map<string, ResourceInfo[]>>, resource: {
93
- type: string;
94
- id: string;
95
- }): boolean;
96
- export declare function checkResourcePresent(presence: ResourcePresence, resource: {
97
- type: string;
98
- id: string;
99
- }): boolean;
100
- export {};