Skip to content

Commit

Permalink
feat: hide api url when using sentences (#32)
Browse files Browse the repository at this point in the history
* feat: hide api url when using sentencts

* fix: declare config

* style: remove redundant declare

Co-authored-by: Maiko Sinkyaet Tan <maiko.tan.coding@gmail.com>

---------

Co-authored-by: Maiko Sinkyaet Tan <maiko.tan.coding@gmail.com>
  • Loading branch information
SaarChaffee and MaikoTan committed Mar 11, 2024
1 parent be240bc commit 138caa8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ declare module 'koishi' {

export class HitokotoApi extends Service {
private _apiUrl: string
config: Config

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

async getHitokoto(params: SentencesParams): Promise<HitokotoRet> {
Expand Down
24 changes: 17 additions & 7 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@ export interface Config {
defaultTypes?: string[]
}

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('一言的最大长度'),
defaultTypes: Schema.array(Schema.string()).description('默认一言类别'),
})
export const Config: Schema<Config> = Schema.intersect([
Schema.object({
sentences: Schema.boolean().description('是否使用本地一言语料库(需要安装 sentences 服务)').default(false),
}),
Schema.union([
Schema.object({
sentences: Schema.const(false),
apiUrl: Schema.string().description('获取一言的 API 地址').default('https://v1.hitokoto.cn'),
}),
Schema.object({}),
]),
Schema.object({
minLength: Schema.number().description('一言的最小长度'),
maxLength: Schema.number().description('一言的最大长度'),
defaultTypes: Schema.array(Schema.string()).description('默认一言类别'),
}),
])

0 comments on commit 138caa8

Please sign in to comment.