Skip to content

Commit

Permalink
docs: Typos
Browse files Browse the repository at this point in the history
  • Loading branch information
matapatos committed May 21, 2024
1 parent fa4db85 commit 8deca93
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
26 changes: 14 additions & 12 deletions docs/docs/quick-start/router.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use Wp\FastEndpoints\Router;
$router = $router ?? new Router('posts');
```

A router is just an instance which allow us to attach and register endpoints.
A router is a class which allow us to attach and register endpoints.

We can have an application with one or multiple routers. One main benefit of using multiple routers is to group endpoints by same namespace and (optionally) same version. For instance,
An application can have one or multiple routers. One main benefit of using multiple routers is to group endpoints
by same namespace and (optionally) same version. For instance,
in this tutorial we are going to create a main router with a base namespace `my-plugin` and a version of `v1`
which will add `/my-plugin/v1/` to the beginning of each attached endpoint from sub-routers.
which will add `/my-plugin/v1/` to the beginning of each attached endpoint from all sub-routers.

#### Create a post

With the posts router in place we can now start attaching our endpoints. We start adding the one to that
allows a user to create a blog post.
With the posts router in place we can now start attaching our endpoints. We start adding the one
responsible to create a new blog post.

```php
$router->post('/', function (\WP_REST_Request $request): int|\WP_Error {
Expand All @@ -30,10 +31,11 @@ $router->post('/', function (\WP_REST_Request $request): int|\WP_Error {

When a request is received by this endpoint the following happens:

1) Firstly, the user permissions are checked - Makes sure that the user has [*publish_posts*](https://wordpress.org/documentation/article/roles-and-capabilities/#publish_posts) capability
2) Then, if successful, it validates the request payload by using the *Posts/CreateOrUpdate* schema.
We still didn't specify where the endpoints should look for the schemas, but don't worry we are getting into that in a moment
3) Lastly, if the validation process also passes the handler is called.
1. Firstly, the user permissions are checked - Makes sure that the user has [*publish_posts*](https://wordpress.org/documentation/article/roles-and-capabilities/#publish_posts) capability
2. Then, if successful, it validates the request payload by using the *Posts/CreateOrUpdate* schema.
We still didn't explain where the endpoints should look for the schemas, but will get into that
in [Service Provider page](/wp-fastendpoints/quick-start/service-provider)
3. Lastly, if the validation process also passes the handler is called.

!!! info
In this scenario we are not using a JSON schema to discard fields because the [_wp_insert_post_](https://developer.wordpress.org/reference/functions/wp_insert_post/)
Expand Down Expand Up @@ -65,13 +67,13 @@ to match only positive integers though 🤔

Going back to the endpoint, this is what happens if a request comes in:

1) Firstly, it checks the user has [_read_](https://wordpress.org/documentation/article/roles-and-capabilities/#read)
1. Firstly, it checks the user has [_read_](https://wordpress.org/documentation/article/roles-and-capabilities/#read)
capability - one of the lowest WordPress users capabilities
2) If so, it then calls the handler which either retrieves the post data (e.g. array or object)
2. If so, it then calls the handler which either retrieves the post data (e.g. array or object)
or a [_WpError_](https://github.com/matapatos/wp-fastendpoints/blob/main/src/Helpers/WpError.php)
in case that is not found. If a WpError or WP_Error is returned it stops further code execution
and returns that error message to the client - avoiding triggering response schema validation for example.
3) Lastly, if the post data is returned by the handler the response schema will be triggered
3. Lastly, if the post data is returned by the handler the response schema will be triggered
and will check the response according to the given schema (e.g. _Posts/Get_)

!!! note
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/quick-start/service-provider.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Now that we have our posts router built the last main three bits missing are the following:

1) create a main router to hold all sub-routers (e.g. posts router)
2) specifying where to look for the JSON schemas (one or multiple directories) and
3) lastly register the router. This is what adds the `rest_api_init` hook for registering all
1. Create a main router to hold all sub-routers (e.g. posts router)
2. Specifying where to look for the JSON schemas (one or multiple directories) and
3. Lastly, register the router. This is what adds the `rest_api_init` hook for registering all
the endpoints.

```php
Expand Down
35 changes: 22 additions & 13 deletions docs/docs/release.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## v1.2.2

Three new filters that allows us to add customise our JSON schema validator.
Three new filters that allows us to customise our JSON schema validator's.

- `fastendpoints_validator` - Triggered by both middleware's
- `fastendpoints_schema_validator` - Only triggered for Schema middleware's validators
- `fastendpoints_response_validator` - Only triggered for Response middleware's validators
- `fastendpoints_validator` - Triggered by both middlewares
- `fastendpoints_schema_validator` - Only triggered for Schema middlewares validators
- `fastendpoints_response_validator` - Only triggered for Response middlewares validators

```php
use Opis\JsonSchema\Validator;
Expand All @@ -21,10 +21,10 @@ add_filter('fastendpoints_validator', function (Validator $validator): Validator

!!! info
For more customisations check the following links:
- [Custom formats](https://opis.io/json-schema/2.x/php-format.html)
- [Custom filters](https://opis.io/json-schema/2.x/php-filter.html)
- [Custom media types](https://opis.io/json-schema/2.x/php-media-type.html)
- [Custom content encoding](https://opis.io/json-schema/2.x/php-content-encoding.html)
1) [Custom formats](https://opis.io/json-schema/2.x/php-format.html),
2) [Custom filters](https://opis.io/json-schema/2.x/php-filter.html),
3) [Custom media types](https://opis.io/json-schema/2.x/php-media-type.html) and,
4) [Custom content encoding](https://opis.io/json-schema/2.x/php-content-encoding.html)

## v1.2.1

Expand All @@ -40,8 +40,7 @@ $router->appendSchemaDir('/my-dir', 'http://www.example.com');

## v1.2.0

We now can take advantage of dependency injection in main handler, middlewares and permission's
handlers.
Dependency injection support in main handler, middlewares and permission handlers.

```php
// In the past, the $request parameter was mandatory:
Expand All @@ -53,6 +52,15 @@ $router->get('/posts/(?P<ID>[\d]+)', function (WP_REST_Request $request) {
$router->get('/posts/(?P<ID>[\d]+)', function ($ID) {
return $ID;
});
// Middleware changes
class MyCustomMiddleware extends \Wp\FastEndpoints\Contracts\Middleware {
public function onRequest(/* Type what you need e.g. $request */) {
// Called before handling the request
}
public function onResponse(/* Type what you need e.g. $response, $request */) {
// Called after the request being handled
}
}
```

## v1.1.0
Expand All @@ -67,12 +75,13 @@ $router->get('/posts/(?P<ID>[\d]+)', function ($ID) {
```php
// Middleware example
class MyCustomMiddleware extends \Wp\FastEndpoints\Contracts\Middleware {
public function onRequest(/* Type what you need e.g. $request, URL params*/) {
public function onRequest(\WP_REST_Request $request): ?\WP_Error {
// Called before handling the request
return null;
}

public function onResponse(/* Type what you need e.g. $response, $request */) {
public function onResponse(\WP_REST_Request $request, mixed $response): mixed {
// Called after the request being handled
return $response;
}
}
```
Expand Down

0 comments on commit 8deca93

Please sign in to comment.