@contello/astro 1.4.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,17 +1,42 @@
1
- import { OperationMap } from '@contello/client';
2
- import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
3
- import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
4
1
  import * as astro from 'astro';
2
+ import { Schema, ContelloClient, SourceDef } from '@contello/client';
3
+ import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
4
+ import { Routes, RoutesSync, LazyRoutes, CreateStoreOptions, SyncCacheOptions, Loadable, I18nMessages, ReadonlyDeep, StoreRoute, SourceKeysOf, ExtractSourceResult, ResolveSource, SingletonOptions, Singleton, SingletonSyncOptions, SingletonSync, CollectionOptions, Collection, CollectionSyncOptions, CollectionSync, LazyCollectionOptions, LazyCollection, RoutesOptions, RoutesSyncOptions, LazyRoutesOptions, AssetsOptions, Assets, AssetsSyncOptions, AssetsSync, LazyAssetsOptions, LazyAssets } from '@contello/store';
5
5
  import { ExcludePattern } from '@astroscope/excludes';
6
6
 
7
+ type ContelloAssetsImagesOptions = {
8
+ cacheControl?: string | undefined;
9
+ };
10
+ type ContelloAssetsFilesOptions = {
11
+ cacheControl?: string | undefined;
12
+ };
13
+ type ContelloAssetsVideoOptions = {};
14
+ type ContelloAssetsMiddlewareOptions = {
15
+ images?: ContelloAssetsImagesOptions | undefined;
16
+ files?: ContelloAssetsFilesOptions | undefined;
17
+ video?: ContelloAssetsVideoOptions | undefined;
18
+ };
19
+
20
+ type AnyRoutes = Routes | RoutesSync | LazyRoutes;
21
+ type ContelloRoutingMiddlewareOptions = {
22
+ /**
23
+ * - `'eager'` (default) — use a `RoutesSync` instance managed by contello (auto-loaded by `contello.init()`)
24
+ * - `'lazy'` — use a `LazyRoutes` instance managed by contello
25
+ * - a user-defined routes instance (`Routes` / `RoutesSync` / `LazyRoutes`) — consumer manages its lifecycle
26
+ */
27
+ routes?: 'eager' | 'lazy' | AnyRoutes | undefined;
28
+ exclude?: ExcludePattern[] | undefined;
29
+ };
30
+
7
31
  type ContelloRequestContext = {
8
32
  url: URL;
9
- route: StoreRoute | undefined;
33
+ route: ReadonlyDeep<StoreRoute> | undefined;
10
34
  rewritten: boolean;
11
35
  };
12
36
  type ContelloI18nOptions = {
13
37
  collection: string;
14
38
  languages: string[];
39
+ cache?: SyncCacheOptions | undefined;
15
40
  };
16
41
  type ContelloInitOptions = {
17
42
  load?: Loadable[] | undefined;
@@ -26,68 +51,67 @@ type ContelloMediaConfig = {
26
51
  readonly videosPath: string;
27
52
  readonly filesPath: string;
28
53
  };
29
- type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = CreateStoreOptions<TOps, TModels> & {
30
- assets?: AssetCollectionOptions | undefined;
54
+ type ContelloOptions<TSchema extends Schema | undefined = undefined> = CreateStoreOptions<TSchema> & {
31
55
  i18n?: ContelloI18nOptions | undefined;
32
- routes?: RouteCollectionOptions | undefined;
33
56
  media?: Partial<ContelloMediaConfig> | undefined;
34
57
  };
35
58
  type HasFallback<O> = O extends {
36
59
  fallback: ImageDef;
37
60
  } ? true : false;
38
61
  declare const runRequest: unique symbol;
39
- declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
62
+ type ModelsOf<TSchema> = TSchema extends {
63
+ models: infer M;
64
+ } ? keyof M & string : string;
65
+ type CollectionArg<TSchema> = SourceKeysOf<TSchema, 'entity'> | SourceDef<ModelsOf<TSchema>, 'entity'>;
66
+ type SingletonArg<TSchema> = SourceKeysOf<TSchema, 'singleton'> | SourceDef<ModelsOf<TSchema>, 'singleton'>;
67
+ type CollectionRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'entity'>>>;
68
+ type SingletonRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'singleton'>>>;
69
+ declare class Contello<TSchema extends Schema | undefined = undefined> {
40
70
  private readonly _store;
41
71
  private readonly _options;
42
- private _assets;
43
- private _routes;
44
72
  private _i18nMessages;
45
- private _i18nSubscription;
73
+ private _i18nUnsubscribe;
46
74
  private _initialized;
47
75
  private readonly _als;
76
+ private _autoEagerRoutes?;
77
+ private _autoLazyRoutes?;
48
78
  readonly media: ContelloMediaConfig;
49
- constructor(options: ContelloOptions<TOps, TModels>);
79
+ constructor(options: ContelloOptions<TSchema>);
50
80
  init(options?: ContelloInitOptions | undefined): Promise<void>;
51
81
  destroy(): Promise<void>;
52
82
  ping(): Promise<void>;
53
83
  get isReady(): boolean;
54
- get assets(): Assets;
55
- get routes(): Routes;
84
+ get client(): ContelloClient<TSchema>;
56
85
  get i18nMessages(): I18nMessages;
57
86
  get request(): ContelloRequestContext;
58
87
  defineMediaResolver<O extends Partial<MediaResolverOptions>>(options?: O): MediaResolver<HasFallback<O>>;
59
- defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
60
- defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
61
- defineCollection<TModel extends TModels, TRaw, TMapped extends {
88
+ defineSingleton<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Singleton<TMapped>;
89
+ defineSingletonSync<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonSyncOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): SingletonSync<TMapped>;
90
+ defineCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
91
+ id: string;
92
+ } = CollectionRaw<TSchema, TArg> & {
62
93
  id: string;
63
- }>(def: CollectionDef<TOps, TModel, TRaw, TMapped, TModels>): Collection<TMapped>;
64
- defineCollectionSync<TModel extends TModels, TRaw, TMapped extends {
94
+ }>(sourceOrKey: TArg, options?: CollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Collection<TMapped>;
95
+ defineCollectionSync<TArg extends CollectionArg<TSchema>, TMapped extends {
65
96
  id: string;
66
- }>(def: CollectionSyncDef<TOps, TModel, TRaw, TMapped, TModels>): CollectionSync<TMapped>;
67
- defineLazyCollection<TModel extends TModels, TRaw, TMapped extends {
97
+ } = CollectionRaw<TSchema, TArg> & {
68
98
  id: string;
69
- }>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
99
+ }>(sourceOrKey: TArg, options?: CollectionSyncOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): CollectionSync<TMapped>;
100
+ defineLazyCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
101
+ id: string;
102
+ } = CollectionRaw<TSchema, TArg> & {
103
+ id: string;
104
+ }>(sourceOrKey: TArg, options?: LazyCollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): LazyCollection<TMapped>;
105
+ defineRoutes(options?: RoutesOptions | undefined): Routes;
106
+ defineRoutesSync(options?: RoutesSyncOptions | undefined): RoutesSync;
107
+ defineLazyRoutes(options?: LazyRoutesOptions | undefined): LazyRoutes;
108
+ defineAssets(options?: AssetsOptions | undefined): Assets;
109
+ defineAssetsSync(options?: AssetsSyncOptions | undefined): AssetsSync;
110
+ defineLazyAssets(options?: LazyAssetsOptions | undefined): LazyAssets;
111
+ createRoutingMiddleware: (options?: ContelloRoutingMiddlewareOptions | undefined) => astro.MiddlewareHandler;
112
+ createAssetsMiddleware: (options?: ContelloAssetsMiddlewareOptions | undefined) => astro.MiddlewareHandler;
70
113
  [runRequest]<T>(ctx: ContelloRequestContext, fn: () => T): T;
71
114
  }
72
- declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: ContelloOptions<TOps, TModels>): Contello<TOps, TModels>;
73
-
74
- type ContelloAssetsImagesOptions = {
75
- cacheControl?: string | undefined;
76
- };
77
- type ContelloAssetsFilesOptions = {
78
- cacheControl?: string | undefined;
79
- };
80
- type ContelloAssetsVideoOptions = {};
81
- type ContelloAssetsMiddlewareOptions = {
82
- images?: ContelloAssetsImagesOptions | undefined;
83
- files?: ContelloAssetsFilesOptions | undefined;
84
- video?: ContelloAssetsVideoOptions | undefined;
85
- };
86
- declare function createAssetsMiddleware(instance: Contello<any, any>, options?: ContelloAssetsMiddlewareOptions | undefined): astro.MiddlewareHandler;
87
-
88
- type ContelloMiddlewareOptions = {
89
- exclude?: ExcludePattern[] | undefined;
90
- };
91
- declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
115
+ declare function createContello<TSchema extends Schema | undefined = undefined>(options: ContelloOptions<TSchema>): Contello<TSchema>;
92
116
 
93
- export { Contello, type ContelloAssetsFilesOptions, type ContelloAssetsImagesOptions, type ContelloAssetsMiddlewareOptions, type ContelloAssetsVideoOptions, type ContelloI18nOptions, type ContelloInitOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createAssetsMiddleware, createContello, createRoutingMiddleware };
117
+ export { type AnyRoutes, Contello, type ContelloAssetsFilesOptions, type ContelloAssetsImagesOptions, type ContelloAssetsMiddlewareOptions, type ContelloAssetsVideoOptions, type ContelloI18nOptions, type ContelloInitOptions, type ContelloOptions, type ContelloRequestContext, type ContelloRoutingMiddlewareOptions, createContello };
package/dist/index.d.ts CHANGED
@@ -1,17 +1,42 @@
1
- import { OperationMap } from '@contello/client';
2
- import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
3
- import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
4
1
  import * as astro from 'astro';
2
+ import { Schema, ContelloClient, SourceDef } from '@contello/client';
3
+ import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
4
+ import { Routes, RoutesSync, LazyRoutes, CreateStoreOptions, SyncCacheOptions, Loadable, I18nMessages, ReadonlyDeep, StoreRoute, SourceKeysOf, ExtractSourceResult, ResolveSource, SingletonOptions, Singleton, SingletonSyncOptions, SingletonSync, CollectionOptions, Collection, CollectionSyncOptions, CollectionSync, LazyCollectionOptions, LazyCollection, RoutesOptions, RoutesSyncOptions, LazyRoutesOptions, AssetsOptions, Assets, AssetsSyncOptions, AssetsSync, LazyAssetsOptions, LazyAssets } from '@contello/store';
5
5
  import { ExcludePattern } from '@astroscope/excludes';
6
6
 
7
+ type ContelloAssetsImagesOptions = {
8
+ cacheControl?: string | undefined;
9
+ };
10
+ type ContelloAssetsFilesOptions = {
11
+ cacheControl?: string | undefined;
12
+ };
13
+ type ContelloAssetsVideoOptions = {};
14
+ type ContelloAssetsMiddlewareOptions = {
15
+ images?: ContelloAssetsImagesOptions | undefined;
16
+ files?: ContelloAssetsFilesOptions | undefined;
17
+ video?: ContelloAssetsVideoOptions | undefined;
18
+ };
19
+
20
+ type AnyRoutes = Routes | RoutesSync | LazyRoutes;
21
+ type ContelloRoutingMiddlewareOptions = {
22
+ /**
23
+ * - `'eager'` (default) — use a `RoutesSync` instance managed by contello (auto-loaded by `contello.init()`)
24
+ * - `'lazy'` — use a `LazyRoutes` instance managed by contello
25
+ * - a user-defined routes instance (`Routes` / `RoutesSync` / `LazyRoutes`) — consumer manages its lifecycle
26
+ */
27
+ routes?: 'eager' | 'lazy' | AnyRoutes | undefined;
28
+ exclude?: ExcludePattern[] | undefined;
29
+ };
30
+
7
31
  type ContelloRequestContext = {
8
32
  url: URL;
9
- route: StoreRoute | undefined;
33
+ route: ReadonlyDeep<StoreRoute> | undefined;
10
34
  rewritten: boolean;
11
35
  };
12
36
  type ContelloI18nOptions = {
13
37
  collection: string;
14
38
  languages: string[];
39
+ cache?: SyncCacheOptions | undefined;
15
40
  };
16
41
  type ContelloInitOptions = {
17
42
  load?: Loadable[] | undefined;
@@ -26,68 +51,67 @@ type ContelloMediaConfig = {
26
51
  readonly videosPath: string;
27
52
  readonly filesPath: string;
28
53
  };
29
- type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = CreateStoreOptions<TOps, TModels> & {
30
- assets?: AssetCollectionOptions | undefined;
54
+ type ContelloOptions<TSchema extends Schema | undefined = undefined> = CreateStoreOptions<TSchema> & {
31
55
  i18n?: ContelloI18nOptions | undefined;
32
- routes?: RouteCollectionOptions | undefined;
33
56
  media?: Partial<ContelloMediaConfig> | undefined;
34
57
  };
35
58
  type HasFallback<O> = O extends {
36
59
  fallback: ImageDef;
37
60
  } ? true : false;
38
61
  declare const runRequest: unique symbol;
39
- declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
62
+ type ModelsOf<TSchema> = TSchema extends {
63
+ models: infer M;
64
+ } ? keyof M & string : string;
65
+ type CollectionArg<TSchema> = SourceKeysOf<TSchema, 'entity'> | SourceDef<ModelsOf<TSchema>, 'entity'>;
66
+ type SingletonArg<TSchema> = SourceKeysOf<TSchema, 'singleton'> | SourceDef<ModelsOf<TSchema>, 'singleton'>;
67
+ type CollectionRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'entity'>>>;
68
+ type SingletonRaw<TSchema, TArg> = ExtractSourceResult<Extract<ResolveSource<TSchema, TArg>, SourceDef<string, 'singleton'>>>;
69
+ declare class Contello<TSchema extends Schema | undefined = undefined> {
40
70
  private readonly _store;
41
71
  private readonly _options;
42
- private _assets;
43
- private _routes;
44
72
  private _i18nMessages;
45
- private _i18nSubscription;
73
+ private _i18nUnsubscribe;
46
74
  private _initialized;
47
75
  private readonly _als;
76
+ private _autoEagerRoutes?;
77
+ private _autoLazyRoutes?;
48
78
  readonly media: ContelloMediaConfig;
49
- constructor(options: ContelloOptions<TOps, TModels>);
79
+ constructor(options: ContelloOptions<TSchema>);
50
80
  init(options?: ContelloInitOptions | undefined): Promise<void>;
51
81
  destroy(): Promise<void>;
52
82
  ping(): Promise<void>;
53
83
  get isReady(): boolean;
54
- get assets(): Assets;
55
- get routes(): Routes;
84
+ get client(): ContelloClient<TSchema>;
56
85
  get i18nMessages(): I18nMessages;
57
86
  get request(): ContelloRequestContext;
58
87
  defineMediaResolver<O extends Partial<MediaResolverOptions>>(options?: O): MediaResolver<HasFallback<O>>;
59
- defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
60
- defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
61
- defineCollection<TModel extends TModels, TRaw, TMapped extends {
88
+ defineSingleton<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Singleton<TMapped>;
89
+ defineSingletonSync<TArg extends SingletonArg<TSchema>, TMapped = SingletonRaw<TSchema, TArg>>(sourceOrKey: TArg, options?: SingletonSyncOptions<SingletonRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): SingletonSync<TMapped>;
90
+ defineCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
91
+ id: string;
92
+ } = CollectionRaw<TSchema, TArg> & {
62
93
  id: string;
63
- }>(def: CollectionDef<TOps, TModel, TRaw, TMapped, TModels>): Collection<TMapped>;
64
- defineCollectionSync<TModel extends TModels, TRaw, TMapped extends {
94
+ }>(sourceOrKey: TArg, options?: CollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): Collection<TMapped>;
95
+ defineCollectionSync<TArg extends CollectionArg<TSchema>, TMapped extends {
65
96
  id: string;
66
- }>(def: CollectionSyncDef<TOps, TModel, TRaw, TMapped, TModels>): CollectionSync<TMapped>;
67
- defineLazyCollection<TModel extends TModels, TRaw, TMapped extends {
97
+ } = CollectionRaw<TSchema, TArg> & {
68
98
  id: string;
69
- }>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
99
+ }>(sourceOrKey: TArg, options?: CollectionSyncOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): CollectionSync<TMapped>;
100
+ defineLazyCollection<TArg extends CollectionArg<TSchema>, TMapped extends {
101
+ id: string;
102
+ } = CollectionRaw<TSchema, TArg> & {
103
+ id: string;
104
+ }>(sourceOrKey: TArg, options?: LazyCollectionOptions<CollectionRaw<TSchema, TArg>, TMapped, ModelsOf<TSchema>>): LazyCollection<TMapped>;
105
+ defineRoutes(options?: RoutesOptions | undefined): Routes;
106
+ defineRoutesSync(options?: RoutesSyncOptions | undefined): RoutesSync;
107
+ defineLazyRoutes(options?: LazyRoutesOptions | undefined): LazyRoutes;
108
+ defineAssets(options?: AssetsOptions | undefined): Assets;
109
+ defineAssetsSync(options?: AssetsSyncOptions | undefined): AssetsSync;
110
+ defineLazyAssets(options?: LazyAssetsOptions | undefined): LazyAssets;
111
+ createRoutingMiddleware: (options?: ContelloRoutingMiddlewareOptions | undefined) => astro.MiddlewareHandler;
112
+ createAssetsMiddleware: (options?: ContelloAssetsMiddlewareOptions | undefined) => astro.MiddlewareHandler;
70
113
  [runRequest]<T>(ctx: ContelloRequestContext, fn: () => T): T;
71
114
  }
72
- declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>(options: ContelloOptions<TOps, TModels>): Contello<TOps, TModels>;
73
-
74
- type ContelloAssetsImagesOptions = {
75
- cacheControl?: string | undefined;
76
- };
77
- type ContelloAssetsFilesOptions = {
78
- cacheControl?: string | undefined;
79
- };
80
- type ContelloAssetsVideoOptions = {};
81
- type ContelloAssetsMiddlewareOptions = {
82
- images?: ContelloAssetsImagesOptions | undefined;
83
- files?: ContelloAssetsFilesOptions | undefined;
84
- video?: ContelloAssetsVideoOptions | undefined;
85
- };
86
- declare function createAssetsMiddleware(instance: Contello<any, any>, options?: ContelloAssetsMiddlewareOptions | undefined): astro.MiddlewareHandler;
87
-
88
- type ContelloMiddlewareOptions = {
89
- exclude?: ExcludePattern[] | undefined;
90
- };
91
- declare function createRoutingMiddleware(instance: Contello<any, any>, options?: ContelloMiddlewareOptions | undefined): astro.MiddlewareHandler;
115
+ declare function createContello<TSchema extends Schema | undefined = undefined>(options: ContelloOptions<TSchema>): Contello<TSchema>;
92
116
 
93
- export { Contello, type ContelloAssetsFilesOptions, type ContelloAssetsImagesOptions, type ContelloAssetsMiddlewareOptions, type ContelloAssetsVideoOptions, type ContelloI18nOptions, type ContelloInitOptions, type ContelloMiddlewareOptions, type ContelloOptions, type ContelloRequestContext, createAssetsMiddleware, createContello, createRoutingMiddleware };
117
+ export { type AnyRoutes, Contello, type ContelloAssetsFilesOptions, type ContelloAssetsImagesOptions, type ContelloAssetsMiddlewareOptions, type ContelloAssetsVideoOptions, type ContelloI18nOptions, type ContelloInitOptions, type ContelloOptions, type ContelloRequestContext, type ContelloRoutingMiddlewareOptions, createContello };