@contello/astro 2.1.0 → 2.2.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
@@ -156,7 +156,7 @@ function customHeaders(headers) {
156
156
  }
157
157
  return result;
158
158
  }
159
- function createBoundRoutingMiddleware(contello, routes, exclude) {
159
+ function createBoundRoutingMiddleware(contello, routes, exclude, resolveRoutePath) {
160
160
  return (0, import_middleware2.defineMiddleware)((ctx, next) => {
161
161
  if ((0, import_excludes.shouldExclude)(ctx, exclude ?? import_excludes.RECOMMENDED_EXCLUDES)) {
162
162
  return next();
@@ -169,7 +169,8 @@ function createBoundRoutingMiddleware(contello, routes, exclude) {
169
169
  if (url.pathname.startsWith("/contello/entities/")) {
170
170
  return contello[runRequest]({ url, route: void 0, rewritten: false }, () => next());
171
171
  }
172
- const lookup = routes.getByPath(url.pathname);
172
+ const path = resolveRoutePath ? resolveRoutePath(ctx) : url.pathname;
173
+ const lookup = routes.getByPath(path);
173
174
  return (0, import_store.maybeThen)(lookup, (route) => {
174
175
  if (!route) {
175
176
  return contello[runRequest]({ url, route: void 0, rewritten: false }, () => next());
@@ -280,7 +281,6 @@ var Contello = class {
280
281
  _i18nUnsubscribe;
281
282
  _initialized = false;
282
283
  _als = new import_node_async_hooks.AsyncLocalStorage();
283
- _autoEagerRoutes;
284
284
  _autoLazyRoutes;
285
285
  media;
286
286
  constructor(options) {
@@ -323,8 +323,7 @@ var Contello = class {
323
323
  })();
324
324
  }
325
325
  }
326
- const loadables = [...options?.load ?? [], ...this._autoEagerRoutes ? [this._autoEagerRoutes] : []];
327
- await Promise.all(loadables.map((l) => l.load()));
326
+ await Promise.all((options?.load ?? []).map((l) => l.load()));
328
327
  this._initialized = true;
329
328
  } catch (err) {
330
329
  await this.destroy().catch(() => {
@@ -405,16 +404,8 @@ var Contello = class {
405
404
  }
406
405
  // --- middleware factories (arrow fields so destructuring works) ---
407
406
  createRoutingMiddleware = (options) => {
408
- const routesOption = options?.routes ?? "eager";
409
- let routes;
410
- if (routesOption === "eager") {
411
- routes = this._autoEagerRoutes ??= this.defineRoutesSync();
412
- } else if (routesOption === "lazy") {
413
- routes = this._autoLazyRoutes ??= this.defineLazyRoutes();
414
- } else {
415
- routes = routesOption;
416
- }
417
- return createBoundRoutingMiddleware(this, routes, options?.exclude);
407
+ const routes = options?.routes ?? (this._autoLazyRoutes ??= this.defineLazyRoutes());
408
+ return createBoundRoutingMiddleware(this, routes, options?.exclude, options?.resolveRoutePath);
418
409
  };
419
410
  createAssetsMiddleware = (options) => {
420
411
  return createBoundAssetsMiddleware(this, options);
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as astro from 'astro';
2
+ import { APIContext } from 'astro';
2
3
  import { Schema, ContelloClient, SourceDef } from '@contello/client';
3
4
  import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
4
5
  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';
@@ -18,14 +19,27 @@ type ContelloAssetsMiddlewareOptions = {
18
19
  };
19
20
 
20
21
  type AnyRoutes = Routes | RoutesSync | LazyRoutes;
22
+ /**
23
+ * Derives the path used to look up a Contello route from the request context.
24
+ *
25
+ * @default `(ctx) => ctx.url.pathname`
26
+ *
27
+ * ```ts
28
+ * resolveRoutePath: (ctx) => `/${ctx.url.hostname}${ctx.url.pathname}`
29
+ * ```
30
+ */
31
+ type RoutePathResolver = (ctx: APIContext) => string;
21
32
  type ContelloRoutingMiddlewareOptions = {
22
33
  /**
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
34
+ * Routes instance backing the middleware.
35
+ *
36
+ * Defaults to a `LazyRoutes` instance managed by contello (fetched on demand, no init-time load).
37
+ * For eager routing, create a `RoutesSync` via `contello.defineRoutesSync()`, add it to
38
+ * `contello.init({ load: [routes] })`, and pass that same instance here.
26
39
  */
27
- routes?: 'eager' | 'lazy' | AnyRoutes | undefined;
40
+ routes?: AnyRoutes | undefined;
28
41
  exclude?: ExcludePattern[] | undefined;
42
+ resolveRoutePath?: RoutePathResolver | undefined;
29
43
  };
30
44
 
31
45
  type ContelloRequestContext = {
@@ -73,7 +87,6 @@ declare class Contello<TSchema extends Schema | undefined = undefined> {
73
87
  private _i18nUnsubscribe;
74
88
  private _initialized;
75
89
  private readonly _als;
76
- private _autoEagerRoutes?;
77
90
  private _autoLazyRoutes?;
78
91
  readonly media: ContelloMediaConfig;
79
92
  constructor(options: ContelloOptions<TSchema>);
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import * as astro from 'astro';
2
+ import { APIContext } from 'astro';
2
3
  import { Schema, ContelloClient, SourceDef } from '@contello/client';
3
4
  import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
4
5
  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';
@@ -18,14 +19,27 @@ type ContelloAssetsMiddlewareOptions = {
18
19
  };
19
20
 
20
21
  type AnyRoutes = Routes | RoutesSync | LazyRoutes;
22
+ /**
23
+ * Derives the path used to look up a Contello route from the request context.
24
+ *
25
+ * @default `(ctx) => ctx.url.pathname`
26
+ *
27
+ * ```ts
28
+ * resolveRoutePath: (ctx) => `/${ctx.url.hostname}${ctx.url.pathname}`
29
+ * ```
30
+ */
31
+ type RoutePathResolver = (ctx: APIContext) => string;
21
32
  type ContelloRoutingMiddlewareOptions = {
22
33
  /**
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
34
+ * Routes instance backing the middleware.
35
+ *
36
+ * Defaults to a `LazyRoutes` instance managed by contello (fetched on demand, no init-time load).
37
+ * For eager routing, create a `RoutesSync` via `contello.defineRoutesSync()`, add it to
38
+ * `contello.init({ load: [routes] })`, and pass that same instance here.
26
39
  */
27
- routes?: 'eager' | 'lazy' | AnyRoutes | undefined;
40
+ routes?: AnyRoutes | undefined;
28
41
  exclude?: ExcludePattern[] | undefined;
42
+ resolveRoutePath?: RoutePathResolver | undefined;
29
43
  };
30
44
 
31
45
  type ContelloRequestContext = {
@@ -73,7 +87,6 @@ declare class Contello<TSchema extends Schema | undefined = undefined> {
73
87
  private _i18nUnsubscribe;
74
88
  private _initialized;
75
89
  private readonly _als;
76
- private _autoEagerRoutes?;
77
90
  private _autoLazyRoutes?;
78
91
  readonly media: ContelloMediaConfig;
79
92
  constructor(options: ContelloOptions<TSchema>);
package/dist/index.js CHANGED
@@ -123,7 +123,7 @@ function customHeaders(headers) {
123
123
  }
124
124
  return result;
125
125
  }
126
- function createBoundRoutingMiddleware(contello, routes, exclude) {
126
+ function createBoundRoutingMiddleware(contello, routes, exclude, resolveRoutePath) {
127
127
  return defineMiddleware2((ctx, next) => {
128
128
  if (shouldExclude(ctx, exclude ?? RECOMMENDED_EXCLUDES)) {
129
129
  return next();
@@ -136,7 +136,8 @@ function createBoundRoutingMiddleware(contello, routes, exclude) {
136
136
  if (url.pathname.startsWith("/contello/entities/")) {
137
137
  return contello[runRequest]({ url, route: void 0, rewritten: false }, () => next());
138
138
  }
139
- const lookup = routes.getByPath(url.pathname);
139
+ const path = resolveRoutePath ? resolveRoutePath(ctx) : url.pathname;
140
+ const lookup = routes.getByPath(path);
140
141
  return maybeThen(lookup, (route) => {
141
142
  if (!route) {
142
143
  return contello[runRequest]({ url, route: void 0, rewritten: false }, () => next());
@@ -247,7 +248,6 @@ var Contello = class {
247
248
  _i18nUnsubscribe;
248
249
  _initialized = false;
249
250
  _als = new AsyncLocalStorage();
250
- _autoEagerRoutes;
251
251
  _autoLazyRoutes;
252
252
  media;
253
253
  constructor(options) {
@@ -290,8 +290,7 @@ var Contello = class {
290
290
  })();
291
291
  }
292
292
  }
293
- const loadables = [...options?.load ?? [], ...this._autoEagerRoutes ? [this._autoEagerRoutes] : []];
294
- await Promise.all(loadables.map((l) => l.load()));
293
+ await Promise.all((options?.load ?? []).map((l) => l.load()));
295
294
  this._initialized = true;
296
295
  } catch (err) {
297
296
  await this.destroy().catch(() => {
@@ -372,16 +371,8 @@ var Contello = class {
372
371
  }
373
372
  // --- middleware factories (arrow fields so destructuring works) ---
374
373
  createRoutingMiddleware = (options) => {
375
- const routesOption = options?.routes ?? "eager";
376
- let routes;
377
- if (routesOption === "eager") {
378
- routes = this._autoEagerRoutes ??= this.defineRoutesSync();
379
- } else if (routesOption === "lazy") {
380
- routes = this._autoLazyRoutes ??= this.defineLazyRoutes();
381
- } else {
382
- routes = routesOption;
383
- }
384
- return createBoundRoutingMiddleware(this, routes, options?.exclude);
374
+ const routes = options?.routes ?? (this._autoLazyRoutes ??= this.defineLazyRoutes());
375
+ return createBoundRoutingMiddleware(this, routes, options?.exclude, options?.resolveRoutePath);
385
376
  };
386
377
  createAssetsMiddleware = (options) => {
387
378
  return createBoundAssetsMiddleware(this, options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contello/astro",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "description": "Astro integration for Contello CMS — route middleware and i18n bridge",
5
5
  "type": "module",
6
6
  "license": "MIT",