Skip to content

Commit 8227854

Browse files
committed
docs: update http docs
1 parent 6b75b89 commit 8227854

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

docs.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
["Service Bindings", "/platform/cloudflare/service-bindings"]
5353
]
5454
],
55-
["Supabase Edge Functions", [["Getting Started", "/platform/supabase"]]]
55+
[
56+
"Supabase Edge Functions",
57+
[
58+
["Getting Started", "/platform/supabase"],
59+
["Supabase Client", "/platform/supabase/client"]
60+
]
61+
]
5662
]
5763
}

docs/guides/fetch.mdx

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,50 @@ description: Learn how to perform HTTP Requests to fetch data from remote resour
55

66
# Fetching Data
77

8-
<Info>These docs are a work in progress, check back soon!</Info>
8+
In Edge environemnts, the platform provides a `fetch` API that allows you to perform HTTP requests to fetch data from remote resources.
9+
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
10+
this API may not be familiar to Dart developers, so a custom Edge client is also available.
11+
12+
## Fetch
13+
14+
To perform basic HTTP requests, you can use the `fetch` API. .
15+
16+
```dart
17+
Future<void> getData() async {
18+
final response = await fetch('https://example.com/data.json');
19+
final data = await response.json();
20+
}
21+
```
22+
23+
To perform requests such as POST, and add other metadata such as headers:
24+
25+
```dart
26+
Future<void> getData() async {
27+
final response = await fetch('https://example.com/data.json', method: 'POST', headers: Headers({
28+
'Content-Type': 'application/json',
29+
}));
30+
final data = await response.json();
31+
}
32+
```
33+
34+
## HTTP Package
35+
36+
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.
37+
38+
First, install the `edge_http_client` package:
39+
40+
```sh
41+
dart pub add edge_http_client
42+
```
43+
44+
Next, import and use the `EdgeHttpClient` instance via the [`runWithClient`](https://pub.dev/documentation/http/latest/http/runWithClient.html) API:
45+
46+
```dart
47+
import 'package:edge_http_client/edge_http_client.dart';
48+
import 'package:http/http.dart' as http;
49+
50+
http.runWithClient(() async {
51+
final response = await http.get('https://example.com/data.json');
52+
final data = jsonDecode(response.body);
53+
}, () => EdgeHttpClient());
54+
```

docs/platform/supabase/client.mdx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: Supabase Client
3+
description: Use the Supabase Client in Dart Edge.
4+
---
5+
6+
# Supabase Client
7+
8+
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.
9+
10+
## Installation
11+
12+
First, install the [`supabase`](https://pub.dev/packages/supabase) package from pub.dev:
13+
14+
```sh
15+
dart pub add supabase
16+
```
17+
18+
Next, install the Dart Edge HTTP Client:
19+
20+
```sh
21+
dart pub add edge_http_client
22+
```
23+
24+
Next, install the `yet_another_json_isolate` package (more on this next...):
25+
26+
```sh
27+
dart pub add yet_another_json_isolate
28+
```
29+
30+
### Create an Isolate stub
31+
32+
> Please note; this code will be extracted out at some point so this step will be easier.
33+
34+
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.
35+
36+
```dart
37+
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart';
38+
39+
class EdgeIsolate implements YAJsonIsolate {
40+
@override
41+
Future decode(String json) {
42+
return Future.value(jsonDecode(json));
43+
}
44+
45+
@override
46+
Future<void> dispose() async {}
47+
48+
@override
49+
Future<String> encode(Object? json) {
50+
return Future.value(jsonEncode(json));
51+
}
52+
53+
@override
54+
Future<void> initialize() async {}
55+
}
56+
```
57+
58+
## Usage
59+
60+
Within your `lib/main` file, import the `supabase` package and create a new instance of the client:
61+
62+
```dart
63+
import 'package:supabase_functions/supabase_functions.dart';
64+
import 'package:edge_http_client/edge_http_client.dart';
65+
import 'package:supabase/supabase.dart';
66+
67+
void main() {
68+
final client = SupabaseClient(
69+
'https://<your-project-id>.supabase.co',
70+
'your-anon-key',
71+
httpClient: EdgeHttpClient(),
72+
isolate: EdgeIsolate(),
73+
);
74+
75+
SupabaseFunctions(fetch: (request) async {
76+
List users = await client.from('users').select();
77+
return Response.json(users);
78+
});
79+
}
80+
```

0 commit comments

Comments
 (0)