fix jQuery JSON POST/PUT/PATCH request, move lint to oauth3.lint.js
This commit is contained in:
parent
2d10171de7
commit
73405e6742
|
@ -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...
|
||||
|
|
70
oauth3.js
70
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();
|
||||
|
|
|
@ -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;
|
||||
};
|
Loading…
Reference in New Issue