Commit 534a56c6 authored by nanahira's avatar nanahira

remove injection of HttpAdapter

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