Commit 534a56c6 authored by nanahira's avatar nanahira

remove injection of HttpAdapter

parent 3a4278fe
......@@ -43,7 +43,6 @@ export class AppModule {}
```ts
import { Module } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { KoishiModule, PluginDef } from 'koishi-nestjs';
import PluginOnebot from '@koishijs/plugin-onebot';
......@@ -52,15 +51,10 @@ import PluginOnebot from '@koishijs/plugin-onebot';
imports: [
KoishiModule.registerAsync({
imports: [ConfigModule.forRoot()],
inject: [ConfigService, HttpAdapterHost],
useFactory: async (
config: ConfigService,
adapterHost: HttpAdapterHost,
) => ({
inject: [ConfigService],
useFactory: async (config: ConfigService) => ({
// 在这里填写 Koishi 配置参数
prefix: '.',
// 可选,如果使用 Onebot 的 ws reverse 协议,那么需要配置该项来保证 ws 服务器正常运行
httpAdapter: adapterHost.httpAdapter,
usePlugins: [
// 预安装的插件
PluginDef(PluginOnebot, {
......@@ -83,8 +77,6 @@ Koishi-Nest 的配置项和 Koishi 配置项一致,参照 [Koishi 文档](http
* `loggerPrefix`: `string` Nest 日志中 Logger 的前缀。默认 `koishi`
* `httpAdapter`: `AbstractHttpAdapter` Nest 的 HTTP 适配器引用,用于 `@koishijs/plugin-adapter-onebot``ws:reverse` 协议的适配。非该协议可以忽略该项。
* `usePlugins`: `KoishiModulePlugin[]` 可选。预先安装的 Koishi 插件列表。使用 `PluginDef(plugin, options, select)` 方法生成该项的定义。该配置项的成员参数如下。
* `plugin` Koishi 插件。
......
import { Injectable } from '@nestjs/common';
import { DiscoveryService, MetadataScanner, Reflector } from '@nestjs/core';
import {
AbstractHttpAdapter,
DiscoveryService,
HttpAdapterHost,
MetadataScanner,
ModulesContainer,
Reflector,
} from '@nestjs/core';
import { Argv, Command, Context } from 'koishi';
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import {
......@@ -22,8 +29,20 @@ export class KoishiMetascanService {
private readonly discoveryService: DiscoveryService,
private readonly metadataScanner: MetadataScanner,
private readonly reflector: Reflector,
private readonly moduleContainer: ModulesContainer,
) {}
getHttpAdapter(): AbstractHttpAdapter {
for (const module of this.moduleContainer.values()) {
const adapterHost = module.providers.get(HttpAdapterHost);
if (adapterHost) {
return (adapterHost as InstanceWrapper<HttpAdapterHost>).instance
.httpAdapter;
}
}
return null;
}
private async handleInstance(
ctx: Context,
instance: Record<string, any>,
......@@ -56,7 +75,6 @@ export class KoishiMetascanService {
baseContext = filter(baseContext) || baseContext;
}
}
console.log(regData);
switch (regData.type) {
case 'middleware':
baseContext.middleware(
......@@ -65,8 +83,9 @@ export class KoishiMetascanService {
);
break;
case 'onevent':
const { data: eventData } =
regData as DoRegisterConfig<EventNameAndPrepend>;
const {
data: eventData,
} = regData as DoRegisterConfig<EventNameAndPrepend>;
baseContext.on(eventData.name, (...args: any[]) =>
methodFun.call(instance, ...args),
);
......@@ -87,9 +106,10 @@ export class KoishiMetascanService {
const { data: commandData } = regData as DoRegisterConfig<
ContextFunction<Command>
>;
let command = commandData(baseContext).action(
(argv: Argv, ...args: any[]) =>
methodFun.call(instance, argv, ...args),
let command = commandData(
baseContext,
).action((argv: Argv, ...args: any[]) =>
methodFun.call(instance, argv, ...args),
);
const commandDefs: CommandDefinitionFun[] = this.reflector.get(
KoishiCommandDefinition,
......
......@@ -9,7 +9,6 @@ import {
KoishiOnContextScope,
} from './koishi.constants';
import {
CommandConfigWIthDescription,
CommandDefinitionFun,
ContextFunction,
DoRegisterConfig,
......@@ -18,7 +17,7 @@ import {
OnContextFunction,
Selection,
} from './koishi.interfaces';
import { Context, Command, Argv } from 'koishi';
import { Argv, Command } from 'koishi';
// Injections
export const InjectContext = () => Inject(KOISHI_CONTEXT);
......
import { ModuleMetadata, Provider, Type } from '@nestjs/common';
import { App, Command, Context, EventMap, MaybeArray, Plugin } from 'koishi';
import { AbstractHttpAdapter } from '@nestjs/core';
const selectors = [
'user',
......@@ -37,7 +36,6 @@ export function PluginDef<T extends Plugin>(
export interface KoishiModuleOptions extends App.Config {
usePlugins?: KoishiModulePlugin<Plugin>[];
httpAdapter?: AbstractHttpAdapter;
loggerPrefix?: string;
}
......
......@@ -35,8 +35,9 @@ export class KoishiService
_nestKoaTmpServerPort: number;
private async setHttpServer() {
if (this.koishiModuleOptions.httpAdapter) {
const httpServer: Server = this.koishiModuleOptions.httpAdapter.getHttpServer();
const httpAdapter = this.metascan.getHttpAdapter();
if (httpAdapter) {
const httpServer: Server = httpAdapter.getHttpServer();
if (httpServer instanceof Server) {
this.logger('app').info('App using Nest HTTP Server.');
this._httpServer = httpServer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment