#Is this good custom $fetch ?

3 messages · Page 1 of 1 (latest)

granite anchorBOT
#

Hi @leaden coral. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the knowledge sources provided, I can offer some insights on your custom $fetch implementation for Nuxt. This implementation appears to be a good starting point, but there are a few considerations and potential improvements to note:

  1. Base Structure: Your implementation follows the recommended pattern described in the Custom useFetch in Nuxt guide. It creates a custom $fetch instance within a Nuxt plugin, which is a good practice.

  2. Runtime Config: You're correctly using useRuntimeConfig() to access configuration values, which aligns with the useRuntimeConfig documentation.

  3. Headers Handling: Your code handles headers appropriately, including setting the 'Accept' header and conditionally adding CSRF tokens for secure methods. This is in line with best practices for API requests.

  4. Server-Side Request Handling: You're using useRequestHeaders() to forward relevant headers during server-side rendering, which is a good practice mentioned in the Passing Headers and Cookies section of the Nuxt documentation.

  5. Error Handling: You've included hooks for request and response errors, which is important for proper error management.

However, there are a few points to consider:

  1. Type Safety: Consider adding type annotations to improve type safety, especially for the api object and the plugin return value.

  2. CSRF Token: The useCsrfHeader() function is not a standard Nuxt function. Ensure this is a custom composable you've defined elsewhere in your project.

  3. Error Handling: The useLaravelErrorHandler() function is also not standard. Make sure this is properly defined and handles errors appropriately.

#
  1. Credentials: You're using credentials: "include", which is fine if you intend to send cookies with every request. Just be aware of the security implications.

  2. Referer and Origin: Setting these headers manually might not be necessary in all cases, as browsers typically handle these automatically for client-side requests.

To improve this implementation, you might consider:

  1. Using useRequestFetch() as mentioned in the useRequestFetch documentation to automatically handle forwarding of headers in SSR contexts.

  2. Implementing a more robust error handling strategy, possibly using the onResponseError hook to handle different types of errors (e.g., 401, 403, 500) in a centralized manner.

  3. Consider creating a custom useAPI composable as shown in the Custom useFetch guide to wrap this custom fetch instance for easier use throughout your application.

Overall, your implementation is on the right track and follows many of the best practices for creating a custom fetch instance in Nuxt. With some minor refinements, it should serve as a solid foundation for your API interactions.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: