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