docs

ApiResponse

ApiResponse is a utility class designed to standardize the responses from the controller in an API. It provides structured methods to handle various types of responses, including:

You can find the ApiResponse.ts in app/utils/ApiResponse.ts.

Example usage scenarios include registering a new user, paginating a list of posts, or managing errors encountered during API requests.

ApiResponse examples:

// Used when you successfully complete a request
// Example 1: When you successfully register a new post and then want to redirect the user to the listing screen.
// Example 2: When you want to display only 1 post.
ApiResponse.success(response, data, path_for_redirect);

// Used to display paginated data.
// PS: Recommended to use ResponseUtils.paginate
ApiResponse.pagination(response, posts);

// Used to display an error
// Recommended to use in the catch block of a try catch
ApiResponse.error(response, error);

// Used when you want to display an error like the AppError mentioned above
// Recommended to use in the catch block of a try catch
ApiResponse.appError(response, error);

Summary