I try to use a very basic API call from Windows Azure to translate some texts. They gives a quickstart example code located here
I try this code and it works pretty well. The text Hello world
is translated into deutch and italian.
I removed my personal subscription key.
Here is the sample:
const request = require('request');
const uuidv4 = require('uuid/v4');
const subscriptionKey = '........';
let options = {
method: 'POST',
baseUrl: 'https://api.cognitive.microsofttranslator.com/',
url: 'translate',
qs: {
'api-version': '3.0',
'to': ['de', 'it']
},
headers: {
'Ocp-Apim-Subscription-Key': subscriptionKey,
'Content-type': 'application/json',
'X-ClientTraceId': uuidv4().toString()
},
body: [{
'text': 'Hello World!'
}],
json: true,
};
request(options, function(err, res, body){
console.log(JSON.stringify(body, null, 4));
});
Now I need to integrate this code into my aurelia application
I use the Aurelia CLI.
Here is what I did:
Added in package.json:
"dependencies": {
....
"@types/request": "^2.48.2",
"@types/uuidv4": "^2.0.0",
...
"request": "^2.88.0"
"uuidv4": "^4.0.0",
}
Added in aurelia.json:
"dependencies": [
...
"uuidv4",
"request"
]
Run npm install
inside my console.
Created a test page:
import * as request from 'request';
import * as uuidv4 from 'uuidv4';
import secret from '../secret';
export class Translator {
constructor() { }
translate() {
let options = {
method: 'POST',
baseUrl: 'https://api.cognitive.microsofttranslator.com/',
url: 'translate',
qs: {
'api-version': '3.0',
'to': ['de', 'it']
},
headers: {
'Ocp-Apim-Subscription-Key': secret.translatorKey,
'Content-type': 'application/json',
'X-ClientTraceId': uuidv4().toString()
},
body: [{
'text': 'Hello World!'
}],
json: true,
};
request(options, function (err, res, body) {
console.log(JSON.stringify(body, null, 4));
});
}
}
Unfortunately, it doesn't work. I noticed when au run
I got some warnings:
WARN [StubNodejs] No avaiable stub for core Node.js module "fs", stubbed with empty module
WARN [StubNodejs] No avaiable stub for core Node.js module "net", stubbed with empty module
WARN [StubNodejs] No avaiable stub for core Node.js module "tls", stubbed with empty module
By the way, when trying to accessing my aurelia app inside a browser I got errors in the console.
If I remove the reference "request"
added in aurelia.json
then I don't get these warnings and I can run my aurelia app. So the problems seems located on this request
npm package. Is it not compatible with Aurelia ?