From 7410206409408937fff36cd79c194975ee2529ae Mon Sep 17 00:00:00 2001 From: Daniele Morosinotto Date: Thu, 28 Mar 2013 17:29:01 +0100 Subject: [PATCH] Azure Mobile Services Typescript descrition Typescript definition to use Azure Mobile Services Client HTML/JS library You need an Azure subcription (even 3-month trial) to get valid applicationUrl and applicationKey to use the library --- .../AzureMobileServicesClient-tests.ts | 73 ++++++++++++++ .../AzureMobileServicesClient.d.ts | 94 +++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 AzureMobileServicesClient/AzureMobileServicesClient-tests.ts create mode 100644 AzureMobileServicesClient/AzureMobileServicesClient.d.ts diff --git a/AzureMobileServicesClient/AzureMobileServicesClient-tests.ts b/AzureMobileServicesClient/AzureMobileServicesClient-tests.ts new file mode 100644 index 0000000000..b557a2baf2 --- /dev/null +++ b/AzureMobileServicesClient/AzureMobileServicesClient-tests.ts @@ -0,0 +1,73 @@ +/// + + +//create base client istance and read properties +var client = new WindowsAzure.MobileServiceClient("your-azure-mobile-application-URL", "your-azure-application-KEY"); +console.log("Azure application URL: " + client.applicationUrl); +console.log("Azure application KEY: " + client.applicationKey.replace(/./gi,'*')); //KEEP IT SECRET!!! + + +//user authentication, to make it work follow this guide: http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-users-html/?fb=it-it#add-authentication +if (client.currentUser === null) { + client.login("facebook") + .then((u: Microsoft.WindowsAzure.User) => alert(u.level)) + .done(() => alert("USER: " + client.currentUser.userId), + (e) => alert("ERROR: " + e)); +} else { client.logout(); } + + +//define an interface that map to server side Table data +interface TodoItem { id?: number; text?: string; complete?: bool; } +var data: TodoItem[]; +var tableTodoItems = client.getTable('todoitem'); + + +//read all data from server using Promise .then() and error handling in .done() +tableTodoItems.read() +.then((retList: TodoItem[]) => { + data = retList; return retList.length +}) +.done((n: number) => + alert(n + " items downloaded"), (e) => alert("ERROR: " + e)); + + +//define simple handler used in callback calls for insert/update and delete +function handlerInsUpd(e, i) => { if (!e) data.push( i); }; +function handlerDelErr(e) => { if (e) alert("ERROR: " + e); } + + +//insert one data passing info in POST + custom data in QueryString + simple callback handler +tableTodoItems.insert({ text: 'hello world!', complete: false }, {timestamp: new Date()} , handlerInsUpd); + + +//update last item changing complete and calling simple handler when done +var todo = data.pop(); +todo.complete = !todo.complete; +tableTodoItems.update(todo, null, handlerInsUpd) + + +//delete first item +tableTodoItems.del({ id: data[0].id },null).done(null, handlerDelErr) + + +//testing some simple Query fluent +var query = tableTodoItems.select('text', 'id') + .where({ complete: false }) + .orderBy('text') +query.read().done(printOut); //Execute query remotly and return data filtered + + +//testing more complicated Query in composition with previous using function Predicate and Projection +var minlength = 15; //parameter value for filter Predicate +query.where(function (len: number) { return this.text != null && this.text.length > len }, minlength) + .orderByDescending('id').skip(2).take(3) //some other ordering and paging filters + .select(function () { return { abc: this.text + '|' + this.id }; }) //Projection + .read().done(printOut); //return 3 object {abd: 'ttttttttttttttt|ID'} + + +//function that printout the query result in JSON +function printOut(ret:any[]) { + if (!ret) console.log("NO DATA FOUND!") + else for (var i = 0; i < ret.length; i++) { + console.log(JSON.stringify(ret[i])); } +} diff --git a/AzureMobileServicesClient/AzureMobileServicesClient.d.ts b/AzureMobileServicesClient/AzureMobileServicesClient.d.ts new file mode 100644 index 0000000000..1dcf9e5a67 --- /dev/null +++ b/AzureMobileServicesClient/AzureMobileServicesClient.d.ts @@ -0,0 +1,94 @@ +// Type definitions for Microsoft.Windows.Azure.MobileService.Web-1.0.0 +// Project: https://.azure-mobile.net/client/MobileServices.Web-1.0.0.min.js +// Definitions by: Morosinotto Daniele +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +module Microsoft.WindowsAzure { + + // MobileServiceClient object based on Microsoft Azure documentation: http://msdn.microsoft.com/en-us/library/windowsazure/jj554219.aspx + interface MobileServiceClient { + new (applicationUrl: string, applicationKey: string): MobileServiceClient; + applicationUrl: string; + applicationKey: string; + currentUser: User; + //for provider:string use one of ProviderEnum: 'microsoftaccount', 'facebook', 'twitter', 'google' + login(provider: string, token: string, callback: (error: any, user: User) => void ): void; + login(provider: string, token: string): asyncPromise; + login(provider: string, callback: (error: any, user: User) => void ): void; + login(provider: string): asyncPromise; + logout(): void; + getTable(tableName: string): MobileServiceTable; + withFilter(serviceFilter: (request: any, next: (request: any, callback: (error:any, response: any) => void ) => void, callback: (error: any, response: any) => void ) => void ) : MobileServiceClient; + } + + // User object based on Microsoft Azure documentation: http://msdn.microsoft.com/en-us/library/windowsazure/jj554220.aspx + interface User { + getIdentities(): any;// { [providerName: string]: { userId: string, accessToken: string, accessTokenSecret?: string }; }; + accessTokens: any; // { [providerName: string]: string; } + level: string; //for level:string use one of LevelEnum: 'admin','anonymous','authenticated' + userId: string; + } + + + // Interface to Platform.async(func) => Platform.Promise based on code MobileServices.Web-1.0.0.js + interface asyncPromise { + then(onSuccess: (result: any) => any, onError?: (error: any) => any): asyncPromise; + done(onSuccess?: (result: any) => void , onError?: (error: any) => void ): void; + } + + // MobileServiceTable object based on Microsoft Azure documentation: http://msdn.microsoft.com/en-us/library/windowsazure/jj554239.aspx + interface MobileServiceTable extends IQuery { + new (tableName: string, client: MobileServiceClient): MobileServiceTable; + getTableName(): string; + getMobileServiceClient(): MobileServiceClient; + + insert(istance: any, paramsQS: Object, callback: (error: any, retInserted: any) => any): void; + insert(istance: any, paramsQS: Object): asyncPromise; + insert(istance: any): asyncPromise; + + update(istance: any, paramsQS: Object, callback: (error: any, retUpdated: any) => any): void; + update(istance: any, paramsQS: Object): asyncPromise; + update(istance: any): asyncPromise; + + lookup(id: number, paramsQS: Object, callback: (error: any, retValue: any) => any): void; + lookup(id: number, paramsQS: Object): asyncPromise; + lookup(id: number): asyncPromise; + + del(istance: any, paramsQS: Object, callback: (error?: any) => void ): void; + del(istance: any, paramsQS: Object): asyncPromise; + del(istance: any): asyncPromise; + + + read(query: IQuery, paramsQS: Object, callback: (error: any, retValues: any) => any): void; + read(query: IQuery, paramsQS: Object): asyncPromise; + read(query: IQuery): asyncPromise; + read(): asyncPromise; + } + + + // Interface to describe Query object fluent creation based on Microsoft Azure documentation: http://msdn.microsoft.com/en-us/library/windowsazure/jj613353.aspx + interface IQuery { + read(paramsQS?: Object): asyncPromise; + + orderBy(...propName: string[]): IQuery; + orderByDescending(...propName: string[]): IQuery; + select(...propNameSelected: string[]): IQuery; + select(funcProjectionFromThis: () => any): IQuery; + where(mapObjFilterCriteria: any): IQuery; + where(funcPredicateOnThis: (...qParams: any[]) => bool, ...qValues: any[]): IQuery; + skip(n: number): IQuery; + take(n: number): IQuery; + includeTotalCount(): IQuery; + + //internals found looking into code MobileServices.Web-1.0.0.js + //new (tableName: string, context: any): IQuery; + //getComponents(): any; + //toOData(): string; + } + + interface WindowsAzureStatic { + MobileServiceClient: MobileServiceClient; + } +} + +declare var WindowsAzure: Microsoft.WindowsAzure.WindowsAzureStatic;