AjaxService is a source-generator, it can generate ajax calls in typescript based on your c# api endpoints.
- detects parameters and return types automatically.
- create models and classes based on associated types.
- support [FromBody],[FromHeader],[FromQuery] (in other words, it can handle body,header and query types).
- validates parameter types and creates valid ajax calls.
- uses best practices for generated typescript code.
- uses built-in fetch api with asynchronous functionality.
- generates immutable classes.
- generates constructor for classes with default value.
dotnet cli: dotnet add package AjaxService.Gen --version 1.0.3
nuget: NuGet\Install-Package AjaxService.Gen -Version 1.0.3
reference: <PackageReference Include="AjaxService.Gen" Version="1.0.3" />
after installing the package. you have access to [Ajax]
attribute.
add it to your endpoint and pass dawn the url:
[Ajax("http://mysite.com/url/to/endpoint")]
public User GetUser(int id)
{
return new User();
}
then build your project.
it will create a tsFiles
directory in your project and 2 files:
AjaxService.ts
Models.ts
Now you can use your endpoint in client code:
import AjaxService:
import {AjaxService} from "./AjaxService";
create an instance of your controller:
let controller = new AjaxService.HomeController();
in my case, my controller was HomeController
,make sure to replace it with yours.
and at the end, call the endpoint:
let result = await controller.GetUserAsync(1);
to use the result you have to await
the call and also check the status of call:
if (result.IsRight()) {
console.log(result.Value);
return;
}
console.log(result.Error);
I highly suggest to use only one parameter for your endpoint and put everything inside it, for example instead of this:
void UpdateUser(string name,int age,datetime birthDate){
//update
}
wrap your input in one single object:
class UpdateUserDto{
public string name { get; set; }
public int age { get; set; }
public datetime birthDate { get; set; }
}
and use that in your endpoint:
void UpdateUser(UpdateUserDto user){
//update user
}
class Parent{
public string name { get; set; }
public Child MyChild { get; set; }
}
class Child {
public Parent MyParent { get; set; }
}
it would presumably blow your pc with infinte object creations(perhaps i handled it but i just cant remember!)
[FromBody]
[FromHeader]
[FromQuery]
[FromBody]
for complex objects(classes) and [FromQuery]
for simple types(string,int,bool...etc).
since AjaxService does not support [FromForm]
, you can only have one complex object as an argument, otherwise it wont be able to send the data. for example this does not work:
void UpdateUser(User user, UserAge age){
// will throw exception on compile time
}
also if you asign a simple type to [FromBody]
and your endpoint has a complex type; that will also throw,e.x:
void UpdateUser([FromBody]int age,User user){
}
because, only one argument can be passed as [FromBody]
and there is no any other option for complex objects.
I mean, not completely, if you have the followings:
int[][]
or List<int[]>
or List<int>[]
..etc
they all will be same as int[]
.
because it cant be fit in a request IF their type is not [FromBody].
so in [FromHeader]
and [FromQuery]
they are same as a simple array, however you can always put them in an object and thay way it will work.
From:
To:
From:
To: