Introduce packages/onebot/src/action-kit.ts — a zero-dependency,
type-inferring declarative layer for OneBot action params. Each action
declares its param contract (f.* coercers + optional cross-field rules)
and a `run` handler that receives typed, already-validated params.
Validation failures now return BAD_REQUEST naming the offending field
instead of falling through to ApiHandler's INTERNAL_ERROR catch.
- 141/142 actions migrated to defineAction/groupAction/groupUserAction
across info/message/friend/group-*/request/extended; only
.handle_quick_operation stays legacy (it needs the ApiHandler instance,
which run() does not receive).
- f.messageId() distinguishes signed/non-zero message ids from positive
group/user ids (f.uint()); int({min:0}).default(1800) fixes duration=0
(unmute), which the old `|| 1800` silently swallowed.
- run(p, ctx, raw) exposes the raw params as an escape hatch for the
irregular tail (alias-key fallbacks, arbitrary objects).
- action-docs.ts renders self-generated API docs from describe().
- ApiHandler dispatch / try-catch / response-wrapping unchanged (narrow seam).
Registered action name set is byte-identical before/after (142, verified
by runtime enumeration). Typecheck clean; onebot suite green (the
pre-existing websocket native-addon test failure is unrelated).
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import { defineAction, registerActions, f } from '../action-kit';
|
|
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
|
import { asString } from '../api-handler';
|
|
import { okResponse } from '../types';
|
|
|
|
export const actions = [
|
|
defineAction({
|
|
name: 'set_friend_add_request',
|
|
summary: '处理好友添加请求',
|
|
params: {
|
|
flag: f.string({ allowEmpty: false }),
|
|
approve: f.bool().default(true),
|
|
},
|
|
run: async (p, ctx) => {
|
|
await ctx.bridge.apis.friend.handleRequest(p.flag, p.approve);
|
|
return okResponse();
|
|
},
|
|
}),
|
|
|
|
defineAction({
|
|
name: 'set_group_add_request',
|
|
summary: '处理加群请求',
|
|
params: {
|
|
flag: f.string({ allowEmpty: false }),
|
|
// sub_type/type pass through with a nested fallback (sub_type → type → 'add');
|
|
// kept raw so the original asString resolution is preserved verbatim.
|
|
sub_type: f.raw(),
|
|
type: f.raw(),
|
|
approve: f.bool().default(true),
|
|
reason: f.string().default(''),
|
|
},
|
|
run: async (p, ctx) => {
|
|
const subType = asString(p.sub_type, asString(p.type, 'add'));
|
|
await ctx.handleGroupRequest(p.flag, subType, p.approve, p.reason);
|
|
return okResponse();
|
|
},
|
|
}),
|
|
];
|
|
|
|
export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
|
registerActions(h, ctx, actions);
|
|
}
|