@botbuildercommunity/spell-check-middleware
Advanced tools
Comparing version 0.2.0 to 0.2.2
{ | ||
"name": "@botbuildercommunity/spell-check-middleware", | ||
"version": "0.2.0", | ||
"version": "0.2.2", | ||
"description": "Bot Framework middleware component for the Cognitive Services Spell Check API", | ||
@@ -43,7 +43,8 @@ "main": "index.js", | ||
"devDependencies": { | ||
"typescript": "^3.2.2", | ||
"mocha": "^5.2.0", | ||
"rewire": "^4.0.1", | ||
"tslint": "^5.11.0", | ||
"tslint-microsoft-contrib": "^5.2.1" | ||
"tslint-microsoft-contrib": "^5.2.1", | ||
"typescript": "^3.2.2" | ||
} | ||
} |
@@ -7,2 +7,15 @@ import { ActivityTypes, Middleware, TurnContext } from 'botbuilder'; | ||
function getUrl(text: string): string { | ||
return `https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/?text=${text}&mode=spell`; | ||
} | ||
async function getWebRequest(url: string, key: string): Promise<WebRequest.Response<string>> { | ||
return await WebRequest.get(url, { | ||
headers : { | ||
'Content-Type' : 'application/x-www-form-urlencoded', | ||
'Ocp-Apim-Subscription-Key' : key | ||
} | ||
}); | ||
} | ||
export class SpellCheck implements Middleware { | ||
@@ -17,10 +30,5 @@ public text: string; | ||
this.text = context.activity.text; | ||
const url: string = `https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/?text=${this.text}&mode=spell`; | ||
const url: string = getUrl(this.text); | ||
try { | ||
const re: WebRequest.Response<string> = await WebRequest.get(url, { | ||
headers : { | ||
'Content-Type' : 'application/x-www-form-urlencoded', | ||
'Ocp-Apim-Subscription-Key' : this.key | ||
} | ||
}); | ||
const re: WebRequest.Response<string> = await getWebRequest(url, this.key); | ||
const obj: any = JSON.parse(re.content); | ||
@@ -27,0 +35,0 @@ if (obj.flaggedTokens && obj.flaggedTokens.length > 0) { |
@@ -1,37 +0,37 @@ | ||
/* | ||
const { WebRequest } = require("web-request"); | ||
//@ts-check | ||
let key = "<yourKey>"; | ||
const { TestAdapter } = require("botbuilder"); | ||
const rewire = require("rewire"); | ||
const spellchecker = rewire("../lib/spellcheck") | ||
async function testSpellcheckConnection() { | ||
text = "Cognutive Services"; | ||
let url = "https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/?text=" + text + "&mode=spell" | ||
try { | ||
var re = await WebRequest.get(url, { | ||
headers : { | ||
'Content-Type' : 'application/x-www-form-urlencoded', | ||
'Content-Length' : 30, | ||
'Ocp-Apim-Subscription-Key' : key, | ||
} | ||
}); | ||
let obj = JSON.parse(re.content); | ||
if(obj.flaggedTokens[0].suggestions[0].suggestion){ | ||
let suggestion = obj.flaggedTokens[0].suggestions[0].suggestion; | ||
let token = obj.flaggedTokens[0].token; | ||
console.log("Did you mean this: " + suggestion); | ||
console.log("Token: " + token); | ||
context.turnState.set("token", token); | ||
context.turnState.set("suggestion", suggestion); | ||
} | ||
} | ||
catch(e) { | ||
throw new Error(`Failed to process spellcheck on ${text}. Error: ${e}`); | ||
} | ||
const mock = async function getWebRequest(url, string) { | ||
return Promise.resolve({ | ||
content: JSON.stringify({ | ||
_type: "SpellCheck", | ||
flaggedTokens: [{ | ||
offset: 0, | ||
token: "hellow", | ||
type: "UnknownToken", | ||
suggestions: [{ | ||
suggestion: "hello", | ||
score: 0.875 | ||
}] | ||
}] | ||
}) | ||
}); | ||
} | ||
testSpellcheckConnection(); | ||
*/ | ||
spellchecker.__set__("getWebRequest", mock); | ||
//Need to put into Mocha format. | ||
describe('Spellcheck middleware tests', function () { | ||
this.timeout(5000); | ||
it('should spellcheck a message', async () => { | ||
const adapter = new TestAdapter(async (context) => { | ||
await context.sendActivity(context.turnState.get('suggestion')); | ||
}); | ||
adapter.use(new spellchecker.SpellCheck("not a real key")); | ||
await adapter.test('hellow', 'hello'); | ||
}); | ||
}); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14627
15
250
0
5