TypeScript code generator

Generates TypeScript interfaces, classes and functions for a JSDL web service.

Output dependencies:
Options:
  • GenerateServiceClass (default: false): If false, the operation functions are exported directly, otherwise a service client class is exported
  • GenerateBaseUrl (default: true)
  • GenerateInterfaces (default: true): Otherwise class generation => not implemented yet!
  • UseKnockoutJs (default: false): Parameter conversion with ko.toJSON
  • LibraryPath (default: "libs"): Directory of the jquery.d.ts and knockout.d.ts files
  • AlertExceptions (default: true): Shows an alert if there was a web service exception
  • See code generator for more options

Usage

Sample command line call:

jsdl --type TypeScript --jsdl "http://rsuter.com/Services/Jsdl/SampleService.php?operation=jsdl" --output SampleService.ts

Usage of the generated TypeScript class (using GenerateServiceClass = false, compiled as AMD module):

import service = module("SampleService");
service.sum(1, 2, (result) => {
	confirm(result.toString());
});

T4 template

A sample T4 template can be found here:
https://jsdl.svn.codeplex.com/svn/-%20T4%20templates/TypeScriptTemplate.tt

Important: Change the "Build Action" of the generated .ts file to "TypeScriptCompiler"!

Authentication

It is recommended to implement an access manager which sets the username / password for a service client object. The following code shows a sample access manager implemented in TypeScript:

import dataService = module("services/DataService");

export var person: dataService.Person;

export var login = function(username: string, password: string, completed: (exception: dataService.JsdlException) => void) {
	dataService.beforeSend = (xhr) => {
		xhr.setRequestHeader(
			"Authorization",
			"Basic " + btoa(username + ":" + password));
	};

	dataService.getCurrentPerson((person, exception) => {
		this.person = person;
		completed(exception);
	});
}

export var logout  = function() {
	dataService.beforeSend = null; 
	this.person(null);
}

Usage:

import accessManager = module("AccessManager");

... 
accessManager.login("foo", "bar", exception => {
	if (exception !== null)
		mt.alert("Error", exception.message);
});

...
// get the current person: 
var currentPerson = accessManager.person;

Last edited Jul 26, 2013 at 12:00 PM by rsuter, version 21

Comments

No comments yet.