-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.ts
58 lines (56 loc) · 1.93 KB
/
api.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
import { Context } from './context.ts'
export const inject = ['hono']
export function apply(ctx: Context) {
let functionality = {
registry: !!ctx.root.get('koishi'),
analyzer: !!ctx.root.get('koishi.analyzer'),
npm: !!ctx.root.get('npm')
}
const checkFunctionality = ctx.throttle(() => (functionality = {
registry: !!ctx.root.get('koishi'),
analyzer: !!ctx.root.get('koishi.analyzer'),
npm: !!ctx.root.get('npm')
}), 100)
ctx.hono.get("/api/status", (c) => {
checkFunctionality()
return c.json({
version: ctx.info.version,
functionality: functionality,
})
})
ctx.inject(['npm'], (ctx) => {
ctx.hono.get("/api/plugins", (c) => {
return c.json({
synchronized: ctx.npm.synchronized,
list: [...ctx.npm.plugins.keys()]
});
});
})
ctx.inject(['koishi', 'koishi.meta', 'koishi.analyzer'], (ctx) => {
ctx.hono.get("/api/registry/status", (c) => {
return c.json({
version: ctx.info.version,
updateAt: ctx.koishi.last_refresh.toUTCString(),
synchronized: ctx.koishi.isSynchronized(),
features: ctx.koishi.getFeatures(),
})
})
ctx.hono.get("/api/registry/:name", async (c) => {
const name = c.req.param('name')!
const should_refresh = c.req.query("use_uncached")
const result = await ctx.koishi.fetchObject(name, !!should_refresh, !!should_refresh)
if (result === null)
return c.json({
name: name,
status: 404,
message: "not found"
}, 404)
return c.json({
name: name,
status: 200,
data: result,
message: "fetched"
})
})
})
}