Skip to content

Commit

Permalink
feat(spry): Support request.fetch, see webfetch fetch function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seven Du committed Apr 30, 2024
1 parent fd058f5 commit b6ad885
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 12 deletions.
6 changes: 0 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ updates:
schedule:
interval: daily

# docs
- package-ecosystem: npm
directory: /
schedule:
interval: daily

# spry
- package-ecosystem: pub
directory: /packages/spry
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Spry is an HTTP middleware framework for Dart to make web applications and APIs
```dart
import 'package:spry/spry.dart';
final app = Application.late();
main() async {
final app = Application.late();
app.get("hello", (request) => "Hello, Spry!");
await app.run(port: 3000);
Expand Down
Binary file modified bun.lockb
Binary file not shown.
19 changes: 19 additions & 0 deletions docs/basics/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,22 @@ print(request.route.path);
For routing parameters, you can check out the [Basics → Routing](/basics/routing.md#route-parameters) documentation.

:::

## `fetch`

Used to retrieve data from the network.

> It also supports you to ignore HTTP schema and host, and directly use path to obtain data from other handlers
```dart
// Remote with fetch
app.get('github/:name', (request) {
return request.fetch('https://api.github.com/users/${request.param('name')}');
});
// Fetch profile
app.get('users/:id', (request) => ...);
app.get('profile/:id', (request) {
return request.fetch('/users/${request.param('id')}');
});
```
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"private": true,
"scripts": {
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
},
"devDependencies": {
"vitepress": "^1.0.0-rc.36"
"dependencies": {
"vitepress": "^1.1.4"
}
}
19 changes: 19 additions & 0 deletions packages/spry/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# Spry v3.3.0

To install Spry v3.3.0 run the following command:

```bash
dart pub add spry:3.3.0
```

or update your `pubspec.yaml` file:

```yaml
dependencies:
spry: ^3.3.0
```
## What's Changed
- **Feature**: Support `request.fetch`, see `webfetch` fetch function.

# Spry v3.2.3

To install Spry v3.2.3 run the following command:
Expand Down
3 changes: 3 additions & 0 deletions packages/spry/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ final app = Application.late();

void main(List<String> args) async {
app.get('hello', (request) => 'Hello, Spry!');
app.get('test', (request) {
return request.fetch('/hello');
});

await app.run(port: 3000);

Expand Down
1 change: 1 addition & 0 deletions packages/spry/lib/spry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export 'src/request/request+route.dart';
export 'src/request/request+search_params.dart';
export 'src/request/request+json.dart';
export 'src/request/request+text.dart';
export 'src/request/request+fetch.dart' show Request$Fetch;

// Response
export 'src/response/response+is_closed.dart';
Expand Down
105 changes: 105 additions & 0 deletions packages/spry/lib/src/request/request+fetch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// ignore_for_file: file_names

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:webfetch/webfetch.dart' hide fetch;
import 'package:webfetch/webfetch.dart' as webfetch show fetch, Client;

import '../_internal/map+value_of.dart';
import 'request+locals.dart';

extension Request$Fetch on HttpRequest {
Fetch get fetch => webfetch.fetch.use(client);
}

extension on HttpRequest {
static const key = #spry.request.fetch.client;

webfetch.Client get client {
return locals.valueOf<webfetch.Client>(key, (_) {
return locals[key] = FetchClient(requestedUri);
});
}
}

class FetchClient implements webfetch.Client {
final Uri base;

const FetchClient(this.base);

@override
Future<Response> send(Request request, {bool keepalive = false}) {
return webfetch.fetch(
URL(request.url, base),
keepalive: keepalive,
method: request.method,
headers: request.headers,
body: request.body,
);
}
}

class SpryFetchRequest implements Request {
const SpryFetchRequest(this.request, this.url);

final Request request;

@override
final String url;

@override
Future<ArrayBuffer> arrayBuffer() => request.arrayBuffer();

@override
Future<Blob> blob() => request.blob();

@override
Stream<Uint8List>? get body => request.body;

@override
bool get bodyUsed => request.bodyUsed;

@override
RequestCache get cache => request.cache;

@override
Request clone() => SpryFetchRequest(request.clone(), url);

@override
RequestCredentials get credentials => request.credentials;

@override
RequestDestination get destination => request.destination;

@override
Future<FormData> formData() => request.formData();

@override
Headers get headers => request.headers;

@override
String get integrity => request.integrity;

@override
Future<Object> json() => request.json();

@override
String get method => request.method;

@override
RequestMode get mode => request.mode;

@override
RequestRedirect get redirect => request.redirect;

@override
String get referrer => request.referrer;

@override
ReferrerPolicy get referrerPolicy => request.referrerPolicy;

@override
Future<String> text() => request.text();
}
4 changes: 2 additions & 2 deletions packages/spry/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: spry
description: Spry is an HTTP middleware framework for Dart to make web applications and APIs server more enjoyable to write.
version: 3.2.3
version: 3.3.0
homepage: https://spry.fun
repository: https://github.com/medz/spry

Expand All @@ -11,7 +11,7 @@ dependencies:
logging: ^1.2.0
path: ^1.9.0
routingkit: ^0.2.0
webfetch: ^0.0.15
webfetch: ^0.0.16

dev_dependencies:
lints: ^3.0.0
Expand Down

0 comments on commit b6ad885

Please sign in to comment.