In my custom package, I have a controller that inherits ManagementApiControllerBase
. It has an action that uses a model that I created.
I’m calling this action from TypeScript code, and it works.
But if I want to add more and more actions and call them from TypeScript, I would have to create interfaces in TypeScript, copying my models.
Example:
Model:
public class Car
{
public string Brand { get; set; } = string.Empty;
public string Model { get; set; } = string.Empty;
}
Controller:
[HttpGet("get-cars")]
[ProducesResponseType(typeof(IEnumerable<Car>), StatusCodes.Status200OK)]
public IActionResult GetCars()
{
var cars = _carService.GetAll()
.Select(contentType => new Car
{
Brand = contentType.Brand,
Model = contentType.Model,
});
return Ok(cars);
}
And in my ts code, I have to define Car
again:
interface Car{
brand: string;
model: string;
}
async firstUpdated() {
const response = await tryExecute(this, umbHttpClient.get<Car[]>({
url: '/umbraco/car-controller/get-cars',
}));
}
What is the best practice to do this? Now contradicting the title of the post: Is it okay if I create a TypeScript file for the types manually, or should I generate them somehow, or something like that?
I’m still new to Umbraco, and I’m interested in best practices for creating my own package.