NSwag
Since all communication with the server is made via AJAX requests, we use a client-side TypeScript layer to call server APIs. It's automatically generated by NSwag tool using Swagger/OpenAPI. ASP.NET Zero solution is properly configured for NSwag.
Regenerating Service Proxies
When you change your server-side services, run the following command while the server-side (.Host project) is running:
npm run nswag
Or alternatively, run nswag/refresh.bat (Windows) or the command inside it (non-Windows).
Generated Files
Generated code is located in src/api/generated/service-proxies.ts. You should not make manual changes in this file since it will be overwritten on the next code generation.
Configuration
NSwag configuration is in nswag/service.config.nswag. Post-processing is handled by nswag/postprocess.cjs.
Using Service Proxies
Import and use service proxies with the useServiceProxy hook:
import { useServiceProxy } from "@/api/service-proxy-factory";
import { UserServiceProxy } from "@api/generated/service-proxies";
const MyComponent: React.FC = () => {
const userService = useServiceProxy(UserServiceProxy, []);
const loadUsers = async () => {
const result = await userService.getUsers(input);
// Handle result
};
// ...
};