Henri Pollet
Henri Pollet

Reputation: 11

Integration Crypto-js in typeScript Angular 2

In our process to learn Angular2 . we are trying to use api done by marvel . The access to this api is done with hash key that we need to send to the api but we don't really know how to implement

On the running process we the error:

require is not defined

Here is the piece of code:

generateMarvelAccessUrl(baseurl: string) {

    var CryptoJS = require("crypto-js");

    var PRIV_KEY = "mypublickey";
    var API_KEY   = "myprivatekey";

    var url = baseurl + "?limit=100&apikey=" + API_KEY;
    var ts = new Date().getTime();
    var hash = CryptoJS.createHash('md5').update(ts + PRIV_KEY + API_KEY).digest('hex');
    url += "&ts=" + ts + "&hash=" + hash;
    return url;
}

Is someone may help us on this item.

Upvotes: 1

Views: 2820

Answers (1)

sreeramu
sreeramu

Reputation: 1223

The require function is provided by SystemJS. You need to add it into your script:

<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script> 
<script src="node_modules/rxjs/bundles/Rx.umd.js"></script>
<script src="node_modules/angular2/bundles/angular2-all.umd.js"></script>

Here is a plunkr describing a working sample:https://plnkr.co/edit/JXLDFBW4A1mi9tyNHoJ3?p=preview

From : https://stackoverflow.com/a/34874540/3279156

Upvotes: 1

Related Questions