Skip to content

Commit 3776cd1

Browse files
Bill7zzStevenicstevengum
authored
[Webpack][ISSUE#818][BotBuilder-AI][BF-Connector] Fix webpack dependency warning (#1540)
* Add getFetch function and tests to botbuilder-ai * Add setGlobals function and tests to bf-connector * Update getFetch function * Update set globals apis from bf-connector * Add Copyright header to BF-Connector index file * Update global functions to set fetch and formData * Remove dom from botbuilder-ai tsconfig * Add Copyright header Co-authored-by: Steven Ickman <stevenic@microsoft.com> Co-authored-by: Steven Gum <14935595+stevengum@users.noreply.github.com>
1 parent aba7431 commit 3776cd1

File tree

7 files changed

+98
-3
lines changed

7 files changed

+98
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* @module botbuilder-ai
3+
*/
4+
/**
5+
* Copyright (c) Microsoft Corporation. All rights reserved.
6+
* Licensed under the MIT License.
7+
*/
8+
9+
declare global {
10+
interface Window {}
11+
}
12+
13+
export interface window extends Window {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
2+
/**
3+
* @module botbuilder
4+
*/
5+
/**
6+
* Copyright (c) Microsoft Corporation. All rights reserved.
7+
* Licensed under the MIT License.
8+
*/
9+
10+
const window = require('./custom.window');
11+
12+
export function getFetch() {
13+
const env = (global || window) as any;
14+
15+
if (!env.hasOwnProperty('fetch')) {
16+
env.fetch = require('node-fetch');
17+
}
18+
return env.fetch;
19+
}

libraries/botbuilder-ai/src/qnamaker-utils/httpRequestUtils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
import * as os from 'os';
1010
const pjson: any = require('../../package.json');
11-
const request = (new Function('require', 'if (!this.hasOwnProperty("fetch")) { return require("node-fetch"); } else { return this.fetch; }'))(require);
1211

1312
import { QnAMakerEndpoint } from '../qnamaker-interfaces/qnamakerEndpoint';
1413

14+
import { getFetch } from '../globals';
15+
const fetch = getFetch();
16+
1517
/**
1618
* Http request utils class.
1719
*
@@ -43,7 +45,7 @@ export class HttpRequestUtils {
4345

4446
const headers: any = this.getHeaders(endpoint);
4547

46-
const qnaResult: any = await request(requestUrl, {
48+
const qnaResult: any = await fetch(requestUrl, {
4749
method: 'POST',
4850
headers: headers,
4951
timeout: timeout,

libraries/botbuilder-ai/tests/qnaMaker.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { TestAdapter, TurnContext, NullTelemetryClient } = require('botbuilder-co
44
const { QnAMaker } = require('../');
55
const nock = require('nock');
66
const fs = require('fs');
7+
const { getFetch } = require('../lib/globals');
78

89
// Save test keys
910
const knowledgeBaseId = process.env.QNAKNOWLEDGEBASEID;
@@ -747,6 +748,25 @@ describe('QnAMaker', function () {
747748
assert.strictEqual(qnaResults, descendingQnaResults, 'answers should be sorted from greatest to least score');
748749
});
749750
});
751+
752+
describe('getFetch()', function(){
753+
754+
it('Should return fetch instance from global', function(){
755+
global.fetch = function() { return 'global fetch fake'; };
756+
757+
const fetch = getFetch();
758+
assert.strictEqual('global fetch fake', fetch());
759+
});
760+
761+
it('Should set fetch API if it does not exist', function(){
762+
if (global.fetch) {
763+
delete global['fetch'];
764+
}
765+
766+
const fetch = getFetch();
767+
assert(typeof fetch === 'function');
768+
});
769+
});
750770
});
751771

752772
class overrideTwoEventsWithOverrideLogger extends QnAMaker
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @module botbuilder
3+
*/
4+
/**
5+
* Copyright (c) Microsoft Corporation. All rights reserved.
6+
* Licensed under the MIT License.
7+
*/
8+
9+
// set FormData and Fetch as global functions
10+
function setGlobals() {
11+
const env = (global || window) as any;
12+
13+
if (!env.hasOwnProperty("FormData")) {
14+
env.FormData = require("form-data");
15+
}
16+
17+
if (!env.hasOwnProperty("fetch")) {
18+
env.fetch = require("node-fetch");
19+
}
20+
}
21+
22+
setGlobals();

libraries/botframework-connector/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
(new Function('require', 'if (!this.hasOwnProperty("FormData")) { this.FormData = require("form-data"); }; if (!this.hasOwnProperty("fetch")) { this.fetch = require("node-fetch"); }'))(require);
1+
/**
2+
* @module botbuilder
3+
*/
4+
/**
5+
* Copyright (c) Microsoft Corporation. All rights reserved.
6+
* Licensed under the MIT License.
7+
*/
28

39
import { TokenResponse } from './connectorApi/models/mappers';
10+
import './globals'
411

512
/**
613
* @module botbuilder

libraries/botframework-connector/tests/connector.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,3 +596,15 @@ describe('Bot Framework Connector SDK', function() {
596596
});
597597
});
598598
});
599+
600+
describe('setGlobals()', function(){
601+
602+
before(function(){
603+
require('../lib/globals');
604+
});
605+
606+
it('Should return fetch and FormData as global functions',async function(){
607+
assert(typeof global.fetch === 'function');
608+
assert(typeof global.FormData === 'function');
609+
});
610+
});

0 commit comments

Comments
 (0)