Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Commit e06ad47

Browse files
authored
Replacing node-fetch with axios to get proxy detection (#1036)
* Replacing node-fetch with axios to get proxy detection * Fixing linting errors * Fixing tests to be cross OS compatible
1 parent 69cf2b9 commit e06ad47

File tree

14 files changed

+107
-40
lines changed

14 files changed

+107
-40
lines changed

packages/luis/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"@oclif/errors": "~1.2.2",
4949
"@types/node-fetch": "~2.5.5",
5050
"@types/sinon": "^7.5.0",
51+
"axios": "~0.21.0",
5152
"cli-ux": "~5.3.3",
5253
"fs-extra": "^8.1.0",
5354
"lodash": "^4.17.19",

packages/luis/src/api/http-request.ts

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'node-fetch'
1+
import axios from 'axios'
22

33
let headers = {
44
'Content-Type': 'application/json',
@@ -9,39 +9,82 @@ export default {
99
async get(
1010
url: string,
1111
subscriptionKey: string) {
12-
setSubscriptionKey(subscriptionKey)
13-
const response = await fetch(url, {method: 'GET', headers})
14-
return response.json()
12+
const resp = await httpRequest(
13+
subscriptionKey,
14+
{
15+
method: 'GET',
16+
url,
17+
headers
18+
})
19+
20+
return resp.data
21+
1522
},
1623

1724
async post(
1825
url: string,
1926
subscriptionKey: string,
2027
body: any,
2128
extraHeaders = {}) {
22-
setSubscriptionKey(subscriptionKey)
2329
headers = {...headers, ...extraHeaders}
24-
const response = await fetch(url, {method: 'POST', headers, body: JSON.stringify(body)})
25-
return response.json()
30+
31+
const resp = await httpRequest(
32+
subscriptionKey,
33+
{
34+
method: 'POST',
35+
url,
36+
data: body,
37+
headers
38+
})
39+
40+
return resp.data
2641
},
2742

2843
async put(
2944
url: string,
3045
subscriptionKey: string,
3146
body: any) {
32-
setSubscriptionKey(subscriptionKey)
33-
const response = await fetch(url, {method: 'PUT', headers, body: JSON.stringify(body)})
47+
const resp = await httpRequest(
48+
subscriptionKey,
49+
{
50+
method: 'PUT',
51+
url,
52+
data: body,
53+
headers
54+
})
3455

35-
return isJSON(response) ? response.json() : {code: 'Success'}
56+
return isJSON(resp.data) ? resp.data : {code: 'Success'}
3657
},
3758

3859
async delete(
3960
url: string,
4061
subscriptionKey: string) {
41-
setSubscriptionKey(subscriptionKey)
42-
const response = await fetch(url, {method: 'DELETE', headers})
43-
return isJSON(response) ? response.json() : {code: 'Success'}
62+
const resp = await httpRequest(
63+
subscriptionKey,
64+
{
65+
method: 'DELETE',
66+
url,
67+
headers
68+
})
69+
return isJSON(resp.data) ? resp.data : {code: 'Success'}
70+
}
71+
}
72+
73+
const httpRequest = async function (subscriptionKey: string, config: any) {
74+
setSubscriptionKey(subscriptionKey)
75+
try {
76+
return await axios(config)
77+
} catch (error) {
78+
if (error.response) {
79+
// The request was made and the server responded with a status code
80+
// that falls out of the range of 2xx
81+
throw Error(error.response.statusText)
82+
} else {
83+
// Something happened in setting up the request that triggered an Error
84+
throw Error(error.message)
85+
}
4486
}
87+
4588
}
4689

4790
const setSubscriptionKey = function (subscriptionKey: string) {

packages/luis/test/commands/luis/application/create.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('luis:application:create', () => {
5959
.command(['luis:application:create', '--endpoint', 'undefined', '--name', 'orange_app', '--subscriptionKey', uuidv1(), '--culture', 'en-us', '--description', 'test description', '--versionId', '0.04'])
6060
.exit(1)
6161
.it('fails to create an app and displays an error message if the endpoint is undefined', ctx => {
62-
expect(ctx.stderr).to.contain('Failed to create app: TypeError: Only absolute URLs are supported\n')
62+
expect(ctx.stderr).to.contain('Failed to create app: Error:')
6363
})
6464

6565
})

packages/luis/test/commands/luis/application/rename.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('luis:application:rename', () => {
6262
.command(['luis:application:rename', '--endpoint', 'undefined', '--subscriptionKey', uuidv1(), '--appId', uuidv1(), '--name', 'sample-app', '--description', 'test description'])
6363
.exit(1)
6464
.it('fails to create an app and displays an error message if the endpoint is undefined', ctx => {
65-
expect(ctx.stderr).to.contain('Failed to rename app: TypeError: Only absolute URLs are supported\n')
65+
expect(ctx.stderr).to.contain('Failed to rename app: Error:')
6666
})
6767

6868
})

packages/luis/test/commands/luis/version/rename.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('luis:version:rename', () => {
5656
.command(['luis:version:rename', '--endpoint', 'undefined', '--subscriptionKey', uuidv1(), '--appId', uuidv1(), '--versionId', '0.1', '--newVersionId', '0.2'])
5757
.exit(1)
5858
.it('fails to rename application version and displays an error message if the endpoint is undefined', ctx => {
59-
expect(ctx.stderr).to.contain('Failed to rename app version: TypeError: Only absolute URLs are supported\n')
59+
expect(ctx.stderr).to.contain('Failed to rename app version: Error')
6060
})
6161

6262
})

packages/qnamaker/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"@oclif/errors": "~1.2.2",
4646
"@oclif/parser": "~3.8.4",
4747
"await-delay": "^1.0.0",
48+
"axios": "~0.21.0",
4849
"camelcase": "^4.1.0",
4950
"chalk": "2.4.1",
5051
"cli-table3": "^0.5.1",

packages/qnamaker/src/commands/qnamaker/init.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export default class QnamakerInit extends Command {
6969

7070
async updateKbId(config: any) {
7171
let response = await new Endpointkeys().getEndpointKeys(config)
72-
config.endpointKey = JSON.parse(await response.text()).primaryEndpointKey
72+
config.endpointKey = response.data.primaryEndpointKey
7373

7474
response = await new Knowledgebase().getKnowledgebaseDetails(config)
75-
let kb = await JSON.parse(await response.text())
75+
let kb = await response.data
7676
config.hostname = kb.hostName
7777
}
7878
}

packages/qnamaker/src/commands/qnamaker/kb/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ export default class QnamakerKbCreate extends Command {
7272

7373
async updateKbId(config: any) {
7474
let response = await new Endpointkeys().getEndpointKeys(config)
75-
config.endpointKey = JSON.parse(await response.text()).primaryEndpointKey
75+
config.endpointKey = response.data.primaryEndpointKey
7676

7777
response = await new Knowledgebase().getKnowledgebaseDetails(config)
78-
const kb = JSON.parse(await response.text())
78+
const kb = response.data
7979
config.hostname = kb.hostName
8080

8181
return kb

packages/qnamaker/src/commands/qnamaker/kb/delete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class QnamakerKbDelete extends Command {
2929

3030
if (!flags.force) {
3131
let kbResult = await new Knowledgebase().getKnowledgebaseDetails(input.config)
32-
let kb = await JSON.parse(await kbResult.text())
32+
let kb = kbResult.data
3333
let answer = await cli.confirm(`Are you sure you would like to delete ${kb.name} [${kb.id}]? (y/n)`)
3434
if (!answer) {
3535
this.log('operation canceled')

packages/qnamaker/src/utils/qnamakerbase.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function processInputs(flags: any, payload: any, configfile: string
2929
result.requestBody = input ? await file.getContentFromFile(input) : stdin
3030
try {
3131
result.requestBody = JSON.parse(result.requestBody)
32-
} catch(ex) {
32+
} catch (ex) {
3333
flags.qnaFormat = true
3434
}
3535
}
@@ -81,37 +81,37 @@ export async function processFlags(flags: any, flagLabels: string[], configDir:
8181

8282
input[flag] = flags[flag] || (config ? config[configPrefix + flag] : null)
8383
})
84-
84+
8585
return input
8686
}
8787

88-
async function getUserConfig (configPath: string) {
88+
async function getUserConfig(configPath: string) {
8989
if (fs.existsSync(path.join(configPath, 'config.json'))) {
9090
return fs.readJSON(path.join(configPath, 'config.json'), {throws: false})
9191
}
9292

9393
return {}
9494
}
9595

96-
function filterByAllowedConfigValues (configObj: any, prefix: string) {
96+
function filterByAllowedConfigValues(configObj: any, prefix: string) {
9797
const allowedConfigValues = [`${prefix}kbId`, `${prefix}endpoint`, `${prefix}region`, `${prefix}subscriptionKey`]
9898
const filtered = Object.keys(configObj)
9999
.filter(key => allowedConfigValues.includes(key))
100100
.reduce((filteredConfigObj: any, key) => {
101101
filteredConfigObj[key] = configObj[key]
102-
102+
103103
return filteredConfigObj
104104
}, {})
105105

106106
return filtered
107107
}
108108

109-
function filterConfig (config: any, prefix: string) {
109+
function filterConfig(config: any, prefix: string) {
110110
return Object.keys(config)
111111
.filter((key: string) => key.startsWith(prefix))
112112
.reduce((filteredConfig: any, key: string) => {
113113
filteredConfig[key] = config[key]
114114

115115
return filteredConfig
116116
}, {})
117-
}
117+
}

0 commit comments

Comments
 (0)