Skip to content

Commit

Permalink
chore(docs): fixing docs.
Browse files Browse the repository at this point in the history
Forgot to commit and push these.
  • Loading branch information
razshare committed Jun 13, 2024
1 parent 139cde0 commit 5ee1d50
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
22 changes: 12 additions & 10 deletions docs/1.router.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ function main(ServerInterface $server): Unsafe {

You can create routes through
```php
$server->router->get('/path', handler(...));
$router->get('/path', handler(...));
```

Here's a complete example

```php
<?php
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
use CatPaw\Core\Unsafe;
Expand All @@ -44,9 +45,9 @@ function handler() {
return success('there are no cats here');
}

function main(ServerInterface $server): Unsafe {
return anyError(function() use($server) {
$server->router->get('/cats', handler(...))->try();
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server, $router) {
$router->get('/cats', handler(...))->try();
$server->start()->try();
});
}
Expand All @@ -62,7 +63,7 @@ This creates a _GET /cats_ route which responds with _"there are no cats here"_.
Similarly to the _GET_ example, the following

```php
$server->router->post('/cats', handler(...))
$router->post('/cats', handler(...))
```
will create a **POST** route.

Expand All @@ -71,6 +72,7 @@ A more complete example would be
```php
<?php
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use CatPaw\Web\Attributes\Consumes;
use CatPaw\Web\Attributes\Produces;
Expand All @@ -92,9 +94,9 @@ function handler(Body $body){
echo "Received body: {$body->asText()}\n";
}

function main(ServerInterface $server): Unsafe {
return anyError(function() use($server) {
$server->router->post('/cats', handler(...))->try();
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server, $router) {
$router->post('/cats', handler(...))->try();
$server->start()->try();
});
}
Expand Down Expand Up @@ -156,7 +158,7 @@ This file will serve requests for _GET /_.
The equivalent programmatic route definition is

```php
$server->router->get('/', fn () => 'hello world');
$router->get('/', fn () => 'hello world');
```

### Path parameters
Expand Down Expand Up @@ -195,5 +197,5 @@ a _404 Not Found_.
The equivalent programmatic definition is

```php
$server->router->get('/about/{username}', handler(...));
$router->get('/about/{username}', handler(...));
```
12 changes: 7 additions & 5 deletions docs/18.open-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use CatPaw\Core\Unsafe;
use function CatPaw\Core\success;
use function CatPaw\Core\anyError;
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use CatPaw\Web\Interfaces\OpenApiInterface;
use CatPaw\Web\Attributes\IgnoreOpenApi;
use CatPaw\Web\Attributes\Produces;
Expand All @@ -38,10 +39,10 @@ function openapi(OpenApiInterface $openApi) {
return success($openApi->getData());
}

function main(ServerInterface $server):Unsafe {
function main(ServerInterface $server, RouterInterface $router):Unsafe {
return anyError(function() use($server) {
$server->router->get('/test', test(...))->try();
$server->router->get("/openapi", openapi(...))->try();
$router->get('/test', test(...))->try();
$router->get("/openapi", openapi(...))->try();
$server->start()->try();
});
}
Expand Down Expand Up @@ -91,6 +92,7 @@ use CatPaw\Core\Unsafe;
use function CatPaw\Core\anyError;
use CatPaw\Web\Attributes\Example;
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use CatPaw\Web\Attributes\Produces;
use CatPaw\Web\Attributes\Summary;
use const CatPaw\Web\TEXT_PLAIN;
Expand All @@ -101,9 +103,9 @@ function handler(string $value, Body $body) {
return "this is a test";
}

function main(ServerInterface $server): Unsafe {
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server) {
$server->router->get('/test/{value}', handler(...))->try();
$router->get('/test/{value}', handler(...))->try();
$server->start()->try();
});
}
Expand Down
28 changes: 16 additions & 12 deletions docs/2.path-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Those variable parts can then be passed to your route handler function as parame
```php
<?php
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
use CatPaw\Core\Unsafe;
Expand All @@ -18,9 +19,9 @@ function handler(string $name) {
return success("This is $name's about page.");
}

function main(ServerInterface $server): Unsafe {
return anyError(function() use($server) {
$server->router->get('/about/{name}', handler(...))->try();
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server, $router) {
$router->get('/about/{name}', handler(...))->try();
$server->start()->try();
});
}
Expand All @@ -40,6 +41,7 @@ Given this definition
```php
<?php
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
use CatPaw\Core\Unsafe;
Expand All @@ -48,9 +50,9 @@ function handler(int $age) {
return success("This cat is now $age years old.");
}

function main(ServerInterface $server): Unsafe {
return anyError(function() use($server) {
$server->router->post('/set/age/{age}', handler(...))->try();
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server, $router) {
$router->post('/set/age/{age}', handler(...))->try();
$server->start()->try();
});
}
Expand All @@ -68,6 +70,7 @@ Use _CatPaw\Web\Attributes\Param_ to modify the matching pattern for your path p
```php
<?php
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
use CatPaw\Web\Attributes\Param;
Expand All @@ -77,9 +80,9 @@ function handler(#[Param('\w{3,15}')] string $name) {
return success("This is $name's about page.");
}

function main(ServerInterface $server): Unsafe {
return anyError(function() use($server) {
$server->router->get('/about/{name}', handler(...))->try();
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server, $router) {
$router->get('/about/{name}', handler(...))->try();
$server->start()->try();
});
}
Expand All @@ -94,6 +97,7 @@ As you would expect, a symbolic path can contain multiple variable parts
```php
<?php
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
use CatPaw\Core\Unsafe;
Expand All @@ -102,9 +106,9 @@ function handler(string $name, string $childName) {
return success("This is $childName's about page, who is $name's kitten.");
}

function main(ServerInterface $server): Unsafe {
return anyError(function() use($server) {
$server->router->get('/about/{name}/child/{childName}', handler(...))->try();
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server, $router) {
$router->get('/about/{name}/child/{childName}', handler(...))->try();
$server->start()->try();
});
}
Expand Down
16 changes: 10 additions & 6 deletions docs/4.session.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Request a `SessionInterface` parameter in your context.

```php
<?php
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\SessionInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use CatPaw\Web\Interfaces\ServerInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
Expand All @@ -26,9 +28,9 @@ function handler(SessionInterface $session){
return success();
}

function main(SessionInterface $server): Unsafe {
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server) {
$server->router->get('/', handler(...))->try();
$router->get('/', handler(...))->try();
$server->start()->try();
});
}
Expand All @@ -46,6 +48,7 @@ You will need to access your parameter by reference.
<?php
use CatPaw\Web\Interfaces\SessionInterface;
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
use CatPaw\Core\Unsafe;
Expand All @@ -56,9 +59,9 @@ function handler(SessionInterface $session) {
return success($created); // serve the result
}

function main(ServerInterface $server): Unsafe {
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server) {
$server->router->get('/', handler(...))->try();
$router->get('/', handler(...))->try();
$server->start()->try();
});
}
Expand All @@ -79,6 +82,7 @@ You can customize your session behavior by overwriting the `SessionInterface` pr
```php
<?php
use CatPaw\Web\Interfaces\SessionInterface;
use CatPaw\Web\Interfaces\ServerInterface;
use function CatPaw\Core\anyError;
use CatPaw\Core\Unsafe;
use CatPaw\Web\Server;
Expand All @@ -87,8 +91,8 @@ class CustomSession implements SessionInterface {
// Your custom implementation goes here
}

function main(): Unsafe {
return anyError(function(){
function main(ServerInterface $server): Unsafe {
return anyError(function() use($server) {
Container::provide(SessionInterface::class, fn(Request $request) => new CustomSession($request));
$server = Server::get();
$server->start()->try();
Expand Down
5 changes: 3 additions & 2 deletions docs/7.byte-range-requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use CatPaw\Core\Unsafe;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use CatPaw\Web\Interfaces\ServerInterfaces;
use CatPaw\Web\Interfaces\RouterInterface;
use CatPaw\Web\Interfaces\ByteRangeInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\success;
Expand All @@ -34,9 +35,9 @@ function handler(
return success($response);
}

function main(ServerInterface $server): Unsafe {
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server) {
$server->router->get('/{fileName}', handler(...))->try();
$router->get('/{fileName}', handler(...))->try();
$server->start()->try();
});
}
Expand Down
5 changes: 3 additions & 2 deletions docs/8.custom-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use CatPaw\Core\Interfaces\AttributeInterface;
use CatPaw\Core\Traits\CoreAttributeDefinition;
use CatPaw\Core\Interfaces\OnParameterMount;
use CatPaw\Web\Interfaces\ServerInterface;
use CatPaw\Web\Interfaces\RouterInterface;
use function CatPaw\Core\anyError;
use function CatPaw\Core\ok;

Expand All @@ -26,9 +27,9 @@ function handler(#[HelloWorldAttribute] string $greeting){
return $greeting;
}

function main(ServerInterface $server): Unsafe {
function main(ServerInterface $server, RouterInterface $router): Unsafe {
return anyError(function() use($server) {
$server->router->get("/", handler(...))->try();
$router->get("/", handler(...))->try();
$server->start()->try();
});
}
Expand Down

0 comments on commit 5ee1d50

Please sign in to comment.