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

package(cloudflare_workers): Missing headers #15

Closed
Mravuri96 opened this issue Feb 9, 2023 · 5 comments
Closed

package(cloudflare_workers): Missing headers #15

Mravuri96 opened this issue Feb 9, 2023 · 5 comments
Assignees

Comments

@Mravuri96
Copy link
Contributor

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

Code sample
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<Map> fetchTickets() async {
    final uri = Uri(
      scheme: 'https',
      host: 'api.tequila.kiwi.com',
      path: 'v2/search',
      queryParameters: <String, String>{
        '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<String, dynamic>);
  }
}
@Ehesp Ehesp self-assigned this Feb 9, 2023
@Ehesp
Copy link
Member

Ehesp commented Feb 10, 2023

Should be fixed via c75720b on 0.0.2+3. Let me know if there's any issues.

@Ehesp Ehesp closed this as completed Feb 10, 2023
@Mravuri96
Copy link
Contributor Author

Mravuri96 commented Feb 10, 2023

@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 Ehesp reopened this Feb 10, 2023
@Ehesp
Copy link
Member

Ehesp commented Feb 10, 2023

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

@Mravuri96
Copy link
Contributor Author

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
Copy link
Member

Ehesp commented Feb 12, 2023

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.

@Ehesp Ehesp closed this as completed Feb 12, 2023
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