Skip to content

Commit e251389

Browse files
committed
Enhancer must be of type function
1 parent f66c541 commit e251389

2 files changed

Lines changed: 17 additions & 21 deletions

File tree

packages/middleware/src/private/templatePolyMiddleware.check.test.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,10 @@ test('should warn if middleware is not an array of function', () => {
1414
expect(warn).toHaveBeenNthCalledWith(1, expect.stringContaining('must be an array of function'));
1515
});
1616

17-
test('should warn if middleware did not return function', () => {
18-
const warn = jest.fn();
17+
test('should throw if middleware is not a function', () => {
1918
const template = templatePolyMiddleware('Check' as any);
2019

21-
jest.spyOn(console, 'warn').mockImplementation(warn);
22-
23-
template.extractMiddleware([() => 1 as any]);
24-
25-
expect(warn).toHaveBeenCalledTimes(1);
26-
expect(warn).toHaveBeenNthCalledWith(1, expect.stringContaining('must return enhancer function'));
20+
expect(() => template.createMiddleware(1 as any)).toThrow('enhancer must be of type function');
2721
});
2822

2923
test('should not warn if middleware return false', () => {
@@ -32,7 +26,7 @@ test('should not warn if middleware return false', () => {
3226

3327
jest.spyOn(console, 'warn').mockImplementation(warn);
3428

35-
template.extractMiddleware([() => false as any]);
29+
template.extractMiddleware([template.createMiddleware(() => false as any)]);
3630

3731
expect(warn).toHaveBeenCalledTimes(0);
3832
});
@@ -43,7 +37,7 @@ test('should not warn if middleware return function', () => {
4337

4438
jest.spyOn(console, 'warn').mockImplementation(warn);
4539

46-
template.extractMiddleware([() => () => 1 as any]);
40+
template.extractMiddleware([template.createMiddleware(() => () => 1 as any)]);
4741

4842
expect(warn).toHaveBeenCalledTimes(0);
4943
});

packages/middleware/src/private/templatePolyMiddleware.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
type ProviderProps,
1212
type ProxyProps
1313
} from 'react-chain-of-responsibility/preview';
14-
import { array, check, function_, pipe, safeParse, type InferOutput } from 'valibot';
14+
import { array, check, function_, parse, pipe, safeParse, type InferOutput } from 'valibot';
1515

1616
const arrayOfFunctionSchema = array(function_());
1717

@@ -37,17 +37,21 @@ function templatePolyMiddleware<Request, Props extends {}>(name: string) {
3737

3838
const middlewareSchema = pipe(
3939
function_(),
40-
check(value => middlewareFactoryMarker in value)
40+
check(value => value === BYPASS_ENHANCER || middlewareFactoryMarker in value)
4141
);
4242

4343
const createMiddleware = (enhancer: TemplatedEnhancer): TemplatedMiddleware => {
44-
const factory: TemplatedMiddleware = init => (init === name ? enhancer : BYPASS_ENHANCER);
44+
parse(function_(`botframework-webchat: ${name} enhancer must be of type function.`), enhancer);
45+
46+
// Clone the enhancer function and add a marker.
47+
const markedEnhancer = enhancer.bind(undefined);
4548

4649
// This is for checking if the middleware is created via factory function or not.
4750
// We enforce middleware to be created using factory function.
51+
(markedEnhancer as any)[middlewareFactoryMarker satisfies symbol] = undefined;
4852

49-
// TODO: Consider using valibot for validation, plus using "Symbol in object" check.
50-
(factory as any)[middlewareFactoryMarker satisfies symbol] = middlewareFactoryMarker;
53+
// TODO: [P*] Remove one-use.
54+
const factory: TemplatedMiddleware = init => (init === name ? markedEnhancer : BYPASS_ENHANCER);
5155

5256
return factory;
5357
};
@@ -62,17 +66,15 @@ function templatePolyMiddleware<Request, Props extends {}>(name: string) {
6266
return Object.freeze(
6367
middleware
6468
.map(middleware => {
65-
if (!safeParse(middlewareSchema, middleware).success) {
66-
console.warn(`botframework-webchat: ${name}.middleware must be created using its factory function`);
67-
68-
return false;
69-
}
70-
7169
const result = middleware(name);
7270

7371
if (typeof result !== 'function' && result !== false) {
7472
console.warn(`botframework-webchat: ${name}.middleware must return enhancer function or false`);
7573

74+
return false;
75+
} else if (!safeParse(middlewareSchema, result).success) {
76+
console.warn(`botframework-webchat: ${name}.middleware must be created using its factory function`);
77+
7678
return false;
7779
}
7880

0 commit comments

Comments
 (0)