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

Error: Dart library 'dart:html' is not available on this platform. #32

Closed
MichealReed opened this issue Apr 29, 2023 · 4 comments
Closed

Comments

@MichealReed
Copy link

Error:

Error: Dart library 'dart:html' is not available on this platform.
import 'dart:html' show Blob, FormData, UrlSearchParams;
       ^
Info: The unavailable library 'dart:html' is imported through these packages:

    package:supabase_edge_functions_example => package:dart_openai => package:fetch_client => package:fetch_api => dart:html

Any edge function that references packages that nest dart:html should trigger this.

https://github.com/anasfik/openai
uses
https://github.com/Zekfad/fetch_client
uses
https://github.com/Zekfad/fetch_api

Example:

create new edge function

dart pub add fetch_api

import 'package:supabase_functions/supabase_functions.dart';
import 'package:fetch_api/fetch_api.dart' as api;

void main() {
  SupabaseFunctions(fetch: (request) {
    void main() async {
      // Simple cors request
      final response = await fetch(
        Resource('https://api.restful-api.dev/objects/1'),
      );

      print(await response.text());
    }

    return Response("Hello from Supabase Edge Functions!");
  });
}

This all makes sense in that dart_edge has created a custom fetch client, but some packages use custom fetch clients as well. I am attempted to get the dart_openai package to work, which uses conditional importing of the referenced fetch package here:

https://github.com/anasfik/openai/blob/aa42ccee89c0fef5b4102eceee11d28890d0bcdb/lib/src/core/networking/client.dart

if I remove the conditional import so that it only sees the http_client_io, the package will compile as an edge function. If the conditional import is present based on dart.library.io, or even refactoring the imports to look like this

import 'package:dart_openai/src/core/utils/http_client_io.dart'
    if (dart.library.js) 'package:dart_openai/src/core/utils/http_client_web.dart'
    if (dart.library.io) 'package:dart_openai/src/core/utils/http_client_io.dart';

I run into the error referenced above as the import takes the web path. I am not sure if this is directly a dart_edge issue, but thought I would start here as it is likely similar imports / nesting of dart:html will be found across the package ecosystem.

@Ehesp
Copy link
Member

Ehesp commented May 2, 2023

Unfortunately the Zekfad/fetch_api package is assuming the user is running on the web environment, not a JS one. I'll take a look at the openai package but I'm not sure there's much we can do.

@Ehesp
Copy link
Member

Ehesp commented May 2, 2023

Hmm, replacing this:

fetch.FetchClient createClient() =>
    fetch.FetchClient(mode: fetch.RequestMode.cors);

with

import 'package:edge_http_client/edge_http_client.dart';

http.Client createClient() => EdgeHttpClient();

Could do the trick.

@MichealReed
Copy link
Author

The problem is that the code execution follows the JS path for building edge functions, if it followed the normal io path it seems your override would work. If I remove the web dependency from the lib the edge function compiles fine.

@MichealReed
Copy link
Author

After forcing the import path, or modifying the package to use

import 'package:edge_http_client/edge_http_client.dart';

http.Client createClient() => EdgeHttpClient();

as suggested, I now see the same issue as in #34. Will close this to resolve there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants