koishi-core
Advanced tools
Comparing version 0.2.4 to 0.2.5
@@ -13,5 +13,5 @@ /// <reference types="node" /> | ||
secret?: string; | ||
sendUrl?: string; | ||
selfId?: number; | ||
wsServer?: string; | ||
httpServer?: string; | ||
database?: DatabaseConfig; | ||
@@ -18,0 +18,0 @@ type?: ServerType; |
@@ -31,3 +31,3 @@ "use strict"; | ||
this._post = async (action, params = {}) => { | ||
const uri = new URL(action, this.app.options.sendUrl).href; | ||
const uri = new URL(action, this.app.options.httpServer).href; | ||
const { data } = await axios_1.default.get(uri, { params, headers }); | ||
@@ -44,16 +44,10 @@ return data; | ||
showSenderLog('request %s %o', action, params); | ||
try { | ||
const response = await this._post(action, koishi_utils_1.snakeCase(params)); | ||
showSenderLog('response %o', response); | ||
const { data, retcode } = response; | ||
if (retcode === 0) { | ||
return koishi_utils_1.camelCase(data); | ||
} | ||
else { | ||
throw new SenderError(params, action, retcode); | ||
} | ||
const response = await this._post(action, koishi_utils_1.snakeCase(params)); | ||
showSenderLog('response %o', response); | ||
const { data, retcode } = response; | ||
if (retcode === 0) { | ||
return koishi_utils_1.camelCase(data); | ||
} | ||
catch (error) { | ||
console.error(error); | ||
throw error; | ||
else { | ||
throw new SenderError(params, action, retcode); | ||
} | ||
@@ -60,0 +54,0 @@ } |
@@ -11,3 +11,2 @@ /// <reference types="node" /> | ||
version: VersionInfo; | ||
readonly type: ServerType; | ||
private _appMap; | ||
@@ -24,4 +23,3 @@ private _isListening; | ||
express: Express; | ||
httpServer: http.Server; | ||
readonly type = "http"; | ||
server: http.Server; | ||
constructor(app: App); | ||
@@ -33,3 +31,2 @@ _listen(): Promise<void>; | ||
socket: WebSocket; | ||
readonly type = "ws"; | ||
private _listeners; | ||
@@ -36,0 +33,0 @@ constructor(app: App); |
@@ -64,3 +64,2 @@ "use strict"; | ||
this.express = express_1.default().use(body_parser_1.json()); | ||
this.type = 'http'; | ||
if (app.options.secret) { | ||
@@ -84,8 +83,16 @@ this.express.use((req, res, next) => { | ||
const { port } = this.apps[0].options; | ||
this.httpServer = this.express.listen(port); | ||
this.server = this.express.listen(port); | ||
if (this.apps[0].options.httpServer) { | ||
try { | ||
this.version = await this.apps[0].sender.getVersionInfo(); | ||
} | ||
catch (error) { | ||
throw new Error('authorization failed'); | ||
} | ||
} | ||
showServerLog('listen to port', port); | ||
} | ||
close() { | ||
if (this.httpServer) | ||
this.httpServer.close(); | ||
if (this.server) | ||
this.server.close(); | ||
showServerLog('http server closed'); | ||
@@ -99,3 +106,2 @@ } | ||
super(app); | ||
this.type = 'ws'; | ||
this._listeners = {}; | ||
@@ -173,2 +179,5 @@ this.socket = new ws_1.default(app.options.wsServer, { | ||
const { type } = app.options; | ||
if (!type) { | ||
throw new Error('missing configuration "type"'); | ||
} | ||
if (!serverTypes[type]) { | ||
@@ -175,0 +184,0 @@ throw new Error(`server type "${type}" is not supported`); |
{ | ||
"name": "koishi-core", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "typings": "dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
234106