Compare commits
4 Commits
d2007fbdcc
...
141c983ddd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
141c983ddd | ||
|
|
63b6dd2630 | ||
|
|
81fa113dae | ||
|
|
0477f4e4e7 |
@@ -1,14 +1,15 @@
|
|||||||
import BaseApi from './BaseApi.ts'
|
|
||||||
import HttpServerLike from '../types/HttpServerLike.ts'
|
import HttpServerLike from '../types/HttpServerLike.ts'
|
||||||
import UserApi from "./UserApi.ts"
|
import UserApi from "./UserApi.ts"
|
||||||
import SocketIo from "socket.io"
|
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 {
|
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 apis_instance: {}
|
static apis_instance: {}
|
||||||
static init(httpServer: HttpServerLike, socketIoServer: SocketIo.Server) {
|
static initServer(httpServer: HttpServerLike, socketIoServer: SocketIo.Server) {
|
||||||
this.httpServer = httpServer
|
this.httpServer = httpServer
|
||||||
this.socketIoServer = socketIoServer
|
this.socketIoServer = socketIoServer
|
||||||
}
|
}
|
||||||
@@ -23,13 +24,19 @@ export default class ApiManager {
|
|||||||
user: new UserApi()
|
user: new UserApi()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
static addEventListener(name: string, func: EventCallbackFunction) {
|
||||||
|
this.event_listeners[name] = func
|
||||||
|
}
|
||||||
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: UnknownFunction) => {
|
socket.on("The_White_Silk", (name: string, args: {}, callback: (ret: ApiCallbackMessage) => void) => {
|
||||||
if (name == null || args == null) return callback({
|
if (name == null || args == null) return callback({
|
||||||
|
msg: "Invalid request.",
|
||||||
|
code: 400
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return callback(this.event_listeners[name]?.(args))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import UnknownFunction from '../types/UnknownFunction.ts'
|
import EventCallbackFunction from "../types/EventCallbackFunction.ts"
|
||||||
import ApiManager from "./ApiManager.ts";
|
import ApiManager from "./ApiManager.ts";
|
||||||
|
|
||||||
export default abstract class BaseApi {
|
export default abstract class BaseApi {
|
||||||
@@ -7,8 +7,7 @@ export default abstract class BaseApi {
|
|||||||
this.onInit()
|
this.onInit()
|
||||||
}
|
}
|
||||||
abstract onInit(): void
|
abstract onInit(): void
|
||||||
registerEvent(name: string, func: UnknownFunction) {
|
registerEvent(name: string, func: EventCallbackFunction) {
|
||||||
const io = ApiManager.getSocketIoServer()
|
ApiManager.addEventListener(this.getName() + "." + name, func)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,14 +18,16 @@ class FileBean {
|
|||||||
declare last_used_time: number
|
declare last_used_time: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FileBeanKey = keyof FileBean
|
||||||
|
|
||||||
class File {
|
class File {
|
||||||
declare bean: FileBean
|
declare bean: FileBean
|
||||||
constructor(bean: FileBean) {
|
constructor(bean: FileBean) {
|
||||||
this.bean = bean
|
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)
|
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() {
|
getMime() {
|
||||||
return this.bean.mime
|
return this.bean.mime
|
||||||
|
|||||||
@@ -1,4 +1,14 @@
|
|||||||
type ApiCallbackMessage = {
|
type ApiCallbackMessage = {
|
||||||
msg: string,
|
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
|
||||||
|
|||||||
5
src/types/EventCallbackFunction.ts
Normal file
5
src/types/EventCallbackFunction.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import ApiCallbackMessage from "./ApiCallbackMessage.ts"
|
||||||
|
|
||||||
|
type EventCallbackFunction = (args: {}) => ApiCallbackMessage
|
||||||
|
|
||||||
|
export default EventCallbackFunction
|
||||||
Reference in New Issue
Block a user