Skip to content

Commit

Permalink
docs: update http docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ehesp committed Apr 5, 2023
1 parent 6b75b89 commit 8227854
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
["Service Bindings", "/platform/cloudflare/service-bindings"]
]
],
["Supabase Edge Functions", [["Getting Started", "/platform/supabase"]]]
[
"Supabase Edge Functions",
[
["Getting Started", "/platform/supabase"],
["Supabase Client", "/platform/supabase/client"]
]
]
]
}
48 changes: 47 additions & 1 deletion docs/guides/fetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,50 @@ description: Learn how to perform HTTP Requests to fetch data from remote resour

# Fetching Data

<Info>These docs are a work in progress, check back soon!</Info>
In Edge environemnts, the platform provides a `fetch` API that allows you to perform HTTP requests to fetch data from remote resources.
If you are familiar with the `fetch` API in the browser, you will find that the Edge version is very similar, however there we appriciate that
this API may not be familiar to Dart developers, so a custom Edge client is also available.

## Fetch

To perform basic HTTP requests, you can use the `fetch` API. .

```dart
Future<void> getData() async {
final response = await fetch('https://example.com/data.json');
final data = await response.json();
}
```

To perform requests such as POST, and add other metadata such as headers:

```dart
Future<void> getData() async {
final response = await fetch('https://example.com/data.json', method: 'POST', headers: Headers({
'Content-Type': 'application/json',
}));
final data = await response.json();
}
```

## HTTP Package

The [`http`](https://pub.dev/packages/http) package is a popular package for performing HTTP requests in Dart. Dart Edge provides a custom client built ontop of the HTTP client that is compatible with the `http` package.

First, install the `edge_http_client` package:

```sh
dart pub add edge_http_client
```

Next, import and use the `EdgeHttpClient` instance via the [`runWithClient`](https://pub.dev/documentation/http/latest/http/runWithClient.html) API:

```dart
import 'package:edge_http_client/edge_http_client.dart';
import 'package:http/http.dart' as http;
http.runWithClient(() async {
final response = await http.get('https://example.com/data.json');
final data = jsonDecode(response.body);
}, () => EdgeHttpClient());
```
80 changes: 80 additions & 0 deletions docs/platform/supabase/client.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: Supabase Client
description: Use the Supabase Client in Dart Edge.
---

# Supabase Client

The Dart [`supabase`](https://pub.dev/packages/supabase) package provides a client for connecting and interacting with your Supabase project. The client can be used directly inside of Dart Edge, however requires a few additional steps to get started.

## Installation

First, install the [`supabase`](https://pub.dev/packages/supabase) package from pub.dev:

```sh
dart pub add supabase
```

Next, install the Dart Edge HTTP Client:

```sh
dart pub add edge_http_client
```

Next, install the `yet_another_json_isolate` package (more on this next...):

```sh
dart pub add yet_another_json_isolate
```

### Create an Isolate stub

> Please note; this code will be extracted out at some point so this step will be easier.
The Supabase client uses isolates to parse JSON off the main thread. This is not supported in Dart Edge, so we need to create a stub that will allow the client to run it in the main thread.

```dart
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
class EdgeIsolate implements YAJsonIsolate {
@override
Future decode(String json) {
return Future.value(jsonDecode(json));
}
@override
Future<void> dispose() async {}
@override
Future<String> encode(Object? json) {
return Future.value(jsonEncode(json));
}
@override
Future<void> initialize() async {}
}
```

## Usage

Within your `lib/main` file, import the `supabase` package and create a new instance of the client:

```dart
import 'package:supabase_functions/supabase_functions.dart';
import 'package:edge_http_client/edge_http_client.dart';
import 'package:supabase/supabase.dart';
void main() {
final client = SupabaseClient(
'https://<your-project-id>.supabase.co',
'your-anon-key',
httpClient: EdgeHttpClient(),
isolate: EdgeIsolate(),
);
SupabaseFunctions(fetch: (request) async {
List users = await client.from('users').select();
return Response.json(users);
});
}
```

0 comments on commit 8227854

Please sign in to comment.