-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api): implement API request utility and test suite #26
Conversation
- Define apiRequest function in api.ts with 'GET', 'POST', 'PUT', and 'DELETE' support. - Add tests for api.ts
c37ad42
to
2a3639a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! I made some suggestions. Please take a look when you get the chance!
Co-authored-by: Ryan James Meneses <[email protected]>
- Handles API response parsing - Processes flash messages (success/error/info) - Throws standardized ApiError with status code - Includes Jest test
- Move error handling to separate utility function - Simplify default options structure - Add credentials option to API requests
- Change api.ts to apiRequest.ts - Change api.test.ts to apiRequest.test.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! I suggest simplifying this a bit more and offloading some of the logic to a future component.
…ency - Remove handleApiError dependency and its test - Add TypeDoc documentation for apiRequest - Add simple error handling inside apiRequest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work @ishaan000! I made some final suggestions. Please take a look then I'll go ahead merge it.
Co-authored-by: Ryan James Meneses <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! 😁🚀
Description
This PR introduces a reusable API utility function and a test suite to streamline HTTP request handling across the application. The utility supports common HTTP methods, authentication, custom headers, and error handling, providing consistency and testability for API integrations.
Key Changes
API Utility (src/hooks/api.ts):
A single entry point for HTTP requests supporting GET, POST, PUT, and DELETE methods.
Accepts parameters:
url: Endpoint to call.
method: HTTP method (default: GET).
body: Payload for the request (if applicable).
headers: Custom headers for the request.
token: Bearer token for authentication.
Automatically uses process.env.NEXT_PUBLIC_BACKEND_URL
Handles structured error responses, including custom error messages (e.g., flash messages).
NEXT_PUBLIC Prefix in env variables : In order to make the value of an environment variable accessible in the browser, Next.js can "inline" a value, at build time, into the js bundle that is delivered to the client, replacing all references to process.env.[variable] with a hard-coded value. To tell it to do this, you just have to prefix the variable.