@contello/astro 1.2.0 → 1.3.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.cjs +25 -11
- package/dist/index.d.cts +24 -8
- package/dist/index.d.ts +24 -8
- package/dist/index.js +25 -11
- package/package.json +4 -3
package/dist/index.cjs
CHANGED
|
@@ -41,6 +41,9 @@ module.exports = __toCommonJS(index_exports);
|
|
|
41
41
|
var import_node_async_hooks = require("async_hooks");
|
|
42
42
|
var import_i18n = require("@astroscope/i18n");
|
|
43
43
|
var import_store = require("@contello/store");
|
|
44
|
+
var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
|
|
45
|
+
var DEFAULT_FILES_PREFIX = "/_contello/f/";
|
|
46
|
+
var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
|
|
44
47
|
var runRequest = /* @__PURE__ */ Symbol("@contello/astro/runRequest");
|
|
45
48
|
function buildI18nRegistrations() {
|
|
46
49
|
const manifest = import_i18n.i18n.getManifest();
|
|
@@ -86,7 +89,16 @@ var Contello = class {
|
|
|
86
89
|
_als = new import_node_async_hooks.AsyncLocalStorage();
|
|
87
90
|
constructor(options) {
|
|
88
91
|
this._options = options;
|
|
89
|
-
|
|
92
|
+
const mediaOptions = {
|
|
93
|
+
baseUrl: options.media?.baseUrl ?? "",
|
|
94
|
+
imagesPath: options.media?.imagesPath ?? DEFAULT_IMAGES_PREFIX,
|
|
95
|
+
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
|
+
};
|
|
101
|
+
this._store = (0, import_store.createStore)({ ...options, media: mediaOptions });
|
|
90
102
|
}
|
|
91
103
|
// --- lifecycle ---
|
|
92
104
|
async init(options) {
|
|
@@ -147,6 +159,9 @@ var Contello = class {
|
|
|
147
159
|
}
|
|
148
160
|
return this._i18nMessages;
|
|
149
161
|
}
|
|
162
|
+
get media() {
|
|
163
|
+
return this._store.media;
|
|
164
|
+
}
|
|
150
165
|
// --- ALS request context ---
|
|
151
166
|
get request() {
|
|
152
167
|
const ctx = this._als.getStore();
|
|
@@ -226,30 +241,29 @@ function wrap(name, fn) {
|
|
|
226
241
|
}
|
|
227
242
|
|
|
228
243
|
// src/assets-middleware.ts
|
|
229
|
-
var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
|
|
230
|
-
var DEFAULT_FILES_PREFIX = "/_contello/f/";
|
|
231
|
-
var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
|
|
232
244
|
var DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
|
|
233
245
|
function createAssetsMiddleware(instance, options) {
|
|
234
|
-
const imagesPrefix =
|
|
246
|
+
const imagesPrefix = instance.media.imagesPath;
|
|
235
247
|
const imagesCacheControl = options?.images?.cacheControl ?? DEFAULT_IMAGES_CACHE_CONTROL;
|
|
236
|
-
const filesPrefix =
|
|
248
|
+
const filesPrefix = instance.media.filesPath;
|
|
237
249
|
const filesCacheControl = options?.files?.cacheControl;
|
|
238
|
-
const videoPrefix =
|
|
250
|
+
const videoPrefix = instance.media.videosPath;
|
|
239
251
|
return (0, import_middleware.defineMiddleware)((ctx, next) => {
|
|
240
252
|
const { pathname } = ctx.url;
|
|
241
|
-
const
|
|
242
|
-
|
|
253
|
+
const isImage = pathname.startsWith(imagesPrefix);
|
|
254
|
+
const isFile = !isImage && pathname.startsWith(filesPrefix);
|
|
255
|
+
const isVideo = !isImage && !isFile && pathname.startsWith(videoPrefix);
|
|
256
|
+
if (!isImage && !isFile && !isVideo) {
|
|
243
257
|
return next();
|
|
244
258
|
}
|
|
245
259
|
if (!instance.isReady) {
|
|
246
260
|
console.warn(`[@contello/astro] not initialized, passing through: ${pathname}`);
|
|
247
261
|
return next();
|
|
248
262
|
}
|
|
249
|
-
if (
|
|
263
|
+
if (isImage) {
|
|
250
264
|
return handleFile(instance, next, pathname.slice(imagesPrefix.length), imagesCacheControl);
|
|
251
265
|
}
|
|
252
|
-
if (
|
|
266
|
+
if (isFile) {
|
|
253
267
|
return handleFile(instance, next, pathname.slice(filesPrefix.length), filesCacheControl);
|
|
254
268
|
}
|
|
255
269
|
return handleVideo(instance, next, pathname.slice(videoPrefix.length), ctx.request.signal);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OperationMap } from '@contello/client';
|
|
2
|
+
import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
|
|
2
3
|
import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
|
|
3
4
|
import * as astro from 'astro';
|
|
4
5
|
import { ExcludePattern } from '@astroscope/excludes';
|
|
@@ -19,13 +20,31 @@ type ContelloInitOptions = {
|
|
|
19
20
|
load?: boolean | undefined;
|
|
20
21
|
} | undefined;
|
|
21
22
|
};
|
|
22
|
-
|
|
23
|
+
/**
|
|
24
|
+
* full `MediaResolverOptions` surface — everything is optional because astro
|
|
25
|
+
* auto-defaults `baseUrl` to `''` (same-origin) and `imagesPath` / `videosPath`
|
|
26
|
+
* to the assets middleware prefixes (`/_contello/i/`, `/_contello/v/`). all
|
|
27
|
+
* other fields (`fallback`, `breakpoints`, `pictureFormats`) pass through as-is.
|
|
28
|
+
*/
|
|
29
|
+
type ContelloMediaOptions = Partial<MediaResolverOptions>;
|
|
30
|
+
type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = Omit<CreateStoreOptions<TOps, TModels>, 'media'> & {
|
|
23
31
|
assets?: AssetCollectionOptions | undefined;
|
|
24
32
|
i18n?: ContelloI18nOptions | undefined;
|
|
25
33
|
routes?: RouteCollectionOptions | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* MediaResolver configuration. baseUrl / imagesPath / videosPath are
|
|
36
|
+
* auto-defaulted to match the assets middleware proxy prefixes, so
|
|
37
|
+
* `contello.media` is always available in astro projects.
|
|
38
|
+
*/
|
|
39
|
+
media?: ContelloMediaOptions | undefined;
|
|
26
40
|
};
|
|
41
|
+
type HasMediaFallback<O> = O extends {
|
|
42
|
+
media: {
|
|
43
|
+
fallback: ImageDef;
|
|
44
|
+
};
|
|
45
|
+
} ? true : false;
|
|
27
46
|
declare const runRequest: unique symbol;
|
|
28
|
-
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
|
|
47
|
+
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, THasMediaFallback extends boolean = false> {
|
|
29
48
|
private readonly _store;
|
|
30
49
|
private readonly _options;
|
|
31
50
|
private _assets;
|
|
@@ -41,6 +60,7 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
41
60
|
get assets(): Assets;
|
|
42
61
|
get routes(): Routes;
|
|
43
62
|
get i18nMessages(): I18nMessages;
|
|
63
|
+
get media(): MediaResolver<THasMediaFallback>;
|
|
44
64
|
get request(): ContelloRequestContext;
|
|
45
65
|
defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
|
|
46
66
|
defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
|
|
@@ -55,19 +75,15 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
55
75
|
}>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
|
|
56
76
|
[runRequest]<T>(ctx: ContelloRequestContext, fn: () => T): T;
|
|
57
77
|
}
|
|
58
|
-
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>
|
|
78
|
+
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, O extends ContelloOptions<TOps, TModels> = ContelloOptions<TOps, TModels>>(options: O): Contello<TOps, TModels, HasMediaFallback<O>>;
|
|
59
79
|
|
|
60
80
|
type ContelloAssetsImagesOptions = {
|
|
61
|
-
prefix?: string | undefined;
|
|
62
81
|
cacheControl?: string | undefined;
|
|
63
82
|
};
|
|
64
83
|
type ContelloAssetsFilesOptions = {
|
|
65
|
-
prefix?: string | undefined;
|
|
66
84
|
cacheControl?: string | undefined;
|
|
67
85
|
};
|
|
68
|
-
type ContelloAssetsVideoOptions = {
|
|
69
|
-
prefix?: string | undefined;
|
|
70
|
-
};
|
|
86
|
+
type ContelloAssetsVideoOptions = {};
|
|
71
87
|
type ContelloAssetsMiddlewareOptions = {
|
|
72
88
|
images?: ContelloAssetsImagesOptions | undefined;
|
|
73
89
|
files?: ContelloAssetsFilesOptions | undefined;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OperationMap } from '@contello/client';
|
|
2
|
+
import { MediaResolverOptions, MediaResolver, ImageDef } from '@contello/media';
|
|
2
3
|
import { CreateStoreOptions, AssetCollectionOptions, RouteCollectionOptions, Loadable, Assets, Routes, I18nMessages, StoreRoute, SingletonDef, Singleton, SingletonSyncDef, SingletonSync, CollectionDef, Collection, CollectionSyncDef, CollectionSync, LazyCollectionDef, LazyCollection } from '@contello/store';
|
|
3
4
|
import * as astro from 'astro';
|
|
4
5
|
import { ExcludePattern } from '@astroscope/excludes';
|
|
@@ -19,13 +20,31 @@ type ContelloInitOptions = {
|
|
|
19
20
|
load?: boolean | undefined;
|
|
20
21
|
} | undefined;
|
|
21
22
|
};
|
|
22
|
-
|
|
23
|
+
/**
|
|
24
|
+
* full `MediaResolverOptions` surface — everything is optional because astro
|
|
25
|
+
* auto-defaults `baseUrl` to `''` (same-origin) and `imagesPath` / `videosPath`
|
|
26
|
+
* to the assets middleware prefixes (`/_contello/i/`, `/_contello/v/`). all
|
|
27
|
+
* other fields (`fallback`, `breakpoints`, `pictureFormats`) pass through as-is.
|
|
28
|
+
*/
|
|
29
|
+
type ContelloMediaOptions = Partial<MediaResolverOptions>;
|
|
30
|
+
type ContelloOptions<TOps extends OperationMap | undefined = undefined, TModels extends string = string> = Omit<CreateStoreOptions<TOps, TModels>, 'media'> & {
|
|
23
31
|
assets?: AssetCollectionOptions | undefined;
|
|
24
32
|
i18n?: ContelloI18nOptions | undefined;
|
|
25
33
|
routes?: RouteCollectionOptions | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* MediaResolver configuration. baseUrl / imagesPath / videosPath are
|
|
36
|
+
* auto-defaulted to match the assets middleware proxy prefixes, so
|
|
37
|
+
* `contello.media` is always available in astro projects.
|
|
38
|
+
*/
|
|
39
|
+
media?: ContelloMediaOptions | undefined;
|
|
26
40
|
};
|
|
41
|
+
type HasMediaFallback<O> = O extends {
|
|
42
|
+
media: {
|
|
43
|
+
fallback: ImageDef;
|
|
44
|
+
};
|
|
45
|
+
} ? true : false;
|
|
27
46
|
declare const runRequest: unique symbol;
|
|
28
|
-
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string> {
|
|
47
|
+
declare class Contello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, THasMediaFallback extends boolean = false> {
|
|
29
48
|
private readonly _store;
|
|
30
49
|
private readonly _options;
|
|
31
50
|
private _assets;
|
|
@@ -41,6 +60,7 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
41
60
|
get assets(): Assets;
|
|
42
61
|
get routes(): Routes;
|
|
43
62
|
get i18nMessages(): I18nMessages;
|
|
63
|
+
get media(): MediaResolver<THasMediaFallback>;
|
|
44
64
|
get request(): ContelloRequestContext;
|
|
45
65
|
defineSingleton<TModel extends TModels, TRaw, TMapped>(def: SingletonDef<TOps, TModel, TRaw, TMapped, TModels>): Singleton<TMapped>;
|
|
46
66
|
defineSingletonSync<TModel extends TModels, TRaw, TMapped>(def: SingletonSyncDef<TOps, TModel, TRaw, TMapped, TModels>): SingletonSync<TMapped>;
|
|
@@ -55,19 +75,15 @@ declare class Contello<TOps extends OperationMap | undefined = undefined, TModel
|
|
|
55
75
|
}>(def: LazyCollectionDef<TOps, TModel, TRaw, TMapped, TModels>): LazyCollection<TMapped>;
|
|
56
76
|
[runRequest]<T>(ctx: ContelloRequestContext, fn: () => T): T;
|
|
57
77
|
}
|
|
58
|
-
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string>
|
|
78
|
+
declare function createContello<TOps extends OperationMap | undefined = undefined, TModels extends string = string, O extends ContelloOptions<TOps, TModels> = ContelloOptions<TOps, TModels>>(options: O): Contello<TOps, TModels, HasMediaFallback<O>>;
|
|
59
79
|
|
|
60
80
|
type ContelloAssetsImagesOptions = {
|
|
61
|
-
prefix?: string | undefined;
|
|
62
81
|
cacheControl?: string | undefined;
|
|
63
82
|
};
|
|
64
83
|
type ContelloAssetsFilesOptions = {
|
|
65
|
-
prefix?: string | undefined;
|
|
66
84
|
cacheControl?: string | undefined;
|
|
67
85
|
};
|
|
68
|
-
type ContelloAssetsVideoOptions = {
|
|
69
|
-
prefix?: string | undefined;
|
|
70
|
-
};
|
|
86
|
+
type ContelloAssetsVideoOptions = {};
|
|
71
87
|
type ContelloAssetsMiddlewareOptions = {
|
|
72
88
|
images?: ContelloAssetsImagesOptions | undefined;
|
|
73
89
|
files?: ContelloAssetsFilesOptions | undefined;
|
package/dist/index.js
CHANGED
|
@@ -4,6 +4,9 @@ import { i18n } from "@astroscope/i18n";
|
|
|
4
4
|
import {
|
|
5
5
|
createStore
|
|
6
6
|
} from "@contello/store";
|
|
7
|
+
var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
|
|
8
|
+
var DEFAULT_FILES_PREFIX = "/_contello/f/";
|
|
9
|
+
var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
|
|
7
10
|
var runRequest = /* @__PURE__ */ Symbol("@contello/astro/runRequest");
|
|
8
11
|
function buildI18nRegistrations() {
|
|
9
12
|
const manifest = i18n.getManifest();
|
|
@@ -49,7 +52,16 @@ var Contello = class {
|
|
|
49
52
|
_als = new AsyncLocalStorage();
|
|
50
53
|
constructor(options) {
|
|
51
54
|
this._options = options;
|
|
52
|
-
|
|
55
|
+
const mediaOptions = {
|
|
56
|
+
baseUrl: options.media?.baseUrl ?? "",
|
|
57
|
+
imagesPath: options.media?.imagesPath ?? DEFAULT_IMAGES_PREFIX,
|
|
58
|
+
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
|
+
};
|
|
64
|
+
this._store = createStore({ ...options, media: mediaOptions });
|
|
53
65
|
}
|
|
54
66
|
// --- lifecycle ---
|
|
55
67
|
async init(options) {
|
|
@@ -110,6 +122,9 @@ var Contello = class {
|
|
|
110
122
|
}
|
|
111
123
|
return this._i18nMessages;
|
|
112
124
|
}
|
|
125
|
+
get media() {
|
|
126
|
+
return this._store.media;
|
|
127
|
+
}
|
|
113
128
|
// --- ALS request context ---
|
|
114
129
|
get request() {
|
|
115
130
|
const ctx = this._als.getStore();
|
|
@@ -189,30 +204,29 @@ function wrap(name, fn) {
|
|
|
189
204
|
}
|
|
190
205
|
|
|
191
206
|
// src/assets-middleware.ts
|
|
192
|
-
var DEFAULT_IMAGES_PREFIX = "/_contello/i/";
|
|
193
|
-
var DEFAULT_FILES_PREFIX = "/_contello/f/";
|
|
194
|
-
var DEFAULT_VIDEO_PREFIX = "/_contello/v/";
|
|
195
207
|
var DEFAULT_IMAGES_CACHE_CONTROL = "public, max-age=31536000";
|
|
196
208
|
function createAssetsMiddleware(instance, options) {
|
|
197
|
-
const imagesPrefix =
|
|
209
|
+
const imagesPrefix = instance.media.imagesPath;
|
|
198
210
|
const imagesCacheControl = options?.images?.cacheControl ?? DEFAULT_IMAGES_CACHE_CONTROL;
|
|
199
|
-
const filesPrefix =
|
|
211
|
+
const filesPrefix = instance.media.filesPath;
|
|
200
212
|
const filesCacheControl = options?.files?.cacheControl;
|
|
201
|
-
const videoPrefix =
|
|
213
|
+
const videoPrefix = instance.media.videosPath;
|
|
202
214
|
return defineMiddleware((ctx, next) => {
|
|
203
215
|
const { pathname } = ctx.url;
|
|
204
|
-
const
|
|
205
|
-
|
|
216
|
+
const isImage = pathname.startsWith(imagesPrefix);
|
|
217
|
+
const isFile = !isImage && pathname.startsWith(filesPrefix);
|
|
218
|
+
const isVideo = !isImage && !isFile && pathname.startsWith(videoPrefix);
|
|
219
|
+
if (!isImage && !isFile && !isVideo) {
|
|
206
220
|
return next();
|
|
207
221
|
}
|
|
208
222
|
if (!instance.isReady) {
|
|
209
223
|
console.warn(`[@contello/astro] not initialized, passing through: ${pathname}`);
|
|
210
224
|
return next();
|
|
211
225
|
}
|
|
212
|
-
if (
|
|
226
|
+
if (isImage) {
|
|
213
227
|
return handleFile(instance, next, pathname.slice(imagesPrefix.length), imagesCacheControl);
|
|
214
228
|
}
|
|
215
|
-
if (
|
|
229
|
+
if (isFile) {
|
|
216
230
|
return handleFile(instance, next, pathname.slice(filesPrefix.length), filesCacheControl);
|
|
217
231
|
}
|
|
218
232
|
return handleVideo(instance, next, pathname.slice(videoPrefix.length), ctx.request.signal);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contello/astro",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Astro integration for Contello CMS — route middleware and i18n bridge",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@contello/
|
|
25
|
-
"@contello/
|
|
24
|
+
"@contello/client": "^1.1.0",
|
|
25
|
+
"@contello/store": "^1.4.0",
|
|
26
|
+
"@contello/media": "^1.0.0"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@astroscope/excludes": "^0.3.2",
|