Skip to content

Commit

Permalink
feat: get sentence from plugin (#25)
Browse files Browse the repository at this point in the history
* feat: get sentence from plugin

* fix: optional sentences

* fix: remove sentences

* fix: use sentense for service & fix package conflict

* fix format

---------

Co-authored-by: Maiko Tan <[email protected]>
  • Loading branch information
SaarChaffee and MaikoTan authored Feb 18, 2024
1 parent bc2998c commit cf2e521
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"fs-extra": "^11.2.0",
"js-yaml": "^4.1.0",
"koishi": "^4.16.0",
"koishi-plugin-hitokoto-sentences": "^1.0.393",
"mocha": "^10.2.0",
"prettier": "^3.1.0",
"tsup": "^8.0.1",
Expand Down
30 changes: 17 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, Service } from 'koishi'
import type { SentencesParams } from 'koishi-plugin-hitokoto-sentences'

import { Config } from '.'

Expand All @@ -8,32 +9,35 @@ declare module 'koishi' {
}
}

export interface HitokotoParams {
c?: string[]
min_length?: number
max_length?: number
}

export class HitokotoApi extends Service {
private _apiUrl: string

constructor(ctx: Context, option: Config) {
constructor(
ctx: Context,
private config: Config,
) {
super(ctx, 'hitokoto', true)
this._apiUrl = option.apiUrl ?? 'https://v1.hitokoto.cn/'
this._apiUrl = config.apiUrl ?? 'https://v1.hitokoto.cn/'
}

async getHitokoto(params: HitokotoParams): Promise<HitokotoRet> {
const resp = await this.ctx.http.get<HitokotoRet>(this._apiUrl, {
params: this.buildSearchParams(params),
})
async getHitokoto(params: SentencesParams): Promise<HitokotoRet> {
const sentences = this.ctx.get('sentences')
let resp: HitokotoRet
if (this.config.sentences && sentences) {
resp = sentences.getSentence(params)
} else {
resp = await this.ctx.http.get<HitokotoRet>(this._apiUrl, {
params: this.buildSearchParams(params),
})
}
return {
...resp,
// the `from_who` field may be null.
from_who: resp.from_who ?? '',
}
}

buildSearchParams(params: HitokotoParams): URLSearchParams {
buildSearchParams(params: SentencesParams): URLSearchParams {
const searchParams = new URLSearchParams()
if (params.c) {
params.c.forEach((type) => searchParams.append('c', type))
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Schema } from 'koishi'

export interface Config {
sentences?: boolean
/**
* @default "https://v1.hitokoto.cn"
*/
Expand All @@ -16,6 +17,7 @@ export interface Config {
}

export const Config: Schema<Config> = Schema.object({
sentences: Schema.boolean().description('是否使用本地一言语料库(需要安装 sentences 服务)').default(false),
apiUrl: Schema.string().description('获取一言的 API 地址').default('https://v1.hitokoto.cn'),
minLength: Schema.number().description('一言的最小长度'),
maxLength: Schema.number().description('一言的最大长度'),
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import * as Command from './command'
import { Config } from './config'
import i18n from './i18n'

export const inject = {
optional: ['sentences'],
}

export const name = 'hitokoto'

export { Config, HitokotoApi }
Expand Down

0 comments on commit cf2e521

Please sign in to comment.