進一步完善 Server API

This commit is contained in:
CrescentLeaf
2025-09-06 01:53:09 +08:00
parent c3c332017e
commit b0c67da340
6 changed files with 30 additions and 15 deletions

View File

@@ -10,5 +10,6 @@ type ApiCallbackMessage = {
* 501: 伺服器端不支持請求的功能 * 501: 伺服器端不支持請求的功能
*/ */
code: 200 | 400 | 401 | 403 | 404 | 500 | 501, code: 200 | 400 | 401 | 403 | 404 | 500 | 501,
data?: { [key: string]: unknown },
} }
export default ApiCallbackMessage export default ApiCallbackMessage

7
server/api/ApiDeclare.ts Normal file
View File

@@ -0,0 +1,7 @@
export type CallMethod =
"User.auth" |
"User.register" |
"User.login"
export type ClientEvent =
"Client.onMessage"

View File

@@ -1,14 +1,15 @@
import HttpServerLike from '../types/HttpServerLike.ts' import HttpServerLike from '../types/HttpServerLike.ts'
import UserApi from "./UserApi.ts" import UserApi from "./UserApi.ts"
import * as SocketIo from "socket.io" import * as SocketIo from "socket.io"
import ApiCallbackMessage from "../types/ApiCallbackMessage.ts" import ApiCallbackMessage from "./ApiCallbackMessage.ts"
import EventCallbackFunction from "../types/EventCallbackFunction.ts" import EventCallbackFunction from "../types/EventCallbackFunction.ts"
import BaseApi from "./BaseApi.ts"
export default class ApiManager { export default class ApiManager {
static httpServer: HttpServerLike static httpServer: HttpServerLike
static socketIoServer: SocketIo.Server static socketIoServer: SocketIo.Server
static event_listeners: { [key: string] : EventCallbackFunction } = {} static event_listeners: { [key: string]: EventCallbackFunction } = {}
static apis_instance: {} static apis_instance: { [key: string]: BaseApi } = {}
static initServer(httpServer: HttpServerLike, socketIoServer: SocketIo.Server) { static initServer(httpServer: HttpServerLike, socketIoServer: SocketIo.Server) {
this.httpServer = httpServer this.httpServer = httpServer
this.socketIoServer = socketIoServer this.socketIoServer = socketIoServer
@@ -30,13 +31,17 @@ export default class ApiManager {
static initEvents() { static initEvents() {
const io = this.socketIoServer const io = this.socketIoServer
io.on('connection', (socket) => { io.on('connection', (socket) => {
socket.on("The_White_Silk", (name: string, args: {}, callback: (ret: ApiCallbackMessage) => void) => { socket.on("The_White_Silk", (name: string, args: { [key: string]: unknown }, callback: (ret: ApiCallbackMessage) => void) => {
if (name == null || args == null) return callback({ try {
msg: "Invalid request.", if (name == null || args == null) return callback({
code: 400 msg: "Invalid request.",
}) code: 400
})
return callback(this.event_listeners[name]?.(args)) return callback(this.event_listeners[name]?.(args))
} catch (e) {
console.error(e)
}
}) })
}) })
} }

View File

@@ -1,5 +1,6 @@
import EventCallbackFunction from "../types/EventCallbackFunction.ts" import EventCallbackFunction from "../types/EventCallbackFunction.ts"
import ApiManager from "./ApiManager.ts"; import ApiManager from "./ApiManager.ts"
import { CallMethod } from './ApiDeclare.ts'
export default abstract class BaseApi { export default abstract class BaseApi {
abstract getName(): string abstract getName(): string
@@ -7,7 +8,8 @@ export default abstract class BaseApi {
this.onInit() this.onInit()
} }
abstract onInit(): void abstract onInit(): void
registerEvent(name: string, func: EventCallbackFunction) { registerEvent(name: CallMethod, func: EventCallbackFunction) {
ApiManager.addEventListener(this.getName() + "." + name, func) if (!name.startsWith(this.getName() + ".")) throw Error("注冊的事件應該與接口集合命名空間相匹配: " + name)
ApiManager.addEventListener(name, func)
} }
} }

View File

@@ -1,11 +1,11 @@
import BaseApi from "./BaseApi.ts"; import BaseApi from "./BaseApi.ts"
export default class UserApi extends BaseApi { export default class UserApi extends BaseApi {
override getName(): string { override getName(): string {
return "User" return "User"
} }
override onInit(): void { override onInit(): void {
this.registerEvent("", () => { this.registerEvent("User.auth", () => {
return { return {
msg: "", msg: "",
code: 200, code: 200,

View File

@@ -1,4 +1,4 @@
import ApiCallbackMessage from "./ApiCallbackMessage.ts" import ApiCallbackMessage from "../api/ApiCallbackMessage.ts"
type EventCallbackFunction = (args: {}) => ApiCallbackMessage type EventCallbackFunction = (args: {}) => ApiCallbackMessage