Skip to content

Commit

Permalink
chore: enhance logger scope
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 27, 2023
1 parent 1803f64 commit cad35d9
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 16 deletions.
2 changes: 2 additions & 0 deletions adapters/line/src/types/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rules:
max-len: off
3 changes: 2 additions & 1 deletion adapters/wechat-official/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ export class WechatOfficialBot<C extends Context = Context> extends Bot<C, Wecha
http: Quester
// internal: Internal
refreshTokenTimer: NodeJS.Timeout
logger = new Logger('wo')
logger: Logger

constructor(ctx: C, config: WechatOfficialBot.Config) {
super(ctx, config)
this.logger = ctx.logger('wechat-official')
this.platform = 'wechat-official'
this.selfId = config.account
this.http = ctx.http.extend(config)
Expand Down
3 changes: 2 additions & 1 deletion adapters/wecom/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export class WecomBot<C extends Context = Context> extends Bot<C, WecomBot.Confi
http: Quester
// internal: Internal
refreshTokenTimer: NodeJS.Timeout
logger = new Logger('wecom')
logger: Logger

constructor(ctx: C, config: WecomBot.Config) {
super(ctx, config)
this.logger = ctx.logger('wecom')
this.selfId = config.agentId
this.platform = 'wecom'
this.http = ctx.http.extend(config)
Expand Down
5 changes: 3 additions & 2 deletions adapters/whatsapp/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import crypto from 'crypto'
class HttpServer {
static inject = ['router']

private logger = new Logger('whatsapp')
private logger: Logger
private adapters: WhatsAppAdapter[] = []

constructor(private ctx: Context) {
this.logger = ctx.logger('whatsapp')
// https://developers.facebook.com/docs/graph-api/webhooks/getting-started
// https://developers.facebook.com/docs/graph-api/webhooks/getting-started/webhooks-for-whatsapp/
ctx.router.post('/whatsapp', async (ctx) => {
Expand Down Expand Up @@ -97,7 +98,7 @@ class HttpServer {

export class WhatsAppAdapter<C extends Context = Context> extends Adapter<C, WhatsAppBot<C>> {
static inject = ['router']
static schema = true
static schema = true as any
static reusable = true

constructor(ctx: C, public config: WhatsAppAdapter.Config) {
Expand Down
2 changes: 1 addition & 1 deletion adapters/whatsapp/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface SendTextMessage extends SendMessageBase {
}

export interface Button {
type: "reply"
type: 'reply'
title: string
id: string
}
Expand Down
2 changes: 1 addition & 1 deletion adapters/whatsapp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function decodeMessage(bot: WhatsAppBot, entry: Entry) {
} else if (message.type === 'interactive' && message.interactive.type === 'button_reply') {
session.type = 'interaction/button'
session.event.button = {
id: message.interactive.button_reply.id
id: message.interactive.button_reply.id,
}
} else {
continue
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/core",
"description": "Core components of Satorijs",
"version": "4.0.0",
"version": "3.1.0",
"main": "lib/index.cjs",
"module": "lib/index.mjs",
"typings": "lib/index.d.ts",
Expand Down Expand Up @@ -37,11 +37,11 @@
"dependencies": {
"@satorijs/element": "^2.5.1",
"@satorijs/protocol": "^1.1.1",
"cordis": "^3.0.2",
"cordis": "^3.1.1",
"cordis-axios": "^4.0.1",
"cosmokit": "^1.5.1",
"ws": "^8.14.2",
"reggol": "^1.6.0",
"reggol": "^1.6.3",
"schemastery": "^3.14.0"
}
}
11 changes: 7 additions & 4 deletions packages/router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import KoaRouter from '@koa/router'
import Koa from 'koa'
import { listen } from './listen'

const logger = new Logger('app')

declare module 'koa' {
// koa-bodyparser
interface Request {
Expand Down Expand Up @@ -60,8 +58,11 @@ export class Router extends KoaRouter {
public host!: string
public port!: number

private logger: Logger

constructor(ctx: Context, public config: Router.Config) {
super()
this.logger = ctx.logger('router')

// create server
const koa = new Koa()
Expand Down Expand Up @@ -95,11 +96,13 @@ export class Router extends KoaRouter {
this.host = host
this.port = await listen(config)
this._http.listen(this.port, host)
logger.info('server listening at %c', this.selfUrl)
this.logger.info('server listening at %c', this.selfUrl)
}, true)

ctx.on('dispose', () => {
logger.info('http server closing')
if (config.port) {
this.logger.info('http server closing')
}
this._ws?.close()
this._http?.close()
})
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { camelCase, Context, Logger, Schema, Session, snakeCase, Time, Universal } from '@satorijs/satori'
import { camelCase, Context, Schema, Session, snakeCase, Time, Universal } from '@satorijs/satori'
import {} from '@satorijs/router'
import WebSocket from 'ws'

const logger = new Logger('server')

export const name = 'server'
export const inject = ['router']

Expand Down Expand Up @@ -65,6 +63,8 @@ function transformKey(source: any, callback: (key: string) => string) {
}

export function apply(ctx: Context, config: Config) {
const logger = ctx.logger('server')

ctx.router.get(config.path + '/v1(/.+)*', async (koa) => {
koa.body = 'Please use POST method to send requests.'
koa.status = 405
Expand Down

0 comments on commit cad35d9

Please sign in to comment.