invertase / dart_edge

Run Dart on the Edge - supporting Vercel & Cloudflare Workers (more coming soon).
https://docs.dartedge.dev
Apache License 2.0
324 stars 23 forks source link

UnsupportedError occurs when using the HTTP package on the Supabase platform #34

Open htsuruo opened 1 year ago

htsuruo commented 1 year ago

I am running on Supabase Edge.

As far as I understand from Fetching Data, it is possible to fetch data using two approaches, Fetch and HTTP Package.

However, the sample does not describe how to use it with platforms such as Supabase or Vercel using http.runWithClient.

When I tried writing the following code, an UnsupportedError occurred:

UnsupportedError {
  message: "Cannot create a client without dart:html or dart:io.",
  "$thrownJsError": Unsupported operation: Cannot create a client without dart:html or dart:io.
    at Object.wrapException (file:///home/deno/functions/dart_edge/main.dart.js:1051:17)
    at Object.throwExpression (file:///home/deno/functions/dart_edge/main.dart.js:1065:15)
    at Object.createClient (file:///home/deno/functions/dart_edge/main.dart.js:11473:16)
    at file:///home/deno/functions/dart_edge/main.dart.js:7067:44
    at _wrapJsFunctionForAsync_closure.$protected (file:///home/deno/functions/dart_edge/main.dart.js:3609:15)
    at _wrapJsFunctionForAsync_closure.call$2 (file:///home/deno/functions/dart_edge/main.dart.js:14042:12)
    at Object._asyncStartSync (file:///home/deno/functions/dart_edge/main.dart.js:3573:20)
    at Object._withClient$body (file:///home/deno/functions/dart_edge/main.dart.js:7103:16)
    at Object._withClient (file:///home/deno/functions/dart_edge/main.dart.js:7050:16)
    at Object.get (file:///home/deno/functions/dart_edge/main.dart.js:7047:16)
}
Reproducible code

```dart void main() { http.runWithClient(() async { SupabaseFunctions(fetch: (request) async { // Get IP address final response = await http.get( Uri.parse('http://httpbin.org/ip'), headers: {'Content-Type': 'application/json'}, ); if (response.statusCode == 200) { final json = response.body as Map?; print(json); return Response.json(json); } return Response.error(); }); }, () => EdgeHttpClient()); } ```

Is my usage incorrect? Or is the HTTP package not yet supported in Supabase? It was available with the Fetch API.


I want to use packages like dart_openai | Dart Package, but since their internal implementation depends on http, I cannot use them without resolving the above issue.

Thanks.

MichealReed commented 1 year ago

Closed my related issue and wanted to add @HTsuruo's very cool example repository. May be very useful in diagnosing this -- https://github.com/HTsuruo/dart-edge-supabase-playground/tree/main

Thanks for the guide and workaround too 🙏

MichealReed commented 1 year ago

Maybe an override of dart:html with https://pub.dev/packages/universal_html would work?

Zekfad commented 1 year ago

If fetch is conforms to browsers' one you may try to use fetch_client, it is independent of io or html, using only js interop to call global fetch function.

noga-dev commented 1 year ago

Maybe an override of dart:html with https://pub.dev/packages/universal_html would work?

Tried.

root@DESKTOP-IAH5ULT:/mnt/d/src/hackathons/2023/intellitask/functions# npx supabase functions serve dart_edge --no-verify-jwt --env-file ./.env
Serving functions with legacy supabase/deno-relay:v1.6.0... Run functions serve instead to use Edge Runtime.
Starting supabase/functions/dart_edge
Serving supabase/functions/dart_edge
Watcher Process started.
Check file:///home/deno/functions/dart_edge/index.ts
UnsupportedError {
  message: "Platform._version",
  "$thrownJsError": Unsupported operation: Platform._version
    at Object.wrapException (file:///home/deno/functions/dart_edge/main.dart.js:2247:17)
    at Object._Platform__version (file:///home/deno/functions/dart_edge/main.dart.js:8509:15)
    at Object._Platform_version (file:///home/deno/functions/dart_edge/main.dart.js:8565:16)
    at file:///home/deno/functions/dart_edge/main.dart.js:39067:74
    at Object.holder.<computed> [as $get$Platform__version] (file:///home/deno/functions/dart_edge/main.dart.js:130:21)
    at Object.HttpClient_HttpClient (file:///home/deno/functions/dart_edge/main.dart.js:250:14)
    at WindowController_onChooseHttpClient_closure.call$1 (file:///home/deno/functions/dart_edge/main.dart.js:36649:75)
    at WindowController.onChooseHttpClient$1 (file:///home/deno/functions/dart_edge/main.dart.js:36642:44)       
    at file:///home/deno/functions/dart_edge/main.dart.js:36609:48
    at _wrapJsFunctionForAsync_closure.$protected (file:///home/deno/functions/dart_edge/main.dart.js:4927:15)
noga-dev commented 1 year ago

Sounds like the only way to get OpenAI's API to work atm is to call another function which handles the call and just returns the result.

https://supabase.com/docs/reference/javascript/functions-invoke

Which defeats the point of using Dart Edge functions at all, and one may as well just use Deno to begin with.