Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(middleware): api desgin draft#2293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Gcaufy wants to merge2 commits intomain
base:main
Choose a base branch
Loading
fromfeat-middleware
Open

Conversation

@Gcaufy
Copy link
Contributor

Issue

#2282

Description

It's gonna be a big change, check the design darft, and see if I can move on.

Demo Code

  1. How to define a middleware
// 抽象通过能力为中间件,由中间件控制事件响应const filterMiddleWare = (options) {  return async (message, next) => {    const room = message.room()    if (!room) return     if (message.type() !== option.type) return     if (!matchers.roomMatcher(option.room)) return    await next();  }}
  1. Add middleware to specified event
// Only room a print `banana`bot.on('message',   filterMiddlerWare({ type: type.Message.Text, room: 'room a'}),  (message) => console.log('banana'));// All rooms print `apple` bot.on('message',   (message) => console.log('apple'));
  1. Add global middleware
WechatyImpl.middleware({  message: filterMiddleWare({ type.Message.Text, room: [ 'room-d', 'room-e' ]})})// Only room-d, room-e print apple.bot.on('message',   (message) => console.log('apple'));// only room-d, room-e print orange(50% probability)bot.on('message', async (message, next) => {  if (Math.random() < 0.5) {    await next();  }}, (message) => console.log('orange'))
  1. Work with plugins
WechatyImpl.middleware({  message: filterMiddleWare({ type.Message.Text, room: [ 'room-d', 'room-e' ]})})// Only room-d, room-e has kickoff featureWechatyImpl.use(KickOffPlugin(options));// TODO: use support rest params, so we need to think about how design it.// WechatyImpl.use({ message: someMiddleWare() }, KickOffPlugin(options));
  1. Work in plugins
const onMessage = (message) => {  // plugin logic}export MyPlugin = (bot: WechatyInterface) => {  bot.on('message', [    MiddleWareA(),    MiddleWareB(),    MiddleWareC(),  ], onMessage)}

@CLAassistant
Copy link

CLAassistant commentedNov 5, 2021
edited
Loading

CLA assistant check
All committers have signed the CLA.

TFunction extends (...args: any) => any,
TParameters extends [...args: any]
> = (
...args: [...Parameters<TFunction>, ...TParameters]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

It seems that we can hard code thenext: () => void | Promise<void> at here to replaceTParameters?

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@huanhuanhuan left review comments

At least 3 approving reviews are required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@Gcaufy@CLAassistant@huan

[8]ページ先頭

©2009-2025 Movatter.jp