watson-developer-cloud / botkit-middleware

A middleware to connect Watson Conversation Service to different chat channels using Botkit
https://www.npmjs.com/package/botkit-middleware-watson
Apache License 2.0
206 stars 255 forks source link

fix: Use correct types in signatures of before,after and postMessage #185

Closed Naktibalda closed 5 years ago

Naktibalda commented 5 years ago

I tried to add headers to payload using this code:

watsonMiddleware.before = async (message: BotkitMessage, payload: Payload) => {
        payload.headers = {
            'X-Watson-Learning-Opt-Out': 'true'
        };
        return payload;
    }

but compilation failed with:

src/bot.ts:104:17 - error TS2339: Property 'headers' does not exist on type 'Payload'.

104         payload.headers = {

Result of before hook is passed to postMessage which calls conversation.message(payload); https://github.com/watson-developer-cloud/botkit-middleware/blob/v2.1.1/src/index.ts#L162-L163 https://github.com/watson-developer-cloud/botkit-middleware/blob/v2.1.1/src/utils.ts#L79-L84

message method is defined at https://github.com/watson-developer-cloud/node-sdk/blob/v4.2.0/assistant/v1.ts#L98

and it takes MessageParams type as the first argument:

public message(params: AssistantV1.MessageParams, callback?: AssistantV1.Callback<AssistantV1.MessageResponse>): Promise<any> | void {
codecov-io commented 5 years ago

Codecov Report

Merging #185 into master will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #185   +/-   ##
=======================================
  Coverage   77.27%   77.27%           
=======================================
  Files           2        2           
  Lines         110      110           
  Branches       23       23           
=======================================
  Hits           85       85           
  Misses         21       21           
  Partials        4        4

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update f4b86da...a13cd05. Read the comment docs.

Naktibalda commented 5 years ago

I realized that workspace_id is already defined in MessageParams, so there is no need to have Payload interface, but I kept it for backwards compatibility just in case someone is using it in their code.

I added MessageParams and MessageResponse to all places where they are appropriate - after, before and postMessage. Also MessageResponse is a type of watsonData field in BotkitWatsonMessage.

watson-github-bot commented 5 years ago

:tada: This PR is included in version 2.1.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: