@contello/store 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.
@@ -0,0 +1,355 @@
1
+ import { Observable } from 'rxjs';
2
+ import { ContelloSdkClient } from '@contello/sdk-client';
3
+ import { MaybePromise } from 'projected';
4
+
5
+ type StoreRouteCustomHeader = {
6
+ name: string;
7
+ value: string;
8
+ };
9
+ type StoreRoute = {
10
+ id: string;
11
+ path: string;
12
+ customHeaders: StoreRouteCustomHeader[];
13
+ } & ({
14
+ type: 'redirect';
15
+ location: string;
16
+ status: number;
17
+ } | {
18
+ type: 'text';
19
+ content: string;
20
+ mimeType: string;
21
+ } | {
22
+ type: 'asset';
23
+ assetId: string;
24
+ fileId: string;
25
+ contentDisposition: 'inline' | 'attachment';
26
+ mimeType: string;
27
+ } | {
28
+ type: 'entity';
29
+ entityType: string;
30
+ entityId: string;
31
+ });
32
+
33
+ type UpdateEvent<TEntityType extends string = string> = {
34
+ id: string;
35
+ mutation: 'create';
36
+ target: 'route';
37
+ after: StoreRoute;
38
+ } | {
39
+ id: string;
40
+ mutation: 'update';
41
+ target: 'route';
42
+ after: StoreRoute;
43
+ before: StoreRoute;
44
+ } | {
45
+ id: string;
46
+ mutation: 'delete';
47
+ target: 'route';
48
+ } | {
49
+ id: string;
50
+ mutation: 'create' | 'update' | 'delete';
51
+ target: 'asset';
52
+ } | {
53
+ id: string;
54
+ mutation: 'create' | 'update' | 'delete';
55
+ target: 'i18nMessage';
56
+ token: string;
57
+ } | {
58
+ id: string;
59
+ mutation: 'create' | 'update' | 'delete';
60
+ target: 'entity';
61
+ model: TEntityType;
62
+ };
63
+ type UpdateMutationType = UpdateEvent['mutation'];
64
+ type UpdateEventFor<T extends UpdateEvent['target']> = Extract<UpdateEvent, {
65
+ target: T;
66
+ }>;
67
+ type UpdateBatch = {
68
+ readonly events: UpdateEvent[];
69
+ readonly route: UpdateEventFor<'route'>[];
70
+ readonly asset: UpdateEventFor<'asset'>[];
71
+ readonly i18nMessage: UpdateEventFor<'i18nMessage'>[];
72
+ /** entity events grouped by model typename */
73
+ readonly entity: Map<string, UpdateEventFor<'entity'>[]>;
74
+ /** route events grouped by target entity model (both after and before targets for updates) */
75
+ readonly routeByEntityModel: Map<string, UpdateEventFor<'route'>[]>;
76
+ };
77
+
78
+ /**
79
+ * passed to `map()` so the mapper can declare which external entities this item depends on.
80
+ * calls are collected during mapping and indexed once the item key is known.
81
+ * designed to be destructurable: `map(raw, { track, trackRoute }) => ...`
82
+ *
83
+ * `track()` inspects `__typename` to auto-detect the dependency type:
84
+ * - `ContelloAsset` → asset dep
85
+ * - `ContelloRoute` → route dep
86
+ * - `*Entity` → entity dep
87
+ * - anything else → throws
88
+ *
89
+ * explicit methods (`trackEntity`, `trackAsset`, `trackRoute`) are available
90
+ * for cases where only an id is known without the full GraphQL object.
91
+ */
92
+ type MapperContext<TEntityTypes extends string = string> = {
93
+ track(obj: {
94
+ __typename: string;
95
+ id: string;
96
+ }): void;
97
+ trackEntity(typename: TEntityTypes, id: string): void;
98
+ trackAsset(id: string): void;
99
+ trackRoute(id: string): void;
100
+ };
101
+
102
+ type Fetchable<T> = MaybePromise<T> | Observable<T>;
103
+ type ClientConfig<TSdk> = {
104
+ getSdk: (requester: any) => TSdk;
105
+ pooling?: {
106
+ size?: number | undefined;
107
+ } | undefined;
108
+ };
109
+ type CreateStoreOptions<TSdk, TEntityTypes extends string = string> = {
110
+ url: string;
111
+ project: string;
112
+ token: string;
113
+ client: ClientConfig<TSdk> | ContelloSdkClient<TSdk>;
114
+ /** Entity types this store cares about. Constrains `model` in `define*` methods and enables runtime filtering/warnings. */
115
+ entityTypes?: readonly TEntityTypes[] | undefined;
116
+ };
117
+ /** Cache options for non-lazy collections and singletons. */
118
+ type CacheOptions = {
119
+ /** If set, the cache is refreshed automatically this many ms after each completed fetch. */
120
+ ttl?: number | undefined;
121
+ /**
122
+ * Controls how the cache responds to Contello update events.
123
+ * `'refresh'` (default) serves stale data while a new fetch runs in the background (SWR).
124
+ * `'clear'` wipes the cache immediately so the next `get()` awaits the fresh result.
125
+ */
126
+ eviction?: 'refresh' | 'clear' | undefined;
127
+ };
128
+ /** Cache options for sync collections and singletons. `eviction` is omitted — clearing the cache would break the sync guarantee. */
129
+ type SyncCacheOptions = {
130
+ /** If set, the cache is refreshed automatically this many ms after each completed fetch. */
131
+ ttl?: number | undefined;
132
+ };
133
+ /** Cache options for lazy collections. */
134
+ type LazyCacheOptions = {
135
+ /** Maximum number of items kept in the LRU cache. Defaults to 1000. */
136
+ max?: number | undefined;
137
+ /** Items are evicted from the LRU cache after this many ms. */
138
+ ttl?: number | undefined;
139
+ };
140
+ type SingletonDef<TSdk, TModel extends string, TRaw, TMapped, TEntityTypes extends string = string> = {
141
+ name?: string | undefined;
142
+ model: TModel;
143
+ fetch: (sdk: TSdk) => MaybePromise<TRaw>;
144
+ map: (item: TRaw, ref: MapperContext<TEntityTypes>) => MaybePromise<TMapped>;
145
+ cache?: CacheOptions | undefined;
146
+ onLoad?: (() => void) | undefined;
147
+ onRefresh?: (() => void) | undefined;
148
+ };
149
+ type SingletonSyncDef<TSdk, TModel extends string, TRaw, TMapped, TEntityTypes extends string = string> = {
150
+ name?: string | undefined;
151
+ model: TModel;
152
+ fetch: (sdk: TSdk) => MaybePromise<TRaw>;
153
+ map: (item: TRaw, ref: MapperContext<TEntityTypes>) => TMapped;
154
+ cache?: SyncCacheOptions | undefined;
155
+ onLoad?: (() => void) | undefined;
156
+ onRefresh?: (() => void) | undefined;
157
+ };
158
+ type Singleton<T> = {
159
+ readonly name: string;
160
+ readonly refresh$: Observable<void>;
161
+ load(): Promise<void>;
162
+ get(): MaybePromise<T>;
163
+ refresh(): void;
164
+ };
165
+ type SingletonSync<T> = {
166
+ readonly name: string;
167
+ readonly refresh$: Observable<void>;
168
+ load(): Promise<void>;
169
+ get(): T;
170
+ refresh(): void;
171
+ };
172
+ type CollectionDef<TSdk, TModel extends string, TRaw, TMapped extends {
173
+ id: string;
174
+ }, TEntityTypes extends string = string> = {
175
+ name?: string | undefined;
176
+ model: TModel;
177
+ fetch: (sdk: TSdk) => Fetchable<TRaw[]>;
178
+ map: (item: TRaw, ref: MapperContext<TEntityTypes>) => MaybePromise<TMapped>;
179
+ cache?: CacheOptions | undefined;
180
+ onLoad?: ((ids: string[]) => void) | undefined;
181
+ onRefresh?: ((ids: string[]) => void) | undefined;
182
+ };
183
+ type CollectionSyncDef<TSdk, TModel extends string, TRaw, TMapped extends {
184
+ id: string;
185
+ }, TEntityTypes extends string = string> = {
186
+ name?: string | undefined;
187
+ model: TModel;
188
+ fetch: (sdk: TSdk) => Fetchable<TRaw[]>;
189
+ map: (item: TRaw, ref: MapperContext<TEntityTypes>) => TMapped;
190
+ cache?: SyncCacheOptions | undefined;
191
+ onLoad?: ((ids: string[]) => void) | undefined;
192
+ onRefresh?: ((ids: string[]) => void) | undefined;
193
+ };
194
+ type Collection<T> = {
195
+ readonly name: string;
196
+ readonly refresh$: Observable<string[]>;
197
+ load(): Promise<void>;
198
+ get(id: string): MaybePromise<T | undefined>;
199
+ get(ids: string[]): MaybePromise<T[]>;
200
+ getAll(): MaybePromise<T[]>;
201
+ refresh(): void;
202
+ };
203
+ type CollectionSync<T> = {
204
+ readonly name: string;
205
+ readonly refresh$: Observable<string[]>;
206
+ load(): Promise<void>;
207
+ get(id: string): T | undefined;
208
+ get(ids: string[]): T[];
209
+ getAll(): T[];
210
+ refresh(): void;
211
+ };
212
+ type LazyCollectionDef<TSdk, TModel extends string, TRaw, TMapped extends {
213
+ id: string;
214
+ }, TEntityTypes extends string = string> = {
215
+ name?: string | undefined;
216
+ model: TModel;
217
+ cache?: LazyCacheOptions | undefined;
218
+ fetch: (ids: string[], sdk: TSdk) => Fetchable<TRaw[]>;
219
+ map: (item: TRaw, ref: MapperContext<TEntityTypes>) => MaybePromise<TMapped>;
220
+ onRefresh?: ((ids: string[]) => void) | undefined;
221
+ };
222
+ type LazyCollection<T> = {
223
+ readonly name: string;
224
+ readonly refresh$: Observable<string[]>;
225
+ get(id: string): MaybePromise<T | undefined>;
226
+ get(ids: string[]): MaybePromise<T[]>;
227
+ };
228
+
229
+ type StoreFileMetadata = {
230
+ width: number;
231
+ height: number;
232
+ };
233
+ type StoreFile = {
234
+ uid: string;
235
+ mimeType: string;
236
+ metadata: StoreFileMetadata | null;
237
+ };
238
+ type StoreAsset = {
239
+ id: string;
240
+ original: StoreFile;
241
+ preview: StoreFile | null;
242
+ optimized: StoreFile[];
243
+ };
244
+ type AssetCollectionOptions = {
245
+ cache?: LazyCacheOptions | undefined;
246
+ };
247
+ type Assets = {
248
+ readonly refresh$: Observable<string[]>;
249
+ get(id: string): Promise<StoreAsset | undefined>;
250
+ get(ids: string[]): Promise<StoreAsset[]>;
251
+ download(fileId: string): Promise<Response>;
252
+ };
253
+
254
+ type I18nTranslation = {
255
+ language: string;
256
+ value: string;
257
+ };
258
+ type I18nVariableInput = {
259
+ name: string;
260
+ description: string;
261
+ example: string;
262
+ };
263
+ type I18nInitialTranslation = {
264
+ language: string;
265
+ message: string;
266
+ };
267
+ type I18nMessageRegistrationDefinition = {
268
+ token: string;
269
+ example?: string | undefined;
270
+ description?: string | undefined;
271
+ variables?: I18nVariableInput[] | undefined;
272
+ /** always overwrites existing translations for this token */
273
+ translations?: I18nInitialTranslation[] | undefined;
274
+ /** only applied when the token is newly registered (existing translations are preserved) */
275
+ initialTranslations?: I18nInitialTranslation[] | undefined;
276
+ };
277
+ type I18nMessageDef = {
278
+ collection: string;
279
+ };
280
+ type I18nMessage = {
281
+ id: string;
282
+ token: string;
283
+ translations: Map<string, string>;
284
+ };
285
+ type I18nMessages = {
286
+ readonly refresh$: Observable<string[]>;
287
+ get(id: string): MaybePromise<I18nMessage | undefined>;
288
+ get(ids: string[]): MaybePromise<I18nMessage[]>;
289
+ getAll(): MaybePromise<I18nMessage[]>;
290
+ register(messages: I18nMessageRegistrationDefinition[]): Promise<void>;
291
+ };
292
+
293
+ type RouteCollectionOptions = {
294
+ cache?: LazyCacheOptions | undefined;
295
+ };
296
+ type Routes = {
297
+ readonly refresh$: Observable<string[]>;
298
+ get(id: string): Promise<StoreRoute | undefined>;
299
+ get(ids: string[]): Promise<StoreRoute[]>;
300
+ getByPath(path: string): Promise<StoreRoute | undefined>;
301
+ getByPath(paths: string[]): Promise<StoreRoute[]>;
302
+ };
303
+
304
+ declare class Store<TSdk, TEntityTypes extends string = string> {
305
+ private _sdkClient;
306
+ private _url;
307
+ private _entityTypes;
308
+ private _watcher;
309
+ readonly updates$: Observable<UpdateBatch>;
310
+ constructor(options: CreateStoreOptions<TSdk, TEntityTypes>);
311
+ private get sdk();
312
+ init(): Promise<void>;
313
+ destroy(): Promise<void>;
314
+ defineSingleton<TModel extends TEntityTypes, TRaw, TMapped>(def: SingletonDef<TSdk, TModel, TRaw, TMapped, TEntityTypes>): Singleton<TMapped>;
315
+ defineSingletonSync<TModel extends TEntityTypes, TRaw, TMapped>(def: SingletonSyncDef<TSdk, TModel, TRaw, TMapped, TEntityTypes>): SingletonSync<TMapped>;
316
+ defineCollection<TModel extends TEntityTypes, TRaw, TMapped extends {
317
+ id: string;
318
+ }>(def: CollectionDef<TSdk, TModel, TRaw, TMapped, TEntityTypes>): Collection<TMapped>;
319
+ defineCollectionSync<TModel extends TEntityTypes, TRaw, TMapped extends {
320
+ id: string;
321
+ }>(def: CollectionSyncDef<TSdk, TModel, TRaw, TMapped, TEntityTypes>): CollectionSync<TMapped>;
322
+ defineLazyCollection<TModel extends TEntityTypes, TRaw, TMapped extends {
323
+ id: string;
324
+ }>(def: LazyCollectionDef<TSdk, TModel, TRaw, TMapped, TEntityTypes>): LazyCollection<TMapped>;
325
+ defineAssets(options?: AssetCollectionOptions | undefined): Assets;
326
+ defineRoutes(options?: RouteCollectionOptions | undefined): Routes;
327
+ defineI18nMessages(def: I18nMessageDef): I18nMessages;
328
+ ping: () => Promise<void>;
329
+ }
330
+ declare function createStore<TSdk, TEntityTypes extends string = string>(options: CreateStoreOptions<TSdk, TEntityTypes>): Store<TSdk, TEntityTypes>;
331
+
332
+ declare const NO_MATCH: unique symbol;
333
+ type FlatRef = {
334
+ _flatId?: string | null;
335
+ };
336
+ type MapFn<TResult> = (refs: readonly FlatRef[] | null | undefined) => TResult[];
337
+ type Visitors<TFlat extends {
338
+ __typename: string;
339
+ } & FlatRef, TResult> = {
340
+ [K in TFlat['__typename']]?: (component: Extract<TFlat, {
341
+ __typename: K;
342
+ }>, map: MapFn<TResult>) => TResult | null | undefined | false | 0 | '';
343
+ } & {
344
+ [NO_MATCH]?: (component: TFlat) => void;
345
+ };
346
+ declare class ComponentMapper<TFlat extends {
347
+ __typename: string;
348
+ } & FlatRef, TResult> {
349
+ private readonly visitors;
350
+ constructor(visitors: Visitors<TFlat, TResult>);
351
+ map(flat: readonly TFlat[] | null | undefined, refs: readonly FlatRef[] | null | undefined): TResult[];
352
+ private resolveRefs;
353
+ }
354
+
355
+ export { type AssetCollectionOptions, type Assets, type CacheOptions, type Collection, type CollectionDef, type CollectionSync, type CollectionSyncDef, ComponentMapper, 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 MapperContext, NO_MATCH, 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 };