@isardsat/editorial-server 6.6.2 → 6.6.3
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/routes/data.js +45 -7
- package/package.json +3 -3
package/dist/routes/data.js
CHANGED
|
@@ -1,7 +1,42 @@
|
|
|
1
1
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
2
2
|
import { EditorialDataItemSchema, EditorialDataSchema, EditorialSchemaSchema, } from "@isardsat/editorial-common";
|
|
3
|
+
function createCache() {
|
|
4
|
+
let schemaCache = null;
|
|
5
|
+
const contentCache = new Map();
|
|
6
|
+
const ttl = 5 * 60 * 1000; // 5 minutes TTL
|
|
7
|
+
function isExpired(entry) {
|
|
8
|
+
return Date.now() - entry.timestamp > ttl;
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
async getSchema(storage) {
|
|
12
|
+
if (schemaCache && !isExpired(schemaCache)) {
|
|
13
|
+
return schemaCache.data;
|
|
14
|
+
}
|
|
15
|
+
const schema = await storage.getSchema();
|
|
16
|
+
schemaCache = { data: schema, timestamp: Date.now() };
|
|
17
|
+
return schema;
|
|
18
|
+
},
|
|
19
|
+
async getContent(storage, options = {}) {
|
|
20
|
+
const cacheKey = options.production ? "production" : "preview";
|
|
21
|
+
const cachedEntry = contentCache.get(cacheKey);
|
|
22
|
+
if (cachedEntry && !isExpired(cachedEntry)) {
|
|
23
|
+
return cachedEntry.data;
|
|
24
|
+
}
|
|
25
|
+
const content = await storage.getContent(options);
|
|
26
|
+
contentCache.set(cacheKey, { data: content, timestamp: Date.now() });
|
|
27
|
+
return content;
|
|
28
|
+
},
|
|
29
|
+
invalidateSchema() {
|
|
30
|
+
schemaCache = null;
|
|
31
|
+
},
|
|
32
|
+
invalidateContent() {
|
|
33
|
+
contentCache.clear();
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
3
37
|
export function createDataRoutes(config, storage) {
|
|
4
38
|
const app = new OpenAPIHono();
|
|
39
|
+
const cache = createCache();
|
|
5
40
|
const publicFilesUrl = config.filesUrl;
|
|
6
41
|
app.openapi(createRoute({
|
|
7
42
|
method: "get",
|
|
@@ -17,7 +52,7 @@ export function createDataRoutes(config, storage) {
|
|
|
17
52
|
},
|
|
18
53
|
},
|
|
19
54
|
}), async (c) => {
|
|
20
|
-
const schema = await
|
|
55
|
+
const schema = await cache.getSchema(storage);
|
|
21
56
|
return c.json(schema);
|
|
22
57
|
});
|
|
23
58
|
app.openapi(createRoute({
|
|
@@ -47,7 +82,7 @@ export function createDataRoutes(config, storage) {
|
|
|
47
82
|
},
|
|
48
83
|
}), async (c) => {
|
|
49
84
|
const { preview } = c.req.valid("query");
|
|
50
|
-
const content = await
|
|
85
|
+
const content = await cache.getContent(storage, { production: !preview });
|
|
51
86
|
return c.json(content);
|
|
52
87
|
});
|
|
53
88
|
app.openapi(createRoute({
|
|
@@ -88,8 +123,8 @@ export function createDataRoutes(config, storage) {
|
|
|
88
123
|
const { itemType } = c.req.valid("param");
|
|
89
124
|
const { lang, preview } = c.req.valid("query");
|
|
90
125
|
const origin = preview ? new URL(c.req.url).origin : publicFilesUrl;
|
|
91
|
-
const content = await
|
|
92
|
-
const schema = await
|
|
126
|
+
const content = await cache.getContent(storage, { production: !preview });
|
|
127
|
+
const schema = await cache.getSchema(storage);
|
|
93
128
|
const collection = content[itemType];
|
|
94
129
|
if (!collection) {
|
|
95
130
|
return c.notFound();
|
|
@@ -142,7 +177,7 @@ export function createDataRoutes(config, storage) {
|
|
|
142
177
|
}), async (c) => {
|
|
143
178
|
const { itemType } = c.req.valid("param");
|
|
144
179
|
const { preview } = c.req.valid("query");
|
|
145
|
-
const content = await
|
|
180
|
+
const content = await cache.getContent(storage, { production: !preview });
|
|
146
181
|
return c.json(Object.keys(content[itemType]));
|
|
147
182
|
});
|
|
148
183
|
app.openapi(createRoute({
|
|
@@ -187,8 +222,8 @@ export function createDataRoutes(config, storage) {
|
|
|
187
222
|
const { itemType, id } = c.req.valid("param");
|
|
188
223
|
const { lang, preview } = c.req.valid("query");
|
|
189
224
|
const origin = preview ? new URL(c.req.url).origin : publicFilesUrl;
|
|
190
|
-
const content = await
|
|
191
|
-
const schema = await
|
|
225
|
+
const content = await cache.getContent(storage, { production: !preview });
|
|
226
|
+
const schema = await cache.getSchema(storage);
|
|
192
227
|
const collection = content[itemType];
|
|
193
228
|
if (!collection) {
|
|
194
229
|
return c.notFound();
|
|
@@ -252,6 +287,7 @@ export function createDataRoutes(config, storage) {
|
|
|
252
287
|
}), async (c) => {
|
|
253
288
|
const itemAtts = await c.req.json();
|
|
254
289
|
const newItem = await storage.createItem(itemAtts);
|
|
290
|
+
cache.invalidateContent();
|
|
255
291
|
return c.json(newItem);
|
|
256
292
|
});
|
|
257
293
|
app.openapi(createRoute({
|
|
@@ -290,6 +326,7 @@ export function createDataRoutes(config, storage) {
|
|
|
290
326
|
}), async (c) => {
|
|
291
327
|
const itemAtts = await c.req.json();
|
|
292
328
|
const newItem = await storage.updateItem(itemAtts);
|
|
329
|
+
cache.invalidateContent();
|
|
293
330
|
return c.json(newItem);
|
|
294
331
|
});
|
|
295
332
|
app.openapi(createRoute({
|
|
@@ -320,6 +357,7 @@ export function createDataRoutes(config, storage) {
|
|
|
320
357
|
}), async (c) => {
|
|
321
358
|
const { itemType, id } = c.req.valid("param");
|
|
322
359
|
await storage.deleteItem({ type: itemType, id });
|
|
360
|
+
cache.invalidateContent();
|
|
323
361
|
return c.json(true, 200);
|
|
324
362
|
});
|
|
325
363
|
return app;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isardsat/editorial-server",
|
|
3
|
-
"version": "6.6.
|
|
3
|
+
"version": "6.6.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"hono": "^4.6.20",
|
|
15
15
|
"yaml": "^2.7.0",
|
|
16
16
|
"zod": "^3.24.1",
|
|
17
|
-
"@isardsat/editorial-
|
|
18
|
-
"@isardsat/editorial-
|
|
17
|
+
"@isardsat/editorial-common": "^6.6.3",
|
|
18
|
+
"@isardsat/editorial-admin": "^6.6.3"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@tsconfig/node22": "^22.0.0",
|