Skip to content

Instantly share code, notes, and snippets.

@slovely
Created March 7, 2017 15:08
Show Gist options
  • Save slovely/c5e3644cffbd76e85e1276043830d0f7 to your computer and use it in GitHub Desktop.
Save slovely/c5e3644cffbd76e85e1276043830d0f7 to your computer and use it in GitHub Desktop.
Generated WebAPI actions
// Assume a WebAPI action on the server
// [HttpPost]
// public SomeType TheAction(string s, AnotherType obj) {
// return new SomeType(s.Length, obj.name);
// }
// Where SomeType is
// public class SomeType{ public int Property1{get;set;} public string Property2{get;set;}}
// and AnotherType is
// public class AnotherType {public string Name{get;set;}}
// Call the server...
Api.myController.theAction("Hello, world", { Name: "It's me" })
.done(response => {
// In here response will be typed to SomeType
const num: number = response.Property1; // VALID
const num2: number = response.Property2; // INVALID - Property2 on the server is a string
});
// Works because the following TypeScript is generated:
declare interface SomeType{
Property1: number;
Property2: string;
}
declare interface SomeType{
Name: string;
}
// And we generate stubs that can call the server...
module Api {
export class myController {
public static theAction(s: number, obj: AnotherType): JQueryPromise<SomeType> {
return ServiceCaller.post("/mycontroller/theAction", s, obj);
}
}
}
@slovely
Copy link
Author

slovely commented Mar 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment