@contello/astro 1.3.1 → 1.4.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.
package/dist/index.cjs CHANGED
@@ -40,6 +40,7 @@ module.exports = __toCommonJS(index_exports);
40
40
  // src/contello.ts
41
41
  var import_node_async_hooks = require("async_hooks");
42
42
  var import_i18n = require("@astroscope/i18n");
43
+ var import_media = require("@contello/media");
43
44
  var import_store = require("@contello/store");
44
45
  var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
45
46
  var DEFAULT_FILES_PREFIX = "/_contello/f/";
@@ -86,47 +87,54 @@ var Contello = class {
86
87
  _routes;
87
88
  _i18nMessages;
88
89
  _i18nSubscription;
90
+ _initialized = false;
89
91
  _als = new import_node_async_hooks.AsyncLocalStorage();
92
+ media;
90
93
  constructor(options) {
91
94
  this._options = options;
92
- const mediaOptions = {
95
+ this._store = (0, import_store.createStore)(options);
96
+ this.media = {
93
97
  baseUrl: options.media?.baseUrl ?? "",
94
98
  imagesPath: options.media?.imagesPath ?? DEFAULT_IMAGES_PREFIX,
95
99
  videosPath: options.media?.videosPath ?? DEFAULT_VIDEO_PREFIX,
96
- filesPath: options.media?.filesPath ?? DEFAULT_FILES_PREFIX,
97
- ...options.media?.fallback !== void 0 ? { fallback: options.media.fallback } : {},
98
- ...options.media?.breakpoints !== void 0 ? { breakpoints: options.media.breakpoints } : {},
99
- ...options.media?.pictureFormats !== void 0 ? { pictureFormats: options.media.pictureFormats } : {}
100
+ filesPath: options.media?.filesPath ?? DEFAULT_FILES_PREFIX
100
101
  };
101
- this._store = (0, import_store.createStore)({ ...options, media: mediaOptions });
102
102
  }
103
103
  // --- lifecycle ---
104
104
  async init(options) {
105
- await this._store.init();
106
- this._assets = this._store.defineAssets(this._options.assets);
107
- this._routes = this._store.defineRoutes(this._options.routes);
108
- if (this._options.i18n) {
109
- const { collection, languages } = this._options.i18n;
110
- const { register = true, load = true } = options?.i18n ?? {};
111
- this._i18nMessages = this._store.defineI18nMessages({ collection });
112
- await import_i18n.i18n.configure({ locales: languages });
113
- if (register) {
114
- const registrations = buildI18nRegistrations();
115
- if (registrations.length > 0) {
116
- await this._i18nMessages.register(registrations);
105
+ try {
106
+ await this._store.init();
107
+ this._assets = this._store.defineAssets(this._options.assets);
108
+ this._routes = this._store.defineRoutes(this._options.routes);
109
+ if (this._options.i18n) {
110
+ const { collection, languages } = this._options.i18n;
111
+ const { register = true, load = true } = options?.i18n ?? {};
112
+ this._i18nMessages = this._store.defineI18nMessages({ collection });
113
+ await import_i18n.i18n.configure({ locales: languages });
114
+ if (register) {
115
+ const registrations = buildI18nRegistrations();
116
+ if (registrations.length > 0) {
117
+ await this._i18nMessages.register(registrations);
118
+ }
119
+ }
120
+ if (load) {
121
+ await applyTranslations(this._i18nMessages);
122
+ const messages = this._i18nMessages;
123
+ this._i18nSubscription = messages.refresh$.subscribe(() => applyTranslations(messages));
117
124
  }
118
125
  }
119
- if (load) {
120
- await applyTranslations(this._i18nMessages);
121
- const messages = this._i18nMessages;
122
- this._i18nSubscription = messages.refresh$.subscribe(() => applyTranslations(messages));
126
+ if (options?.load) {
127
+ await Promise.all(options.load.map((l) => l.load()));
123
128
  }
124
- }
125
- if (options?.load) {
126
- await Promise.all(options.load.map((l) => l.load()));
129
+ this._initialized = true;
130
+ } catch (err) {
131
+ await this.destroy().catch(() => {
132
+ });
133
+ throw err;
127
134
  }
128
135
  }
129
136
  async destroy() {
137
+ this._initialized = false;
130
138
  this._i18nSubscription?.unsubscribe();
131
139
  this._i18nSubscription = void 0;
132
140
  await this._store.destroy();
@@ -138,30 +146,27 @@ var Contello = class {
138
146
  return this._store.ping();
139
147
  }
140
148
  get isReady() {
141
- return !!this._routes;
149
+ return this._initialized;
142
150
  }
143
151
  // --- pre-wired collections ---
144
152
  get assets() {
145
- if (!this._assets) {
146
- throw new Error("@contello/astro: .assets accessed before init()");
153
+ if (!this._initialized || !this._assets) {
154
+ throw new Error("@contello/astro: .assets accessed before init() completed");
147
155
  }
148
156
  return this._assets;
149
157
  }
150
158
  get routes() {
151
- if (!this._routes) {
152
- throw new Error("@contello/astro: .routes accessed before init()");
159
+ if (!this._initialized || !this._routes) {
160
+ throw new Error("@contello/astro: .routes accessed before init() completed");
153
161
  }
154
162
  return this._routes;
155
163
  }
156
164
  get i18nMessages() {
157
- if (!this._i18nMessages) {
158
- throw new Error("@contello/astro: .i18nMessages accessed before init() with i18n config");
165
+ if (!this._initialized || !this._i18nMessages) {
166
+ throw new Error("@contello/astro: .i18nMessages accessed before init() completed (or i18n is not configured)");
159
167
  }
160
168
  return this._i18nMessages;
161
169
  }
162
- get media() {
163
- return this._store.media;
164
- }
165
170
  // --- ALS request context ---
166
171
  get request() {
167
172
  const ctx = this._als.getStore();
@@ -170,6 +175,13 @@ var Contello = class {
170
175
  }
171
176
  return ctx;
172
177
  }
178
+ // --- media resolvers (on-demand) ---
179
+ defineMediaResolver(options) {
180
+ return (0, import_media.createMediaResolver)({
181
+ ...this.media,
182
+ ...options
183
+ });
184
+ }
173
185
  // --- store delegation ---
174
186
  defineSingleton(def) {
175
187
  return this._store.defineSingleton(def);
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { OperationMap } from '@contello/client';
2
- import { MediaResolverOptions, MediaResolver } from '@contello/media';
2
+ import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
3
3
  import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
4
4
  import * as astro from 'astro';
5
5
  import { ExcludePattern } from '@astroscope/excludes';
@@ -20,13 +20,21 @@ type ContelloInitOptions = {
20
20
  load?: boolean | undefined;
21
21
  } | undefined;
22
22
  };
23
- type ContelloMediaOptions = Partial<MediaResolverOptions>;
24
- type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = Omit<CreateStoreOptions<TOps, TModels>, 'media'> & {
23
+ type ContelloMediaConfig = {
24
+ readonly baseUrl: string;
25
+ readonly imagesPath: string;
26
+ readonly videosPath: string;
27
+ readonly filesPath: string;
28
+ };
29
+ type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = CreateStoreOptions<TOps, TModels> & {
25
30
  assets?: AssetCollectionOptions | undefined;
26
31
  i18n?: ContelloI18nOptions | undefined;
27
32
  routes?: RouteCollectionOptions | undefined;
28
- media?: ContelloMediaOptions | undefined;
33
+ media?: Partial<ContelloMediaConfig> | undefined;
29
34
  };
35
+ type HasFallback<O> = O extends {
36
+ fallback: ImageDef;
37
+ } ? true : false;
30
38
  declare const runRequest: unique symbol;
31
39
  declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
32
40
  private readonly _store;
@@ -35,7 +43,9 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
35
43
  private _routes;
36
44
  private _i18nMessages;
37
45
  private _i18nSubscription;
46
+ private _initialized;
38
47
  private readonly _als;
48
+ readonly media: ContelloMediaConfig;
39
49
  constructor(options: ContelloOptions<TOps, TModels>);
40
50
  init(options?: ContelloInitOptions | undefined): Promise<void>;
41
51
  destroy(): Promise<void>;
@@ -44,8 +54,8 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
44
54
  get assets(): Assets;
45
55
  get routes(): Routes;
46
56
  get i18nMessages(): I18nMessages;
47
- get media(): MediaResolver;
48
57
  get request(): ContelloRequestContext;
58
+ defineMediaResolver<O extends Partial<MediaResolverOptions>>(options?: O): MediaResolver<HasFallback<O>>;
49
59
  defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
50
60
  defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
51
61
  defineCollection<TModel extends TModels, TRaw, TMapped extends {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { OperationMap } from '@contello/client';
2
- import { MediaResolverOptions, MediaResolver } from '@contello/media';
2
+ import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
3
3
  import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
4
4
  import * as astro from 'astro';
5
5
  import { ExcludePattern } from '@astroscope/excludes';
@@ -20,13 +20,21 @@ type ContelloInitOptions = {
20
20
  load?: boolean | undefined;
21
21
  } | undefined;
22
22
  };
23
- type ContelloMediaOptions = Partial<MediaResolverOptions>;
24
- type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = Omit<CreateStoreOptions<TOps, TModels>, 'media'> & {
23
+ type ContelloMediaConfig = {
24
+ readonly baseUrl: string;
25
+ readonly imagesPath: string;
26
+ readonly videosPath: string;
27
+ readonly filesPath: string;
28
+ };
29
+ type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = CreateStoreOptions<TOps, TModels> & {
25
30
  assets?: AssetCollectionOptions | undefined;
26
31
  i18n?: ContelloI18nOptions | undefined;
27
32
  routes?: RouteCollectionOptions | undefined;
28
- media?: ContelloMediaOptions | undefined;
33
+ media?: Partial<ContelloMediaConfig> | undefined;
29
34
  };
35
+ type HasFallback<O> = O extends {
36
+ fallback: ImageDef;
37
+ } ? true : false;
30
38
  declare const runRequest: unique symbol;
31
39
  declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
32
40
  private readonly _store;
@@ -35,7 +43,9 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
35
43
  private _routes;
36
44
  private _i18nMessages;
37
45
  private _i18nSubscription;
46
+ private _initialized;
38
47
  private readonly _als;
48
+ readonly media: ContelloMediaConfig;
39
49
  constructor(options: ContelloOptions<TOps, TModels>);
40
50
  init(options?: ContelloInitOptions | undefined): Promise<void>;
41
51
  destroy(): Promise<void>;
@@ -44,8 +54,8 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
44
54
  get assets(): Assets;
45
55
  get routes(): Routes;
46
56
  get i18nMessages(): I18nMessages;
47
- get media(): MediaResolver;
48
57
  get request(): ContelloRequestContext;
58
+ defineMediaResolver<O extends Partial<MediaResolverOptions>>(options?: O): MediaResolver<HasFallback<O>>;
49
59
  defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
50
60
  defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
51
61
  defineCollection<TModel extends TModels, TRaw, TMapped extends {
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  // src/contello.ts
2
2
  import { AsyncLocalStorage } from "async_hooks";
3
3
  import { i18n } from "@astroscope/i18n";
4
+ import { createMediaResolver } from "@contello/media";
4
5
  import {
5
6
  createStore
6
7
  } from "@contello/store";
@@ -49,47 +50,54 @@ var Contello = class {
49
50
  _routes;
50
51
  _i18nMessages;
51
52
  _i18nSubscription;
53
+ _initialized = false;
52
54
  _als = new AsyncLocalStorage();
55
+ media;
53
56
  constructor(options) {
54
57
  this._options = options;
55
- const mediaOptions = {
58
+ this._store = createStore(options);
59
+ this.media = {
56
60
  baseUrl: options.media?.baseUrl ?? "",
57
61
  imagesPath: options.media?.imagesPath ?? DEFAULT_IMAGES_PREFIX,
58
62
  videosPath: options.media?.videosPath ?? DEFAULT_VIDEO_PREFIX,
59
- filesPath: options.media?.filesPath ?? DEFAULT_FILES_PREFIX,
60
- ...options.media?.fallback !== void 0 ? { fallback: options.media.fallback } : {},
61
- ...options.media?.breakpoints !== void 0 ? { breakpoints: options.media.breakpoints } : {},
62
- ...options.media?.pictureFormats !== void 0 ? { pictureFormats: options.media.pictureFormats } : {}
63
+ filesPath: options.media?.filesPath ?? DEFAULT_FILES_PREFIX
63
64
  };
64
- this._store = createStore({ ...options, media: mediaOptions });
65
65
  }
66
66
  // --- lifecycle ---
67
67
  async init(options) {
68
- await this._store.init();
69
- this._assets = this._store.defineAssets(this._options.assets);
70
- this._routes = this._store.defineRoutes(this._options.routes);
71
- if (this._options.i18n) {
72
- const { collection, languages } = this._options.i18n;
73
- const { register = true, load = true } = options?.i18n ?? {};
74
- this._i18nMessages = this._store.defineI18nMessages({ collection });
75
- await i18n.configure({ locales: languages });
76
- if (register) {
77
- const registrations = buildI18nRegistrations();
78
- if (registrations.length > 0) {
79
- await this._i18nMessages.register(registrations);
68
+ try {
69
+ await this._store.init();
70
+ this._assets = this._store.defineAssets(this._options.assets);
71
+ this._routes = this._store.defineRoutes(this._options.routes);
72
+ if (this._options.i18n) {
73
+ const { collection, languages } = this._options.i18n;
74
+ const { register = true, load = true } = options?.i18n ?? {};
75
+ this._i18nMessages = this._store.defineI18nMessages({ collection });
76
+ await i18n.configure({ locales: languages });
77
+ if (register) {
78
+ const registrations = buildI18nRegistrations();
79
+ if (registrations.length > 0) {
80
+ await this._i18nMessages.register(registrations);
81
+ }
82
+ }
83
+ if (load) {
84
+ await applyTranslations(this._i18nMessages);
85
+ const messages = this._i18nMessages;
86
+ this._i18nSubscription = messages.refresh$.subscribe(() => applyTranslations(messages));
80
87
  }
81
88
  }
82
- if (load) {
83
- await applyTranslations(this._i18nMessages);
84
- const messages = this._i18nMessages;
85
- this._i18nSubscription = messages.refresh$.subscribe(() => applyTranslations(messages));
89
+ if (options?.load) {
90
+ await Promise.all(options.load.map((l) => l.load()));
86
91
  }
87
- }
88
- if (options?.load) {
89
- await Promise.all(options.load.map((l) => l.load()));
92
+ this._initialized = true;
93
+ } catch (err) {
94
+ await this.destroy().catch(() => {
95
+ });
96
+ throw err;
90
97
  }
91
98
  }
92
99
  async destroy() {
100
+ this._initialized = false;
93
101
  this._i18nSubscription?.unsubscribe();
94
102
  this._i18nSubscription = void 0;
95
103
  await this._store.destroy();
@@ -101,30 +109,27 @@ var Contello = class {
101
109
  return this._store.ping();
102
110
  }
103
111
  get isReady() {
104
- return !!this._routes;
112
+ return this._initialized;
105
113
  }
106
114
  // --- pre-wired collections ---
107
115
  get assets() {
108
- if (!this._assets) {
109
- throw new Error("@contello/astro: .assets accessed before init()");
116
+ if (!this._initialized || !this._assets) {
117
+ throw new Error("@contello/astro: .assets accessed before init() completed");
110
118
  }
111
119
  return this._assets;
112
120
  }
113
121
  get routes() {
114
- if (!this._routes) {
115
- throw new Error("@contello/astro: .routes accessed before init()");
122
+ if (!this._initialized || !this._routes) {
123
+ throw new Error("@contello/astro: .routes accessed before init() completed");
116
124
  }
117
125
  return this._routes;
118
126
  }
119
127
  get i18nMessages() {
120
- if (!this._i18nMessages) {
121
- throw new Error("@contello/astro: .i18nMessages accessed before init() with i18n config");
128
+ if (!this._initialized || !this._i18nMessages) {
129
+ throw new Error("@contello/astro: .i18nMessages accessed before init() completed (or i18n is not configured)");
122
130
  }
123
131
  return this._i18nMessages;
124
132
  }
125
- get media() {
126
- return this._store.media;
127
- }
128
133
  // --- ALS request context ---
129
134
  get request() {
130
135
  const ctx = this._als.getStore();
@@ -133,6 +138,13 @@ var Contello = class {
133
138
  }
134
139
  return ctx;
135
140
  }
141
+ // --- media resolvers (on-demand) ---
142
+ defineMediaResolver(options) {
143
+ return createMediaResolver({
144
+ ...this.media,
145
+ ...options
146
+ });
147
+ }
136
148
  // --- store delegation ---
137
149
  defineSingleton(def) {
138
150
  return this._store.defineSingleton(def);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/astro",
3
- "version": "1.3.1",
3
+ "version": "1.4.1",
4
4
  "description": "Astro integration for Contello CMS — route middleware and i18n bridge",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -14,9 +14,9 @@
14
14
  "@astroscope/excludes": ">=0.1.0",
15
15
  "@astroscope/i18n": ">=0.3.0",
16
16
  "astro": "^6.0.0",
17
- "@contello/client": "^1.1.0",
17
+ "@contello/client": "^1.1.1",
18
18
  "@contello/media": "^1.0.0",
19
- "@contello/store": "^1.4.2"
19
+ "@contello/store": "^1.5.1"
20
20
  },
21
21
  "peerDependenciesMeta": {
22
22
  "@astroscope/i18n": {
@@ -27,9 +27,9 @@
27
27
  "@astroscope/excludes": "^0.3.2",
28
28
  "@astroscope/i18n": "^0.4.5",
29
29
  "astro": "^6.1.8",
30
- "@contello/client": "^1.1.0",
31
30
  "@contello/media": "^1.0.0",
32
- "@contello/store": "^1.4.2"
31
+ "@contello/store": "^1.5.1",
32
+ "@contello/client": "^1.1.1"
33
33
  },
34
34
  "files": [
35
35
  "dist"