Skip to content

Commit

Permalink
refactor(types): Remove generic state handler type and fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
MrOrz committed Nov 12, 2023
1 parent 20ae085 commit 9c83fb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 59 deletions.
54 changes: 3 additions & 51 deletions src/types/chatbotState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Message, MessageEvent, PostbackEvent } from '@line/bot-sdk';
import type { Message, MessageEvent } from '@line/bot-sdk';

export type ChatbotState =
| '__INIT__'
Expand All @@ -9,38 +9,6 @@ export type ChatbotState =
| 'ASKING_ARTICLE_SUBMISSION_CONSENT'
| 'Error';

/**
* Dummy event, used exclusively when calling handler from another handler
*/
type ServerChooseEvent = {
type: 'server_choose';
};

/**
* Parameters that are added by handleInput.
*
* @todo: We should consider using value from authentic event instead of manually adding fields.
*/
type ArgumentedEventParams = {
/**
* The text in text message, or value from payload in actions.
*/
input: string;
};

export type ChatbotEvent = (
| MessageEvent
| ServerChooseEvent
/**
* A special format of postback that Chatbot actually uses: postback + input (provided in `ArgumentedEventParams`)
* @FIXME Replace with original PostbackEvent and parse its action to support passing more thing than a string
*/
| {
type: 'postback';
}
) &
ArgumentedEventParams;

export type Context = {
/** Used to differientiate different search sessions (searched text or media) */
sessionId: number;
Expand All @@ -58,27 +26,11 @@ export type Context = {
}
);

export type ChatbotStateHandlerParams = {
/** Record<string, never> is for empty object and it's the default parameter in handleInput and handlePostback */
data: Context | Record<string, never>;
state: ChatbotState;
event: ChatbotEvent;
userId: string;
export type ChatbotStateHandlerReturnType = {
data: Context;
replies: Message[];
};

export type ChatbotStateHandlerReturnType = Pick<
ChatbotStateHandlerParams,
'data' | 'replies'
>;

/**
* Generic handler type for function under src/webhook/handlers
*/
export type ChatbotStateHandler = (
params: ChatbotStateHandlerParams
) => Promise<ChatbotStateHandlerReturnType>;

/**
* The data that postback action stores as JSON.
*
Expand Down
17 changes: 11 additions & 6 deletions src/webhook/__tests__/handlePostback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ it('invokes state handler specified by event.postbackHandlerState', async () =>
] as const) {
expectedHandler.mockImplementationOnce(() => {
return Promise.resolve({
// Bare minimal return values by handlers for handlePostback to work without crash
data: {},
data: { sessionId: 0, searchedText: '' },
replies: [],
} as ChatbotStateHandlerReturnType);
});
Expand All @@ -109,7 +108,7 @@ describe('defaultState', () => {
const data: Context = { sessionId: FIXED_DATE, searchedText: '' };
defaultState.mockImplementationOnce(() => {
return {
data: {},
data: { sessionId: 0, searchedText: '' },
replies: [],
};
});
Expand All @@ -127,7 +126,10 @@ describe('defaultState', () => {
).resolves.toMatchInlineSnapshot(`
Object {
"context": Object {
"data": Object {},
"data": Object {
"searchedText": "",
"sessionId": 0,
},
},
"replies": Array [],
}
Expand Down Expand Up @@ -231,7 +233,7 @@ describe('tutorial', () => {

tutorial.mockImplementationOnce(() => {
return {
data: {},
data: { sessionId: 0, searchedText: '' },
replies: [],
};
});
Expand All @@ -245,7 +247,10 @@ describe('tutorial', () => {
).resolves.toMatchInlineSnapshot(`
Object {
"context": Object {
"data": Object {},
"data": Object {
"searchedText": "",
"sessionId": 0,
},
},
"replies": Array [],
}
Expand Down
3 changes: 1 addition & 2 deletions src/webhook/handlers/defaultState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Message } from '@line/bot-sdk';
import {
ChatbotStateHandlerParams,
ChatbotPostbackHandlerParams,
ChatbotStateHandlerReturnType,
} from 'src/types/chatbotState';

export default function defaultState(
params: ChatbotPostbackHandlerParams | ChatbotStateHandlerParams
params: ChatbotPostbackHandlerParams
): ChatbotStateHandlerReturnType {
const replies: Message[] = [
{
Expand Down

0 comments on commit 9c83fb4

Please sign in to comment.