diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 083204e8f..2561b16b9 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -336,6 +336,7 @@ All definitions files include a header with the author and editors, so at some p * [ProgressJs](http://usablica.github.io/progress.js/) (by [Shunsuke Ohtani](https://github.com/zaneli)) * [promise-pool](https://github.com/vilic/promise-pool) (by [VILIC VANE](https://github.com/vilic)) * [Q](https://github.com/kriskowal/q) (by Barrie Nemetchek, Andrew Gaspar) +* [Qajax](https://github.com/gre/qajax) (by [Boltmade](https://github.com/Boltmade)) * [Q-io](https://github.com/kriskowal/q-io) (by [Bart van der Schoor](https://github.com/Bartvds)) * [q-retry](https://github.com/vilic/q-retry) (by [VILIC VANE](https://github.com/vilic)) * [QUnit](http://qunitjs.com/) (by [Diullei Gomes](https://github.com/Diullei)) diff --git a/qajax/qajax-tests.ts b/qajax/qajax-tests.ts new file mode 100644 index 000000000..820dce455 --- /dev/null +++ b/qajax/qajax-tests.ts @@ -0,0 +1,68 @@ +/// + +// Based on https://github.com/gre/qajax/blob/master/test/qajax.js + +function resetDefaults () { + Qajax.defaults.logs = true; + Qajax.defaults.timeout = 1000; + Qajax.defaults.method = "GET"; + Qajax.defaults.headers = {}; + Qajax.defaults.base = ""; +} + +var sample01url = "/test/dataset/sample01.json"; +var sample01json = [ + { "name": "Jerome", "age": 20 }, + { "name": "Gerard", "age": 30 }, + { "name": "Martine", "age": 43 } +]; + +var emptyUrl = "/test/dataset/empty"; + +function urlWithOptions (url : string, options : any) { + return url+"?"+Qajax.serialize(options); +} + +Qajax; +Qajax.filterStatus; +Qajax.filterSuccess; +Qajax.getJSON; +Qajax.serialize({ foo: 123, bar: "toto" }); +Qajax.getJSON(sample01url).then((res) => true, (err) => true); +Qajax(sample01url).then(Qajax.filterSuccess).then(Qajax.toJSON); +Qajax(emptyUrl, { params: { status: 404 } }).then(Qajax.filterSuccess).then(Qajax.toJSON); +Qajax({method: "POST", url: "/ECHO", data: "1234567890\nazerty\nuiopqsdfghjklm\nwxcvbn\n"}).then(Qajax.filterSuccess); +Qajax({method: "POST", url: "/ECHO", data: { foo: 123, bar: { value: 42 }, arr: [1, 2, 3] }}).then(Qajax.filterSuccess).then(Qajax.toJSON); +Qajax({ url: emptyUrl, params: { status: 201 } }).then(Qajax.filterStatus(200)); +Qajax({method: "POST", url: emptyUrl, params: { status: 500 }}).then(Qajax.filterSuccess).then(Qajax.toJSON); + +var cancellationD = Q.defer(); +var cancellation = cancellationD.promise; +Qajax({ cancellation: cancellation, url: urlWithOptions(emptyUrl, { latency: 400 }) }).then(Qajax.filterSuccess).then(Qajax.toJSON); + +Qajax.defaults.timeout = 200; +Qajax({method: "DELETE", url: sample01url, params: { latency: 500 }}); + +resetDefaults(); +Qajax({method: "POST", url: sample01url, params: { latency: 300 }, timeout: 2000}); + +Qajax.defaults.timeout = 200; +Qajax({method: "DELETE", url: sample01url, params: { latency: 500 }, timeout: 0}); + +resetDefaults(); +Qajax({headers: { "X-Hello": "world" }, method: "GET", url: "/ECHO_HEADERS"}).then(Qajax.filterSuccess).then(Qajax.toJSON); +Qajax({headers: { "X-Hi": "world" }, method: "GET", url: "/ECHO_HEADERS"}).then(Qajax.filterSuccess).then(Qajax.toJSON); +Qajax.defaults.headers = {"X-Foo": "bar"}; +Qajax({method: "GET", url: "/ECHO_HEADERS"}).then(Qajax.filterSuccess).then(Qajax.toJSON); + +resetDefaults(); +Qajax("sample01.json", {base: "/test/dataset/"}).then(Qajax.filterSuccess).then(Qajax.toJSON); +Qajax.defaults.base = "/test/dataset/"; +Qajax("empty").then(Qajax.filterSuccess); + +resetDefaults(); +Qajax.defaults.method = "POST"; +Qajax("/ECHO", { data: "1234567890\nazerty\nuiopqsdfghjklm\nwxcvbn\n", responseType: "text/plain" }).then(Qajax.filterSuccess); + +resetDefaults(); +Qajax.getJSON(urlWithOptions(sample01url, { latency: 50 })).then((res) => true, (err) => true, () => true); diff --git a/qajax/qajax.d.ts b/qajax/qajax.d.ts new file mode 100644 index 000000000..6989ea6d2 --- /dev/null +++ b/qajax/qajax.d.ts @@ -0,0 +1,27 @@ +// Type definitions for Qajax +// Project: https://github.com/gre/qajax +// Definitions by: Boltmade +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare function Qajax(url : string) : Q.Promise; +declare function Qajax(options : any) : Q.Promise; +declare function Qajax(url : string, options : any) : Q.Promise; + +declare module Qajax { + export var defaults : any; + export function filterStatus(validStatus : number) : (xhr : XMLHttpRequest) => Q.Promise; + export function filterStatus(validStatus : (status : number) => boolean) : (xhr : XMLHttpRequest) => Q.Promise; + export function filterSuccess() : Q.Promise; + + export function toJSON(xhr : XMLHttpRequest) : Q.Promise; + + export function getJSON(url : string) : Q.Promise; + + export function serialize(paramsObj : any) : string; +} + +declare module "qajax" { + export = Qajax; +}