-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathresources.ts
134 lines (115 loc) · 4.6 KB
/
resources.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import path from "path";
import fs from "fs";
import type Router from "koa-router";
import { globalServerConfig } from "./config.ts";
import { readJsonFile, ensureDirExists, buildNodeRegistry } from "./utils.ts";
export const setupResourcesApi = (router: Router) => {
const dirData = globalServerConfig.dirData;
// API: node_registry (fetch only)
router.get("/api/node_registry", (ctx) => {
// Build a serializable node registry without flow functions
ctx.body = JSON.stringify(
buildNodeRegistry(globalServerConfig.dirCustomNodes, true)
);
});
// API: single-file resources
router.get("/api/resource/single/:resource", (ctx) => {
const pathToFile = path.join(dirData, `${ctx.params.resource}.json`);
if (!fs.existsSync(pathToFile)) {
ctx.body = JSON.stringify({});
} else {
ctx.body = JSON.stringify(readJsonFile(pathToFile));
}
});
router.post("/api/resource/single/:resource", async (ctx) => {
await new Promise((resolve, reject) => {
fs.writeFile(
path.join(dirData, `${ctx.params.resource}.json`),
JSON.stringify(ctx.request.body),
(err) => {
if (err) reject(err);
else resolve(null);
}
);
});
ctx.body = "OK";
});
router.delete("/api/resource/single/:resource", (ctx) => {
const pathToFile = path.join(dirData, `${ctx.params.resource}.json`);
if (fs.existsSync(pathToFile)) fs.unlinkSync(pathToFile);
ctx.body = "OK";
});
// API: multi-file resources
router.get("/api/resource/multi/index/:resource", (ctx) => {
const resourceDir = path.join(dirData, ctx.params.resource);
ensureDirExists(resourceDir);
const resourceFiles = fs.readdirSync(resourceDir);
ctx.body = JSON.stringify(
Object.fromEntries(
resourceFiles.map((file) => {
const content = readJsonFile(
path.join(dirData, ctx.params.resource, file)
);
// Grab id from filename (strip out the extension, use regex)
const id = file.replace(/\.[^/.]+$/, "");
return [
id,
{
name: content.name ?? id,
created: content.created ?? Date.now(),
},
];
})
)
);
});
router.get("/api/resource/multi/all/:resource", (ctx) => {
const resourceDir = path.join(dirData, ctx.params.resource);
ensureDirExists(resourceDir);
const resourceFiles = fs.readdirSync(resourceDir);
ctx.body = JSON.stringify(
Object.fromEntries(
resourceFiles.map((file) => {
const content = readJsonFile(
path.join(dirData, ctx.params.resource, file)
);
// Grab id from filename (strip out the extension, use regex)
const id = file.replace(/\.[^/.]+$/, "");
return [id, content];
})
)
);
});
router.get("/api/resource/multi/single/:resource/:id", (ctx) => {
const resourceDir = path.join(dirData, ctx.params.resource);
ensureDirExists(resourceDir);
const pathToFile = path.join(resourceDir, `${ctx.params.id}.json`);
if (!fs.existsSync(pathToFile)) {
ctx.body = JSON.stringify({});
} else {
ctx.body = JSON.stringify(readJsonFile(pathToFile));
}
});
router.post("/api/resource/multi/single/:resource/:id", async (ctx) => {
const resourceDir = path.join(dirData, ctx.params.resource);
ensureDirExists(resourceDir);
await new Promise((resolve, reject) => {
fs.writeFile(
path.join(resourceDir, `${ctx.params.id}.json`),
JSON.stringify(ctx.request.body),
(err) => {
if (err) reject(err);
else resolve(null);
}
);
});
ctx.body = "OK";
});
router.delete("/api/resource/multi/single/:resource/:id", (ctx) => {
const resourceDir = path.join(dirData, ctx.params.resource);
ensureDirExists(resourceDir);
const pathToFile = path.join(resourceDir, `${ctx.params.id}.json`);
if (fs.existsSync(pathToFile)) fs.unlinkSync(pathToFile);
ctx.body = "OK";
});
};