@isardsat/editorial-server 6.0.0-canary-001
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/LICENSE +21 -0
- package/README.md +8 -0
- package/dist/app.d.ts +14 -0
- package/dist/app.js +39 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/config.d.ts +6 -0
- package/dist/lib/config.js +8 -0
- package/dist/lib/hooks.d.ts +8 -0
- package/dist/lib/hooks.js +32 -0
- package/dist/lib/schema.d.ts +6 -0
- package/dist/lib/schema.js +9 -0
- package/dist/lib/storage.d.ts +33 -0
- package/dist/lib/storage.js +67 -0
- package/dist/lib/utils/fs.d.ts +6 -0
- package/dist/lib/utils/fs.js +20 -0
- package/dist/routes/actions.d.ts +4 -0
- package/dist/routes/actions.js +58 -0
- package/dist/routes/admin.d.ts +2 -0
- package/dist/routes/admin.js +22 -0
- package/dist/routes/data.d.ts +3 -0
- package/dist/routes/data.js +242 -0
- package/dist/routes/files.d.ts +2 -0
- package/dist/routes/files.js +125 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lobelia Earth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
+
import type { EditorialConfig } from "@isardsat/editorial-common";
|
|
3
|
+
import { type Storage } from "./lib/storage.js";
|
|
4
|
+
export declare const BASE_EDITORIAL_PATH = "./editorial";
|
|
5
|
+
export interface EditorialServerConfig {
|
|
6
|
+
configDirectory?: string;
|
|
7
|
+
editorialDirectory?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface EditorialServer {
|
|
10
|
+
app: OpenAPIHono;
|
|
11
|
+
config: EditorialConfig;
|
|
12
|
+
storage: Storage;
|
|
13
|
+
}
|
|
14
|
+
export declare function createEditorialServer({ configDirectory, editorialDirectory, }: EditorialServerConfig): Promise<EditorialServer>;
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { serveStatic } from "@hono/node-server/serve-static";
|
|
2
|
+
import { swaggerUI } from "@hono/swagger-ui";
|
|
3
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
4
|
+
import { logger } from "hono/logger";
|
|
5
|
+
import { createConfig } from "./lib/config.js";
|
|
6
|
+
import { createHooks } from "./lib/hooks.js";
|
|
7
|
+
import { createStorage } from "./lib/storage.js";
|
|
8
|
+
import { createActionRoutes } from "./routes/actions.js";
|
|
9
|
+
import { createAdminRoutes } from "./routes/admin.js";
|
|
10
|
+
import { createDataRoutes } from "./routes/data.js";
|
|
11
|
+
import { createFilesRoutes } from "./routes/files.js";
|
|
12
|
+
export const BASE_EDITORIAL_PATH = "./editorial";
|
|
13
|
+
export async function createEditorialServer({ configDirectory = BASE_EDITORIAL_PATH, editorialDirectory = BASE_EDITORIAL_PATH, }) {
|
|
14
|
+
const app = new OpenAPIHono();
|
|
15
|
+
const config = await createConfig(configDirectory);
|
|
16
|
+
const storage = createStorage(editorialDirectory);
|
|
17
|
+
const hooks = await createHooks(configDirectory);
|
|
18
|
+
app.use(logger());
|
|
19
|
+
app.route("/api/v1", createDataRoutes(storage));
|
|
20
|
+
app.route("/api/v1", createFilesRoutes());
|
|
21
|
+
app.route("/api/v1", createActionRoutes(storage, hooks));
|
|
22
|
+
app.route("/", createAdminRoutes());
|
|
23
|
+
app.doc("/doc", {
|
|
24
|
+
openapi: "3.0.0",
|
|
25
|
+
info: {
|
|
26
|
+
version: "1.0.0",
|
|
27
|
+
title: `Editorial API: ${config.name}`,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
app.get("/doc/ui", swaggerUI({ url: "/doc" }));
|
|
31
|
+
app.use("/public/*", serveStatic({
|
|
32
|
+
root: "./",
|
|
33
|
+
}));
|
|
34
|
+
return {
|
|
35
|
+
app,
|
|
36
|
+
config,
|
|
37
|
+
storage,
|
|
38
|
+
};
|
|
39
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEditorialServer } from './app.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createEditorialServer } from './app.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { EditorialConfigSchema } from '@isardsat/editorial-common';
|
|
4
|
+
export async function createConfig(configDirectory) {
|
|
5
|
+
const configFile = await readFile(join(configDirectory, 'config.json'), 'utf-8').then((value) => JSON.parse(value));
|
|
6
|
+
const config = EditorialConfigSchema.parse(configFile);
|
|
7
|
+
return config;
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function createHooks(configDirectory: string): Promise<{
|
|
2
|
+
onPublish: (content: any, schema: any) => Promise<any>;
|
|
3
|
+
onLocalize: (content: any, schema: any) => Promise<any>;
|
|
4
|
+
onLocalizeEnd: (content: any, schema: any) => Promise<any>;
|
|
5
|
+
onPull: () => Promise<any>;
|
|
6
|
+
onPush: () => Promise<any>;
|
|
7
|
+
}>;
|
|
8
|
+
export type Hooks = Awaited<ReturnType<typeof createHooks>>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
export async function createHooks(configDirectory) {
|
|
3
|
+
const hooksDirPath = join(configDirectory, 'hooks');
|
|
4
|
+
async function loadHook(name) {
|
|
5
|
+
const hookScript = join(process.cwd(), hooksDirPath, name);
|
|
6
|
+
const module = await import(`${hookScript}.mjs`);
|
|
7
|
+
const hookFunction = typeof module.default === 'function' ? module.default : null;
|
|
8
|
+
return hookFunction;
|
|
9
|
+
}
|
|
10
|
+
async function executeHook(hookName, ...args) {
|
|
11
|
+
const hook = await loadHook(hookName);
|
|
12
|
+
if (!hook)
|
|
13
|
+
return;
|
|
14
|
+
return hook(...args);
|
|
15
|
+
}
|
|
16
|
+
async function onPublish(content, schema) {
|
|
17
|
+
return executeHook('onPublish', content, schema);
|
|
18
|
+
}
|
|
19
|
+
async function onLocalize(content, schema) {
|
|
20
|
+
return executeHook('onLocalize', content, schema);
|
|
21
|
+
}
|
|
22
|
+
async function onLocalizeEnd(content, schema) {
|
|
23
|
+
return executeHook('onLocalizeEnd', content, schema);
|
|
24
|
+
}
|
|
25
|
+
async function onPull() {
|
|
26
|
+
return executeHook('onPull');
|
|
27
|
+
}
|
|
28
|
+
async function onPush() {
|
|
29
|
+
return executeHook('onPush');
|
|
30
|
+
}
|
|
31
|
+
return { onPublish, onLocalize, onLocalizeEnd, onPull, onPush };
|
|
32
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { EditorialConfigSchema } from '@isardsat/editorial-common';
|
|
2
|
+
import { readFile } from 'fs/promises';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { parse } from 'yaml';
|
|
5
|
+
export async function createSchema(configDirectory) {
|
|
6
|
+
const configFile = await readFile(join(configDirectory, 'schema.yaml'), 'utf-8').then((value) => parse(value));
|
|
7
|
+
const config = EditorialConfigSchema.parse(configFile);
|
|
8
|
+
return config;
|
|
9
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { EditorialData, EditorialDataObjectWithType } from '@isardsat/editorial-common';
|
|
2
|
+
export declare function createStorage(dataDirectory: string): {
|
|
3
|
+
getSchema: () => Promise<Record<string, {
|
|
4
|
+
displayName: string;
|
|
5
|
+
fields: Record<string, import("zod").objectOutputType<{
|
|
6
|
+
type: import("zod").ZodEnum<["string", "boolean", "date", "datetime", "markdown", "number", "color", "select", "color"]>;
|
|
7
|
+
displayName: import("zod").ZodString;
|
|
8
|
+
displayExtra: import("zod").ZodOptional<import("zod").ZodString>;
|
|
9
|
+
placeholder: import("zod").ZodOptional<import("zod").ZodString>;
|
|
10
|
+
isRequired: import("zod").ZodOptional<import("zod").ZodBoolean>;
|
|
11
|
+
showInSummary: import("zod").ZodOptional<import("zod").ZodBoolean>;
|
|
12
|
+
}, import("zod").ZodTypeAny, "passthrough">>;
|
|
13
|
+
singleton?: boolean | undefined;
|
|
14
|
+
}>>;
|
|
15
|
+
getContent: () => Promise<EditorialData>;
|
|
16
|
+
saveLocalisationMessages: (messages: any) => Promise<boolean>;
|
|
17
|
+
createItem: (item: EditorialDataObjectWithType) => Promise<import("zod").objectOutputType<{
|
|
18
|
+
id: import("zod").ZodString;
|
|
19
|
+
type: import("zod").ZodString;
|
|
20
|
+
}, import("zod").ZodTypeAny, "passthrough">>;
|
|
21
|
+
updateItem: (item: EditorialDataObjectWithType) => Promise<import("zod").objectOutputType<{
|
|
22
|
+
id: import("zod").ZodString;
|
|
23
|
+
createdAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
24
|
+
updatedAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
25
|
+
}, import("zod").ZodTypeAny, "passthrough">>;
|
|
26
|
+
deleteItem: (item: EditorialDataObjectWithType) => Promise<Record<string, Record<string, import("zod").objectOutputType<{
|
|
27
|
+
id: import("zod").ZodString;
|
|
28
|
+
createdAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
29
|
+
updatedAt: import("zod").ZodDefault<import("zod").ZodString>;
|
|
30
|
+
}, import("zod").ZodTypeAny, "passthrough">>>>;
|
|
31
|
+
saveProdContent: () => Promise<boolean>;
|
|
32
|
+
};
|
|
33
|
+
export type Storage = ReturnType<typeof createStorage>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { EditorialDataItemSchema, EditorialDataSchema, EditorialSchemaSchema, } from '@isardsat/editorial-common';
|
|
2
|
+
import { readFile } from 'fs/promises';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { parse } from 'yaml';
|
|
5
|
+
import { writeFileSafe } from './utils/fs.js';
|
|
6
|
+
export function createStorage(dataDirectory) {
|
|
7
|
+
const schemaPath = join(dataDirectory, 'schema.yaml');
|
|
8
|
+
const dataPath = join(dataDirectory, 'data.json');
|
|
9
|
+
const dataProdPath = join(dataDirectory, 'data.prod.json');
|
|
10
|
+
const dataExtractedPath = join(dataDirectory, 'data.messages.json');
|
|
11
|
+
async function getSchema() {
|
|
12
|
+
const schemaFile = await readFile(schemaPath, 'utf-8').then((value) => parse(value));
|
|
13
|
+
const schema = EditorialSchemaSchema.parse(schemaFile);
|
|
14
|
+
return schema;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* TODO: This should ideally cache the result of reading the file until an update occurs.
|
|
18
|
+
*/
|
|
19
|
+
async function getContent() {
|
|
20
|
+
return await readFile(dataPath, 'utf-8').then((value) => JSON.parse(value));
|
|
21
|
+
}
|
|
22
|
+
async function saveProdContent() {
|
|
23
|
+
const content = await getContent();
|
|
24
|
+
await writeFileSafe(dataProdPath, JSON.stringify(EditorialDataSchema.parse(content), null, 2));
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
async function saveLocalisationMessages(messages) {
|
|
28
|
+
await writeFileSafe(dataExtractedPath, JSON.stringify(messages, null, 2));
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
async function createItem(item) {
|
|
32
|
+
const content = await getContent();
|
|
33
|
+
content[item.type][item.id] = EditorialDataItemSchema.parse(item);
|
|
34
|
+
// TODO: Use superjson to safely encode different types.
|
|
35
|
+
await writeFileSafe(dataPath, JSON.stringify(content, null, 2));
|
|
36
|
+
return item;
|
|
37
|
+
}
|
|
38
|
+
async function updateItem(item) {
|
|
39
|
+
const content = await getContent();
|
|
40
|
+
const oldItem = content[item.type][item.id];
|
|
41
|
+
const newItem = EditorialDataItemSchema.parse({
|
|
42
|
+
...oldItem,
|
|
43
|
+
...item,
|
|
44
|
+
updatedAt: new Date().toISOString(),
|
|
45
|
+
});
|
|
46
|
+
content[item.type][item.id] = newItem;
|
|
47
|
+
// TODO: Use superjson to safely encode different types.
|
|
48
|
+
await writeFileSafe(dataPath, JSON.stringify(content, null, 2));
|
|
49
|
+
return newItem;
|
|
50
|
+
}
|
|
51
|
+
async function deleteItem(item) {
|
|
52
|
+
const content = await getContent();
|
|
53
|
+
delete content[item.type][item.id];
|
|
54
|
+
// TODO: Use superjson to safely encode different types.
|
|
55
|
+
await writeFileSafe(dataPath, JSON.stringify(content, null, 2));
|
|
56
|
+
return content;
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
getSchema,
|
|
60
|
+
getContent,
|
|
61
|
+
saveLocalisationMessages,
|
|
62
|
+
createItem,
|
|
63
|
+
updateItem,
|
|
64
|
+
deleteItem,
|
|
65
|
+
saveProdContent,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This function writes a file in as atomic a way as possible. It first creates
|
|
3
|
+
* a backup of the file to be written to, writes new content to a temporary
|
|
4
|
+
* file, and finally renames the temporary file to the final name.
|
|
5
|
+
*/
|
|
6
|
+
export declare function writeFileSafe(file: string, contents: string): Promise<void>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { copyFile, rename, writeFile } from 'fs/promises';
|
|
2
|
+
import { tmpdir } from 'os';
|
|
3
|
+
import { basename, dirname, join } from 'path';
|
|
4
|
+
/**
|
|
5
|
+
* This function writes a file in as atomic a way as possible. It first creates
|
|
6
|
+
* a backup of the file to be written to, writes new content to a temporary
|
|
7
|
+
* file, and finally renames the temporary file to the final name.
|
|
8
|
+
*/
|
|
9
|
+
export async function writeFileSafe(file, contents) {
|
|
10
|
+
const fileName = basename(file);
|
|
11
|
+
const destination = join(dirname(file), `${fileName}.${Date.now()}.tmp`);
|
|
12
|
+
try {
|
|
13
|
+
await copyFile(file, join(tmpdir(), fileName));
|
|
14
|
+
await writeFile(destination, contents);
|
|
15
|
+
await rename(destination, file);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
|
|
2
|
+
export function createActionRoutes(storage, hooks) {
|
|
3
|
+
const app = new OpenAPIHono();
|
|
4
|
+
app.openapi(createRoute({
|
|
5
|
+
method: 'post',
|
|
6
|
+
path: '/publish',
|
|
7
|
+
responses: {
|
|
8
|
+
202: {
|
|
9
|
+
content: {
|
|
10
|
+
'application/json': {
|
|
11
|
+
schema: z.boolean(),
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
description: 'Trigger the publishing process',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
}),
|
|
18
|
+
// TODO: Don't async, let the promises run in the background.
|
|
19
|
+
async (c) => {
|
|
20
|
+
await storage.saveProdContent();
|
|
21
|
+
const content = await storage.getContent();
|
|
22
|
+
const schema = await storage.getSchema();
|
|
23
|
+
try {
|
|
24
|
+
const scriptResult = await hooks.onLocalize(content, schema);
|
|
25
|
+
await storage.saveLocalisationMessages(scriptResult);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error('Error executing script:', error);
|
|
29
|
+
return c.json(false);
|
|
30
|
+
}
|
|
31
|
+
return c.json(true);
|
|
32
|
+
});
|
|
33
|
+
app.openapi(createRoute({
|
|
34
|
+
method: 'post',
|
|
35
|
+
path: '/pull',
|
|
36
|
+
responses: {
|
|
37
|
+
200: {
|
|
38
|
+
description: 'Trigger the pull process',
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
}), async (c) => {
|
|
42
|
+
await hooks.onPull();
|
|
43
|
+
return c.json(true);
|
|
44
|
+
});
|
|
45
|
+
app.openapi(createRoute({
|
|
46
|
+
method: 'post',
|
|
47
|
+
path: '/push',
|
|
48
|
+
responses: {
|
|
49
|
+
200: {
|
|
50
|
+
description: 'Trigger the push process',
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
}), async (c) => {
|
|
54
|
+
await hooks.onPush();
|
|
55
|
+
return c.json(true);
|
|
56
|
+
});
|
|
57
|
+
return app;
|
|
58
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { serveStatic } from "@hono/node-server/serve-static";
|
|
2
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
3
|
+
import path from "path";
|
|
4
|
+
export function createAdminRoutes() {
|
|
5
|
+
const app = new OpenAPIHono();
|
|
6
|
+
// TODO: This is package manager dependent
|
|
7
|
+
const adminPath = path.join(import.meta.dirname, "..", "..", "node_modules", "@isardsat", "editorial-admin", "build", "client");
|
|
8
|
+
const relativeAdminPath = path.relative(process.cwd(), adminPath);
|
|
9
|
+
app.use("/admin/*", serveStatic({
|
|
10
|
+
root: relativeAdminPath,
|
|
11
|
+
rewriteRequestPath(path) {
|
|
12
|
+
return path.split("/admin/")[0];
|
|
13
|
+
},
|
|
14
|
+
onNotFound(path, c) {
|
|
15
|
+
console.log("onNotFound", path, c);
|
|
16
|
+
},
|
|
17
|
+
}));
|
|
18
|
+
app.use("/assets/*", serveStatic({
|
|
19
|
+
root: relativeAdminPath,
|
|
20
|
+
}));
|
|
21
|
+
return app;
|
|
22
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
2
|
+
import { EditorialDataItemSchema, EditorialDataSchema, EditorialSchemaSchema, } from "@isardsat/editorial-common";
|
|
3
|
+
export function createDataRoutes(storage) {
|
|
4
|
+
const app = new OpenAPIHono();
|
|
5
|
+
app.openapi(createRoute({
|
|
6
|
+
method: "get",
|
|
7
|
+
path: "/schema",
|
|
8
|
+
responses: {
|
|
9
|
+
200: {
|
|
10
|
+
content: {
|
|
11
|
+
"application/json": {
|
|
12
|
+
schema: EditorialSchemaSchema,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
description: "Get Editorial schema",
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
}), async (c) => {
|
|
19
|
+
const schema = await storage.getSchema();
|
|
20
|
+
return c.json(schema);
|
|
21
|
+
});
|
|
22
|
+
app.openapi(createRoute({
|
|
23
|
+
method: "get",
|
|
24
|
+
path: "/data",
|
|
25
|
+
responses: {
|
|
26
|
+
200: {
|
|
27
|
+
content: {
|
|
28
|
+
"application/json": {
|
|
29
|
+
schema: EditorialDataSchema,
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
description: "Get all Editorial data",
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
}), async (c) => {
|
|
36
|
+
const content = await storage.getContent();
|
|
37
|
+
return c.json(content);
|
|
38
|
+
});
|
|
39
|
+
app.openapi(createRoute({
|
|
40
|
+
method: "get",
|
|
41
|
+
path: "/data/{itemType}",
|
|
42
|
+
request: {
|
|
43
|
+
params: z.object({
|
|
44
|
+
itemType: z.string().openapi({
|
|
45
|
+
param: { name: "itemType", in: "path" },
|
|
46
|
+
example: "newsItem",
|
|
47
|
+
}),
|
|
48
|
+
locale: z
|
|
49
|
+
.string()
|
|
50
|
+
.optional()
|
|
51
|
+
.openapi({
|
|
52
|
+
param: { name: "locale", in: "query" },
|
|
53
|
+
example: "es_ES",
|
|
54
|
+
}),
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
responses: {
|
|
58
|
+
200: {
|
|
59
|
+
content: {
|
|
60
|
+
"application/json": {
|
|
61
|
+
schema: EditorialDataSchema,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
description: "Get objects by type",
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
}), async (c) => {
|
|
68
|
+
const { itemType } = c.req.valid("param");
|
|
69
|
+
const content = await storage.getContent();
|
|
70
|
+
return c.json(content[itemType]);
|
|
71
|
+
});
|
|
72
|
+
app.openapi(createRoute({
|
|
73
|
+
method: "get",
|
|
74
|
+
path: "/data/{itemType}/ids",
|
|
75
|
+
request: {
|
|
76
|
+
params: z.object({
|
|
77
|
+
itemType: z.string().openapi({
|
|
78
|
+
param: { name: "itemType", in: "path" },
|
|
79
|
+
example: "newsItem",
|
|
80
|
+
}),
|
|
81
|
+
}),
|
|
82
|
+
},
|
|
83
|
+
responses: {
|
|
84
|
+
200: {
|
|
85
|
+
content: {
|
|
86
|
+
"application/json": {
|
|
87
|
+
schema: z.array(z.string()),
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
description: "Get object ids by type",
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
}), async (c) => {
|
|
94
|
+
const { itemType } = c.req.valid("param");
|
|
95
|
+
const content = await storage.getContent();
|
|
96
|
+
return c.json(Object.keys(content[itemType]));
|
|
97
|
+
});
|
|
98
|
+
app.openapi(createRoute({
|
|
99
|
+
method: "get",
|
|
100
|
+
path: "/data/{itemType}/{id}",
|
|
101
|
+
request: {
|
|
102
|
+
params: z.object({
|
|
103
|
+
itemType: z.string().openapi({
|
|
104
|
+
param: { name: "itemType", in: "path" },
|
|
105
|
+
example: "newsItem",
|
|
106
|
+
}),
|
|
107
|
+
id: z.string().openapi({
|
|
108
|
+
param: { name: "id", in: "path" },
|
|
109
|
+
example: "about-us",
|
|
110
|
+
}),
|
|
111
|
+
locale: z
|
|
112
|
+
.string()
|
|
113
|
+
.optional()
|
|
114
|
+
.openapi({
|
|
115
|
+
param: { name: "locale", in: "query" },
|
|
116
|
+
example: "es_ES",
|
|
117
|
+
}),
|
|
118
|
+
}),
|
|
119
|
+
},
|
|
120
|
+
responses: {
|
|
121
|
+
200: {
|
|
122
|
+
content: {
|
|
123
|
+
"application/json": {
|
|
124
|
+
schema: EditorialDataSchema,
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
description: "Get object data",
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
}), async (c) => {
|
|
131
|
+
const { itemType, id } = c.req.valid("param");
|
|
132
|
+
const content = await storage.getContent();
|
|
133
|
+
return c.json(content[itemType][id]);
|
|
134
|
+
});
|
|
135
|
+
app.openapi(createRoute({
|
|
136
|
+
method: "put",
|
|
137
|
+
path: "/data/{itemType}/{id}",
|
|
138
|
+
request: {
|
|
139
|
+
params: z.object({
|
|
140
|
+
itemType: z.string().openapi({
|
|
141
|
+
param: { name: "itemType", in: "path" },
|
|
142
|
+
example: "newsItem",
|
|
143
|
+
}),
|
|
144
|
+
id: z.string().openapi({
|
|
145
|
+
param: { name: "id", in: "path" },
|
|
146
|
+
example: "learn-about-us",
|
|
147
|
+
}),
|
|
148
|
+
}),
|
|
149
|
+
body: {
|
|
150
|
+
content: {
|
|
151
|
+
"application/json": {
|
|
152
|
+
schema: EditorialDataItemSchema,
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
required: true,
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
responses: {
|
|
159
|
+
200: {
|
|
160
|
+
content: {
|
|
161
|
+
"application/json": {
|
|
162
|
+
schema: EditorialDataItemSchema,
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
description: "Create a new object",
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
}), async (c) => {
|
|
169
|
+
const itemAtts = await c.req.json();
|
|
170
|
+
const newItem = await storage.createItem(itemAtts);
|
|
171
|
+
return c.json(newItem);
|
|
172
|
+
});
|
|
173
|
+
app.openapi(createRoute({
|
|
174
|
+
method: "patch",
|
|
175
|
+
path: "/data/{itemType}/{id}",
|
|
176
|
+
request: {
|
|
177
|
+
params: z.object({
|
|
178
|
+
itemType: z.string().openapi({
|
|
179
|
+
param: { name: "itemType", in: "path" },
|
|
180
|
+
example: "newsItem",
|
|
181
|
+
}),
|
|
182
|
+
id: z.string().openapi({
|
|
183
|
+
param: { name: "id", in: "path" },
|
|
184
|
+
example: "about-us",
|
|
185
|
+
}),
|
|
186
|
+
}),
|
|
187
|
+
body: {
|
|
188
|
+
content: {
|
|
189
|
+
"application/json": {
|
|
190
|
+
schema: EditorialDataItemSchema,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
required: true,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
responses: {
|
|
197
|
+
200: {
|
|
198
|
+
content: {
|
|
199
|
+
"application/json": {
|
|
200
|
+
schema: EditorialDataSchema,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
description: "Update object",
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
}), async (c) => {
|
|
207
|
+
const itemAtts = await c.req.json();
|
|
208
|
+
const newItem = await storage.updateItem(itemAtts);
|
|
209
|
+
return c.json(newItem);
|
|
210
|
+
});
|
|
211
|
+
app.openapi(createRoute({
|
|
212
|
+
method: "delete",
|
|
213
|
+
path: "/data/{itemType}/{id}",
|
|
214
|
+
request: {
|
|
215
|
+
params: z.object({
|
|
216
|
+
itemType: z.string().openapi({
|
|
217
|
+
param: { name: "itemType", in: "path" },
|
|
218
|
+
example: "newsItem",
|
|
219
|
+
}),
|
|
220
|
+
id: z.string().openapi({
|
|
221
|
+
param: { name: "id", in: "path" },
|
|
222
|
+
example: "about-us",
|
|
223
|
+
}),
|
|
224
|
+
}),
|
|
225
|
+
},
|
|
226
|
+
responses: {
|
|
227
|
+
200: {
|
|
228
|
+
content: {
|
|
229
|
+
"application/json": {
|
|
230
|
+
schema: z.boolean(),
|
|
231
|
+
},
|
|
232
|
+
},
|
|
233
|
+
description: "Delete object",
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
}), async (c) => {
|
|
237
|
+
const { itemType, id } = c.req.valid("param");
|
|
238
|
+
await storage.deleteItem({ type: itemType, id });
|
|
239
|
+
return c.json(true, 200);
|
|
240
|
+
});
|
|
241
|
+
return app;
|
|
242
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { createRoute, OpenAPIHono, z } from '@hono/zod-openapi';
|
|
2
|
+
import { EditorialFilesSchema, } from '@isardsat/editorial-common';
|
|
3
|
+
import { readdirSync, statSync } from 'node:fs';
|
|
4
|
+
import { access, constants, mkdir, rename } from 'node:fs/promises';
|
|
5
|
+
import { basename, join, normalize } from 'node:path';
|
|
6
|
+
export function createFilesRoutes() {
|
|
7
|
+
const app = new OpenAPIHono();
|
|
8
|
+
const publicDirPath = 'public';
|
|
9
|
+
const deletedDirPath = 'public/.deleted';
|
|
10
|
+
app.openapi(createRoute({
|
|
11
|
+
method: 'get',
|
|
12
|
+
path: '/files',
|
|
13
|
+
responses: {
|
|
14
|
+
200: {
|
|
15
|
+
content: {
|
|
16
|
+
'application/json': {
|
|
17
|
+
schema: EditorialFilesSchema,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
description: 'Get tree of public files',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
}), async (c) => {
|
|
24
|
+
function readDirectoryChildren(path) {
|
|
25
|
+
const directory = readdirSync(path);
|
|
26
|
+
return directory
|
|
27
|
+
.filter((fileName) => !fileName.startsWith('.'))
|
|
28
|
+
.map((fileName) => {
|
|
29
|
+
const file = statSync(join(path, fileName));
|
|
30
|
+
const isDirectory = file.isDirectory();
|
|
31
|
+
return {
|
|
32
|
+
name: basename(fileName),
|
|
33
|
+
path: join(path, fileName),
|
|
34
|
+
size: file.size,
|
|
35
|
+
type: isDirectory ? 'directory' : 'file',
|
|
36
|
+
children: isDirectory
|
|
37
|
+
? readDirectoryChildren(join(path, fileName))
|
|
38
|
+
: undefined,
|
|
39
|
+
};
|
|
40
|
+
})
|
|
41
|
+
.sort((a, b) => (a.type === 'directory' ? -1 : 0));
|
|
42
|
+
}
|
|
43
|
+
const files = readDirectoryChildren(publicDirPath);
|
|
44
|
+
return c.json(files);
|
|
45
|
+
});
|
|
46
|
+
app.openapi(createRoute({
|
|
47
|
+
method: 'delete',
|
|
48
|
+
path: '/files',
|
|
49
|
+
request: {
|
|
50
|
+
body: {
|
|
51
|
+
content: {
|
|
52
|
+
'application/json': {
|
|
53
|
+
schema: z.object({
|
|
54
|
+
path: z.string(),
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
required: true,
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
responses: {
|
|
62
|
+
200: {
|
|
63
|
+
content: {
|
|
64
|
+
'application/json': {
|
|
65
|
+
schema: z.boolean(),
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
description: '',
|
|
69
|
+
},
|
|
70
|
+
400: {
|
|
71
|
+
content: {
|
|
72
|
+
'application/json': {
|
|
73
|
+
schema: z.object({
|
|
74
|
+
error: z.string(),
|
|
75
|
+
}),
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
description: 'Invalid file path',
|
|
79
|
+
},
|
|
80
|
+
404: {
|
|
81
|
+
content: {
|
|
82
|
+
'application/json': {
|
|
83
|
+
schema: z.object({
|
|
84
|
+
error: z.string(),
|
|
85
|
+
}),
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
description: 'File not found',
|
|
89
|
+
},
|
|
90
|
+
500: {
|
|
91
|
+
content: {
|
|
92
|
+
'application/json': {
|
|
93
|
+
schema: z.object({
|
|
94
|
+
error: z.string(),
|
|
95
|
+
}),
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
description: 'Server error',
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
}), async (c) => {
|
|
102
|
+
const { path: filePath } = c.req.valid('json');
|
|
103
|
+
try {
|
|
104
|
+
const normalizedPath = normalize(filePath);
|
|
105
|
+
if (!normalizedPath.startsWith(publicDirPath)) {
|
|
106
|
+
return c.json({ error: 'Invalid file path' }, 400);
|
|
107
|
+
}
|
|
108
|
+
const exists = await access(filePath, constants.W_OK)
|
|
109
|
+
.then(() => true)
|
|
110
|
+
.catch(() => false);
|
|
111
|
+
if (!exists) {
|
|
112
|
+
return c.json({ error: 'File not found' }, 404);
|
|
113
|
+
}
|
|
114
|
+
// Move to deleted directory
|
|
115
|
+
await mkdir(deletedDirPath, { recursive: true });
|
|
116
|
+
await rename(filePath, join(deletedDirPath, basename(filePath)));
|
|
117
|
+
return c.json(true, 200);
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
console.error('Delete failed:', err);
|
|
121
|
+
return c.json({ error: 'Server error' }, 500);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return app;
|
|
125
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@isardsat/editorial-server",
|
|
3
|
+
"version": "6.0.0-canary-001",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@hono/node-server": "^1.13.8",
|
|
12
|
+
"@hono/swagger-ui": "^0.5.0",
|
|
13
|
+
"@hono/zod-openapi": "^0.18.4",
|
|
14
|
+
"hono": "^4.6.20",
|
|
15
|
+
"yaml": "^2.7.0",
|
|
16
|
+
"zod": "^3.24.1",
|
|
17
|
+
"@isardsat/editorial-admin": "^6.0.0-canary-001",
|
|
18
|
+
"@isardsat/editorial-common": "^6.0.0-canary-001"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@tsconfig/node22": "^22.0.0",
|
|
22
|
+
"@types/node": "22.13.1",
|
|
23
|
+
"@types/react": "^19.0.8",
|
|
24
|
+
"@types/react-dom": "^19.0.3",
|
|
25
|
+
"esbuild": "^0.24.2",
|
|
26
|
+
"react": "^19.0.0",
|
|
27
|
+
"react-dom": "^19.0.0",
|
|
28
|
+
"tsx": "^4.7.1",
|
|
29
|
+
"typescript": "^5.7.3"
|
|
30
|
+
},
|
|
31
|
+
"volta": {
|
|
32
|
+
"extends": "../../package.json"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"compile": "tsc",
|
|
36
|
+
"compile:watch": "tsc --watch"
|
|
37
|
+
}
|
|
38
|
+
}
|