@contello/store 1.3.0 → 1.4.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.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * resolved absolute paths to the `.gql` fragment files that store-backed
3
+ * codegen should include. combines media's primitives with store's own
4
+ * `StoreAsset` / `StoreRoute` fragments. intended for use in a project's
5
+ * codegen `documents` config.
6
+ */
7
+ declare const fragments: string[];
8
+
9
+ export { fragments };
@@ -0,0 +1,12 @@
1
+ // src/fragments.ts
2
+ import { fileURLToPath } from "url";
3
+ import { fragments as mediaFragments } from "@contello/media/fragments";
4
+ var packageRoot = fileURLToPath(new URL("..", import.meta.url));
5
+ var fragments = [
6
+ ...mediaFragments,
7
+ `${packageRoot}fragments/asset.gql`,
8
+ `${packageRoot}fragments/route.gql`
9
+ ];
10
+ export {
11
+ fragments
12
+ };
package/dist/index.cjs CHANGED
@@ -37,6 +37,7 @@ module.exports = __toCommonJS(index_exports);
37
37
 
38
38
  // src/store.ts
39
39
  var import_client = require("@contello/client");
40
+ var import_media = require("@contello/media");
40
41
 
41
42
  // src/assets.ts
42
43
  var import_projected = require("projected");
@@ -1298,6 +1299,7 @@ var Store = class {
1298
1299
  _client;
1299
1300
  _resolver;
1300
1301
  _watcher;
1302
+ _media;
1301
1303
  updates$;
1302
1304
  constructor(options) {
1303
1305
  const { url, project, token, operations } = options;
@@ -1315,8 +1317,20 @@ var Store = class {
1315
1317
  this._resolver = new ModelResolver(options.models);
1316
1318
  this._watcher = createInternalWatcher(this._client, this._resolver);
1317
1319
  this.updates$ = this._watcher.updates$;
1320
+ this._media = options.media ? (0, import_media.createMediaResolver)(options.media) : void 0;
1318
1321
  this.ping = () => this._client.ping();
1319
1322
  }
1323
+ /**
1324
+ * Returns the configured `MediaResolver`. Throws if `createStore` was called
1325
+ * without a `media` option. Type-narrows to `never` when `THasMedia` is `false`,
1326
+ * so compile-time consumers get an error on access without configuration.
1327
+ */
1328
+ get media() {
1329
+ if (!this._media) {
1330
+ throw new Error("store.media requires the `media` option to be configured in createStore()");
1331
+ }
1332
+ return this._media;
1333
+ }
1320
1334
  async init() {
1321
1335
  await wrap("store:init", () => this._client.init());
1322
1336
  this._watcher.start();
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { OperationMap, ContelloClient, ConnectionEvents, UploadData, UploadMetadata, UploadOptions, DownloadResult, ProxyResult } from '@contello/client';
2
+ import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
2
3
  import { Observable } from 'rxjs';
3
4
  import { MaybePromise } from 'projected';
4
5
 
@@ -112,6 +113,7 @@ type CreateStoreOptions<TOps extends OperationMap | undefined = undefined, TMode
112
113
  onReconnecting?: (() => void) | undefined;
113
114
  onError?: ((error: unknown) => void) | undefined;
114
115
  connectionEvents?: ConnectionEvents | undefined;
116
+ media?: MediaResolverOptions | undefined;
115
117
  };
116
118
  /** Cache options for non-lazy collections and singletons. */
117
119
  type CacheOptions = {
@@ -311,12 +313,27 @@ type Routes = {
311
313
  clear(): void;
312
314
  };
313
315
 
314
- declare class Store<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
316
+ type HasMedia<O> = O extends {
317
+ media: MediaResolverOptions;
318
+ } ? true : false;
319
+ type HasMediaFallback<O> = O extends {
320
+ media: {
321
+ fallback: ImageDef;
322
+ };
323
+ } ? true : false;
324
+ declare class Store<TOps extends OperationMap | undefined = undefined, TModels extends string = string, THasMedia extends boolean = false, THasMediaFallback extends boolean = false> {
315
325
  private _client;
316
326
  private _resolver;
317
327
  private _watcher;
328
+ private _media;
318
329
  readonly updates$: Observable<UpdateBatch>;
319
330
  constructor(options: CreateStoreOptions<TOps, TModels>);
331
+ /**
332
+ * Returns the configured `MediaResolver`. Throws if `createStore` was called
333
+ * without a `media` option. Type-narrows to `never` when `THasMedia` is `false`,
334
+ * so compile-time consumers get an error on access without configuration.
335
+ */
336
+ get media(): THasMedia extends true ? MediaResolver<THasMediaFallback> : never;
320
337
  init(): Promise<void>;
321
338
  destroy(): Promise<void>;
322
339
  defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
@@ -335,6 +352,6 @@ declare class Store<TOps extends OperationMap | undefined = undefined, TModels e
335
352
  defineI18nMessages(def: I18nMessageDef): I18nMessages;
336
353
  ping: () => Promise<void>;
337
354
  }
338
- declare function createStore<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: CreateStoreOptions<TOps, TModels>): Store<TOps, TModels>;
355
+ declare function createStore<TOps extends OperationMap | undefined = undefined, TModels extends string = string, O extends CreateStoreOptions<TOps, TModels> = CreateStoreOptions<TOps, TModels>>(options: O): Store<TOps, TModels, HasMedia<O>, HasMediaFallback<O>>;
339
356
 
340
357
  export { type AssetCollectionOptions, type Assets, type CacheOptions, type Collection, type CollectionDef, type CollectionSync, type CollectionSyncDef, type CreateStoreOptions, type Fetchable, type I18nInitialTranslation, type I18nMessage, type I18nMessageDef, type I18nMessageRegistrationDefinition, type I18nMessages, type I18nTranslation, type I18nVariableInput, type LazyCacheOptions, type LazyCollection, type LazyCollectionDef, type Loadable, type MapperContext, type RouteCollectionOptions, type Routes, type Singleton, type SingletonDef, type SingletonSync, type SingletonSyncDef, Store, type StoreAsset, type StoreFile, type StoreFileMetadata, type StoreRoute, type StoreRouteCustomHeader, type SyncCacheOptions, type UpdateBatch, type UpdateEvent, type UpdateEventFor, type UpdateMutationType, createStore };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { OperationMap, ContelloClient, ConnectionEvents, UploadData, UploadMetadata, UploadOptions, DownloadResult, ProxyResult } from '@contello/client';
2
+ import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
2
3
  import { Observable } from 'rxjs';
3
4
  import { MaybePromise } from 'projected';
4
5
 
@@ -112,6 +113,7 @@ type CreateStoreOptions<TOps extends OperationMap | undefined = undefined, TMode
112
113
  onReconnecting?: (() => void) | undefined;
113
114
  onError?: ((error: unknown) => void) | undefined;
114
115
  connectionEvents?: ConnectionEvents | undefined;
116
+ media?: MediaResolverOptions | undefined;
115
117
  };
116
118
  /** Cache options for non-lazy collections and singletons. */
117
119
  type CacheOptions = {
@@ -311,12 +313,27 @@ type Routes = {
311
313
  clear(): void;
312
314
  };
313
315
 
314
- declare class Store<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
316
+ type HasMedia<O> = O extends {
317
+ media: MediaResolverOptions;
318
+ } ? true : false;
319
+ type HasMediaFallback<O> = O extends {
320
+ media: {
321
+ fallback: ImageDef;
322
+ };
323
+ } ? true : false;
324
+ declare class Store<TOps extends OperationMap | undefined = undefined, TModels extends string = string, THasMedia extends boolean = false, THasMediaFallback extends boolean = false> {
315
325
  private _client;
316
326
  private _resolver;
317
327
  private _watcher;
328
+ private _media;
318
329
  readonly updates$: Observable<UpdateBatch>;
319
330
  constructor(options: CreateStoreOptions<TOps, TModels>);
331
+ /**
332
+ * Returns the configured `MediaResolver`. Throws if `createStore` was called
333
+ * without a `media` option. Type-narrows to `never` when `THasMedia` is `false`,
334
+ * so compile-time consumers get an error on access without configuration.
335
+ */
336
+ get media(): THasMedia extends true ? MediaResolver<THasMediaFallback> : never;
320
337
  init(): Promise<void>;
321
338
  destroy(): Promise<void>;
322
339
  defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
@@ -335,6 +352,6 @@ declare class Store<TOps extends OperationMap | undefined = undefined, TModels e
335
352
  defineI18nMessages(def: I18nMessageDef): I18nMessages;
336
353
  ping: () => Promise<void>;
337
354
  }
338
- declare function createStore<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: CreateStoreOptions<TOps, TModels>): Store<TOps, TModels>;
355
+ declare function createStore<TOps extends OperationMap | undefined = undefined, TModels extends string = string, O extends CreateStoreOptions<TOps, TModels> = CreateStoreOptions<TOps, TModels>>(options: O): Store<TOps, TModels, HasMedia<O>, HasMediaFallback<O>>;
339
356
 
340
357
  export { type AssetCollectionOptions, type Assets, type CacheOptions, type Collection, type CollectionDef, type CollectionSync, type CollectionSyncDef, type CreateStoreOptions, type Fetchable, type I18nInitialTranslation, type I18nMessage, type I18nMessageDef, type I18nMessageRegistrationDefinition, type I18nMessages, type I18nTranslation, type I18nVariableInput, type LazyCacheOptions, type LazyCollection, type LazyCollectionDef, type Loadable, type MapperContext, type RouteCollectionOptions, type Routes, type Singleton, type SingletonDef, type SingletonSync, type SingletonSyncDef, Store, type StoreAsset, type StoreFile, type StoreFileMetadata, type StoreRoute, type StoreRouteCustomHeader, type SyncCacheOptions, type UpdateBatch, type UpdateEvent, type UpdateEventFor, type UpdateMutationType, createStore };
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // src/store.ts
2
2
  import { createContelloClient } from "@contello/client";
3
+ import { createMediaResolver } from "@contello/media";
3
4
 
4
5
  // src/assets.ts
5
6
  import { ProjectedLazyMap } from "projected";
@@ -1261,6 +1262,7 @@ var Store = class {
1261
1262
  _client;
1262
1263
  _resolver;
1263
1264
  _watcher;
1265
+ _media;
1264
1266
  updates$;
1265
1267
  constructor(options) {
1266
1268
  const { url, project, token, operations } = options;
@@ -1278,8 +1280,20 @@ var Store = class {
1278
1280
  this._resolver = new ModelResolver(options.models);
1279
1281
  this._watcher = createInternalWatcher(this._client, this._resolver);
1280
1282
  this.updates$ = this._watcher.updates$;
1283
+ this._media = options.media ? createMediaResolver(options.media) : void 0;
1281
1284
  this.ping = () => this._client.ping();
1282
1285
  }
1286
+ /**
1287
+ * Returns the configured `MediaResolver`. Throws if `createStore` was called
1288
+ * without a `media` option. Type-narrows to `never` when `THasMedia` is `false`,
1289
+ * so compile-time consumers get an error on access without configuration.
1290
+ */
1291
+ get media() {
1292
+ if (!this._media) {
1293
+ throw new Error("store.media requires the `media` option to be configured in createStore()");
1294
+ }
1295
+ return this._media;
1296
+ }
1283
1297
  async init() {
1284
1298
  await wrap("store:init", () => this._client.init());
1285
1299
  this._watcher.start();
@@ -1,27 +1,7 @@
1
1
  fragment StoreFile on ContelloFile {
2
- uid
3
- mimeType
4
- metadata {
5
- ... on ContelloImageMetadata {
6
- width
7
- height
8
- }
9
- ... on ContelloVideoMetadata {
10
- width
11
- height
12
- }
13
- }
2
+ ...MediaFile
14
3
  }
15
4
 
16
5
  fragment StoreAsset on ContelloAsset {
17
- id
18
- original {
19
- ...StoreFile
20
- }
21
- preview {
22
- ...StoreFile
23
- }
24
- optimized {
25
- ...StoreFile
26
- }
6
+ ...MediaAsset
27
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/store",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Framework-agnostic store for Contello CMS with reactive collections and automatic dependency tracking",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -17,7 +17,8 @@
17
17
  "dependencies": {
18
18
  "backoff-rxjs": "^7.0.0",
19
19
  "lru-cache": "^11.3.5",
20
- "@contello/client": "^1.1.0"
20
+ "@contello/client": "^1.1.0",
21
+ "@contello/media": "^1.0.0"
21
22
  },
22
23
  "devDependencies": {
23
24
  "projected": "^2.2.1",
@@ -32,6 +33,10 @@
32
33
  ".": {
33
34
  "import": "./dist/index.js",
34
35
  "require": "./dist/index.cjs"
36
+ },
37
+ "./fragments": {
38
+ "import": "./dist/fragments.js",
39
+ "types": "./dist/fragments.d.ts"
35
40
  }
36
41
  },
37
42
  "publishConfig": {