https://github.com/efureev/response-actions
Single Action structure for HTTP-Response
https://github.com/efureev/response-actions
Last synced: 5 months ago
JSON representation
Single Action structure for HTTP-Response
- Host: GitHub
- URL: https://github.com/efureev/response-actions
- Owner: efureev
- Created: 2023-04-21T16:01:31.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-05T12:53:53.000Z (over 1 year ago)
- Last Synced: 2025-01-06T09:06:29.425Z (over 1 year ago)
- Language: PHP
- Size: 40 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
# Response Action

[](https://github.com/efureev/response-actions/actions/workflows/php.yml)
[](https://travis-ci.org/efureev/response-actions)
[](https://packagist.org/packages/efureev/response-actions)
[](https://qlty.sh/gh/efureev/projects/response-actions)
[](https://qlty.sh/gh/efureev/projects/response-actions)
[](https://codecov.io/github/efureev/response-actions)
## Install
For php >= 8.4
```bash
composer require efureev/response-actions "^2.0"
```
## Action Message Response Structure
```json5
{
// Action Message block (can be overwritten with a custom key)
"_responseAction": {
// Request Execution Status. See: ResponseActions\\StatusEnum
"status": "success",
// list of actions: array of ResponseActions\\Actions\\Action
"actions": [
{
// ... some action's body
}
],
// optional extra payload attached to the response action
"extra": { }
}
}
```
### Actions
- Message
- Command
- Download
- Event
- Redirect
#### Common Action's Props
```json5
{
// Action's Name
"name": "message",
// Action order to perform. Default = 0
"order": 1,
// Private action. boolean true or string channel name
"private": true
}
```
#### Action Message
```json5
{
// message to show to user
"message": "It's done!",
// Type of the message (optional when type is empty)
"type": "info"
}
```
```php
use ResponseActions\ResponseAction;
use ResponseActions\Actions\Message;
ResponseAction::successMessage('Operation has success!');
// Multi-message with different types
ResponseAction::errorMessage('Operation has failed!')
->addAction(Message::info('Try to restart page'));
```
#### Action Command
```json5
{
// pending | done | failed
"status": "failed",
// optional description
"description": "Reason..."
}
```
Helpers:
- ResponseAction::cmd() // pending
- ResponseAction::cmdDone() // done
- ResponseAction::cmdFailed() // failed
#### Action Download
```json5
{
"url": "https://example.com/file.pdf",
"file": "Readme.pdf",
// optional params passed to the client handler
"params": {}
}
```
#### Action Event
```json5
{
"event": "uploadData",
"params": {}
}
```
#### Action Redirect
```json5
{
"url": "https://example.com",
"target": "_blank",
// native | router
"type": "native",
// HTTP code (when applicable)
"code": 302
}
```
You can also use helpers:
- ResponseAction::redirect('https://example.com')
- ResponseActions\\Actions\\Redirect::router('/route')
- ResponseActions\\Actions\\Redirect::native('https://example.com')
### Private
You can use private props and order:
```php
use ResponseActions\ResponseAction;
use ResponseActions\Actions\{Event, Redirect, Download};
$responseAction = ResponseAction::successMessage('Operation has done!')
->addAction(
new Event('log', ['saved!', 'continue watching...']),
(new Event('uploadModuleData'))->private(),
(new Event('uploadData'))->private('menu'),
(new Event('refreshUser'))->private('authUser')->withOrder(1),
(new Redirect('https://example.com'))->withOrder(5),
(new Download('https://example.com/file.pdf', 'Readme.pdf'))->withOrder(2),
);
```
### ExtraData
You can attach extra data to the whole ResponseAction or to Message actions:
```php
use ResponseActions\ResponseAction;
$responseAction = ResponseAction::cmdDone()
->withExtra(['any' => 'thing']);
```