From 3ca924574c2538e8c3469d09fa4a767e10af3ed3 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sat, 17 Feb 2024 00:12:31 +0800 Subject: [PATCH] refa: migrate undios http response type --- adapters/discord/src/types/internal.ts | 2 +- adapters/kook/src/bot.ts | 3 +-- adapters/kook/src/message.ts | 6 +++--- adapters/lark/src/bot.ts | 3 --- adapters/lark/src/types/internal.ts | 2 +- adapters/line/src/types/internal.ts | 2 +- adapters/qq/src/internal/internal.ts | 2 +- adapters/slack/src/types/internal.ts | 2 +- adapters/zulip/src/bot.ts | 3 +-- adapters/zulip/src/message.ts | 2 +- adapters/zulip/src/types/internal.ts | 4 ++-- 11 files changed, 13 insertions(+), 18 deletions(-) diff --git a/adapters/discord/src/types/internal.ts b/adapters/discord/src/types/internal.ts index cb627fbb..8b26d922 100644 --- a/adapters/discord/src/types/internal.ts +++ b/adapters/discord/src/types/internal.ts @@ -30,7 +30,7 @@ export class Internal { } try { this.bot.logger.debug(`${method} ${url}`, config) - return await this.bot.http(method, url, config) + return (await this.bot.http(method, url, config)).data } catch (error) { if (!Quester.isAxiosError(error) || !error.response) throw error throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`) diff --git a/adapters/kook/src/bot.ts b/adapters/kook/src/bot.ts index 48806551..53541965 100644 --- a/adapters/kook/src/bot.ts +++ b/adapters/kook/src/bot.ts @@ -17,7 +17,6 @@ export class KookBot extends MessageEnco const src = attrs.src || attrs.url if (await this.bot.http.isLocal(src)) { const payload = new FormData() - const result = await this.bot.ctx.http.file(src, attrs) + const result = await this.bot.http.file(src, attrs) payload.append('file', new Blob([result.data], { type: result.mime }), attrs.file || result.filename) - const { url } = await this.bot.request('POST', '/asset/create', payload) + const { data: { url } } = await this.bot.http.post('/asset/create', payload) return url } else if (!src.includes('kookapp.cn')) { const { data, headers } = await this.bot.ctx.http('GET', src, { @@ -63,7 +63,7 @@ export class KookMessageEncoder extends MessageEnco }) const payload = new FormData() payload.append('file', new Blob([data], { type: headers.get('Content-Type') }), 'file') - const { url } = await this.bot.request('POST', '/asset/create', payload) + const { data: { url } } = await this.bot.http.post('/asset/create', payload) return url } else { return src diff --git a/adapters/lark/src/bot.ts b/adapters/lark/src/bot.ts index c865e4cd..327789ee 100644 --- a/adapters/lark/src/bot.ts +++ b/adapters/lark/src/bot.ts @@ -26,9 +26,6 @@ export class LarkBot extends Bot this.selfId = config.appId this.http = ctx.http.extend({ endpoint: config.endpoint, - headers: { - 'Content-Type': 'application/json; charset=utf-8', - }, }) this.assetsQuester = ctx.http this.internal = new Internal(this) diff --git a/adapters/lark/src/types/internal.ts b/adapters/lark/src/types/internal.ts index 69fbcd77..a15e4dad 100644 --- a/adapters/lark/src/types/internal.ts +++ b/adapters/lark/src/types/internal.ts @@ -49,7 +49,7 @@ export class Internal { } else if (args.length > 1) { throw new Error(`too many arguments for ${path}, received ${raw}`) } - return this.processReponse(await this.bot.http(method, url, config)) + return this.processReponse((await this.bot.http(method, url, config)).data) } } } diff --git a/adapters/line/src/types/internal.ts b/adapters/line/src/types/internal.ts index 98f9d5d4..883df466 100644 --- a/adapters/line/src/types/internal.ts +++ b/adapters/line/src/types/internal.ts @@ -28,7 +28,7 @@ export class Internal { throw new Error(`too many arguments for ${path}, received ${raw}`) } try { - return await this.http(method, url, config) + return (await this.http(method, url, config)).data } catch (error) { if (!Quester.isAxiosError(error) || !error.response) throw error throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`) diff --git a/adapters/qq/src/internal/internal.ts b/adapters/qq/src/internal/internal.ts index be68f9b5..7fca44e0 100644 --- a/adapters/qq/src/internal/internal.ts +++ b/adapters/qq/src/internal/internal.ts @@ -29,7 +29,7 @@ export class Internal { } try { this.bot.logger.debug(`${method} ${url} request: %o`, config) - const response = await this.http().axios(url, { ...config, method }) + const response = await this.http()(url, { ...config, method }) this.bot.logger.debug(`${method} ${url} response: %o, trace id: %s`, response.data, response.headers['x-tps-trace-id']) return response.data } catch (error) { diff --git a/adapters/slack/src/types/internal.ts b/adapters/slack/src/types/internal.ts index 80afc1f8..c9a447a4 100644 --- a/adapters/slack/src/types/internal.ts +++ b/adapters/slack/src/types/internal.ts @@ -44,7 +44,7 @@ export class Internal { config.data = args[1] } try { - return await this.http(method, path, config) + return (await this.http(method, path, config)).data } catch (error) { if (!Quester.isAxiosError(error) || !error.response) throw error throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`) diff --git a/adapters/zulip/src/bot.ts b/adapters/zulip/src/bot.ts index 4a01e02c..9974fcda 100644 --- a/adapters/zulip/src/bot.ts +++ b/adapters/zulip/src/bot.ts @@ -18,12 +18,11 @@ export class ZulipBot extends Bot extends MessageEnc form.append('content', this.buffer) if (!this.session.isDirect) form.append('topic', this.session.channelId) - const { id } = await this.bot.http.post('/messages', form) + const { id } = await this.bot.http.post('messages', form) const session = this.bot.session() session.content = this.buffer session.messageId = id.toString() diff --git a/adapters/zulip/src/types/internal.ts b/adapters/zulip/src/types/internal.ts index f16220f4..8b7b64c5 100644 --- a/adapters/zulip/src/types/internal.ts +++ b/adapters/zulip/src/types/internal.ts @@ -10,7 +10,7 @@ export class Internal { for (const name of makeArray(routes[path][method])) { Internal.prototype[name] = async function (this: Internal, ...args: any[]) { const raw = args.join(', ') - const url = path.replace(/\{([^}]+)\}/g, () => { + const url = path.replace(/^\//, '').replace(/\{([^}]+)\}/g, () => { if (!args.length) throw new Error(`too few arguments for ${path}, received ${raw}`) return args.shift() }) @@ -28,7 +28,7 @@ export class Internal { throw new Error(`too many arguments for ${path}, received ${raw}`) } try { - return await this.http(method, url, config) + return (await this.http(method, url, config)).data } catch (error) { if (!Quester.isAxiosError(error) || !error.response) throw error throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`)