From 40d34e139e93e1e8f0731389a1c91088748b0952 Mon Sep 17 00:00:00 2001 From: Jonas Eriksson Date: Thu, 22 Aug 2013 12:30:26 +0200 Subject: [PATCH 1/3] Type definitions for AmplifyJs --- amplifyjs/amplifyjs.d.ts | 144 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 amplifyjs/amplifyjs.d.ts diff --git a/amplifyjs/amplifyjs.d.ts b/amplifyjs/amplifyjs.d.ts new file mode 100644 index 000000000..db7ae49ec --- /dev/null +++ b/amplifyjs/amplifyjs.d.ts @@ -0,0 +1,144 @@ +// Type definitions for AmplifyJs 1.1.0 +// Project: http://amplifyjs.com/ +// Definitions by: Jonas Eriksson +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface amplifyRequestSettings { + resourceId: string; + data?: Object; + success?: Function; + error?: Function; +} + +interface amplifyRequest { + + /*** + * Request a resource. + * resourceId: Identifier string for the resource. + * data: A set of key/value pairs of data to be sent to the resource. + * callback: A function to invoke if the resource is retrieved successfully. + */ + (resourceId: string, hash?: Object, callback?: Function): void; + + /*** + * Request a resource. + * settings: A set of key/value pairs of settings for the request. + * resourceId: Identifier string for the resource. + * data (optional): Data associated with the request. + * success (optional): Function to invoke on success. + * error (optional): Function to invoke on error. + */ + (settings: amplifyRequestSettings); + + /*** + * Define a resource. + * resourceId: Identifier string for the resource. + * requestType: The type of data retrieval method from the server. See the request types sections for more information. + * settings: A set of key/value pairs that relate to the server communication technology. The following settings are available: + * Any settings found in jQuery.ajax(). + * cache: See the cache section for more details. + * decoder: See the decoder section for more details. + */ + define(resourceId: string, requestType: string, settings?: Object): void; + + /*** + * Define a custom request. + * resourceId: Identifier string for the resource. + * resource: Function to handle requests. Receives a hash with the following properties: + * resourceId: Identifier string for the resource. + * data: Data provided by the user. + * success: Callback to invoke on success. + * error: Callback to invoke on error. + */ + define(resourceId: string, resource: Function): void; + +} + +interface amplifySubscribe { + /*** + * Subscribe to a message. + * topic: Name of the message to subscribe to. + * [context]: What this will be when the callback is invoked. + * callback: Function to invoke when the message is published. + * [priority]: Priority relative to other subscriptions for the same message. Lower values have higher priority. Default is 10. + */ + (topic: string, context?: Object, callback?: Function, priority?: number): void; +} +interface amplifyStorageTypeStore { + /*** + * Stores a value for a given key using the default storage type. + * + * key: Identifier for the value being stored. + * value: The value to store. The value can be anything that can be serialized as JSON. + * [options]: A set of key/value pairs that relate to settings for storing the value. + */ + (key: string, value: Object, options?: Object): void; + + /*** + * Gets a stored value based on the key. + */ + (key: string): Object; + + /*** + * Gets a hash of all stored values. + */ + (): Object; +} + +interface amplifyStore extends amplifyStorageTypeStore{ + + /*** + * IE 8+, Firefox 3.5+, Safari 4+, Chrome, Opera 10.5+, iPhone 2+, Android 2+ + */ + localStorage: amplifyStorageTypeStore; + + /*** + * IE 8+, Firefox 2+, Safari 4+, Chrome, Opera 10.5+, iPhone 2+, Android 2+ + */ + sessionStorage: amplifyStorageTypeStore; + + /*** + * Firefox 2+ + */ + globalStorage: amplifyStorageTypeStore; + + /*** + * IE 5 - 7 + */ + userData: amplifyStorageTypeStore; + + /*** + * An in-memory store is provided as a fallback if none of the other storage types are available. + */ + memory: amplifyStorageTypeStore; + + +} + +interface amplifyStatic { + + subscribe: amplifySubscribe; + + /*** + * Remove a subscription. + * topic: The topic being unsubscribed from. + * callback: The callback that was originally subscribed. + */ + unsubscribe(topic: string, callback: Function): void; + + /*** + * Publish a message. + * topic: The name of the message to publish. + * Any additional parameters will be passed to the subscriptions. + * amplify.publish returns a boolean indicating whether any subscriptions returned false. The return value is true if none of the subscriptions returned false, and false otherwise. Note that only one subscription can return false because doing so will prevent additional subscriptions from being invoked. + */ + publish(topic: string, ...args: any[]): boolean; + + store: amplifyStore; + + request: amplifyRequest; + +} + +declare var amplify: amplifyStatic; + From 8d4c567c55153774a604a331fbd583ad7585e591 Mon Sep 17 00:00:00 2001 From: Jonas Eriksson Date: Thu, 22 Aug 2013 12:47:47 +0200 Subject: [PATCH 2/3] Some adjustments + added tests --- amplifyjs/amplifyjs-tests.ts | 68 ++++++++++++++++++++++++++++++++++++ amplifyjs/amplifyjs.d.ts | 29 ++++++++++----- 2 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 amplifyjs/amplifyjs-tests.ts diff --git a/amplifyjs/amplifyjs-tests.ts b/amplifyjs/amplifyjs-tests.ts new file mode 100644 index 000000000..55a37836f --- /dev/null +++ b/amplifyjs/amplifyjs-tests.ts @@ -0,0 +1,68 @@ +// Subscribe and publish with no data + +amplify.subscribe("nodataexample", function () { + alert("nodataexample topic published!"); +}); + +// Subscribe and publish with data + +amplify.publish("nodataexample"); + +amplify.subscribe("dataexample", function (data) { + alert(data.foo); // bar +}); + + +amplify.publish("dataexample", { foo: "bar" }); + +amplify.subscribe("dataexample2", function (param1, param2) { + alert(param1 + param2); // barbaz +}); + +//... + +amplify.publish("dataexample2", "bar", "baz"); + +// Subscribe and publish with context and data + +amplify.subscribe("datacontextexample", $("p:first"), function (data) { + this.text(data.exampleText); // first p element would have "foo bar baz" as text +}); + +amplify.publish("datacontextexample", { exampleText: "foo bar baz" }); + +// Subscribe to a topic with high priority + +amplify.subscribe("priorityexample", function (data) { + alert(data.foo); +}); + +amplify.subscribe("priorityexample", function (data) { + if (data.foo === "oops") { + return false; + } +}, 1); + + +// Store data with amplify storage picking the default storage technology: + +amplify.publish("priorityexample", { foo: "bar" }); +amplify.publish("priorityexample", { foo: "oops" }); + +amplify.store("storeExample1", { foo: "bar" }); +amplify.store("storeExample2", "baz"); +// retrieve the data later via the key +var myStoredValue = amplify.store("storeExample1"), + myStoredValue2 = amplify.store("storeExample2"), + myStoredValues = amplify.store(); +myStoredValue.foo; // bar +myStoredValue2; // baz +myStoredValues.storeExample1.foo; // bar +myStoredValues.storeExample2; // baz + +// Store data explicitly with session storage + +amplify.store.sessionStorage("explicitExample", { foo2: "baz" }); +// retrieve the data later via the key +var myStoredValue2 = amplify.store.sessionStorage("explicitExample"); +myStoredValue2.foo2; // baz diff --git a/amplifyjs/amplifyjs.d.ts b/amplifyjs/amplifyjs.d.ts index db7ae49ec..44ead1bcf 100644 --- a/amplifyjs/amplifyjs.d.ts +++ b/amplifyjs/amplifyjs.d.ts @@ -5,7 +5,7 @@ interface amplifyRequestSettings { resourceId: string; - data?: Object; + data?: any; success?: Function; error?: Function; } @@ -18,7 +18,7 @@ interface amplifyRequest { * data: A set of key/value pairs of data to be sent to the resource. * callback: A function to invoke if the resource is retrieved successfully. */ - (resourceId: string, hash?: Object, callback?: Function): void; + (resourceId: string, hash?: any, callback?: Function): void; /*** * Request a resource. @@ -39,7 +39,7 @@ interface amplifyRequest { * cache: See the cache section for more details. * decoder: See the decoder section for more details. */ - define(resourceId: string, requestType: string, settings?: Object): void; + define(resourceId: string, requestType: string, settings?: any): void; /*** * Define a custom request. @@ -58,11 +58,24 @@ interface amplifySubscribe { /*** * Subscribe to a message. * topic: Name of the message to subscribe to. - * [context]: What this will be when the callback is invoked. + * callback: Function to invoke when the message is published. + */ + (topic: string, callback: Function): void; + /*** + * Subscribe to a message. + * topic: Name of the message to subscribe to. + * context: What this will be when the callback is invoked. * callback: Function to invoke when the message is published. * [priority]: Priority relative to other subscriptions for the same message. Lower values have higher priority. Default is 10. */ - (topic: string, context?: Object, callback?: Function, priority?: number): void; + (topic: string, context: any, callback: Function, priority?: number): void; + /*** + * Subscribe to a message. + * topic: Name of the message to subscribe to. + * callback: Function to invoke when the message is published. + * [priority]: Priority relative to other subscriptions for the same message. Lower values have higher priority. Default is 10. + */ + (topic: string, callback: Function, priority?: number): void; } interface amplifyStorageTypeStore { /*** @@ -72,17 +85,17 @@ interface amplifyStorageTypeStore { * value: The value to store. The value can be anything that can be serialized as JSON. * [options]: A set of key/value pairs that relate to settings for storing the value. */ - (key: string, value: Object, options?: Object): void; + (key: string, value: any, options?: any): void; /*** * Gets a stored value based on the key. */ - (key: string): Object; + (key: string): any; /*** * Gets a hash of all stored values. */ - (): Object; + (): any; } interface amplifyStore extends amplifyStorageTypeStore{ From 698ead9d7ea254053dc5b5845946745d479c9ba1 Mon Sep 17 00:00:00 2001 From: Jonas Eriksson Date: Thu, 22 Aug 2013 13:01:45 +0200 Subject: [PATCH 3/3] Decoders and cache and more tests --- amplifyjs/amplifyjs-tests.ts | 178 +++++++++++++++++++++++++++++++++++ amplifyjs/amplifyjs.d.ts | 4 +- 2 files changed, 181 insertions(+), 1 deletion(-) diff --git a/amplifyjs/amplifyjs-tests.ts b/amplifyjs/amplifyjs-tests.ts index 55a37836f..4bead9605 100644 --- a/amplifyjs/amplifyjs-tests.ts +++ b/amplifyjs/amplifyjs-tests.ts @@ -1,3 +1,5 @@ +// Copied examples directly from AmplifyJs site + // Subscribe and publish with no data amplify.subscribe("nodataexample", function () { @@ -66,3 +68,179 @@ amplify.store.sessionStorage("explicitExample", { foo2: "baz" }); // retrieve the data later via the key var myStoredValue2 = amplify.store.sessionStorage("explicitExample"); myStoredValue2.foo2; // baz + + +// REQUEST + +// Set up and use a request utilizing Ajax + + +amplify.request.define("ajaxExample1", "ajax", { + url: "/myApiUrl", + dataType: "json", + type: "GET" +}); + +// later in code +amplify.request("ajaxExample1", function (data) { + data.foo; // bar +}); + +// Set up and use a request utilizing Ajax and Caching + +amplify.request.define("ajaxExample2", "ajax", { + url: "/myApiUrl", + dataType: "json", + type: "GET", + cache: "persist" +}); + +// later in code +amplify.request("ajaxExample2", function (data) { + data.foo; // bar +}); + +// a second call will result in pulling from the cache +amplify.request("ajaxExample2", function (data) { + data.baz; // qux +}) + +// Set up and use a RESTful request utilizing Ajax + +amplify.request.define("ajaxRESTFulExample", "ajax", { + url: "/myRestFulApi/{type}/{id}", + type: "GET" +}) + +// later in code +amplify.request("ajaxRESTFulExample", + { + type: "foo", + id: "bar" + }, + function (data) { + // /myRESTFulApi/foo/bar was the URL used + data.foo; // bar + } + ); + +// POST data with Ajax + +amplify.request.define("ajaxPostExample", "ajax", { + url: "/myRestFulApi", + type: "POST" + }) + +// later in code +amplify.request("ajaxPostExample", + { + type: "foo", + id: "bar" + }, + function (data) { + data.foo; // bar + } + ); +// Using data maps + +// When searching Twitter, the key for the search phrase is q.If we want a more descriptive name, such as term, we can use a data map: + +amplify.request.define("twitter-search", "ajax", { + url: "http://search.twitter.com/search.json", + dataType: "jsonp", + dataMap: { + term: "q" + } +}); + +amplify.request("twitter-search", { term: "amplifyjs" } ); + +// Similarly, we can create a request that searches for mentions, by accepting a username: + + amplify.request.define("twitter-mentions", "ajax", { + url: "http://search.twitter.com/search.json", + dataType: "jsonp", + dataMap: function (data) { + return { + q: "@" + data.user + }; + } + }); + +amplify.request("twitter-mentions", { user: "amplifyjs" }); + +// Setting up and using decoders + +//Example: + +amplify.request.decoders.appEnvelope = +function (data, status, xhr, success, error) { + if (data.status === "success") { + success(data.data); + } else if (data.status === "fail" || data.status === "error") { + error(data.message, data.status); + } else { + error(data.message, "fatal"); + } +}; + +amplify.request.define("decoderExample", "ajax", { + url: "/myAjaxUrl", + type: "POST", + decoder: "appEnvelope" +}); + +amplify.request({ + resourceId: "decoderExample", + success: function (data) { + data.foo; // bar + }, + error: function (message, level) { + alert("always handle errors with alerts."); + } +}); + +// POST with caching and single - use decoder + +// Example: + +amplify.request.define("decoderSingleExample", "ajax", { + url: "/myAjaxUrl", + type: "POST", + decoder: function (data, status, xhr, success, error) { + if (data.status === "success") { + success(data.data); + } else if (data.status === "fail" || data.status === "error") { + error(data.message, data.status); + } else { + error(data.message, "fatal"); + } + } +}); + +amplify.request({ + resourceId: "decoderSingleExample", + success: function (data) { + data.foo; // bar + }, + error: function (message, level) { + alert("always handle errors with alerts."); + } +}); +// Handling Status +// Status in Success and Error Callbacks + +// amplify.request comes with built in support for status.The status parameter appears in the default success or error callbacks when using an ajax definition. + + amplify.request.define("statusExample1", "ajax", { + //... + }); + + amplify.request({ + resourceId: "statusExample1", + success: function (data, status) { + }, + error: function (data, status) { + } +}); + diff --git a/amplifyjs/amplifyjs.d.ts b/amplifyjs/amplifyjs.d.ts index 44ead1bcf..c8de7260a 100644 --- a/amplifyjs/amplifyjs.d.ts +++ b/amplifyjs/amplifyjs.d.ts @@ -51,7 +51,9 @@ interface amplifyRequest { * error: Callback to invoke on error. */ define(resourceId: string, resource: Function): void; - + + decoders: any; + cache: any; } interface amplifySubscribe {