{"id":13629527,"url":"https://github.com/MrAliSalehi/Ajax-Service","last_synced_at":"2025-04-17T09:34:40.452Z","repository":{"id":65663353,"uuid":"596795814","full_name":"MrAliSalehi/Ajax-Service","owner":"MrAliSalehi","description":"Generate typescript Ajax calls based on your c# endpoints","archived":false,"fork":false,"pushed_at":"2023-12-12T17:29:28.000Z","size":1296,"stargazers_count":20,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-24T09:10:44.715Z","etag":null,"topics":["csharp","sourcegenerator","typescript"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MrAliSalehi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-02-03T00:01:03.000Z","updated_at":"2024-07-21T07:12:48.000Z","dependencies_parsed_at":"2024-01-06T02:33:37.168Z","dependency_job_id":null,"html_url":"https://github.com/MrAliSalehi/Ajax-Service","commit_stats":null,"previous_names":["mralisalehi/ajax-service","mralisalehi/ajaxservice"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrAliSalehi%2FAjax-Service","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrAliSalehi%2FAjax-Service/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrAliSalehi%2FAjax-Service/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrAliSalehi%2FAjax-Service/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MrAliSalehi","download_url":"https://codeload.github.com/MrAliSalehi/Ajax-Service/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223751220,"owners_count":17196589,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","sourcegenerator","typescript"],"created_at":"2024-08-01T22:01:12.902Z","updated_at":"2024-11-08T20:31:12.084Z","avatar_url":"https://github.com/MrAliSalehi.png","language":"C#","funding_links":[],"categories":["Do not want to test 113 ( old ISourceGenerator )"],"sub_categories":["1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category"],"readme":"[![Generic badge](https://img.shields.io/badge/dotnet-standard2.0-darkgreen.svg)](https://github.com/MrAliSalehi/AjaxService.Gen)\n[![MIT License](https://img.shields.io/badge/License-MIT-white.svg)](https://choosealicense.com/licenses/mit/)\n![Nuget](https://img.shields.io/nuget/dt/AjaxService.Gen?color=darkred)\n[![NuGet stable version](https://badgen.net/nuget/v/AjaxService.Gen)](https://www.nuget.org/packages/AjaxService.Gen)\n\n\u003c!--![Views](https://gpvc.arturio.dev/AjaxService)\n--\u003e\n# AjaxService.Gen\n  \n#### [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*.\n\n\n\n# Table of contents\n\n- [Table of contents](#table-of-contents)\n- [Features](#features)\n- [Install](#install)\n- [How to Use](#how-to-use)\n- [Recommendations](#recommandations)\n    - [**only one parameter**](#i-highly-suggest-to-use-only-one-parameter-for-your-endpoint-and-put-everything-inside-it-for-example-instead-of-this)\n    - [**DO NOT use circular objects**](#do-not-use-circular-parent-child-objects-example)\n    - [**Use Attributes**](#use-attributes-to-specify-type-of-an-parameter-supported-types-are)\n    - [**`[FromBody]` and Complex Object**](#only-one-frombody-only-one-complex-object)\n    - [**Jagged/Multidimensional arrays**](#jaggedmultidimensional-arrays-are-ignored)\n- [Demo](#demo)\n    - [**models**](#models)\n    - [**Endpoints**](#endpoints)\n\n\n\n# Features\n\n- **detects parameters and return types automatically.**\n- **create models and classes based on associated types.**\n- **support [FromBody],[FromHeader],[FromQuery]** (in other words, it can handle body,header and query types).\n- **validates parameter types and creates valid ajax calls.**\n- **uses best practices for generated typescript code.**\n- **uses built-in fetch api with asynchronous functionality.**\n- **generates immutable classes.**\n- **generates constructor for classes with default value.**\n\n# Install\n\n\ndotnet cli: `dotnet add package AjaxService.Gen --version 1.0.3`\n\nnuget: `NuGet\\Install-Package AjaxService.Gen -Version 1.0.3`\n\nreference: `\u003cPackageReference Include=\"AjaxService.Gen\" Version=\"1.0.3\" /\u003e`\n\n###\n\n\n# How to Use\n\nafter [installing](#install) the package. you have access to `[Ajax]` attribute.\n\nadd it to your endpoint and pass dawn the url:\n\n```csharp\n[Ajax(\"http://mysite.com/url/to/endpoint\")]\npublic User GetUser(int id)\n{\n    return new User();\n}\n```\n\nthen **build** your project.\n\nit will create a `tsFiles` directory in your project and 2 files:\n\n- `AjaxService.ts`\n- `Models.ts`\n\nNow you can use your endpoint in client code: \n\nimport AjaxService:\n```typescript\nimport {AjaxService} from \"./AjaxService\";\n```\ncreate an instance of your controller:\n```typescript\nlet controller = new AjaxService.HomeController();\n```\nin my case, my controller was `HomeController`,make sure to replace it with yours.\n\nand at the end, call the endpoint:\n\n```typescript\nlet result = await controller.GetUserAsync(1);\n```\nto use the result you **have** to `await` the call and also check the status of call:\n\n```typescript\nif (result.IsRight()) {\n    console.log(result.Value);\n    return;\n}\nconsole.log(result.Error);\n\n```\n\n\n# Recommendations\n\n### **I highly suggest to use only one parameter for your endpoint and put everything inside it, for example instead of this:**\n\n```csharp\nvoid UpdateUser(string name,int age,datetime birthDate){\n    //update\n}\n```\nwrap your input in one single object:\n```csharp\nclass UpdateUserDto{\n    public string name { get; set; }\n    public int age { get; set; }\n    public datetime birthDate { get; set; }\n}\n```\nand use that in your endpoint:\n\n```csharp\nvoid UpdateUser(UpdateUserDto user){\n    //update user\n}\n```\n\n\n### **DO NOT use circular parent child objects, example:**\n```csharp\nclass Parent{\n    public string name { get; set; }\n    public Child MyChild { get; set; }\n}\nclass Child { \n    public Parent MyParent { get; set; }\n}\n```\nit would presumably blow your pc with infinte object creations(perhaps i handled it but i just cant remember!)\n\n\n### **Use Attributes to specify type of an parameter, supported types are:**\n  - `[FromBody]`\n  - `[FromHeader]`\n  - `[FromQuery]`\n\n: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).\n\n\n### **Only One `[FromBody]`, only one Complex Object**\n\nsince 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:\n\n```csharp\nvoid UpdateUser(User user, UserAge age){ \n    // will throw exception on compile time\n}\n```\nalso if you asign a simple type to `[FromBody]` and your endpoint has a complex type; that will also throw,e.x:\n```csharp\nvoid UpdateUser([FromBody]int age,User user){\n\n}\n```\nbecause, **only one** argument can be passed as `[FromBody]` and there is no any other option for complex objects.\n\n\n### **Jagged/Multidimensional arrays are ignored.** \n\nI mean, not completely, if you have the followings:\n\n`int[][]` or `List\u003cint[]\u003e` or `List\u003cint\u003e[]` ..etc\n\nthey all will be same as `int[]`.\nbecause it cant be fit in a request **IF their type is not [FromBody]**.\n\nso 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.\n\n\n# Demo\n\n### **models:**\n\n**From:**\n\n\u003cimg src=\"https://github.com/MrAliSalehi/AjaxService/blob/master/images/csharpClass.png?raw=true\" width=\"600\" height=\"400\"/\u003e\n\n**To:**\n\n\u003cimg src=\"https://github.com/MrAliSalehi/AjaxService/blob/master/images/tsClass.png?raw=true\" width=\"750\" height=\"680\"/\u003e\n\n### **Endpoints:**\n\n**From:**\n\n\u003cimg src=\"https://github.com/MrAliSalehi/AjaxService/blob/master/images/csharpEndpoint.png?raw=true\" width=\"500\" height=\"320\"/\u003e\n\n**To:**\n\n\u003cimg src=\"https://github.com/MrAliSalehi/AjaxService/blob/master/images/tsEndpoint.png?raw=true\" width=\"650\" height=\"480\"/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMrAliSalehi%2FAjax-Service","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FMrAliSalehi%2FAjax-Service","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FMrAliSalehi%2FAjax-Service/lists"}