@warp-drive/core 5.8.0-alpha.4 → 5.8.0-alpha.40
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 +22 -38
- package/declarations/build-config.d.ts +18 -1
- package/declarations/graph/-private/-edge-definition.d.ts +12 -2
- package/declarations/index.d.ts +90 -8
- package/declarations/reactive/-private/document.d.ts +58 -46
- package/declarations/reactive/-private/record.d.ts +10 -1
- package/declarations/reactive/-private/schema.d.ts +77 -4
- package/declarations/reactive/-private.d.ts +1 -0
- package/declarations/reactive.d.ts +13 -7
- package/declarations/request/-private/types.d.ts +1 -1
- package/declarations/request.d.ts +47 -0
- package/declarations/store/-private/caches/instance-cache.d.ts +5 -6
- package/declarations/store/-private/default-cache-policy.d.ts +147 -129
- package/declarations/store/-private/managers/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/-private/managers/cache-key-manager.d.ts +26 -8
- package/declarations/store/-private/managers/cache-manager.d.ts +6 -4
- package/declarations/store/-private/managers/notification-manager.d.ts +1 -1
- package/declarations/store/-private/new-core-tmp/promise-state.d.ts +1 -0
- package/declarations/store/-private/new-core-tmp/request-state.d.ts +1 -1
- package/declarations/store/-private/store-service.d.ts +43 -64
- package/declarations/store/-private.d.ts +0 -1
- package/declarations/store/-types/q/cache-capabilities-manager.d.ts +1 -1
- package/declarations/store/deprecated/-private.d.ts +1 -1
- package/declarations/store/deprecated/store.d.ts +33 -32
- package/declarations/store.d.ts +1 -0
- package/declarations/types/cache.d.ts +8 -6
- package/declarations/types/record.d.ts +132 -0
- package/declarations/types/request.d.ts +26 -14
- package/declarations/types/schema/fields.d.ts +33 -9
- package/declarations/{store/-types/q → types/schema}/schema-service.d.ts +15 -13
- package/declarations/types/spec/document.d.ts +34 -0
- package/declarations/types/symbols.d.ts +2 -2
- package/declarations/types.d.ts +1 -1
- package/dist/build-config.js +1 -1
- package/dist/default-cache-policy-D7_u4YRH.js +572 -0
- package/dist/graph/-private.js +13 -4
- package/dist/{request-state-CUuZzgvE.js → index-BKcD4JZK.js} +10018 -8847
- package/dist/index.js +6 -382
- package/dist/reactive.js +4 -778
- package/dist/{context-C_7OLieY.js → request-oqoLC9rz.js} +219 -172
- package/dist/request.js +1 -1
- package/dist/store/-private.js +1 -1
- package/dist/store.js +1 -533
- package/dist/types/-private.js +1 -1
- package/dist/types/record.js +127 -0
- package/dist/types/request.js +14 -12
- package/dist/types/schema/fields.js +14 -0
- package/dist/types/schema/schema-service.js +0 -0
- package/dist/types/symbols.js +2 -2
- package/dist/unpkg/dev/-private-3C1OkYtZ.js +39 -0
- package/dist/unpkg/dev/build-config/babel-macros.js +1 -0
- package/dist/unpkg/dev/build-config/canary-features.js +1 -0
- package/dist/unpkg/dev/build-config/debugging.js +1 -0
- package/dist/unpkg/dev/build-config/deprecations.js +1 -0
- package/dist/unpkg/dev/build-config/env.js +1 -0
- package/dist/unpkg/dev/build-config/macros.js +1 -0
- package/dist/unpkg/dev/build-config.js +1 -0
- package/dist/unpkg/dev/configure-BC66sfNO.js +183 -0
- package/dist/unpkg/dev/configure.js +1 -0
- package/dist/unpkg/dev/graph/-private.js +3131 -0
- package/dist/unpkg/dev/index-DqhXrNZ_.js +11160 -0
- package/dist/unpkg/dev/index.js +6 -0
- package/dist/unpkg/dev/reactive/-private.js +1 -0
- package/dist/unpkg/dev/reactive.js +127 -0
- package/dist/unpkg/dev/request-CA9K0gXq.js +719 -0
- package/dist/unpkg/dev/request.js +1 -0
- package/dist/unpkg/dev/runtime-DGG4CvlW.js +135 -0
- package/dist/unpkg/dev/store/-private.js +56 -0
- package/dist/unpkg/dev/store.js +558 -0
- package/dist/unpkg/dev/types/-private.js +69 -0
- package/dist/unpkg/dev/types/cache/aliases.js +0 -0
- package/dist/unpkg/dev/types/cache/change.js +0 -0
- package/dist/unpkg/dev/types/cache/mutations.js +0 -0
- package/dist/unpkg/dev/types/cache/operations.js +0 -0
- package/dist/unpkg/dev/types/cache/relationship.js +0 -0
- package/dist/unpkg/dev/types/cache.js +0 -0
- package/dist/unpkg/dev/types/graph.js +0 -0
- package/dist/unpkg/dev/types/identifier.js +61 -0
- package/dist/unpkg/dev/types/json/raw.js +0 -0
- package/dist/unpkg/dev/types/params.js +0 -0
- package/dist/unpkg/dev/types/record.js +191 -0
- package/dist/unpkg/dev/types/request.js +77 -0
- package/dist/unpkg/dev/types/runtime.js +34 -0
- package/dist/unpkg/dev/types/schema/concepts.js +0 -0
- package/dist/unpkg/dev/types/schema/fields.js +505 -0
- package/dist/unpkg/dev/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/dev/types/schema/schema-service.js +0 -0
- package/dist/unpkg/dev/types/spec/document.js +0 -0
- package/dist/unpkg/dev/types/spec/error.js +0 -0
- package/dist/unpkg/dev/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/dev/types/symbols.js +84 -0
- package/dist/unpkg/dev/types/utils.js +0 -0
- package/dist/unpkg/dev/types.js +0 -0
- package/dist/unpkg/dev/utils/string.js +91 -0
- package/dist/unpkg/dev-deprecated/-private-3C1OkYtZ.js +39 -0
- package/dist/unpkg/dev-deprecated/build-config/babel-macros.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/canary-features.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/debugging.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/deprecations.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/env.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config/macros.js +1 -0
- package/dist/unpkg/dev-deprecated/build-config.js +1 -0
- package/dist/unpkg/dev-deprecated/configure-BC66sfNO.js +183 -0
- package/dist/unpkg/dev-deprecated/configure.js +1 -0
- package/dist/unpkg/dev-deprecated/graph/-private.js +3326 -0
- package/dist/unpkg/dev-deprecated/index-BBlq5is_.js +11775 -0
- package/dist/unpkg/dev-deprecated/index.js +5 -0
- package/dist/unpkg/dev-deprecated/reactive/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/reactive.js +127 -0
- package/dist/unpkg/dev-deprecated/request-CA9K0gXq.js +719 -0
- package/dist/unpkg/dev-deprecated/request.js +1 -0
- package/dist/unpkg/dev-deprecated/runtime-DfhJzpZH.js +135 -0
- package/dist/unpkg/dev-deprecated/store/-private.js +2 -0
- package/dist/unpkg/dev-deprecated/store.js +558 -0
- package/dist/unpkg/dev-deprecated/types/-private.js +69 -0
- package/dist/unpkg/dev-deprecated/types/cache/aliases.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/change.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/mutations.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/operations.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache/relationship.js +0 -0
- package/dist/unpkg/dev-deprecated/types/cache.js +0 -0
- package/dist/unpkg/dev-deprecated/types/graph.js +0 -0
- package/dist/unpkg/dev-deprecated/types/identifier.js +61 -0
- package/dist/unpkg/dev-deprecated/types/json/raw.js +0 -0
- package/dist/unpkg/dev-deprecated/types/params.js +0 -0
- package/dist/unpkg/dev-deprecated/types/record.js +191 -0
- package/dist/unpkg/dev-deprecated/types/request.js +77 -0
- package/dist/unpkg/dev-deprecated/types/runtime.js +34 -0
- package/dist/unpkg/dev-deprecated/types/schema/concepts.js +0 -0
- package/dist/unpkg/dev-deprecated/types/schema/fields.js +505 -0
- package/dist/unpkg/dev-deprecated/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/dev-deprecated/types/schema/schema-service.js +0 -0
- package/dist/unpkg/dev-deprecated/types/spec/document.js +0 -0
- package/dist/unpkg/dev-deprecated/types/spec/error.js +0 -0
- package/dist/unpkg/dev-deprecated/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/dev-deprecated/types/symbols.js +84 -0
- package/dist/unpkg/dev-deprecated/types/utils.js +0 -0
- package/dist/unpkg/dev-deprecated/types.js +0 -0
- package/dist/unpkg/dev-deprecated/utils/string.js +91 -0
- package/dist/unpkg/prod/-private-3C1OkYtZ.js +39 -0
- package/dist/unpkg/prod/build-config/babel-macros.js +1 -0
- package/dist/unpkg/prod/build-config/canary-features.js +1 -0
- package/dist/unpkg/prod/build-config/debugging.js +1 -0
- package/dist/unpkg/prod/build-config/deprecations.js +1 -0
- package/dist/unpkg/prod/build-config/env.js +1 -0
- package/dist/unpkg/prod/build-config/macros.js +1 -0
- package/dist/unpkg/prod/build-config.js +1 -0
- package/dist/unpkg/prod/configure-C0C1LpG6.js +158 -0
- package/dist/unpkg/prod/configure.js +1 -0
- package/dist/unpkg/prod/graph/-private.js +2234 -0
- package/dist/unpkg/prod/handler-LAyD1Y5l.js +1619 -0
- package/dist/unpkg/prod/hooks-BfiqDg3O.js +26 -0
- package/dist/unpkg/prod/index.js +481 -0
- package/dist/unpkg/prod/promise-state-ipG60SdD.js +6738 -0
- package/dist/unpkg/prod/reactive/-private.js +1 -0
- package/dist/unpkg/prod/reactive.js +127 -0
- package/dist/unpkg/prod/request-CN2LxbYX.js +437 -0
- package/dist/unpkg/prod/request.js +1 -0
- package/dist/unpkg/prod/store/-private.js +127 -0
- package/dist/unpkg/prod/store.js +437 -0
- package/dist/unpkg/prod/types/-private.js +49 -0
- package/dist/unpkg/prod/types/cache/aliases.js +0 -0
- package/dist/unpkg/prod/types/cache/change.js +0 -0
- package/dist/unpkg/prod/types/cache/mutations.js +0 -0
- package/dist/unpkg/prod/types/cache/operations.js +0 -0
- package/dist/unpkg/prod/types/cache/relationship.js +0 -0
- package/dist/unpkg/prod/types/cache.js +0 -0
- package/dist/unpkg/prod/types/graph.js +0 -0
- package/dist/unpkg/prod/types/identifier.js +61 -0
- package/dist/unpkg/prod/types/json/raw.js +0 -0
- package/dist/unpkg/prod/types/params.js +0 -0
- package/dist/unpkg/prod/types/record.js +191 -0
- package/dist/unpkg/prod/types/request.js +77 -0
- package/dist/unpkg/prod/types/runtime.js +34 -0
- package/dist/unpkg/prod/types/schema/concepts.js +0 -0
- package/dist/unpkg/prod/types/schema/fields.js +505 -0
- package/dist/unpkg/prod/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/prod/types/schema/schema-service.js +0 -0
- package/dist/unpkg/prod/types/spec/document.js +0 -0
- package/dist/unpkg/prod/types/spec/error.js +0 -0
- package/dist/unpkg/prod/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/prod/types/symbols.js +84 -0
- package/dist/unpkg/prod/types/utils.js +0 -0
- package/dist/unpkg/prod/types.js +0 -0
- package/dist/unpkg/prod/utils/string.js +72 -0
- package/dist/unpkg/prod-deprecated/-private-3C1OkYtZ.js +39 -0
- package/dist/unpkg/prod-deprecated/build-config/babel-macros.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/canary-features.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/debugging.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/deprecations.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/env.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config/macros.js +1 -0
- package/dist/unpkg/prod-deprecated/build-config.js +1 -0
- package/dist/unpkg/prod-deprecated/configure-BQ8CpIcW.js +158 -0
- package/dist/unpkg/prod-deprecated/configure.js +1 -0
- package/dist/unpkg/prod-deprecated/graph/-private.js +2407 -0
- package/dist/unpkg/prod-deprecated/handler-D639oFvl.js +334 -0
- package/dist/unpkg/prod-deprecated/hooks-DGvi9teJ.js +26 -0
- package/dist/unpkg/prod-deprecated/index.js +481 -0
- package/dist/unpkg/prod-deprecated/promise-state-CYvoIPna.js +8458 -0
- package/dist/unpkg/prod-deprecated/reactive/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/reactive.js +126 -0
- package/dist/unpkg/prod-deprecated/request-CN2LxbYX.js +437 -0
- package/dist/unpkg/prod-deprecated/request.js +1 -0
- package/dist/unpkg/prod-deprecated/store/-private.js +89 -0
- package/dist/unpkg/prod-deprecated/store.js +437 -0
- package/dist/unpkg/prod-deprecated/types/-private.js +49 -0
- package/dist/unpkg/prod-deprecated/types/cache/aliases.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/change.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/mutations.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/operations.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache/relationship.js +0 -0
- package/dist/unpkg/prod-deprecated/types/cache.js +0 -0
- package/dist/unpkg/prod-deprecated/types/graph.js +0 -0
- package/dist/unpkg/prod-deprecated/types/identifier.js +61 -0
- package/dist/unpkg/prod-deprecated/types/json/raw.js +0 -0
- package/dist/unpkg/prod-deprecated/types/params.js +0 -0
- package/dist/unpkg/prod-deprecated/types/record.js +191 -0
- package/dist/unpkg/prod-deprecated/types/request.js +77 -0
- package/dist/unpkg/prod-deprecated/types/runtime.js +34 -0
- package/dist/unpkg/prod-deprecated/types/schema/concepts.js +0 -0
- package/dist/unpkg/prod-deprecated/types/schema/fields.js +505 -0
- package/dist/unpkg/prod-deprecated/types/schema/fields.type-test.js +0 -0
- package/dist/unpkg/prod-deprecated/types/schema/schema-service.js +0 -0
- package/dist/unpkg/prod-deprecated/types/spec/document.js +0 -0
- package/dist/unpkg/prod-deprecated/types/spec/error.js +0 -0
- package/dist/unpkg/prod-deprecated/types/spec/json-api-raw.js +0 -0
- package/dist/unpkg/prod-deprecated/types/symbols.js +84 -0
- package/dist/unpkg/prod-deprecated/types/utils.js +0 -0
- package/dist/unpkg/prod-deprecated/types.js +0 -0
- package/dist/unpkg/prod-deprecated/utils/string.js +72 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +11 -3
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -4,18 +4,18 @@ import type { PrivateRequestManager, RequestManager } from "../../request/-priva
|
|
|
4
4
|
import type { Cache } from "../../types/cache.js";
|
|
5
5
|
import type { PersistedResourceKey, ResourceKey } from "../../types/identifier.js";
|
|
6
6
|
import type { TypedRecordInstance, TypeFromInstance } from "../../types/record.js";
|
|
7
|
+
import type { SchemaService } from "../../types/schema/schema-service.js";
|
|
7
8
|
import type { CollectionResourceDocument, EmptyResourceDocument, JsonApiDocument, ResourceIdentifierObject, SingleResourceDocument } from "../../types/spec/json-api-raw.js";
|
|
8
9
|
import type { Type } from "../../types/symbols.js";
|
|
9
10
|
import type { CacheCapabilitiesManager } from "../-types/q/cache-capabilities-manager.js";
|
|
10
11
|
import type { OpaqueRecordInstance } from "../-types/q/record-instance.js";
|
|
11
|
-
import type { SchemaService } from "../-types/q/schema-service.js";
|
|
12
12
|
import type { StoreRequestInput } from "./cache-handler/handler.js";
|
|
13
13
|
import type { CachePolicy } from "./cache-handler/types.js";
|
|
14
14
|
import { InstanceCache, storeFor } from "./caches/instance-cache.js";
|
|
15
15
|
import type { PrivateCacheKeyManager } from "./managers/cache-key-manager.js";
|
|
16
16
|
import { CacheKeyManager } from "./managers/cache-key-manager.js";
|
|
17
17
|
import type { PrivateNotificationManager } from "./managers/notification-manager.js";
|
|
18
|
-
import NotificationManager from "./managers/notification-manager.js";
|
|
18
|
+
import { NotificationManager } from "./managers/notification-manager.js";
|
|
19
19
|
import type { PrivateRecordArrayManager } from "./managers/record-array-manager.js";
|
|
20
20
|
import type { PrivateRequestStateService } from "./network/request-cache.js";
|
|
21
21
|
import { RequestStateService } from "./network/request-cache.js";
|
|
@@ -101,7 +101,7 @@ export interface Store {
|
|
|
101
101
|
* For Example, to use the default SchemaService for ReactiveResource
|
|
102
102
|
*
|
|
103
103
|
* ```ts
|
|
104
|
-
* import { SchemaService } from '@warp-drive/
|
|
104
|
+
* import { SchemaService } from '@warp-drive/core/reactive';
|
|
105
105
|
*
|
|
106
106
|
* class extends Store {
|
|
107
107
|
* createSchemaService() {
|
|
@@ -110,10 +110,10 @@ export interface Store {
|
|
|
110
110
|
* }
|
|
111
111
|
* ```
|
|
112
112
|
*
|
|
113
|
-
* Or to use the SchemaService for @
|
|
113
|
+
* Or to use the SchemaService for @warp-drive/legacy/model
|
|
114
114
|
*
|
|
115
115
|
* ```ts
|
|
116
|
-
* import { buildSchema } from '@
|
|
116
|
+
* import { buildSchema } from '@warp-drive/legacy/model';
|
|
117
117
|
*
|
|
118
118
|
* class extends Store {
|
|
119
119
|
* createSchemaService() {
|
|
@@ -125,13 +125,13 @@ export interface Store {
|
|
|
125
125
|
* If you wish to chain services, you must either
|
|
126
126
|
* instantiate each schema source directly or super to retrieve
|
|
127
127
|
* an existing service. For convenience, when migrating from
|
|
128
|
-
* `@
|
|
128
|
+
* `@warp-drive/legacy/model` to {@link ReactiveResource} a
|
|
129
129
|
* SchemaService is provided that handles this transition
|
|
130
130
|
* for you:
|
|
131
131
|
*
|
|
132
132
|
* ```ts
|
|
133
|
-
* import { DelegatingSchemaService } from '@
|
|
134
|
-
* import { SchemaService } from '@warp-drive/
|
|
133
|
+
* import { DelegatingSchemaService } from '@warp-drive/legacy/model/migration-support';
|
|
134
|
+
* import { SchemaService } from '@warp-drive/core/reactive';
|
|
135
135
|
*
|
|
136
136
|
* class extends Store {
|
|
137
137
|
* createSchemaService() {
|
|
@@ -176,7 +176,7 @@ export interface Store {
|
|
|
176
176
|
* For Example:
|
|
177
177
|
*
|
|
178
178
|
* ```ts
|
|
179
|
-
* import Store from '@
|
|
179
|
+
* import { Store } from '@warp-drive/core';
|
|
180
180
|
*
|
|
181
181
|
* class SchemaDelegator {
|
|
182
182
|
* constructor(schema) {
|
|
@@ -210,7 +210,6 @@ export interface Store {
|
|
|
210
210
|
* }
|
|
211
211
|
* ```
|
|
212
212
|
*
|
|
213
|
-
* @param {SchemaService} schema
|
|
214
213
|
* @deprecated
|
|
215
214
|
* @public
|
|
216
215
|
*/
|
|
@@ -230,7 +229,7 @@ export interface Store {
|
|
|
230
229
|
* For Example:
|
|
231
230
|
*
|
|
232
231
|
* ```ts
|
|
233
|
-
* import Store from '@
|
|
232
|
+
* import { Store } from '@warp-drive/core';
|
|
234
233
|
*
|
|
235
234
|
* class SchemaDelegator {
|
|
236
235
|
* constructor(schema) {
|
|
@@ -264,7 +263,6 @@ export interface Store {
|
|
|
264
263
|
* }
|
|
265
264
|
* ```
|
|
266
265
|
*
|
|
267
|
-
* @param {SchemaService} schema
|
|
268
266
|
* @deprecated
|
|
269
267
|
* @public
|
|
270
268
|
*/
|
|
@@ -290,7 +288,7 @@ export interface Store {
|
|
|
290
288
|
*/
|
|
291
289
|
export declare class Store extends BaseClass {
|
|
292
290
|
/**
|
|
293
|
-
* Provides access to the NotificationManager associated
|
|
291
|
+
* Provides access to the {@link NotificationManager} associated
|
|
294
292
|
* with this Store instance.
|
|
295
293
|
*
|
|
296
294
|
* The NotificationManager can be used to subscribe to
|
|
@@ -321,20 +319,18 @@ export declare class Store extends BaseClass {
|
|
|
321
319
|
*/
|
|
322
320
|
readonly cacheKeyManager: CacheKeyManager;
|
|
323
321
|
/**
|
|
324
|
-
* Provides access to the
|
|
322
|
+
* Provides access to the {@link RequestManager} instance associated
|
|
325
323
|
* with this Store instance.
|
|
326
324
|
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
*
|
|
325
|
+
* See also:
|
|
326
|
+
* - {@link Fetch}
|
|
327
|
+
* - {@link CacheHandlerInterface | CacheHandler (Interface)}
|
|
328
|
+
* - {@link CacheHandler | CacheHandler (Class)}
|
|
331
329
|
*
|
|
332
330
|
* ```ts
|
|
333
|
-
* import
|
|
334
|
-
* import RequestManager from '@ember-data/request';
|
|
335
|
-
* import Fetch from '@ember-data/request/fetch';
|
|
331
|
+
* import { CacheHandler, Fetch, RequestManager, Store } from '@warp-drive/core';
|
|
336
332
|
*
|
|
337
|
-
* class extends Store {
|
|
333
|
+
* class AppStore extends Store {
|
|
338
334
|
* requestManager = new RequestManager()
|
|
339
335
|
* .use([Fetch])
|
|
340
336
|
* .useCache(CacheHandler);
|
|
@@ -350,9 +346,9 @@ export declare class Store extends BaseClass {
|
|
|
350
346
|
*
|
|
351
347
|
* Note, when defined, these methods will only be invoked if a
|
|
352
348
|
* cache key exists for the request, either because the request
|
|
353
|
-
* contains `cacheOptions.key` or because the
|
|
349
|
+
* contains `cacheOptions.key` or because the {@link CacheKeyManager}
|
|
354
350
|
* was able to generate a key for the request using the configured
|
|
355
|
-
*
|
|
351
|
+
* {@link setIdentifierGenerationMethod | generation method}.
|
|
356
352
|
*
|
|
357
353
|
* `isSoftExpired` will only be invoked if `isHardExpired` returns `false`.
|
|
358
354
|
*
|
|
@@ -557,14 +553,10 @@ export declare class Store extends BaseClass {
|
|
|
557
553
|
otherwise it will return `null`. A record is available if it has been fetched earlier, or
|
|
558
554
|
pushed manually into the store.
|
|
559
555
|
|
|
560
|
-
See [findRecord](../methods/findRecord?anchor=findRecord) if you would like to request this record from the backend.
|
|
561
|
-
|
|
562
|
-
_Note: This is a synchronous method and does not return a promise._
|
|
563
|
-
|
|
564
556
|
**Example 1**
|
|
565
557
|
|
|
566
|
-
```
|
|
567
|
-
|
|
558
|
+
```ts
|
|
559
|
+
const post = store.peekRecord('post', '1');
|
|
568
560
|
|
|
569
561
|
post.id; // '1'
|
|
570
562
|
```
|
|
@@ -575,8 +567,8 @@ export declare class Store extends BaseClass {
|
|
|
575
567
|
|
|
576
568
|
**Example 2**
|
|
577
569
|
|
|
578
|
-
```
|
|
579
|
-
|
|
570
|
+
```ts
|
|
571
|
+
const post = store.peekRecord({ type: 'post', id: '1' });
|
|
580
572
|
post.id; // '1'
|
|
581
573
|
```
|
|
582
574
|
|
|
@@ -590,40 +582,34 @@ export declare class Store extends BaseClass {
|
|
|
590
582
|
post.id; // '1'
|
|
591
583
|
```
|
|
592
584
|
|
|
593
|
-
|
|
594
585
|
@since 1.13.0
|
|
595
586
|
@public
|
|
596
|
-
@param
|
|
597
|
-
@param
|
|
598
|
-
@return {Model|null} record
|
|
587
|
+
@param type - either a string representing the modelName or a ResourceIdentifier object containing both the type (a string) and the id (a string) for the record or an lid (a string) of an existing record
|
|
588
|
+
@param id - optional only if the first param is a ResourceIdentifier, else the string id of the record to be retrieved.
|
|
599
589
|
*/
|
|
600
590
|
peekRecord<T>(type: TypeFromInstance<T>, id: string | number): T | null;
|
|
601
591
|
peekRecord(type: string, id: string | number): unknown | null;
|
|
602
592
|
peekRecord<T>(identifier: ResourceIdentifierObject<TypeFromInstance<T>>): T | null;
|
|
603
593
|
peekRecord(identifier: ResourceIdentifierObject): unknown | null;
|
|
604
594
|
/**
|
|
605
|
-
This method returns
|
|
606
|
-
known records for a given type in the store.
|
|
595
|
+
This method returns the {@link LegacyLiveArray} that contains all of the
|
|
596
|
+
known records for a given type in the store. Each ResourceType has only
|
|
597
|
+
one LiveArray instance, so multiple calls to `peekAll` with the same type
|
|
598
|
+
will always return the same instance.
|
|
607
599
|
|
|
608
|
-
Note that because it's
|
|
600
|
+
Note that because it's a LiveArray, the result will contain any
|
|
609
601
|
locally created records of the type, however, it will not make a
|
|
610
|
-
request to the backend to retrieve additional records.
|
|
611
|
-
would like to request all the records from the backend please use
|
|
612
|
-
[store.findAll](../methods/findAll?anchor=findAll).
|
|
613
|
-
|
|
614
|
-
Also note that multiple calls to `peekAll` for a given type will always
|
|
615
|
-
return the same `RecordArray`.
|
|
602
|
+
request to the backend to retrieve additional records.
|
|
616
603
|
|
|
617
604
|
Example
|
|
618
605
|
|
|
619
|
-
```
|
|
620
|
-
|
|
606
|
+
```ts
|
|
607
|
+
const allPosts = store.peekAll('post');
|
|
621
608
|
```
|
|
622
609
|
|
|
623
610
|
@since 1.13.0
|
|
624
611
|
@public
|
|
625
|
-
@param
|
|
626
|
-
@return {RecordArray}
|
|
612
|
+
@param type the name of the resource
|
|
627
613
|
*/
|
|
628
614
|
peekAll<T>(type: TypeFromInstance<T>): LegacyLiveArray<T>;
|
|
629
615
|
peekAll(type: string): LegacyLiveArray;
|
|
@@ -708,7 +694,7 @@ export declare class Store extends BaseClass {
|
|
|
708
694
|
For this model:
|
|
709
695
|
|
|
710
696
|
```js [app/models/person.js]
|
|
711
|
-
import Model, { attr, hasMany } from '@
|
|
697
|
+
import Model, { attr, hasMany } from '@warp-drive/legacy/model';
|
|
712
698
|
|
|
713
699
|
export default class PersonRoute extends Route {
|
|
714
700
|
@attr('string') firstName;
|
|
@@ -775,23 +761,17 @@ export declare class Store extends BaseClass {
|
|
|
775
761
|
}
|
|
776
762
|
```
|
|
777
763
|
|
|
778
|
-
If you're streaming data or implementing
|
|
779
|
-
that you have converted the incoming data into this form.
|
|
780
|
-
store's [normalize](../methods/normalize?anchor=normalize) method is a convenience
|
|
781
|
-
helper for converting a json payload into the form Ember Data
|
|
782
|
-
expects.
|
|
783
|
-
|
|
784
|
-
```js
|
|
785
|
-
store.push(store.normalize('person', data));
|
|
786
|
-
```
|
|
764
|
+
If you're streaming data, or implementing response handling, make sure
|
|
765
|
+
that you have converted the incoming data into this form.
|
|
787
766
|
|
|
788
767
|
This method can be used both to push in brand new
|
|
789
768
|
records, as well as to update existing records.
|
|
790
769
|
|
|
770
|
+
See also {@link Cache.patch}
|
|
771
|
+
|
|
791
772
|
@public
|
|
792
|
-
@param
|
|
793
|
-
@return the record(s) that
|
|
794
|
-
updated.
|
|
773
|
+
@param data
|
|
774
|
+
@return the primary record(s) that created or updated.
|
|
795
775
|
*/
|
|
796
776
|
push(data: EmptyResourceDocument): null;
|
|
797
777
|
push<T>(data: SingleResourceDocument<TypeFromInstance<T>>): T;
|
|
@@ -803,8 +783,7 @@ export declare class Store extends BaseClass {
|
|
|
803
783
|
without creating materialized records.
|
|
804
784
|
|
|
805
785
|
@private
|
|
806
|
-
@
|
|
807
|
-
@return {ResourceKey|Array<ResourceKey>|null} identifiers for the primary records that had data loaded
|
|
786
|
+
@return identifiers for the primary records that had data loaded
|
|
808
787
|
*/
|
|
809
788
|
_push(jsonApiDoc: JsonApiDocument, asyncFlush?: boolean): PersistedResourceKey | PersistedResourceKey[] | null;
|
|
810
789
|
/**
|
|
@@ -22,7 +22,6 @@ export { normalizeModelName as _deprecatingNormalize } from "./-private/utils/no
|
|
|
22
22
|
export type { StoreRequestInput } from "./-private/cache-handler/handler.js";
|
|
23
23
|
export { type LegacyManyArray, type LegacyManyArray as RelatedCollection, createLegacyManyArray } from "./-private/record-arrays/legacy-many-array.js";
|
|
24
24
|
export { log, logGroup } from "./-private/debug/utils.js";
|
|
25
|
-
export { getPromiseState, type PromiseState } from "./-private/new-core-tmp/promise-state.js";
|
|
26
25
|
export { DISPOSE, createRequestSubscription, type RequestArgs, type SubscriptionArgs, type RequestComponentArgs, type RequestSubscription, type ContentFeatures, type RecoveryFeatures, type AutorefreshBehaviorCombos, type AutorefreshBehaviorType } from "./-private/new-core-tmp/request-subscription.js";
|
|
27
26
|
export { getRequestState, type RequestLoadingState, type RequestCacheRequestState as RequestState } from "./-private/new-core-tmp/request-state.js";
|
|
28
27
|
export { type SignalHooks, waitFor } from "./-private/new-core-tmp/reactivity/configure.js";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { RequestKey, ResourceKey } from "../../../types/identifier.js";
|
|
2
|
+
import type { SchemaService } from "../../../types/schema/schema-service.js";
|
|
2
3
|
import type { CacheKeyManager } from "../../-private/managers/cache-key-manager.js";
|
|
3
4
|
import type { NotificationType } from "../../-private/managers/notification-manager.js";
|
|
4
|
-
import type { SchemaService } from "./schema-service.js";
|
|
5
5
|
/**
|
|
6
6
|
* CacheCapabilitiesManager provides encapsulated API access to the minimal
|
|
7
7
|
* subset of the Store's functionality that Cache implementations
|
|
@@ -42,7 +42,7 @@ export type KeyOrString<T> = keyof T & string extends never ? string : keyof T &
|
|
|
42
42
|
* "model" class.
|
|
43
43
|
*
|
|
44
44
|
* Only used when using the legacy schema-service implementation
|
|
45
|
-
* for @
|
|
45
|
+
* for @warp-drive/legacy/model or when wrapping schema for legacy
|
|
46
46
|
* Adapters/Serializers.
|
|
47
47
|
*
|
|
48
48
|
*/
|
|
@@ -248,13 +248,13 @@ declare module "../-private/store-service" {
|
|
|
248
248
|
}
|
|
249
249
|
```
|
|
250
250
|
|
|
251
|
-
See
|
|
251
|
+
See {@link Store.peekRecord | peekRecord} to get the cached version of a record.
|
|
252
252
|
|
|
253
253
|
### Retrieving Related Model Records
|
|
254
254
|
|
|
255
|
-
If you use an adapter such as
|
|
256
|
-
[
|
|
257
|
-
|
|
255
|
+
If you use an adapter such as the
|
|
256
|
+
[JSONAPIAdapter](/api/@warp-drive/legacy/adapter/json-api/classes/JSONAPIAdapter)
|
|
257
|
+
which supports the [{json:api} specification](http://jsonapi.org/) and if your server
|
|
258
258
|
endpoint supports the use of an
|
|
259
259
|
['include' query parameter](http://jsonapi.org/format/#fetching-includes),
|
|
260
260
|
you can use `findRecord()` or `findAll()` to automatically retrieve additional records related to
|
|
@@ -421,7 +421,7 @@ declare module "../-private/store-service" {
|
|
|
421
421
|
records in the store:
|
|
422
422
|
|
|
423
423
|
```js [app/adapters/application.js]
|
|
424
|
-
import Adapter from '@
|
|
424
|
+
import { Adapter } from '@warp-drive/legacy/adapter';
|
|
425
425
|
|
|
426
426
|
export default class ApplicationAdapter extends Adapter {
|
|
427
427
|
shouldReloadAll(store, snapshotsArray) {
|
|
@@ -498,13 +498,13 @@ declare module "../-private/store-service" {
|
|
|
498
498
|
}
|
|
499
499
|
```
|
|
500
500
|
|
|
501
|
-
See
|
|
501
|
+
See {@link Store.peekAll | peekAll} to get an array of current records in the
|
|
502
502
|
store, without waiting until a reload is finished.
|
|
503
503
|
|
|
504
504
|
### Retrieving Related Model Records
|
|
505
505
|
|
|
506
|
-
If you use an adapter such as
|
|
507
|
-
[
|
|
506
|
+
If you use an adapter such as the default
|
|
507
|
+
[JSONAPIAdapter](/api/@warp-drive/legacy/adapter/json-api/classes/JSONAPIAdapter)
|
|
508
508
|
that supports the [JSON API specification](http://jsonapi.org/) and if your server
|
|
509
509
|
endpoint supports the use of an
|
|
510
510
|
['include' query parameter](http://jsonapi.org/format/#fetching-includes),
|
|
@@ -535,7 +535,7 @@ declare module "../-private/store-service" {
|
|
|
535
535
|
}
|
|
536
536
|
```
|
|
537
537
|
|
|
538
|
-
See
|
|
538
|
+
See {@link Store.query | query} to only get a subset of records from the server.
|
|
539
539
|
|
|
540
540
|
@public
|
|
541
541
|
@deprecated use {@link Store.request} instead
|
|
@@ -575,7 +575,7 @@ declare module "../-private/store-service" {
|
|
|
575
575
|
|
|
576
576
|
If you do something like this:
|
|
577
577
|
|
|
578
|
-
```
|
|
578
|
+
```js
|
|
579
579
|
store.query('person', { ids: ['1', '2', '3'] });
|
|
580
580
|
```
|
|
581
581
|
|
|
@@ -602,7 +602,7 @@ declare module "../-private/store-service" {
|
|
|
602
602
|
query(type: string, query: LegacyResourceQuery, options?: QueryOptions): Promise<LegacyQueryArray>;
|
|
603
603
|
/**
|
|
604
604
|
This method makes a request for one record, where the `id` is not known
|
|
605
|
-
beforehand (if the `id` is known, use
|
|
605
|
+
beforehand (if the `id` is known, use {@link Store.findRecord | findRecord}
|
|
606
606
|
instead).
|
|
607
607
|
|
|
608
608
|
This method can be used when it is certain that the server will return a
|
|
@@ -611,37 +611,40 @@ declare module "../-private/store-service" {
|
|
|
611
611
|
Each time this method is called a new request is made through the adapter.
|
|
612
612
|
|
|
613
613
|
Let's assume our API provides an endpoint for the currently logged in user
|
|
614
|
-
via:
|
|
615
614
|
|
|
616
|
-
```
|
|
617
|
-
// GET /api/
|
|
615
|
+
```ts
|
|
616
|
+
// GET /api/user/me
|
|
618
617
|
{
|
|
619
|
-
|
|
620
|
-
|
|
618
|
+
data: {
|
|
619
|
+
type: 'user',
|
|
620
|
+
id: '1234',
|
|
621
|
+
attributes: {
|
|
621
622
|
username: 'admin'
|
|
622
623
|
}
|
|
623
624
|
}
|
|
625
|
+
}
|
|
624
626
|
```
|
|
625
627
|
|
|
626
628
|
Since the specific `id` of the `user` is not known beforehand, we can use
|
|
627
629
|
`queryRecord` to get the user:
|
|
628
630
|
|
|
629
|
-
```
|
|
630
|
-
store.queryRecord('user', {})
|
|
631
|
-
|
|
632
|
-
// do thing
|
|
633
|
-
});
|
|
631
|
+
```ts
|
|
632
|
+
const user = await store.queryRecord('user', { me: true });
|
|
633
|
+
user.username; // admin
|
|
634
634
|
```
|
|
635
635
|
|
|
636
636
|
The request is made through the adapters' `queryRecord`:
|
|
637
637
|
|
|
638
|
-
```
|
|
639
|
-
import Adapter from '@
|
|
640
|
-
import $ from 'jquery';
|
|
638
|
+
```ts [app/adapters/user.ts]
|
|
639
|
+
import Adapter from '@warp-drive/legacy/adapter';
|
|
641
640
|
|
|
642
641
|
export default class UserAdapter extends Adapter {
|
|
643
|
-
queryRecord(modelName, query) {
|
|
644
|
-
|
|
642
|
+
async queryRecord(modelName, query) {
|
|
643
|
+
if (query.me) {
|
|
644
|
+
const response = await fetch('/api/me');
|
|
645
|
+
return await response.json();
|
|
646
|
+
}
|
|
647
|
+
throw new Error('Unsupported query');
|
|
645
648
|
}
|
|
646
649
|
}
|
|
647
650
|
```
|
|
@@ -664,7 +667,7 @@ declare module "../-private/store-service" {
|
|
|
664
667
|
}
|
|
665
668
|
```
|
|
666
669
|
|
|
667
|
-
```
|
|
670
|
+
```js
|
|
668
671
|
store.query('user', { username: 'unique' }).then(function(users) {
|
|
669
672
|
return users.firstObject;
|
|
670
673
|
}).then(function(user) {
|
|
@@ -684,7 +687,7 @@ declare module "../-private/store-service" {
|
|
|
684
687
|
}
|
|
685
688
|
```
|
|
686
689
|
|
|
687
|
-
```
|
|
690
|
+
```js
|
|
688
691
|
store.queryRecord('user', { username: 'unique' }).then(function(user) {
|
|
689
692
|
// user is null
|
|
690
693
|
});
|
|
@@ -744,7 +747,7 @@ declare module "../-private/store-service" {
|
|
|
744
747
|
/**
|
|
745
748
|
Returns the schema for a particular resource type (modelName).
|
|
746
749
|
|
|
747
|
-
When used with Model
|
|
750
|
+
When used with [Model](/api/@warp-drive/legacy/model/classes/Model) the return is the model class,
|
|
748
751
|
but this is not guaranteed.
|
|
749
752
|
|
|
750
753
|
If looking to query attribute or relationship information it is
|
|
@@ -754,9 +757,7 @@ declare module "../-private/store-service" {
|
|
|
754
757
|
signatures.
|
|
755
758
|
|
|
756
759
|
The class of a model might be useful if you want to get a list of all the
|
|
757
|
-
relationship names of the model
|
|
758
|
-
[`relationshipNames`](/ember-data/release/classes/Model?anchor=relationshipNames)
|
|
759
|
-
for example.
|
|
760
|
+
relationship names of the model.
|
|
760
761
|
|
|
761
762
|
@public
|
|
762
763
|
@deprecated use {@link Store.schema} instead
|
package/declarations/store.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import type { RequestKey, ResourceKey } from "./identifier.js";
|
|
|
7
7
|
import type { Value } from "./json/raw.js";
|
|
8
8
|
import type { TypeFromInstanceOrString } from "./record.js";
|
|
9
9
|
import type { RequestContext, StructuredDataDocument, StructuredDocument } from "./request.js";
|
|
10
|
-
import type { ResourceDocument, SingleResourceDataDocument } from "./spec/document.js";
|
|
10
|
+
import type { CollectionResourceDataDocument, ResourceDocument, SingleResourceDataDocument } from "./spec/document.js";
|
|
11
11
|
import type { ApiError } from "./spec/error.js";
|
|
12
12
|
/**
|
|
13
13
|
* A hash of changed attributes with the key being the attribute name and the value being an
|
|
@@ -253,23 +253,25 @@ export interface Cache {
|
|
|
253
253
|
*
|
|
254
254
|
* @public
|
|
255
255
|
*/
|
|
256
|
-
willCommit(cacheKey: ResourceKey, context: RequestContext | null): void;
|
|
256
|
+
willCommit(cacheKey: ResourceKey | ResourceKey[], context: RequestContext | null): void;
|
|
257
257
|
/**
|
|
258
258
|
* [LIFECYCLE] Signals to the cache that a resource
|
|
259
259
|
* was successfully updated as part of a save transaction.
|
|
260
260
|
*
|
|
261
261
|
* @public
|
|
262
|
-
* @param the primary ResourceKey that was operated on
|
|
263
|
-
* @param
|
|
262
|
+
* @param cacheKey - the primary ResourceKey that was operated on
|
|
263
|
+
* @param result - a document in the cache format containing any updated data
|
|
264
264
|
*/
|
|
265
|
-
didCommit(cacheKey: ResourceKey, result: StructuredDataDocument<
|
|
265
|
+
didCommit(cacheKey: ResourceKey, result: StructuredDataDocument<SingleResourceDataDocument> | null): SingleResourceDataDocument;
|
|
266
|
+
didCommit(cacheKey: ResourceKey[], result: StructuredDataDocument<SingleResourceDataDocument> | null): SingleResourceDataDocument;
|
|
267
|
+
didCommit(cacheKey: ResourceKey[], result: StructuredDataDocument<CollectionResourceDataDocument> | null): CollectionResourceDataDocument;
|
|
266
268
|
/**
|
|
267
269
|
* [LIFECYCLE] Signals to the cache that a resource
|
|
268
270
|
* was update via a save transaction failed.
|
|
269
271
|
*
|
|
270
272
|
* @public
|
|
271
273
|
*/
|
|
272
|
-
commitWasRejected(cacheKey: ResourceKey, errors?: ApiError[]): void;
|
|
274
|
+
commitWasRejected(cacheKey: ResourceKey | ResourceKey[], errors?: ApiError[]): void;
|
|
273
275
|
/**
|
|
274
276
|
* [LIFECYCLE] Signals to the cache that all data for a resource
|
|
275
277
|
* should be cleared.
|
|
@@ -134,4 +134,136 @@ export type StringSatisfiesIncludes<
|
|
|
134
134
|
SET extends string
|
|
135
135
|
> = _StringSatisfiesIncludes<T, SET, T>;
|
|
136
136
|
export declare function createIncludeValidator<T extends TypedRecordInstance>(): <U extends string>(includes: StringSatisfiesIncludes<U, Includes<T>>) => U;
|
|
137
|
+
/**
|
|
138
|
+
* A utility that takes two types, K and T, and produces a new type that is a "mask" of T based on K.
|
|
139
|
+
*
|
|
140
|
+
* That's a mouthful, so let's break it down:
|
|
141
|
+
*
|
|
142
|
+
* Let's say you have a User type and an Address type.
|
|
143
|
+
*
|
|
144
|
+
* ```ts
|
|
145
|
+
* interface Address {
|
|
146
|
+
* street: string;
|
|
147
|
+
* city: string;
|
|
148
|
+
* state: string;
|
|
149
|
+
* zip: string;
|
|
150
|
+
* }
|
|
151
|
+
*
|
|
152
|
+
* interface User {
|
|
153
|
+
* name: string;
|
|
154
|
+
* title: string;
|
|
155
|
+
* address: Address;
|
|
156
|
+
* }
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* Now, imagine you want to load a preview of the user with some information about their address,
|
|
160
|
+
* but you don't want to load the entire user or address. You probably want to still ensure
|
|
161
|
+
* the type of the data you do load matches the underlying Address and User types, but doesn't
|
|
162
|
+
* include everything.
|
|
163
|
+
*
|
|
164
|
+
* If you did this manually, you might do something like this:
|
|
165
|
+
*
|
|
166
|
+
* ```ts
|
|
167
|
+
* interface UserPreview {
|
|
168
|
+
* name: string;
|
|
169
|
+
* address: AddressPreview;
|
|
170
|
+
* }
|
|
171
|
+
*
|
|
172
|
+
* interface AddressPreview {
|
|
173
|
+
* city: string;
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* From a TypeScript performance perspective, this is the best way to approach these preview
|
|
178
|
+
* types, but this is also tedious and error-prone, especially if the User or Address types change.
|
|
179
|
+
*
|
|
180
|
+
* For Address, we could create a validated type using `Pick`:
|
|
181
|
+
*
|
|
182
|
+
* ```ts
|
|
183
|
+
* type AddressPreview = Pick<Address, 'city'>;
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* This ensures that if the Address type changes, our AddressPreview will still be valid.
|
|
187
|
+
* However, for UserPreview, we can't just use `Pick` because the `address` property is of type `Address`,
|
|
188
|
+
* not `AddressPreview`. This is where the `Mask` type comes in.
|
|
189
|
+
*
|
|
190
|
+
* With `Mask`, we define the `UserPreview` in two parts
|
|
191
|
+
* - first, we define the subset of fields we want to include from `User`, using `Pick` or an interface.
|
|
192
|
+
* - then, we use `Mask` to replace the related types of fields like Address with their more limited subset.
|
|
193
|
+
*
|
|
194
|
+
* Here's how we can do it:
|
|
195
|
+
*
|
|
196
|
+
* ```ts
|
|
197
|
+
* // First, we define the base of UserPreview with Pick
|
|
198
|
+
* type UserPreviewBase = Pick<User, 'name' | 'address'>;
|
|
199
|
+
* // Then, we use Mask to replace Address with AddressPreview
|
|
200
|
+
* type UserPreview = Mask<{ address: AddressPreview }, UserPreviewBase>;
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* Now, `UserPreview` will have the `name` field from `User` and the `address` field will be of type `AddressPreview`.
|
|
204
|
+
* This way, if the `User` or `Address` types change, TypeScript will ensure that our `UserPreview` and `AddressPreview`
|
|
205
|
+
* types remain valid and consistent with the underlying types.
|
|
206
|
+
*
|
|
207
|
+
* But what if your app has data with massive interfaces such that the TypeScript performance of this
|
|
208
|
+
* approach becomes a problem? In that case, see {@link Validate}
|
|
209
|
+
*/
|
|
210
|
+
export type Mask<
|
|
211
|
+
K extends object,
|
|
212
|
+
T extends K
|
|
213
|
+
> = { [P in keyof T] : P extends keyof K ? (T[P] extends K[P] ? K[P] : never) : T[P] };
|
|
214
|
+
/**
|
|
215
|
+
* A utility that takes two types, K and T, and ensures that K is a valid subset of T.
|
|
216
|
+
*
|
|
217
|
+
* That's a mouthful, so let's break it down:
|
|
218
|
+
*
|
|
219
|
+
* Let's say you have a User type and an Address type.
|
|
220
|
+
*
|
|
221
|
+
* ```ts
|
|
222
|
+
* interface Address {
|
|
223
|
+
* street: string;
|
|
224
|
+
* city: string;
|
|
225
|
+
* state: string;
|
|
226
|
+
* zip: string;
|
|
227
|
+
* }
|
|
228
|
+
*
|
|
229
|
+
* interface User {
|
|
230
|
+
* name: string;
|
|
231
|
+
* title: string;
|
|
232
|
+
* address: Address;
|
|
233
|
+
* }
|
|
234
|
+
* ```
|
|
235
|
+
*
|
|
236
|
+
* Now, imagine you want to load a preview of the user with some information about their address,
|
|
237
|
+
* but you don't want to load the entire user or address. You probably want to still ensure
|
|
238
|
+
* the type of the data you do load matches the underlying Address and User types, but doesn't
|
|
239
|
+
* include everything.
|
|
240
|
+
*
|
|
241
|
+
* You might do something like this:
|
|
242
|
+
*
|
|
243
|
+
* ```ts
|
|
244
|
+
* interface UserPreview {
|
|
245
|
+
* name: string;
|
|
246
|
+
* address: AddressPreview;
|
|
247
|
+
* }
|
|
248
|
+
*
|
|
249
|
+
* interface AddressPreview {
|
|
250
|
+
* city: string;
|
|
251
|
+
* }
|
|
252
|
+
* ```
|
|
253
|
+
*
|
|
254
|
+
* From a TypeScript performance perspective, this is the best way to approach these preview
|
|
255
|
+
* types, but this is also error-prone, especially if the User or Address types change.
|
|
256
|
+
*
|
|
257
|
+
* Validate can help ensure that your preview types remain valid.
|
|
258
|
+
*
|
|
259
|
+
* ```ts
|
|
260
|
+
* type IsValidUserPreview = Validate<UserPreview, User>; // This will be valid
|
|
261
|
+
* ```
|
|
262
|
+
*
|
|
263
|
+
* For help creating subsets of types, see {@link Mask}
|
|
264
|
+
*/
|
|
265
|
+
export type Validate<
|
|
266
|
+
K extends object,
|
|
267
|
+
T extends K
|
|
268
|
+
> = T extends K ? K : never;
|
|
137
269
|
export {};
|