Skip to content

Commit 807905f

Browse files
committed
Update README, use modern typed response.
1 parent 3d18711 commit 807905f

1 file changed

Lines changed: 27 additions & 15 deletions

File tree

README.md

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ final class MyAwesomeEndpoint extends BaseEndpoint
4646
*
4747
* @param string $hello some user-defined parameter.
4848
*/
49-
public function actionDefault(string $hello = 'world'): array
49+
public function actionDefault(string $hello = 'world'): MyAwesomeResponse
5050
{
51-
// The endpoint response can be simply returned as an array or object.
51+
// The endpoint response can be simply returned as an simple array or typed object.
52+
// A type object is much better because it will be used as the basis for documentation.
5253
// It will be automatically converted to JSON.
5354

54-
return [
55-
'name' => 'Test API endpoint',
56-
'hello' => $hello,
57-
]);
55+
return new MyAwesomeResponse(
56+
name: 'Test API endpoint',
57+
hello: $hello,
58+
);
5859
}
5960

6061
// or use old syntax:
@@ -72,15 +73,17 @@ final class MyAwesomeEndpoint extends BaseEndpoint
7273
]);
7374
}
7475

76+
// or return simple array directly:
77+
7578
/**
7679
* @param array<mixed, mixed> $data
7780
*/
78-
public function postCreateUser(array $data): void
81+
public function postCreateUser(array $data): array
7982
{
80-
$this->sendJson([
83+
return [
8184
'state' => 'ok',
8285
'data' => $data,
83-
]);
86+
];
8487
}
8588
}
8689
```
@@ -137,7 +140,7 @@ List of aliases (aliases are optional):
137140

138141
For processing complex data structures, it may be useful to obtain the data in its original raw form.
139142

140-
The library reserves the key variable `array $ data`, which always contains the original input values from the user, regardless of validation.
143+
The library reserves the key variable `array $data`, which always contains the original input values from the user, regardless of validation.
141144

142145
For example:
143146

@@ -146,7 +149,7 @@ final class OrderEndpoint extends BaseEndpoint
146149
{
147150
public function postProcessOrder(array $data): void
148151
{
149-
// variable $data constains all raw data from user.
152+
// variable $data contains all raw data from user.
150153
}
151154
}
152155
```
@@ -174,8 +177,17 @@ final class ArticleEndpoint extends BaseEndpoint
174177
* @param string|null $sort sort by supported field
175178
* @param string|null $orderBy direction by `ASC` or `DESC`
176179
*/
177-
public function actionDefault(?string $locale = null, int $page = 1, int $limit = 32, ?string $status = null, ?string $query = null, ?string $filterFrom = null, ?string $filterTo = null, ?string $sort = null, ?string $orderBy = null): void
178-
{
180+
public function actionDefault(
181+
?string $locale = null,
182+
int $page = 1,
183+
int $limit = 32,
184+
?string $status = null,
185+
?string $query = null,
186+
?string $filterFrom = null,
187+
?string $filterTo = null,
188+
?string $sort = null,
189+
?string $orderBy = null,
190+
): void {
179191
}
180192
}
181193
```
@@ -208,9 +220,9 @@ final class ArticleEndpoint extends BaseEndpoint
208220
}
209221
```
210222

211-
Each method always returns a type of `void` and the output logic is solved via methods. The reason is that it is often necessary to pass a number of parameters and one output is not enough.
223+
Each method can return output either via `send` methods or directly as a type object. A more modern approach is to return the entire object, as this gives us type checking and the underlying basis for automatically generated documentation.
212224

213-
> **Warning:** If you do not pass any output, endpoint processing will fail.
225+
> **Warning:** If you do not pass any output (by method or return statement), endpoint processing will fail.
214226
215227
When processing actions, it is a good idea to return success or error information:
216228

0 commit comments

Comments
 (0)