Skip to content
This repository was archived by the owner on May 28, 2024. It is now read-only.

use conversation type of app in middleware #344

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/service/dialogflow/_test/dialogflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ test('app uses middleware using Object.assign', async t => {
test(): void
}
const middleware: DialogflowMiddleware<
{}, {}, Contexts,
TestMiddleware & DialogflowConversation<{}, {}, Contexts>
> = conv => Object.assign(conv, {
test() {
Expand Down Expand Up @@ -329,6 +330,7 @@ test('app uses async middleware using Object.assign', async t => {
test(): void
}
const middleware: DialogflowMiddleware<
{}, {}, Contexts,
TestMiddleware & DialogflowConversation<{}, {}, Contexts>
> = async conv => Object.assign(conv, {
test() {
Expand Down Expand Up @@ -367,6 +369,7 @@ test('app uses async middleware returning void', async t => {
test(): void
}
const middleware: DialogflowMiddleware<
{}, {}, Contexts,
TestMiddleware & DialogflowConversation<{}, {}, Contexts>
> = async conv => {
(conv as TestMiddleware & DialogflowConversation<{}, {}, Contexts>)
Expand Down Expand Up @@ -404,6 +407,7 @@ test('app uses async middleware returning promise', async t => {
test(): void
}
const middleware: DialogflowMiddleware<
{}, {}, Contexts,
TestMiddleware & DialogflowConversation<{}, {}, Contexts>
> = conv => Promise.resolve(Object.assign(conv, {
test() {
Expand Down Expand Up @@ -444,8 +448,8 @@ test('app gives middleware framework metadata', async t => {
}
const response = 'abcdefg1234567'
let called = false
const middleware: DialogflowMiddleware<DialogflowConversation<{}, {}, Contexts>
> = (conv, framework) => {
const middleware: DialogflowMiddleware<{}, {}, Contexts> =
(conv, framework) => {
called = true
t.is(framework, metadata)
}
Expand Down
29 changes: 22 additions & 7 deletions src/service/dialogflow/dialogflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { Contexts, Parameters } from './context'
import { DialogflowConversation } from './conv'
import { OAuth2Client } from 'google-auth-library'
import { BuiltinFrameworkMetadata } from '../../framework'
import {JsonObject} from '../../common'

/** @public */
export interface DialogflowIntentHandler<
Expand Down Expand Up @@ -79,17 +80,21 @@ export interface DialogflowHandlers<

/** @public */
export interface DialogflowMiddleware<
TConversationPlugin extends DialogflowConversation
TConvData = JsonObject,
TUserStorage = JsonObject,
TContexts extends Contexts = Contexts,
TConversation extends DialogflowConversation<TConvData, TUserStorage, TContexts> =
DialogflowConversation<TConvData, TUserStorage, TContexts>,
> {
(
/** @public */
conv: DialogflowConversation,
conv: TConversation,

/** @public */
framework: BuiltinFrameworkMetadata,
): (DialogflowConversation & TConversationPlugin) |
): TConversation |
void |
Promise<DialogflowConversation & TConversationPlugin> |
Promise<TConversation> |
Promise<void>
}

Expand Down Expand Up @@ -260,11 +265,21 @@ export interface DialogflowApp<
): this

/** @hidden */
_middlewares: DialogflowMiddleware<DialogflowConversation<{}, {}, Contexts>>[]
_middlewares: DialogflowMiddleware<
TConvData,
TUserStorage,
TContexts,
TConversation
>[]

/** @public */
middleware<TConversationPlugin extends DialogflowConversation<{}, {}, Contexts>>(
middleware: DialogflowMiddleware<TConversationPlugin>,
middleware(
middleware: DialogflowMiddleware<
TConvData,
TUserStorage,
TContexts,
TConversation
>,
): this

/** @public */
Expand Down