surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
271 stars 46 forks source link

Feature: support custom fetch functions #251

Open Sandros94 opened 2 months ago

Sandros94 commented 2 months ago

Is your feature request related to a problem?

Ability to override what fetch to use when using this SDK.

Also noticed that this was possible in 0.11.

Describe the solution

Most frameworks do have a custom fetch built-in. This make sure that the fetch function works in all the production environment supported by the framework itself, with the added ability to customize other logics (eg.: logging framework side).

When defining a new Surreal to be able to override the http fetch function, with something like ofetch:

import { Surreal } from 'surrealdb.js';
import { ofetch } from 'ofetch';

const db = new Surreal({
  fetch: ofetch.create({
    onRequestError: ({ request, options, error }) => {
      if (import.meta.dev) {
        console.error('Request error:', request, options, error)
      }
    },
    onResponseError: ({ request, options, response, error }) => {
      if (import.meta.dev) {
        console.error('Response error:', request, options, response, error)
      }
    }
  })
});

Alternative methods

Dropping the use of the SDK and reimplement its functionality using framework's native functions. But its just duplicate work.

SurrealDB version

1.4.2 for windows on x86_64

SurrealDB.js version

1.0.0-beta.5

Contact Details

sandro.circi@digitoolmedia.com

Is there an existing issue for this?

Code of Conduct

kearfy commented 2 months ago

Hiya @Sandros94, we have intentionally chosen to drop support for custom fetch functions as affects the integrity of the code and can cause unexpected issues. From NodeJS v20 onwards, all major runtimes and platforms support fetch natively. As the version 1 of this library is still in beta, this is not yet documented, however once we stabilize the version we will make sure to document this requirement

Sandros94 commented 2 months ago

Ehy @kearfy, objectively speaking and I hope not to sound like the drama guy:

This is the Javascript driver repo and not the NodeJS driver repo. Is the node driver going to be deprecated?

Custom fetch do more than just be platform agnostic. Depending on the implementation, for example, they also let you define different network connection and configuration based on where the event is executed (client or server side). While bigger platforms like Vercel, AWS, Cloudflare tend to be as much compatible as possible, they often use additional configurations for things like worker to worker communication to prevent public network requests.

Now, instead, my personal opinion: when I build a new module for Nuxt ecosystem I want to make it as easy to debug as possible. Nowdays, it is becoming harder and harder to debug what people are facing when they come by and open up an issue in your repository. So, to help my future-self as much as possible I tend to enable any possible logging during development (taking advantage of onRequest, onResponse, onRequestError and onResponseError functions within a custom fetch), something that most framework are able to. By removing these type of customizations, in a general purpose driver like the JS one, it would make life harder for framework-specific drivers. The result is always a full driver rewrite, duplicated work, project frammentation and pointing fingers when edge cases do inevitably rise.

kearfy commented 2 months ago

Thanks for your thoughts @Sandros94, I'll take it into consideration and discuss it with the team!

Sandros94 commented 2 months ago

Thanks for your thoughts @Sandros94, I'll take it into consideration and discuss it with the team!

I'm happy to share them! Feel free to ping me here or on discord