jovo-plugin-class-handler
Allows defining classes which serve as handlers via decorators.
Examples
Handler:
root.handler.ts
or
Table of Contents
- jovo-plugin-class-handler
Getting Started
These instructions will get you the plugin installed and ready to be used.
Prerequisites
- TypeScript v3.8 or newer
- jovo/jovo-framework v3.0 or newer
Installation
Make sure that
experimentalDecorators
andemitDecoratorMetadata
are set totrue
in thetsconfig.json
In your app.ts
:
Handlers previously added by calling
app.setHandler(...)
will be merged with handlers that were loaded by this plugin! It is recommended to not set any handlers.
Configuration
You need to setup a configuration for the plugin in the config.ts
or via the constructor in order for the plugin to detect all handlers.
Example
This configuration assumes that all handlers follow the pattern: (name).handler.(ts or js).
In your config.ts
:
Parts of handlers
can be a string like in the example above (regex is supported), but also a direct import:
Usage
After following the installation the plugin is usable. \
You can find a working example in the github-repository in the example
folder.
Handler
To get started, create a new TypeScript file and export a class and annotate it with @Handler
. \
The class could look like this:
The first parameter of a
@Intent
decorated method is always aJovo
-object if the handler is defined this way.
Handler - State
Additionally you can set the state of the handler:
Handler - alternative way to access the Jovo object
You can also define a handler the following way:
If you define a handler this way, you have access to the
Jovo
-object viathis
.
For more information look at the API here
Intent
After you have defined a handler you can define the intents. For that you have to annotate a method with @Intent
. \
Here is an example:
For more information look at the API here
Data-Decorators
You can decorate @Intent
-annotated methods with parameter decorators that bind data of the Jovo
-object to the corresponding parameter.
Decorator | Binds ... |
---|---|
@Data(key?: string) / @RequestData(key?: string) |
$data / $data.{key} |
@Session(key?: string) / @SessionData(key?: string) |
$session.$data / $session.$data.{key} |
@User(key?: string) / @UserData(key?: string) |
$user.$data / $user.$data.{key} |
@AppData(key?: string) |
$app.$data / $app.$data.{key} |
@InputData(key?: string) |
$inputs / $inputs.{key} |
Example:
API
@Handler(options?: HandlerOptions | string)
HandlerOptions
:{state?: string}
BaseHandler
The BaseHandler
-class is just a wrapper that extends the Jovo
-class and gets it's value injected at runtime. \
If a @Handler
decorated class extends the BaseHandler
all properties and methods of the Jovo
object are accessible via this
.
Parameter options
-
if no
options
: The handler's state will be stateless. -
if
options
of typestring
: The handler's state will be set tooptions
. -
if
options
of typeHandlerOptions
andstate
: The handler's state will be set tooptions.state
. -
if
options
of typeHandlerOptions
and nostate
: The handler will be stateless
@Intent(options?: IntentOptions | string)
IntentOptions
:{name?: string}
Attention! The first parameter always is the
Jovo
-object for a method that is decorated with@Intent
if the handler does not extendBaseHandler
.
Parameter options
-
if no
options
: The intent's name will be the annotated method's name. -
if
options
of typestring
: The intent's name will be set tooptions
. -
if
options
of typeHandlerOptions
andstate
: The intent's name will be set tooptions.name
. -
if
options
of typeHandlerOptions
and nostate
: The intent's name will be the method's name
Data Decorators
The first parameter of a
@Intent
decorated method is reserved for theJovo
-object if the handler-class does not extendBaseHandler
.
@Data(key?: string) / @RequestData(key?: string)
Binds $data
or $data.{key}
if key is given.
@Session(key?: string) / @SessionData(key?: string)
Binds $session.$data
or $session.$data.{key}
if key is given.
@User(key?: string) / @UserData(key?: string)
Binds $user.$data
or $user.$data.{key}
if key is given.
@AppData(key?: string)
Binds $app.$data
or $app.$data.{key}
if key is given.
@InputData(key?: string)
Binds $inputs
or $inputs.{key}
if key is given.