1

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.

enter image description here

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 ?

Bronzato
  • 9,438
  • 29
  • 120
  • 212
  • 1
    it looks like a server-side library for `node`. you need to use client-side libraries for making request like [`aurelia-fetch-client`](https://aurelia.io/docs/plugins/http-services/). – avrahamcool Jul 29 '19 at 17:54
  • 1
    Thanks I didn't know about that. I tried to replace the request method with the aurelia-fetch-client but got `Redirect is not allowed for a preflight request` error. I'll ask for help in another SO question to be clear. – Bronzato Jul 30 '19 at 07:48
  • 1
    Possible duplicate of [Using the aurelia-fetch-client with some specific options](https://stackoverflow.com/questions/57267326/using-the-aurelia-fetch-client-with-some-specific-options) – Juliën Jul 30 '19 at 20:38

0 Answers0