Building HTTP service¶
HG HTTP annotations for TypeScript use the same style as Java's Spring Boot.
@RequestMapping("/users")
export class UserController {
private readonly _userService: UserService;
constructor (userService: UserService) {
this._userService = userService;
}
@GetMapping("/", "/list")
public async getUserList(
@RequestParam("p", Request.ParamType.INTEGER)
pageNumber: number = 0,
@RequestParam("l", Request.ParamType.INTEGER)
pageSize: number = 10,
@RequestHeader('accept', {defaultValue: '*/*'})
accept: string
): Promise<ResponseEntity<ListDTO<UserModel>>> {
// const parsedPageNumber = pageNumber ? parseInt(pageNumber, 10) : 0;
// const parsedPageSize = pageSize ? parseInt(pageSize, 10) : 10;
return ResponseEntity.ok({
pageNumber: pageNumber,
pageSize: pageSize,
content: await this._userService.getUserList(pageNumber, pageSize),
});
}
@GetMapping("/items/{id}")
public async getUserList(
@PathVariable('id')
id: string
): Promise<ResponseEntity<Json>> {
return ResponseEntity.ok({
itemId: id
});
}
@PostMapping("/addUser")
public async addUser (
@RequestBody user : Json,
@RequestHeader headers : Headers
) : Promise<ResponseEntity<Json>> {
const host = headers.getHost();
await this._userService.addUser(user);
return ResponseEntity.ok({
user: user,
host: host
});
}
}
For the actual server implementing REST API, see RequestServer.