Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mops1k/json-rpc-bundle
This bundle provides easy way to up your json-rpc server on symfony with parameters validation and possibility to get parameters as DTO object inside method.
https://github.com/mops1k/json-rpc-bundle
api-documentation json-rpc-server library symfony-bundle
Last synced: about 1 month ago
JSON representation
This bundle provides easy way to up your json-rpc server on symfony with parameters validation and possibility to get parameters as DTO object inside method.
- Host: GitHub
- URL: https://github.com/mops1k/json-rpc-bundle
- Owner: mops1k
- License: mit
- Created: 2024-06-20T19:01:54.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-07-27T08:57:24.000Z (4 months ago)
- Last Synced: 2024-09-30T16:04:22.697Z (about 2 months ago)
- Topics: api-documentation, json-rpc-server, library, symfony-bundle
- Language: PHP
- Homepage:
- Size: 110 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON-RPC Server bundle
This bundle provide an easy way to implement json-rpc server with fully specification supporting.
Bundle supports multiple procedure calls in one request as described in specification.[JSON-RPC](https://www.jsonrpc.org/specification) specification is fully compatible
with [CQRS](https://en.wikipedia.org/wiki/Command_Query_Responsibility_Segregation) architecture## Install
```bash
composer require mops1k/json-rpc-bundle
```Import route declaration in your routes:
```yaml
#config/routes/json-rpc.yaml
app_file:
# loads routes from the given routing file stored in some bundle
resource: '@JsonRpcBundle/Resources/config/routing/json-rpc-bundle.yaml'
```Or add your own paths by template:
```yaml
json_rpc_entrypoint:
path: '/path/to/rpc'
methods: 'POST'
controller: 'JsonRpcBundle\Controller\JsonRpcController'json_rpc_namespace_entrypoint:
path: '/path/to/rpc/{namespace}'
methods: 'POST'
controller: 'JsonRpcBundle\Controller\JsonRpcController'
```## Usage
To create method you have to create invokable class with
attribute [`\JsonRpcBundle\Attribute\AsRpcMethod`](./src/Attribute/AsRpcMethod.php), where `methodName` constructor
parameter must contain method name. Example:```php
$contract->id,
'text' => $contract->text,
];
}
}
```#### Validation for method params without contract
If you don't want to use DTO, you still able to validate method parameters and set its groups. In this case you need to
implement your method class
from [`\JsonRpcBundle\MethodResolver\ValidateMethodParametersInterface`](./src/MethodResolver/ValidateMethodParametersInterface.php).
Example:```php
[
new GreaterThanOrEqual(0),
],
], groups: ['rpc']);
}public function validationGroups(): array|string|GroupSequence|null
{
return ['rpc'];
}
}
```#### Notification
Json rpc supports notification requests what does not return any response. To make your method as notification, just
add `void` in `__invoke` return type hint.
Example:```php
use JsonRpcBundle\Attribute\AsRpcMethod;#[AsRpcMethod('updateUser')]
class UpdateUser
{
public function __invoke(int $id): void
{
// some logic
}
}
```#### Namespace
Bundle supports method namespacing. To set method namespace, use `\JsonRpcBundle\Attribute\AsRpcMethod::$namespace`
attribute parameter.
Example:```php
use JsonRpcBundle\Attribute\AsRpcMethod;#[AsRpcMethod(methodName: 'update', namespace: 'user')]
class UpdateUser
{
public function __invoke(int $id): void
{
// some logic
}
}
```To fetch namespaced method you can call it by method name (`namespace.methodName`) or call to path `/rpc/{namespace}`
and use regular method name.
Examples:1. Call to `/rpc`:
```json
{
"jsonrpc": "2.0",
"method": "user.update",
"params": null,
"id": 32
}
```
2. Call to `/rpc/user`:
```json
{
"jsonrpc": "2.0",
"method": "update",
"params": null,
"id": 32
}
```> This feature also supports bath requests.
## API Documentation
![documentation example](./doc-example.png "documentation example")
Bundle supports documentation semi-auto generation
thru [nelmio/api-doc-bundle](https://github.com/nelmio/NelmioApiDocBundle).
By default documentation generate params from method parameters, method contract. If you need more specific params
documentation,
just use `OA\Property` attribute. If you need specific response result, then
use `\JsonRpcBundle\ApiDoc\Attribute\Result`
attribute (set array of `OA\Property` inside `$properties` parameter).
Object responses auto-generated to documentation by it's describing inside class.