diff options
| -rw-r--r-- | StoneIsland/www/js/index.js | 2 | ||||
| -rw-r--r-- | StoneIsland/www/js/sdk/_sdk.js | 15 | ||||
| -rw-r--r-- | proxy/index.js | 59 | ||||
| -rw-r--r-- | proxy/package.json | 5 | ||||
| -rw-r--r-- | test/lib/sdk.js | 2 |
5 files changed, 75 insertions, 8 deletions
diff --git a/StoneIsland/www/js/index.js b/StoneIsland/www/js/index.js index 332e780d..a8c36ef1 100644 --- a/StoneIsland/www/js/index.js +++ b/StoneIsland/www/js/index.js @@ -5,7 +5,7 @@ var app = (function(){ app.bind() app.build() - sdk.init({ production: true }) + sdk.init({ env: 'production' }) if (window.cordova) { document.addEventListener('deviceready', app.ready, false) diff --git a/StoneIsland/www/js/sdk/_sdk.js b/StoneIsland/www/js/sdk/_sdk.js index a7ecf6d3..dedcd5f0 100644 --- a/StoneIsland/www/js/sdk/_sdk.js +++ b/StoneIsland/www/js/sdk/_sdk.js @@ -4,8 +4,17 @@ var sdk = (function(){ var endpoint = "https://sandbox.api.yoox.biz/" sdk.init = function(opt){ - if (opt && opt.production) { - endpoint = "https://secure.api.yoox.biz/" + switch (opt.env || "development") { + case "production": + endpoint = "https://secure.api.yoox.biz/" + break + case "test": + endpoint = "http://lvh.me:4567/" + break + case "development": + default: + endpoint = "https://sandbox.api.yoox.biz/" + break } } @@ -23,4 +32,4 @@ var sdk = (function(){ }) return sdk -})()
\ No newline at end of file +})() diff --git a/proxy/index.js b/proxy/index.js index c2e12c60..159712e3 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -1,15 +1,70 @@ var http = require('http') +var najax = require('najax') var PORT = 4567 +var endpoint = "https://secure.api.yoox.biz" + var server = http.createServer(function (req, res){ - res.end('It Works!! Path Hit: ' + req.url) - console.log(req.headers) + console.log("_________________") + console.log("[200] " + req.method + " to " + req.url); + var headers = parse_headers(req) + console.log(headers) + + if (req.method != 'GET') { + var fullBody = ''; + + req.on('data', function(chunk) { + fullBody += chunk.toString(); + }); + + req.on('end', function() { + res.writeHead(200, "OK", {'Content-Type': 'text/html'}); + + // console.log(fullBody) + + najax({ + method: req.method, + url: endpoint + req.url, + data: fullBody, + headers: headers, + success: respond(res), + error: respond(res), + }) + }) + } + else { +// console.log(req) + najax({ + method: req.method, + url: endpoint + req.url, + headers: headers, + success: respond(res), + error: respond(res), + }) + } }) server.listen(PORT, function(){ console.log("Proxy listening on: http://localhost:%s", PORT) }) +function parse_headers (req) { + var headers = {} + var fields = "x-yoox-appname x-yoox-account-token x-yoox-device x-yoox-api-key x-yoox-cart-token".split(" ") + fields.forEach(function(field){ + if (req.headers[field]) headers[field] = field + }) + return headers +} + +function respond(res){ + return function(data, status, xhr){ + res.writeHead(xhr.status, "OK", { 'Content-Type': 'application/json' }) + res.end(data) + console.log(xhr.status, data.length) + } +} + /* headers: { "x-yoox-appname": auth.appname, diff --git a/proxy/package.json b/proxy/package.json index f7304d43..fd5c7ee2 100644 --- a/proxy/package.json +++ b/proxy/package.json @@ -7,5 +7,8 @@ "test": "echo \"Error: no test specified\" && exit 1" }, "author": "okfocus", - "license": "LNT" + "license": "LNT", + "dependencies": { + "najax": "^0.2.1" + } } diff --git a/test/lib/sdk.js b/test/lib/sdk.js index a34b18f9..a52d63c2 100644 --- a/test/lib/sdk.js +++ b/test/lib/sdk.js @@ -23,7 +23,7 @@ for (var i = 0; i < files.length; i++) { eval( fs.readFileSync(fn) + '' ) } -sdk.init({ production: true }) +sdk.init({ env: 'test' }) sdk.auth = auth // stick this here for now module.exports = sdk |
