Skip to content
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

Merged
merged 9 commits into from
Jan 8, 2025

Conversation

ishaan000
Copy link
Collaborator

@ishaan000 ishaan000 commented Dec 22, 2024

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.

@ishaan000 ishaan000 added enhancement New feature or request api Related to APIs labels Dec 22, 2024
@ishaan000 ishaan000 requested a review from hiyaryan December 22, 2024 20:15
@ishaan000 ishaan000 self-assigned this Dec 22, 2024
@ishaan000 ishaan000 removed the request for review from hiyaryan December 22, 2024 22:38
- Define apiRequest function in api.ts with 'GET', 'POST', 'PUT', and 'DELETE' support.
- Add tests for api.ts
Copy link
Member

@hiyaryan hiyaryan left a 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!

src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
src/hooks/api.ts Outdated Show resolved Hide resolved
ishaan000 and others added 4 commits January 7, 2025 11:39
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
Copy link
Member

@hiyaryan hiyaryan left a 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.

src/hooks/apiRequest.ts Outdated Show resolved Hide resolved
src/hooks/apiRequest.ts Outdated Show resolved Hide resolved
…ency

- Remove handleApiError dependency and its test
- Add TypeDoc documentation for apiRequest
- Add simple error handling inside apiRequest
Copy link
Member

@hiyaryan hiyaryan left a 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.

src/hooks/apiRequest.ts Outdated Show resolved Hide resolved
tests/app/hooks/apiRequest.test.tsx Outdated Show resolved Hide resolved
Co-authored-by: Ryan James Meneses <[email protected]>
@hiyaryan hiyaryan self-requested a review January 8, 2025 20:31
Copy link
Member

@hiyaryan hiyaryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 😁🚀

@hiyaryan hiyaryan merged commit 66083ff into main Jan 8, 2025
1 check passed
@hiyaryan hiyaryan deleted the api-utility branch January 8, 2025 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to APIs enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants