Skip to content

Commit

Permalink
refa: migrate Header and Error.is
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 18, 2024
1 parent 6b50991 commit 3224f5d
Show file tree
Hide file tree
Showing 17 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion adapters/dingtalk/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class Internal {
try {
return await quester(method, url, config)
} catch (error) {
if (!Quester.isAxiosError(error) || !error.response) throw error
if (!Quester.Error.is(error) || !error.response) throw error
throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`)
}
}
Expand Down
4 changes: 2 additions & 2 deletions adapters/discord/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE

return message
} catch (e) {
if (Quester.isAxiosError(e) && e.response) {
if (Quester.Error.is(e) && e.response) {
if (e.response.data?.code === 10015) {
this.bot.logger.debug('webhook has been deleted, recreating..., %o', e.response.data)
if (!this.bot.webhookLock[this.channelId]) this.bot.webhooks[this.channelId] = null
Expand Down Expand Up @@ -133,7 +133,7 @@ export class DiscordMessageEncoder<C extends Context = Context> extends MessageE
headers: { accept: type + '/*' },
timeout: 1000,
}).then(
(headers) => headers['content-type'].startsWith(type),
(headers) => headers.get('content-type').startsWith(type),
() => false,
)
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/discord/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Internal {
this.bot.logger.debug(`${method} ${url}`, config)
return (await this.bot.http(method, url, config)).data
} catch (error) {
if (!Quester.isAxiosError(error) || !error.response) throw error
if (!Quester.Error.is(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/lark/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, FeishuBo
})

ctx.status = 200
ctx.response.headers['Content-Type'] = resp.headers['content-type']
ctx.response.headers['Content-Type'] = resp.headers.get('content-type')
ctx.response.body = resp.data
})
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/lark/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class LarkMessageEncoder<C extends Context = Context> extends MessageEnco
this.results.push(session.event.message)
} catch (e) {
// try to extract error message from Lark API
if (Quester.isAxiosError(e)) {
if (Quester.Error.is(e)) {
if (e.response?.data?.code) {
const generalErrorMsg = `Check error code at https://open.larksuite.com/document/server-docs/getting-started/server-error-codes`
e.message += ` (Lark error code ${e.response.data.code}: ${e.response.data.msg ?? generalErrorMsg})`
Expand Down
4 changes: 2 additions & 2 deletions adapters/line/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, LineBot<
method: 'GET',
responseType: 'stream',
})
ctx.type = resp.headers['content-type']
ctx.set('cache-control', resp.headers['cache-control'])
ctx.type = resp.headers.get('content-type')
ctx.set('cache-control', resp.headers.get('cache-control'))
ctx.response.body = resp.data
ctx.status = 200
})
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 @@ -30,7 +30,7 @@ export class Internal {
try {
return (await this.http(method, url, config)).data
} catch (error) {
if (!Quester.isAxiosError(error) || !error.response) throw error
if (!Quester.Error.is(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/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class QQBot<C extends Context = Context> extends Bot<C, QQBot.Config> {
this._ensureAccessToken()
}, (result.data.expires_in - 40) * 1000)
} catch (e) {
if (!Quester.isAxiosError(e) || !e.response) throw e
if (!Quester.Error.is(e) || !e.response) throw e
this.logger.warn(`POST https://bots.qq.com/app/getAppAccessToken response: %o, trace id: %s`, e.response.data, e.response.headers.get('x-tps-trace-id'))
throw e
}
Expand Down
6 changes: 3 additions & 3 deletions adapters/qq/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class QQGuildMessageEncoder<C extends Context = Context> extends MessageE
else r = await this.bot.internal.sendMessage(this.channelId, payload)
}
} catch (e) {
if (Quester.isAxiosError(e)) {
if (Quester.Error.is(e)) {
if (this.bot.parent.config.retryWhen.includes(e.response.data.code) && !this.retry && this.fileUrl) {
this.bot.logger.warn('retry image sending')
this.retry = true
Expand Down Expand Up @@ -265,7 +265,7 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
}
}
} catch (e) {
if (!Quester.isAxiosError(e)) throw e
if (!Quester.Error.is(e)) throw e
this.errors.push(e)
if (!this.retry && this.bot.config.retryWhen.includes(e.response.data.code)) {
this.bot.logger.warn('%s retry message sending', this.session.cid)
Expand Down Expand Up @@ -329,7 +329,7 @@ export class QQMessageEncoder<C extends Context = Context> extends MessageEncode
res = await this.bot.internal.sendFileGuild(this.session.guildId, data)
}
} catch (e) {
if (!Quester.isAxiosError(e)) throw e
if (!Quester.Error.is(e)) throw e
this.errors.push(e)
if (!this.retry && this.bot.config.retryWhen.includes(e.response.data.code)) {
this.bot.logger.warn('%s retry message sending', this.session.cid)
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 @@ -46,7 +46,7 @@ export class Internal {
try {
return (await this.http(method, path, config)).data
} catch (error) {
if (!Quester.isAxiosError(error) || !error.response) throw error
if (!Quester.Error.is(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/telegram/src/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class HttpPolling<C extends Context = Context> extends Adapter<C, Telegra
}
this.timeout = setTimeout(polling, 0)
} catch (e) {
if (!Quester.isAxiosError(e) || !e.response?.data) {
if (!Quester.Error.is(e) || !e.response?.data) {
// Other error
bot.logger.warn('failed to get updates. reason: %s', e.message)
} else {
Expand Down
6 changes: 3 additions & 3 deletions adapters/wechat-official/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, WechatOf
media_id: mediaId,
},
})
ctx.type = resp.headers['content-type']
ctx.set('date', resp.headers['date'])
ctx.set('cache-control', resp.headers['cache-control'])
ctx.type = resp.headers.get('content-type')
ctx.set('date', resp.headers.get('date'))
ctx.set('cache-control', resp.headers.get('cache-control'))
ctx.response.body = resp.data
ctx.status = 200
})
Expand Down
6 changes: 3 additions & 3 deletions adapters/wecom/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ export class HttpServer<C extends Context = Context> extends Adapter<C, WecomBot
media_id: mediaId,
},
})
ctx.type = resp.headers['content-type']
ctx.set('date', resp.headers['date'])
ctx.set('cache-control', resp.headers['cache-control'])
ctx.type = resp.headers.get('content-type')
ctx.set('date', resp.headers.get('date'))
ctx.set('cache-control', resp.headers.get('cache-control'))
ctx.response.body = resp.data
ctx.status = 200
})
Expand Down
4 changes: 2 additions & 2 deletions adapters/whatsapp/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class HttpServer {
method: 'GET',
responseType: 'stream',
})
ctx.type = resp.headers['content-type']
ctx.set('cache-control', resp.headers['cache-control'])
ctx.type = resp.headers.get('content-type')
ctx.set('cache-control', resp.headers.get('cache-control'))
ctx.response.body = resp.data
ctx.status = 200
})
Expand Down
2 changes: 1 addition & 1 deletion adapters/zulip/src/polling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class HttpPolling<C extends Context = Context> extends Adapter<C, ZulipBo
}
setTimeout(polling, 0)
} catch (e) {
if (!Quester.isAxiosError(e) || !e.response?.data) {
if (!Quester.Error.is(e) || !e.response?.data) {
bot.logger.warn('failed to get updates. reason: %s', e.stack)
} else {
bot.logger.error(e.stack)
Expand Down
2 changes: 1 addition & 1 deletion adapters/zulip/src/types/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Internal {
try {
return (await this.http(method, url, config)).data
} catch (error) {
if (!Quester.isAxiosError(error) || !error.response) throw error
if (!Quester.Error.is(error) || !error.response) throw error
throw new Error(`[${error.response.status}] ${JSON.stringify(error.response.data)}`)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/server-proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ProxyServer {
try {
koa.body = await ctx.http.get<internal.Readable>(koa.params.url, { responseType: 'stream' })
} catch (error) {
if (!Quester.isAxiosError(error) || !error.response) throw error
if (!Quester.Error.is(error) || !error.response) throw error
koa.status = error.response.status
koa.body = error.response.data
}
Expand Down

0 comments on commit 3224f5d

Please sign in to comment.