Skip to content

Commit

Permalink
refa: migrate undios http response type
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 16, 2024
1 parent f60ca8c commit 3ca9245
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 18 deletions.
2 changes: 1 addition & 1 deletion adapters/discord/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`)
Expand Down
3 changes: 1 addition & 2 deletions adapters/kook/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class KookBot<C extends Context = Context, T extends KookBot.Config = Koo
this.http = ctx.http.extend({
headers: {
'Authorization': `Bot ${config.token}`,
'Content-Type': 'application/json',
},
}).extend(config)
this.internal = new Kook.Internal(this.http)
Expand All @@ -33,7 +32,7 @@ export class KookBot<C extends Context = Context, T extends KookBot.Config = Koo
if (method === 'GET') {
return (await this.http.get(path, { params: data, headers })).data
} else {
return (await this.http(method, path, { data, headers })).data
return (await this.http(method, path, { data, headers })).data?.data
}
}

Expand Down
6 changes: 3 additions & 3 deletions adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ export class KookMessageEncoder<C extends Context = Context> 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<ArrayBuffer>('GET', src, {
Expand All @@ -63,7 +63,7 @@ export class KookMessageEncoder<C extends Context = Context> 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
Expand Down
3 changes: 0 additions & 3 deletions adapters/lark/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export class LarkBot<C extends Context = Context> extends Bot<C, LarkBot.Config>
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)
Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/line/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`)
Expand Down
2 changes: 1 addition & 1 deletion adapters/qq/src/internal/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion adapters/slack/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`)
Expand Down
3 changes: 1 addition & 2 deletions adapters/zulip/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ export class ZulipBot<C extends Context = Context> extends Bot<C, ZulipBot.Confi
this.http = ctx.http.extend({
headers: {
Authorization: `Basic ${Buffer.from(`${config.email}:${config.key}`).toString('base64')}`,
'content-type': 'application/x-www-form-urlencoded',
'user-agent': `Koishi/${version}`,
},
}).extend({
...config,
endpoint: config.endpoint + '/api/v1',
baseURL: config.endpoint + '/api/v1/',
})
this.internal = new Internal(this.http)

Expand Down
2 changes: 1 addition & 1 deletion adapters/zulip/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ZulipMessageEncoder<C extends Context = Context> 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()
Expand Down
4 changes: 2 additions & 2 deletions adapters/zulip/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand All @@ -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)}`)
Expand Down

0 comments on commit 3ca9245

Please sign in to comment.