Skip to content

Commit

Permalink
feat(core): root-level selfUrl config (updated utils, tg, khl)
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 24, 2021
1 parent 9e962dc commit 967d3f5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
8 changes: 2 additions & 6 deletions packages/adapter-kaiheila/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import { App, Bot, Adapter } from 'koishi-core'
import { Logger, assertProperty, omit } from 'koishi-utils'
import { Logger, assertProperty, omit, trimSlash, sanitize } from 'koishi-utils'
import { KaiheilaBot } from './bot'
import { adaptSession } from './utils'

const logger = new Logger('kaiheila')

function trimSlash(source: string) {
return source.replace(/\/$/, '')
}

export default class HttpServer extends Adapter<'kaiheila'> {
constructor(app: App) {
assertProperty(app.options, 'port')
super(app, KaiheilaBot)
const config = this.app.options.kaiheila ||= {}
config.path ||= '/kaiheila'
config.path = sanitize(config.path || '/kaiheila')
config.endpoint = trimSlash(config.endpoint || 'https://www.kaiheila.cn/api/v3')
}

Expand Down
16 changes: 8 additions & 8 deletions packages/adapter-telegram/src/http.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import axios from 'axios'
import { App, Adapter, Session } from 'koishi-core'
import { assertProperty, camelCase, Logger, segment } from 'koishi-utils'
import { assertProperty, camelCase, Logger, segment, trimSlash, sanitize } from 'koishi-utils'
import { TelegramBot } from './bot'
import * as Telegram from './types'

const logger = new Logger('telegram')

function trimSlash(source: string) {
return source.replace(/\/$/, '')
}

export default class HttpServer extends Adapter<'telegram'> {
constructor(app: App) {
super(app, TelegramBot)
const config = this.app.options.telegram ||= {}
config.path ||= '/telegram'
const config = app.options.telegram ||= {}
config.path = sanitize(config.path || '/telegram')
config.endpoint = trimSlash(config.endpoint || 'https://api.telegram.org')
config.selfUrl = trimSlash(assertProperty(config, 'selfUrl'))
if (config.selfUrl) {
config.selfUrl = trimSlash(config.selfUrl)
} else {
config.selfUrl = assertProperty(app.options, 'selfUrl')
}
}

async start() {
Expand Down
4 changes: 3 additions & 1 deletion packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp, makeArray, noop, template, merge } from 'koishi-utils'
import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp, makeArray, noop, template, trimSlash, merge } from 'koishi-utils'
import { Context, Middleware, NextFunction, Plugin } from './context'
import { Argv } from './parser'
import { BotOptions, Adapter, createBots } from './adapter'
Expand Down Expand Up @@ -35,6 +35,7 @@ export interface AppOptions extends BotOptions {
channelCacheLength?: number
channelCacheAge?: number
minSimilarity?: number
selfUrl?: string
axiosConfig?: AxiosRequestConfig
}

Expand Down Expand Up @@ -83,6 +84,7 @@ export class App extends Context {
constructor(options: AppOptions = {}) {
super(() => true)
if (!options.bots) options.bots = [options]
if (options.selfUrl) options.selfUrl = trimSlash(options.selfUrl)
this.options = merge(options, App.defaultConfig)
this.registry.set(null, {
parent: null,
Expand Down
9 changes: 9 additions & 0 deletions packages/koishi-utils/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ export function escapeRegExp(source: string) {
.replace(/-/g, '\\x2d')
}

export function trimSlash(source: string) {
return source.replace(/\/$/, '')
}

export function sanitize(source: string) {
if (!source.startsWith('/')) source = '/' + source
return trimSlash(source)
}

export function template(path: string | string[], ...params: any[]) {
if (!Array.isArray(path)) path = [path]
for (const item of path) {
Expand Down

0 comments on commit 967d3f5

Please sign in to comment.