fix jQuery JSON POST/PUT/PATCH request, move lint to oauth3.lint.js

This commit is contained in:
AJ ONeal 2017-02-06 20:10:24 -05:00
parent 2d10171de7
commit 73405e6742
3 changed files with 67 additions and 59 deletions

View File

@ -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...

View File

@ -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();

50
oauth3.lint.js Normal file
View File

@ -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;
};