cloudflare_workers 0.0.4+4 copy "cloudflare_workers: ^0.0.4+4" to clipboard
cloudflare_workers: ^0.0.4+4 copied to clipboard

Write and deploy Dart functions to Cloudflare Workers.

example/lib/main.dart

import 'package:cloudflare_workers/cloudflare_workers.dart';

void main() {
  CloudflareWorkers(
    durableObjects: {
      'MyDurableObject': () => MyDurableObject(),
    },
    fetch: (request, env, ctx) async {
      // Access the namespace of the Durable Object (defined in wrangler.toml)
      final durable = env.getDurableObjectNamespace('FOO_BAR');

      // Generate an ID for the Durable Object (can be any string).
      final id = durable.idFromName(request.url.path);

      // Send a request to the Durable Object.
      return durable.get(id).fetch(request);
    },
  );
}

class MyDurableObject extends DurableObject {
  @override
  FutureOr<Response> fetch(Request request) async {
    // These calls are transactional.
    final views = (await state.storage.get<int>('views') ?? 0) + 1;
    await state.storage.put('views', views);

    return Response('This page has been viewed $views times.');
  }
}
6
likes
90
points
1
downloads

Publisher

verified publisherinvertase.io

Weekly Downloads

Write and deploy Dart functions to Cloudflare Workers.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

edge, edge_runtime, js, js_bindings

More

Packages that depend on cloudflare_workers