efureev/response-actions

Single Action structure for HTTP-Response

v1.5.1 2024-12-05 11:52 UTC

This package is auto-updated.

Last update: 2024-12-05 12:53:56 UTC


README

Action Message Response Structure

{
  // Logic data block
  "data": [],
  // Action Message block (can be overwritten)
  "_responseAction": {
    // Request Execution Status. See: \App\Shared\UI\Response\ActionMessage\Types\StatusEnum::name()
    "status": "success",
    // list of actions \App\Shared\UI\Response\ActionMessage\Actions\Action
    "actions": [
      {
        // ... some action's body
      }
    ]
  }
}

Actions

  • Message
  • MessageError: Message with Code
  • Command
  • HttpCommand
  • Download
  • Event
  • Redirect

Common Action's Props

{
  // Action's Name
  "name": "message",
  // Action Order to perform. Default = 0
  "order": 1,
}

Action Message

{
  // message to show to user
  "message": "It's done!",
  // Type of the message
  "type": "info",
}
ResponseAction::successMessage('Operation has success!');

You can make multi-message with different type:

ResponseAction::errorMessage('Operation has failed!')
    ->addAction(Message::info('Try to restart page'));

Action MessageError

{
  // message to show to user
  "message": "It's done!",
  // type: int|string
  "code": 321,
  "type": "error",
}

Action Command

{
  // done | failed
  "status": "failed",
  // type: string|null
  "description": "Reason..."
}

Action Download

{
  "url": "https://example.com/file.pdf",
  "name": "Readme.pdf",
}

Action Event

{
  "event": "uploadData",
  "params": [],
}

Action Redirect

{
  "url": "https://example.com",
  "target": "_blank",
}

Private

You can use private props:

$responseAction = ResponseAction::successMessage('Operation has done!')
    ->addAction(
        Event::make('log', ['saved!', 'continue watching...']),
        Event::make('uploadModuleData')->private(),
        Event::make('uploadData')->private('menu'),
        Event::make('refreshUser')->private('authUser')->setOrder(1),
        Redirect::make('https://example.com')->setOrder(5),
        Download::make('https://example.com/file.pdf', 'Readme.pdf')->setOrder(2),
    );

ExtraData

You can use extra data:

$responseAction = ResponseAction::cmdDone()
    ->withExtra([]);