Commit 23d82194 authored by nanahira's avatar nanahira

http adapter injection

parent d1c6a13a
Pipeline #6071 passed with stages
in 48 seconds
...@@ -23,13 +23,14 @@ import PluginOnebot from '@koishijs/plugin-onebot'; ...@@ -23,13 +23,14 @@ import PluginOnebot from '@koishijs/plugin-onebot';
imports: [ imports: [
KoishiModule.register({ KoishiModule.register({
// Koishi config goes here // Koishi config goes here
prefix: '.',
usePlugins: [ usePlugins: [
// Plugins to install // Plugins to install
PluginDef(PluginOnebot, { PluginDef(PluginOnebot, {
protocol: 'ws', protocol: 'ws',
endpoint: config.get('CQ_ENDPOINT'), endpoint: 'CQ_ENDPOINT',
selfId: config.get('CQ_SELFID'), selfId: 'CQ_ENDPOINT',
token: config.get('CQ_TOKEN'), token: 'CQ_ENDPOINT',
}), }),
], ],
}) })
...@@ -44,14 +45,24 @@ You may also register Koishi plugins later. ...@@ -44,14 +45,24 @@ You may also register Koishi plugins later.
```ts ```ts
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
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';
@Module({ @Module({
imports: [ imports: [
KoishiModule.registerAsync({ KoishiModule.registerAsync({
useFactory: () => ({ imports: [ConfigModule.forRoot()],
inject: [ConfigService, HttpAdapterHost],
useFactory: async (
config: ConfigService,
adapterHost: HttpAdapterHost,
) => ({
// Koishi config goes here // Koishi config goes here
prefix: '.',
// OPTIONAL: Injects Http Server here to Koishi instance making routes functioning.
httpAdapter: adapterHost.httpAdapter,
usePlugins: [ usePlugins: [
// Plugins to install // Plugins to install
PluginDef(PluginOnebot, { PluginDef(PluginOnebot, {
......
{ {
"name": "koishi-nestjs", "name": "koishi-nestjs",
"version": "1.0.8", "version": "1.0.9",
"description": "Koishi.js as Nest.js Module", "description": "Koishi.js as Nest.js Module",
"main": "dist/index.js", "main": "dist/index.js",
"typings": "dist/index.d.ts", "typings": "dist/index.d.ts",
......
import { ModuleMetadata, Provider, Type } from '@nestjs/common'; import { ModuleMetadata, Provider, Type } from '@nestjs/common';
import { App, Plugin, MaybeArray } from 'koishi'; import { App, Plugin, MaybeArray } from 'koishi';
import { AbstractHttpAdapter } from '@nestjs/core';
const selectors = [ const selectors = [
'user', 'user',
...@@ -36,6 +37,7 @@ export function PluginDef<T extends Plugin>( ...@@ -36,6 +37,7 @@ 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;
} }
export interface KoishiModuleOptionsFactory { export interface KoishiModuleOptionsFactory {
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
} from '@nestjs/common'; } from '@nestjs/common';
import { KOISHI_MODULE_OPTIONS } from './koishi.constants'; import { KOISHI_MODULE_OPTIONS } from './koishi.constants';
import { KoishiModuleOptions } from './koishi.interfaces'; import { KoishiModuleOptions } from './koishi.interfaces';
import { Server } from 'http';
@Injectable() @Injectable()
export class KoishiService export class KoishiService
...@@ -14,12 +15,26 @@ export class KoishiService ...@@ -14,12 +15,26 @@ export class KoishiService
implements OnModuleInit, OnApplicationBootstrap { implements OnModuleInit, OnApplicationBootstrap {
constructor( constructor(
@Inject(KOISHI_MODULE_OPTIONS) @Inject(KOISHI_MODULE_OPTIONS)
private koishiModuleOptions: KoishiModuleOptions, private readonly koishiModuleOptions: KoishiModuleOptions,
) { ) {
super(koishiModuleOptions); super(koishiModuleOptions);
} }
private setHttpServer() {
if (
this.koishiModuleOptions.httpAdapter &&
!this.koishiModuleOptions.port
) {
const httpServer: Server = this.koishiModuleOptions.httpAdapter.getHttpServer();
if (httpServer instanceof Server) {
this.logger('app').info('App using Nest HTTP Server.');
this._httpServer = httpServer;
}
}
}
onModuleInit() { onModuleInit() {
this.setHttpServer();
if (this.koishiModuleOptions.usePlugins) { if (this.koishiModuleOptions.usePlugins) {
for (const pluginDesc of this.koishiModuleOptions.usePlugins) { for (const pluginDesc of this.koishiModuleOptions.usePlugins) {
const ctx = pluginDesc.select const ctx = pluginDesc.select
......
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