From 73405e674214e3b44e88b3a91bf430c4146e04fe Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 6 Feb 2017 20:10:24 -0500 Subject: [PATCH] fix jQuery JSON POST/PUT/PATCH request, move lint to oauth3.lint.js --- oauth3.jquery.js | 6 ++++- oauth3.js | 70 +++++++++--------------------------------------- oauth3.lint.js | 50 ++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 59 deletions(-) create mode 100644 oauth3.lint.js diff --git a/oauth3.jquery.js b/oauth3.jquery.js index f1abcfe..5c230dc 100644 --- a/oauth3.jquery.js +++ b/oauth3.jquery.js @@ -68,12 +68,16 @@ , method: opts.method // leaving type for backwards compat , type: opts.method - , headers: opts.headers + , headers: opts.headers || {} }; // don't allow accidetal querystring via 'data' if (opts.data && !/get|delete/i.test(opts.method)) { req.data = opts.data; + if (opts.data && 'object' === typeof opts.data) { + req.data = JSON.stringify(req.data); + req.headers['Content-Type'] = 'application/json; charset=utf-8'; + } } // I don't trust jQuery promises... diff --git a/oauth3.js b/oauth3.js index 379ece2..a2b7bf2 100644 --- a/oauth3.js +++ b/oauth3.js @@ -23,62 +23,16 @@ console.warn("[oauth3.js] Remember to call oauth3.providePromise(Promise) with a proper Promise implementation"); } - // TODO move to a test / lint suite? - oauth3._testPromise = function (PromiseA) { - var promise; - var x = 1; - - // tests that this promise has all of the necessary api - promise = new PromiseA(function (resolve, reject) { - //console.log('x [2]', x); - if (x !== 1) { - throw new Error("bad promise, create not Synchronous [0]"); - } - - PromiseA.resolve().then(function () { - var promise2; - - //console.log('x resolve', x); - if (x !== 2) { - throw new Error("bad promise, resolve not Asynchronous [1]"); - } - - promise2 = PromiseA.reject().then(reject, function () { - //console.log('x reject', x); - if (x !== 4) { - throw new Error("bad promise, reject not Asynchronous [2]"); - } - - if ('undefined' === typeof angular) { - throw new Error("[NOT AN ERROR] Dear angular users: ignore this error-handling test"); - } else { - return PromiseA.reject(new Error("[NOT AN ERROR] ignore this error-handling test")); - } - }); - - x = 4; - - return promise2; - }).catch(function (e) { - if (e.message.match('NOT AN ERROR')) { - resolve({ success: true }); - } else { - reject(e); - } - }); - - x = 3; - }); - - x = 2; - return promise; - }; - oauth3.providePromise = function (PromiseA) { oauth3.PromiseA = PromiseA; - return oauth3._testPromise(PromiseA).then(function () { - oauth3.PromiseA = PromiseA; - }); + if (oauth3._testPromise) { + return oauth3._testPromise(PromiseA).then(function () { + oauth3.PromiseA = PromiseA; + }); + } + + oauth3.PromiseA = PromiseA; + return PromiseA.resolve(); }; oauth3._recaseRequest = function (recase, req) { @@ -389,13 +343,13 @@ var $iframe; function cleanup() { - delete window['__oauth3_' + state]; + delete window['--oauth3-callback-' + state]; $iframe.remove(); clearTimeout(tok); tok = null; } - window['__oauth3_' + state] = function (params) { + window['--oauth3-callback-' + state] = function (params) { //console.info('[iframe] complete', params); resolve(params); cleanup(); @@ -428,7 +382,7 @@ var tok; function cleanup() { - delete window['__oauth3_' + state]; + delete window['--oauth3-callback-' + state]; clearTimeout(tok); tok = null; // this is last in case the window self-closes synchronously @@ -436,7 +390,7 @@ //winref.close(); } - window['__oauth3_' + state] = function (params) { + window['--oauth3-callback-' + state] = function (params) { //console.info('[popup] (or window) complete', params); resolve(params); cleanup(); diff --git a/oauth3.lint.js b/oauth3.lint.js new file mode 100644 index 0000000..d41722e --- /dev/null +++ b/oauth3.lint.js @@ -0,0 +1,50 @@ + // TODO move to a test / lint suite? + oauth3._testPromise = function (PromiseA) { + var promise; + var x = 1; + + // tests that this promise has all of the necessary api + promise = new PromiseA(function (resolve, reject) { + //console.log('x [2]', x); + if (x !== 1) { + throw new Error("bad promise, create not Synchronous [0]"); + } + + PromiseA.resolve().then(function () { + var promise2; + + //console.log('x resolve', x); + if (x !== 2) { + throw new Error("bad promise, resolve not Asynchronous [1]"); + } + + promise2 = PromiseA.reject().then(reject, function () { + //console.log('x reject', x); + if (x !== 4) { + throw new Error("bad promise, reject not Asynchronous [2]"); + } + + if ('undefined' === typeof angular) { + throw new Error("[NOT AN ERROR] Dear angular users: ignore this error-handling test"); + } else { + return PromiseA.reject(new Error("[NOT AN ERROR] ignore this error-handling test")); + } + }); + + x = 4; + + return promise2; + }).catch(function (e) { + if (e.message.match('NOT AN ERROR')) { + resolve({ success: true }); + } else { + reject(e); + } + }); + + x = 3; + }); + + x = 2; + return promise; + };