Skip to content

Commit e36cf88

Browse files
authored
botonic-core: infer type from Route to matcher (#3027)
## Description To be able to infer the type of the plugins and the user.extra_data to the route matchers, it is necessary to infer the type first to Route ## To document / Usage example Example of how to infer the type through the Route. With this change you have de request.plugins with the type of BotPlugins and request.session.user.extra_data with the type of UserData inside the matchers. ```typescript import { Route } from '@botonic/core' import { UserData } from './domain/user-data' import { BotPlugins } from './plugins' import { BotRequest } from './types' export function routes(request: BotRequest): Route<BotPlugins, UserData>[] { return [ { path: 'welcome-action', payload: WELCOME_PAYLOAD, request: (request: BotRequest) => { console.log('WELCOME_ACTION', request.plugins) return request.session.is_first_interaction }, action: WelcomeAction, }, ... ```
1 parent c72141f commit e36cf88

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/botonic-core/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ All notable changes to Botonic will be documented in this file.
2020

2121
</details>
2222

23+
## [0.35.2] - 2025-05-27
24+
25+
### Fixed
26+
27+
- [PR-3027](https://github.com/hubtype/botonic/pull/3027): Infer type from Route to matchers
28+
2329
## [0.35.1] - 2025-05-26
2430

2531
### Fixed

packages/botonic-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@botonic/core",
3-
"version": "0.35.1",
3+
"version": "0.35.2",
44
"license": "MIT",
55
"description": "Build Chatbots using React",
66
"main": "./lib/cjs/index.js",

packages/botonic-core/src/models/legacy-types.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,13 @@ export type InputMatcher = (input: Input) => boolean
280280
export type ParamsMatcher =
281281
| { [key: string]: string }
282282
| ((params: { [key: string]: string }) => boolean)
283-
export type SessionMatcher = (session: Session) => boolean
284-
export type RequestMatcher = (request: BotContext) => boolean
283+
export type SessionMatcher<TExtraData = any> = (
284+
session: Session<TExtraData>
285+
) => boolean
286+
export type RequestMatcher<
287+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
288+
TExtraData = any,
289+
> = (request: BotContext<TPlugins, TExtraData>) => boolean
285290
export type StringMatcher = RegExp | string | ((data: string) => boolean)
286291

287292
export type RouteMatcher =
@@ -291,7 +296,10 @@ export type RouteMatcher =
291296
| SessionMatcher
292297
| StringMatcher
293298

294-
export interface Route {
299+
export interface Route<
300+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
301+
TExtraData = any,
302+
> {
295303
action?: any
296304
childRoutes?: Route[]
297305
lastRoutePath?: string
@@ -305,14 +313,14 @@ export interface Route {
305313
intent?: StringMatcher
306314
params?: ParamsMatcher
307315
payload?: StringMatcher
308-
request?: RequestMatcher
309-
session?: SessionMatcher
316+
request?: RequestMatcher<TPlugins, TExtraData>
317+
session?: SessionMatcher<TExtraData>
310318
text?: StringMatcher
311319
data?: StringMatcher
312320
type?: StringMatcher
313321
}
314322

315-
export type Routes<R = Route> = R[] | ((_: BotContext) => R[])
323+
export type Routes<R = Route> = R[] | ((request: BotContext) => R[])
316324

317325
export interface BotRequest<TExtraData = any> {
318326
input: Input

0 commit comments

Comments
 (0)