From 045d13a1f8cd5aad7adb98b9344c185abc206a1e Mon Sep 17 00:00:00 2001 From: Lindsay Evans Date: Sat, 4 Jul 2015 11:34:11 +1000 Subject: [PATCH 1/2] Adds type definitions for qwest --- qwest/qwest-tests.ts | 78 ++++++++++++++++++++++++++++++ qwest/qwest.d.ts | 110 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 qwest/qwest-tests.ts create mode 100644 qwest/qwest.d.ts diff --git a/qwest/qwest-tests.ts b/qwest/qwest-tests.ts new file mode 100644 index 000000000..7d023d28c --- /dev/null +++ b/qwest/qwest-tests.ts @@ -0,0 +1,78 @@ +/// + +// Examples taken from https://github.com/pyrsmk/qwest/blob/master/README.md + +qwest.get('example.com') + .then(function(response) { + alert(response); + }); + +qwest.post('example.com', { + firstname: 'Pedro', + lastname: 'Sanchez', + age: 30 +}) + .then(function(response) { + // Make some useful actions + }) + .catch(function(e, response) { + // Process the error + }); + +qwest.get('example.com') + .then(function(response) { + // Blah blah blah + }) + .catch(function(e, response) { + throw e; + }); + +qwest.base = 'http://example.com/'; + +qwest.limit(4); + +qwest.before(function() { + this.uploadonprogress = function(e: any) { + // Upload in progress + }; +}) + .get('example.com') + .then(function(response) { + // Blah blah blah + }); + +if (qwest.xhr2) { + // Actions for XHR2 +} else { + // Actions for XHR1 +} + +qwest.setDefaultXdrResponseType('text'); + + +// Extra tests for anything not covered above + +// Tests for method types +qwest.post('foo') +qwest.put('foo') +qwest.delete('foo') + +// Tests for request method options +qwest.get('foo', null, { + dataType: 'foo', + responseType: 'bar', + cache: true, + async: false, + user: 'bob', + password: 'pass', + headers: { + 'X-Foo': 'Bar', + 'X-Something': { + foo: 'bar' + } + }, + withCredentials: true, + timeout: 2000, + attempts: 7 +}) +qwest.get('foo', null, {}) \ No newline at end of file diff --git a/qwest/qwest.d.ts b/qwest/qwest.d.ts new file mode 100644 index 000000000..31a4e1db6 --- /dev/null +++ b/qwest/qwest.d.ts @@ -0,0 +1,110 @@ +// Type definitions for qwest v1.7.0 +// Project: https://github.com/pyrsmk/qwest +// Definitions by: Lindsay Evans +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module Qwest { + + interface Static { + + /** Base URI for requests. Prepended to request URIs */ + base: string; + + /** Is XHR2 supported by the browser? */ + xhr2: boolean; + + /** Sets the request limit */ + limit(by: number): void; + + /** Set default cross-domain response type for IE8/9 (defaults to 'json') */ + setDefaultXdrResponseType(type: string): void; + + /** Set XHR options before request */ + before(callback: () => any): Static; + + /** + * Perfoms an AJAX GET request + * @param url URL that the request is sent to + * @param data Data to send to the server + * @param options Configuration options for the AJAX request + * */ + get(url: string, data?: any, options?: Options): Promise; + + /** + * Perfoms an AJAX POST request + * @param url URL that the request is sent to + * @param data Data to send to the server + * @param options Configuration options for the AJAX request + * */ + post(url: string, data?: any, options?: Options): Promise; + + /** + * Perfoms an AJAX PUT request + * @param url URL that the request is sent to + * @param data Data to send to the server + * @param options Configuration options for the AJAX request + * */ + put(url: string, data?: any, options?: Options): Promise; + + /** + * Perfoms an AJAX DELETE request + * @param url URL that the request is sent to + * @param data Data to send to the server + * @param options Configuration options for the AJAX request + * */ + delete(url: string, data?: any, options?: Options): Promise; + } + + interface Promise { + + /** Request is successful */ + then(callback: (xhr: XMLHttpRequest, response: any) => any): Promise; + + /** Request has failed */ + catch(callback: (xhr: XMLHttpRequest, response: any) => any): Promise; + + /** Always run */ + complete(callback: (response: any) => any): Promise; + } + + interface Options { + + /** post (by default), json, text, arraybuffer, blob, document or formdata */ + dataType?: string; + + /** the response type; either auto (default), json, xml, text, arraybuffer, blob or document */ + responseType?: string; + + /** browser caching; default is false for GET requests and true for POST requests */ + cache?: boolean; + + /** true (default) or false; used to make asynchronous or synchronous requests */ + async?: boolean; + + /** the user to access to the URL, if needed */ + user?: string; + + /** the password to access to the URL, if needed */ + password?: string; + + /** javascript object containing headers to be sent */ + headers?: { [key: string]: any; }; + + /** false by default; sends credentials with your XHR2 request */ + withCredentials?: boolean; + + /** the timeout for the request in ms; 30000 by default */ + timeout?: number; + + /** the total number of times to attempt the request through timeouts; 1 by default; if you want to remove the limit set it to null */ + attempts?: number; + + } + +} + +declare module "qwest" { + export = qwest; +} + +declare var qwest: Qwest.Static; From 7c3fbd694575c8b0dbbc2f14600cb975e1221fd2 Mon Sep 17 00:00:00 2001 From: Lindsay Evans Date: Mon, 6 Jul 2015 10:27:30 +1000 Subject: [PATCH 2/2] Updating Promise methods to use correct parameters --- qwest/qwest-tests.ts | 6 +++++- qwest/qwest.d.ts | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/qwest/qwest-tests.ts b/qwest/qwest-tests.ts index 7d023d28c..67e2de21b 100644 --- a/qwest/qwest-tests.ts +++ b/qwest/qwest-tests.ts @@ -75,4 +75,8 @@ qwest.get('foo', null, { timeout: 2000, attempts: 7 }) -qwest.get('foo', null, {}) \ No newline at end of file +qwest.get('foo', null, {}) + +qwest.get('foo', null, {}).complete(() => { + //done +}) \ No newline at end of file diff --git a/qwest/qwest.d.ts b/qwest/qwest.d.ts index 31a4e1db6..d0442975e 100644 --- a/qwest/qwest.d.ts +++ b/qwest/qwest.d.ts @@ -58,13 +58,13 @@ declare module Qwest { interface Promise { /** Request is successful */ - then(callback: (xhr: XMLHttpRequest, response: any) => any): Promise; + then(callback: (response: any) => any): Promise; /** Request has failed */ - catch(callback: (xhr: XMLHttpRequest, response: any) => any): Promise; + catch(callback: (e: any, response: any) => any): Promise; /** Always run */ - complete(callback: (response: any) => any): Promise; + complete(callback: () => any): Promise; } interface Options {