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

package(cloudflare_workers): Missing headers #15

Closed Mravuri96 closed 1 year ago

Mravuri96 commented 1 year ago

Custom headers are not being passed through the exposed fetch function.

https://github.com/invertase/dart_edge/blob/052fc4f94ccd1b7f16ed800cc8aeac96789cb9ff/packages/cloudflare_workers/lib/public/top.dart#L38

Code sample ```dart import 'dart:convert'; import 'package:cloudflare_workers/cloudflare_workers.dart'; import 'package:intl/intl.dart'; void main() async { final kiwiRepository = KiwiRepository(); CloudflareWorkers( fetch: (final Request request, final _, final __) async { if (request.url.toString().contains('favicon.ico')) { return Response(null); } try { final tickets = await kiwiRepository.fetchTickets(); return Response(jsonEncode(tickets)); } catch (e) { return Response('Error: $e'); } }, ); } class KiwiRepository { const KiwiRepository(); FutureOr fetchTickets() async { final uri = Uri( scheme: 'https', host: 'api.tequila.kiwi.com', path: 'v2/search', queryParameters: { 'fly_from': 'FAT', 'fly_to': 'JFK', 'date_from': DateFormat('dd/MM/yyyy').format( DateTime.now(), ), 'date_to': DateFormat('dd/MM/yyyy').format( DateTime.now().add(const Duration(days: 30)), ), 'limit': '25', 'sort': 'price', }, ); final headers = Headers( { 'apikey': '****', }, ); final response = await fetch( Resource.uri(uri), headers: headers, ); final data = await response.json(); return (data as Map); } } ```
Ehesp commented 1 year ago

Should be fixed via https://github.com/invertase/dart_edge/commit/c75720b1f2af10021b869c561d5b17f82049aba0 on 0.0.2+3. Let me know if there's any issues.

Mravuri96 commented 1 year ago

@Ehesp Could you re-open this. Just tried testing bit more and it seems to be only working in wrangler dev --local mode. Publishing it or removing the --local flag during development also seems to break it.

Ehesp commented 1 year ago

That's odd, the difference there is its running in miniflare vs workers. Not sure what the difference is there... Any errors?

Mravuri96 commented 1 year ago

Console output


A hanging Promise was canceled. This happens when the worker runtime is waiting for a Promise from JavaScript to resolve, but has detected that the Promise cannot possibly ever resolve because all code and events related to the Promise's I/O context have already finished.
✘ [ERROR] Uncaught (in response) Error: The script will never generate a response.
Ehesp commented 1 year ago

Oh, yeah @Mravuri96 this won't work. When there's no --dev flag the code is actually running on Cloudflare's services, so it's not going to be able to find localhost.