Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/MrAliSalehi/Ajax-Service
Generate typescript Ajax calls based on your c# endpoints
https://github.com/MrAliSalehi/Ajax-Service
csharp sourcegenerator typescript
Last synced: about 1 month ago
JSON representation
Generate typescript Ajax calls based on your c# endpoints
- Host: GitHub
- URL: https://github.com/MrAliSalehi/Ajax-Service
- Owner: MrAliSalehi
- License: mit
- Created: 2023-02-03T00:01:03.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-12-12T17:29:28.000Z (about 1 year ago)
- Last Synced: 2024-09-24T09:10:44.715Z (3 months ago)
- Topics: csharp, sourcegenerator, typescript
- Language: C#
- Homepage:
- Size: 1.24 MB
- Stars: 20
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- RSCG_Examples - AjaxService.Gen
README
[![Generic badge](https://img.shields.io/badge/dotnet-standard2.0-darkgreen.svg)](https://github.com/MrAliSalehi/AjaxService.Gen)
[![MIT License](https://img.shields.io/badge/License-MIT-white.svg)](https://choosealicense.com/licenses/mit/)
![Nuget](https://img.shields.io/nuget/dt/AjaxService.Gen?color=darkred)
[![NuGet stable version](https://badgen.net/nuget/v/AjaxService.Gen)](https://www.nuget.org/packages/AjaxService.Gen)# AjaxService.Gen
#### [AjaxService](https://github.com/MrAliSalehi/AjaxService.Gen) is a source-generator, it can generate *[ajax](https://www.w3schools.com/xml/ajax_intro.asp)* calls in *typescript* **based** on your c\# *api endpoints*.# Table of contents
- [Table of contents](#table-of-contents)
- [Features](#features)
- [Install](#install)
- [How to Use](#how-to-use)
- [Recommendations](#recommandations)
- [**only one parameter**](#i-highly-suggest-to-use-only-one-parameter-for-your-endpoint-and-put-everything-inside-it-for-example-instead-of-this)
- [**DO NOT use circular objects**](#do-not-use-circular-parent-child-objects-example)
- [**Use Attributes**](#use-attributes-to-specify-type-of-an-parameter-supported-types-are)
- [**`[FromBody]` and Complex Object**](#only-one-frombody-only-one-complex-object)
- [**Jagged/Multidimensional arrays**](#jaggedmultidimensional-arrays-are-ignored)
- [Demo](#demo)
- [**models**](#models)
- [**Endpoints**](#endpoints)# Features
- **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.**# Install
dotnet cli: `dotnet add package AjaxService.Gen --version 1.0.3`
nuget: `NuGet\Install-Package AjaxService.Gen -Version 1.0.3`
reference: ``
###
# How to Use
after [installing](#install) the package. you have access to `[Ajax]` attribute.
add it to your endpoint and pass dawn the url:
```csharp
[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:
```typescript
import {AjaxService} from "./AjaxService";
```
create an instance of your controller:
```typescript
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:
```typescript
let result = await controller.GetUserAsync(1);
```
to use the result you **have** to `await` the call and also check the status of call:```typescript
if (result.IsRight()) {
console.log(result.Value);
return;
}
console.log(result.Error);```
# Recommendations
### **I highly suggest to use only one parameter for your endpoint and put everything inside it, for example instead of this:**
```csharp
void UpdateUser(string name,int age,datetime birthDate){
//update
}
```
wrap your input in one single object:
```csharp
class UpdateUserDto{
public string name { get; set; }
public int age { get; set; }
public datetime birthDate { get; set; }
}
```
and use that in your endpoint:```csharp
void UpdateUser(UpdateUserDto user){
//update user
}
```### **DO NOT use circular parent child objects, example:**
```csharp
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!)### **Use Attributes to specify type of an parameter, supported types are:**
- `[FromBody]`
- `[FromHeader]`
- `[FromQuery]`:warning: if you dont specify anything with these Attributes, AjaxService by default uses `[FromBody]` for **complex objects**(classes) and `[FromQuery]` for **simple types**(string,int,bool...etc).
### **Only One `[FromBody]`, only one Complex Object**
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:
```csharp
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:
```csharp
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.### **Jagged/Multidimensional arrays are ignored.**
I mean, not completely, if you have the followings:
`int[][]` or `List` or `List[]` ..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](https://github.com/MrAliSalehi/AjaxService#small_red_triangle_down-i-highly-suggest-to-use-only-one-parameter-for-your-endpoint-and-put-everything-inside-it-for-example-instead-of-this) and thay way it will work.
# Demo
### **models:**
**From:**
**To:**
### **Endpoints:**
**From:**
**To:**