Skip to content

Commit

Permalink
chore(docs): refactor readme
Browse files Browse the repository at this point in the history
  • Loading branch information
iyifr committed Jan 1, 2025
1 parent 726fca4 commit 9803c55
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
![og](/assets/H4-banner.png)

**A sleek and powerful HTTP framework that makes Dart API development a breeze**
**A lightweight web framework for dart**

Inspired by [unjs H3](https://h3.unjs.io).
Inspired by [unjs H3](https://h3.unjs.io), built with familiar API's and a functional style.

**This is a new project under active development**, production use is not advised.

The docs site is still in construction🚧🚧 - [link](https://h4-tau.vercel.app)
The documentation site is a WIP - [link](https://h4-tau.vercel.app)

## Features

- **Lightweight**: H4 ships with a small core and a set of composable utilities.
- **Middleware**: H4 comes with `onRequest` and `onError` middleware.
- **Middleware**: H4 comes with `onRequest`,`onError` and `afterResponse` middleware.
- **Generic Handlers**: Specify the return type of your handler functions.

## Getting Started
Expand Down Expand Up @@ -65,6 +64,8 @@ Specify the return type of your handlers

```dart
router.get<bool>("/25/**", (event) => true);
router.get<int>("/42/**", (event) => 42);
router.get<String>("/hello/**", (event) => "Hello world");
```

### Global Hooks
Expand Down Expand Up @@ -128,15 +129,15 @@ router.get('/users/:id', (event) {
### Wildcard Routing

```dart
// Matches 'articles/page' and '/articles/otherPage' but not 'articles/page/otherPage'
// Matches 'articles/page' and '/articles/otherPage' but not 'articles/page/subPage'
router.get('/articles/*', (event) {
final path = event.path;
return 'The tea is teaing!!'
});
```

```dart
// Matches 'articles/foo/bar' and 'articles/rice/eba/beans'
// Matches 'articles/foo/bar' and 'articles/page/subPage/subSubPage'
router.get('/articles/**', (event) {
final path = event.path;
return 'The tea is teaing!!'
Expand Down Expand Up @@ -172,8 +173,8 @@ router.post("/vamos", (event) async {

### Contributing

A good first PR would be helping me improve the test coverage of this library, or adding one of the
utilities listed [here](https://h3.unjs.io/utils).
A great first PR would be improve the test coverage of this library, or adding a helpful util, for
inspiration see [here](https://h3.unjs.io/utils).

### Running tests

Expand All @@ -185,5 +186,4 @@ dart test

## Code of Conduct.

Show respect and consideration for others when creating issues and contributing to the library. Only
good vibes!
Be cool and calm.
4 changes: 4 additions & 0 deletions bin/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ void main(List<String> arguments) async {
router.get("/vamos", (event) {
throw CreateError(message: 'A grave error happened');
});

router.get('/hi', (event) {
return [13];
});
}
21 changes: 7 additions & 14 deletions lib/src/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import 'package:h4/src/error_middleware.dart';
import 'package:h4/src/h4.dart';
import 'package:h4/src/logger.dart';

/// Represents an event in the H4 framework.
///
/// The `H4Event` class encapsulates an HTTP request and provides methods and properties
/// to interact with the request and generate the appropriate response.
/// Represents an `HTTP request` event in the H4 framework.
// DOC: The H4Event class encapsulates an incoming HTTP request and adds necessary API's to interface with the request and write responses to the client.
class H4Event {
Map<String, String> params;
Map<String, dynamic> context;
Expand Down Expand Up @@ -73,10 +72,6 @@ class H4Event {
void setResponseFormat(String type) {
var headers = _request.response.headers;

if (_isHeaderSet(HttpHeaders.contentTypeHeader)) {
return;
}

switch (type) {
case 'html':
headers.add(HttpHeaders.contentTypeHeader, 'text/html');
Expand All @@ -100,10 +95,10 @@ class H4Event {
}
}

/// Writes the provided [handlerResult] to the HTTP response and closes the response.
/// Sends the HTTP response based on the [handlerResult] and terminates the response stream.
///
/// If the [handlerResult] is `null`, the response will be closed without writing any content.
/// The [handled] flag is set to `true` after the response is sent.
/// If [handlerResult] is `null`, the response is closed without sending any content, and the status code is set to 204 (No Content).
/// After the response is sent, the [handled] flag is set to `true`, indicating that the request has been fully processed.
void respond(dynamic handlerResult, {required MiddlewareStack middlewares}) {
if (_handled) {
return;
Expand Down Expand Up @@ -144,9 +139,7 @@ class H4Event {
_writeToClient(value);
}

/// Will close the response `IOSink` and complete the request.
///
/// Avoid calling this in handlers and middleware.
/// Will close the native response `IOSink` and stop the response stream.
void _shutDown() {
_request.response.close();
}
Expand Down
20 changes: 9 additions & 11 deletions lib/src/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import 'dart:io';
import 'package:h4/src/event.dart';
import 'package:h4/src/h4.dart';

/// A function that takes the following parameters
/// - A [EventHandler] event handler,
/// - A params object that contains the request parameters (if any).
/// An internal function that interfaces between the incoming HTTP requests and the H4 interface.
///
/// It takes the following arguments.
/// - A [EventHandler] event handler
/// - A params object that contains the request parameters (if any)
/// - An optional `onRequest` middleware that runs before constructing a response
///
/// It returns a function that is called with a HTTP request, it's job is to write a response and close the request.
Expand All @@ -30,18 +32,14 @@ Function(HttpRequest) defineEventHandler(
}
}

// Call the handler with the event.
var handlerResult = handler(event);
event.respond(handlerResult, middlewares: middlewares);
};
}

/// A function type alias for event handlers in the H4 framework.
/// A type alias for event handlers in the H4 framework.
///
/// The `EventHandler` type represents a function that takes an [H4Event] as input and
/// returns a `T`, where `T` is the return type of the event handler.
///
/// It represents the type of data you want to send back to the client.
///
/// Only serializable objects are appropriate return types for handlerss
/// `EventHandlers` are used to respond to http request events which are denoted in H4 by `H4Event`.
///
/// Event handlers are generic.
typedef EventHandler<T> = T Function(H4Event event);

0 comments on commit 9803c55

Please sign in to comment.