Compare commits

...

4 Commits

Author SHA1 Message Date
CrescentLeaf
141c983ddd feat: implment registerEvent func 2025-08-30 11:49:44 +08:00
CrescentLeaf
63b6dd2630 chore: 對 ApiCallbackMessage 的狀態碼進行注解 2025-08-30 11:07:06 +08:00
CrescentLeaf
81fa113dae chore: make linit happy (FileManager.ts) 2025-08-30 10:59:29 +08:00
CrescentLeaf
0477f4e4e7 export ApiCallbackMessage.ts 2025-08-30 10:54:07 +08:00
5 changed files with 36 additions and 13 deletions

View File

@@ -1,14 +1,15 @@
import BaseApi from './BaseApi.ts'
import HttpServerLike from '../types/HttpServerLike.ts'
import UserApi from "./UserApi.ts"
import SocketIo from "socket.io"
import UnknownFunction from "../types/UnknownFunction.ts";
import ApiCallbackMessage from "../types/ApiCallbackMessage.ts"
import EventCallbackFunction from "../types/EventCallbackFunction.ts"
export default class ApiManager {
static httpServer: HttpServerLike
static socketIoServer: SocketIo.Server
static event_listeners: { [key: string] : EventCallbackFunction } = {}
static apis_instance: {}
static init(httpServer: HttpServerLike, socketIoServer: SocketIo.Server) {
static initServer(httpServer: HttpServerLike, socketIoServer: SocketIo.Server) {
this.httpServer = httpServer
this.socketIoServer = socketIoServer
}
@@ -23,13 +24,19 @@ export default class ApiManager {
user: new UserApi()
}
}
static addEventListener(name: string, func: EventCallbackFunction) {
this.event_listeners[name] = func
}
static initEvents() {
const io = this.socketIoServer
io.on('connection', (socket) => {
socket.on("The_White_Silk", (name: string, args: {}, callback: UnknownFunction) => {
socket.on("The_White_Silk", (name: string, args: {}, callback: (ret: ApiCallbackMessage) => void) => {
if (name == null || args == null) return callback({
msg: "Invalid request.",
code: 400
})
return callback(this.event_listeners[name]?.(args))
})
})
}

View File

@@ -1,4 +1,4 @@
import UnknownFunction from '../types/UnknownFunction.ts'
import EventCallbackFunction from "../types/EventCallbackFunction.ts"
import ApiManager from "./ApiManager.ts";
export default abstract class BaseApi {
@@ -7,8 +7,7 @@ export default abstract class BaseApi {
this.onInit()
}
abstract onInit(): void
registerEvent(name: string, func: UnknownFunction) {
const io = ApiManager.getSocketIoServer()
registerEvent(name: string, func: EventCallbackFunction) {
ApiManager.addEventListener(this.getName() + "." + name, func)
}
}

View File

@@ -18,14 +18,16 @@ class FileBean {
declare last_used_time: number
}
type FileBeanKey = keyof FileBean
class File {
declare bean: FileBean
constructor(bean: FileBean) {
this.bean = bean
}
private setAttr(key: string, value: SQLInputValue) {
private setAttr(key: FileBeanKey, value: SQLInputValue) {
FileManager.database.prepare(`UPDATE ${FileManager.table_name} SET ${key} = ? WHERE count = ?`).run(value, this.bean.count)
this.bean[key] = value
this.bean[key] = value as never
}
getMime() {
return this.bean.mime

View File

@@ -1,4 +1,14 @@
type ApiCallbackMessage = {
msg: string,
code: 200 | 400 | 401 | 403 | 404 | 500 | 501
}
/**
* 200: 成功
* 400: 伺服器端無法理解客戶端請求
* 401: 需要身份驗證
* 403: 伺服器端拒絕執行客戶端請求
* 404: Not Found
* 500: 伺服器端錯誤
* 501: 伺服器端不支持請求的功能
*/
code: 200 | 400 | 401 | 403 | 404 | 500 | 501,
}
export default ApiCallbackMessage

View File

@@ -0,0 +1,5 @@
import ApiCallbackMessage from "./ApiCallbackMessage.ts"
type EventCallbackFunction = (args: {}) => ApiCallbackMessage
export default EventCallbackFunction