Skip to content

Commit 875c5b3

Browse files
committed
Adds http-api package
1 parent c1561e0 commit 875c5b3

19 files changed

+562
-0
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"tempest/event-bus": "self.version",
8383
"tempest/generation": "self.version",
8484
"tempest/http": "self.version",
85+
"tempest/http-api": "self.version",
8586
"tempest/http-client": "self.version",
8687
"tempest/log": "self.version",
8788
"tempest/mapper": "self.version",
@@ -115,6 +116,7 @@
115116
"Tempest\\EventBus\\": "packages/event-bus/src",
116117
"Tempest\\Framework\\": "src/Tempest/Framework",
117118
"Tempest\\Generation\\": "packages/generation/src",
119+
"Tempest\\HttpApi\\": "packages/http-api/src",
118120
"Tempest\\HttpClient\\": "packages/http-client/src",
119121
"Tempest\\Http\\": "packages/http/src",
120122
"Tempest\\Log\\": "packages/log/src",
@@ -171,6 +173,7 @@
171173
"Tempest\\DateTime\\Tests\\": "packages/datetime/tests",
172174
"Tempest\\EventBus\\Tests\\": "packages/event-bus/tests",
173175
"Tempest\\Generation\\Tests\\": "packages/generation/tests",
176+
"Tempest\\HttpApi\\Tests\\": "packages/http-api/tests",
174177
"Tempest\\HttpClient\\Tests\\": "packages/http-client/tests",
175178
"Tempest\\Http\\Tests\\": "packages/http/tests",
176179
"Tempest\\Log\\Tests\\": "packages/log/tests",

packages/http-api/.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Exclude build/test files from the release
2+
.github/ export-ignore
3+
tests/ export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
phpunit.xml export-ignore
7+
README.md export-ignore
8+
9+
# Configure diff output
10+
*.view.php diff=html
11+
*.php diff=php
12+
*.css diff=css
13+
*.html diff=html
14+
*.md diff=markdown

packages/http-api/LICENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Brent Roose [email protected]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/http-api/composer.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "tempest/http-api",
3+
"description": "Standard API HTTP request/response objects.",
4+
"license": "MIT",
5+
"minimum-stability": "dev",
6+
"require": {
7+
"php": "^8.4",
8+
"tempest/core": "dev-main",
9+
"tempest/clock": "dev-main",
10+
"tempest/console": "dev-main",
11+
"tempest/http": "dev-main",
12+
"tempest/mapper": "dev-main",
13+
"tempest/container": "dev-main"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"Tempest\\HttpApi\\": "src"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"Tempest\\HttpApi\\Tests\\": "tests"
23+
}
24+
}
25+
}

packages/http-api/phpunit.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.4/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" displayDetailsOnPhpunitDeprecations="true" failOnPhpunitDeprecation="false" failOnRisky="true" failOnWarning="true">
3+
<testsuites>
4+
<testsuite name="Tempest Http API">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<source restrictNotices="true" restrictWarnings="true" ignoreIndirectDeprecations="true">
9+
<include>
10+
<directory>src</directory>
11+
</include>
12+
</source>
13+
</phpunit>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Tempest\HttpApi;
4+
5+
use Attribute;
6+
use Tempest\Http\Method;
7+
use Tempest\Router\Route;
8+
9+
use function Tempest\Support\path;
10+
11+
#[Attribute]
12+
class ApiResourceRoute implements Route
13+
{
14+
/**
15+
* @param class-string $resourceClass
16+
* @param list<class-string<\Tempest\Router\HttpMiddleware>> $middleware
17+
*/
18+
public function __construct(
19+
string $resourceClass,
20+
public Method $method,
21+
public string $uri = '',
22+
public array $middleware = [],
23+
) {
24+
$resourceName = $resourceClass::getResourceUriName();
25+
$version = $resourceClass::getResourceApiVersion();
26+
$this->uri = path("/api/{$version}", $resourceName, $uri);
27+
}
28+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\HttpApi;
6+
7+
use Tempest\Http\IsResponse;
8+
use Tempest\Http\Response;
9+
use Tempest\Http\Status;
10+
11+
final class ApiResponse implements Response
12+
{
13+
use IsResponse;
14+
15+
public function __construct(
16+
Status $status,
17+
array $body,
18+
array $headers = [],
19+
array $extraData = [],
20+
?string $dataWrappingObject = 'data',
21+
) {
22+
$this->status = $status;
23+
24+
$this->body = $dataWrappingObject
25+
? [
26+
$dataWrappingObject => $body,
27+
...$extraData,
28+
]
29+
: $body;
30+
31+
$this->view = null;
32+
$headers = [
33+
...$headers,
34+
'Content-Type' => 'application/json',
35+
];
36+
37+
foreach ($headers as $key => $values) {
38+
if (! is_array($values)) {
39+
$values = [$values];
40+
}
41+
42+
foreach ($values as $value) {
43+
$this->addHeader($key, $value);
44+
}
45+
}
46+
}
47+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tempest\HttpApi;
4+
5+
final readonly class CursorPagination implements Pagination
6+
{
7+
public function __construct(
8+
public int $defaultLimit = 15,
9+
) {}
10+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Tempest\HttpApi;
4+
5+
use Tempest\Discovery\SkipDiscovery;
6+
use Tempest\Http\Method;
7+
use Tempest\Http\Request;
8+
use Tempest\Http\Status;
9+
use Tempest\HttpApi\ApiResourceRoute;
10+
use Tempest\HttpApi\ApiResponse;
11+
use Tempest\HttpApi\HasResourceAttributes;
12+
use Tempest\HttpApi\HasResourceRecord;
13+
14+
use function Tempest\Database\query;
15+
use function Tempest\map;
16+
17+
#[SkipDiscovery]
18+
trait HasApiCreateEndpoint
19+
{
20+
use HasResourceRecord;
21+
use HasResourceAttributes;
22+
23+
#[ApiResourceRoute(self::class, method: Method::POST)]
24+
public function apiCreate(Request $request): ApiResponse
25+
{
26+
$recordId = query(static::getResourceRecord())
27+
->insert($request->body)
28+
->execute();
29+
30+
$record = query(static::getResourceRecord())
31+
->select()
32+
->get($recordId);
33+
34+
return new ApiResponse(
35+
status: Status::CREATED,
36+
body: map($record)->toArray(),
37+
);
38+
}
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Tempest\HttpApi;
4+
5+
use Tempest\Discovery\SkipDiscovery;
6+
use Tempest\Http\Method;
7+
use Tempest\Http\Response;
8+
use Tempest\Http\Status;
9+
use Tempest\HttpApi\ApiResourceRoute;
10+
use Tempest\HttpApi\ApiResponse;
11+
use Tempest\HttpApi\HasResourceAttributes;
12+
use Tempest\HttpApi\HasResourceRecord;
13+
14+
use function Tempest\Database\query;
15+
16+
#[SkipDiscovery]
17+
trait HasApiDeleteEndpoint
18+
{
19+
use HasResourceRecord;
20+
use HasResourceAttributes;
21+
22+
#[ApiResourceRoute(self::class, method: Method::DELETE, uri: '{resourceRecordId}')]
23+
public function apiDelete(string|int $resourceRecordId): Response
24+
{
25+
// TODO: Validate if record exists?
26+
27+
query(static::getResourceRecord())
28+
->delete()
29+
->where('id = ?', $resourceRecordId)
30+
->execute();
31+
32+
return new ApiResponse(
33+
status: Status::NO_CONTENT,
34+
body: [],
35+
);
36+
}
37+
}

0 commit comments

Comments
 (0)