Search by

miladev / api-response

miladev95

A simple Laravel package for success and fail response traits.

Package info

github.com/miladev95/api-response

pkg:composer/miladev/api-response

Statistics

Installs: 17

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

2.0.0 2026-09-01 06:16 UTC

This package is auto-updated.

Last update: 2026-09-01 06:21:29 UTC


README

A small, framework-friendly trait that standardizes JSON API responses (success and error). The trait is lightweight and designed to be easy to override in consuming applications.

Features

  • Standardized JSON success and error responses
  • Small and framework-agnostic: prefers framework response helpers when available
  • Extensible: override payload shape or header handling

Requirements

  • PHP >= 8.1
  • ext-json

Installation

Install via Composer:

composer require miladev/api-response

Quick usage (Laravel)

<?php

namespace App\Http\Controllers;

use Miladev\ApiResponse\ApiResponse;

class TestController extends Controller
{
    use ApiResponse;

    public function successResponseTest()
    {
        $data = [
            'name' => 'milad',
            'job' => 'dev',
        ];

        // returns JSON with status=200 by default
        return $this->successResponse(data: $data, message: 'OK', statusCode: 200);
    }

    public function failResponseTest()
    {
        // returns JSON error with 404 status
        return $this->failResponse(message: 'Not found', statusCode: 404);
    }
}

Overriding payload structure

If you want a different JSON structure (for example to include meta or to follow a specification), override formatSuccessPayload or formatErrorPayload in your class. Below is a fuller example showing a custom success payload and preserving type hints.

<?php

use Miladev\ApiResponse\ApiResponse;

class MyApiController
{
    use ApiResponse;

    // Example: include a `meta` block and always wrap data under `result`
    protected function formatSuccessPayload($data, string $message): array
    {
        return [
            'status' => self::STATUS_SUCCESS,
            'message' => $message,
            'meta' => [
                'version' => '1.0',
                'timestamp' => time(),
            ],
            'result' => $data,
        ];
    }

    // Optionally override formatErrorPayload similarly to include error codes or details
}

This keeps the trait's public API (successResponse) the same while changing only the response shape.

Overriding headers

If you need to inject or normalize headers (for example, pagination headers), override prepareHeaders. The example below calls the parent to keep the default Content-Type normalization and then adds pagination headers.

<?php

use Miladev\ApiResponse\ApiResponse;

class PaginatedController
{
    use ApiResponse;

    protected function prepareHeaders(array $headers): array
    {
        // Call parent to normalize and ensure Content-Type
        $headers = parent::prepareHeaders($headers);

        // Add pagination metadata into headers
        $headers['X-Total-Count'] = '123';
        $headers['X-Per-Page'] = '25';

        return $headers;
    }
}

Note: prepareHeaders normalizes header values and ensures Content-Type: application/json if not provided.

Macros

You can register custom response helpers at runtime via macro(). Macros are scoped to the class that uses the trait and are invoked with $this bound to the instance (or to the first argument when called statically).

<?php

use Miladev\ApiResponse\ApiResponse;

class TestController extends Controller
{
    use ApiResponse;

    public function boot()
    {
        // Register once (e.g. in a service provider)
        self::macro('teapot', function (string $message = 'I am a teapot') {
            return $this->failResponse($message, 418);
        });
    }

    public function brew()
    {
        return $this->teapot(); // 418 + error JSON
    }

    public function brewStatically()
    {
        return self::teapot($this, 'short and stout');
    }
}

ApiResponse::hasMacro('name') returns whether a macro is registered. Calling an unregistered macro throws BadMethodCallException.

Non-Laravel / Testing

The trait prefers a response() factory when available (Laravel). For testing or non-framework usage you can provide a small response helper. The test bootstrap included in the project demonstrates this approach (it provides a minimal response() helper and a TestResponse object). That keeps unit tests fast and framework-independent.

API reference

Public methods

  • successResponse($data = [], string $message = '', int $statusCode = 200, array $headers = [])

    • Returns a standardized success JSON response. You can pass any payload as $data.
  • failResponse(string $message = '', int $statusCode = 400, array $headers = [])

    • Returns a standardized error JSON response.

Protected / overridable helpers

  • formatSuccessPayload($data, string $message): array — Customize success payload shape.
  • formatErrorPayload(string $message): array — Customize error payload shape.
  • prepareHeaders(array $headers): array — Normalize and add default headers.
  • createResponse($payload, int $statusCode, array $headers) — Central response factory (you can override to integrate with custom response objects).

Testing locally

To run tests locally (from project root):

composer install --no-interaction --prefer-dist
./vendor/bin/phpunit --configuration phpunit.xml

The tests use tests/bootstrap.php which provides a minimal response() helper so tests can run without Laravel.

Roadmap

Planned features and improvements for upcoming releases:

  1. Pagination helper — first-class paginatedResponse($paginator) that auto-includes meta (current page, per page, total) and pagination headers (X-Total-Count, Link).
  2. Built-in exception handler — a reportableExceptions() helper that maps common exceptions (ValidationException, ModelNotFoundException, AuthenticationException) to standard error responses.
  3. Laravel package auto-discovery — add extra.laravel.providers to composer.json so Laravel auto-registers the trait and helpers.
  4. Response caching helpercachedResponse($key, $ttl, $callback) for ETag/Last-Modified style caching wrappers.
  5. Locale-aware messages — support translating $message arguments via Laravel's trans() helper, with a fallback when used outside Laravel.
  6. Convenience shortcutsok(), created(), noContent(), unauthorized(), forbidden(), notFound(), validationError() covering common HTTP status codes.
  7. OpenAPI / JSON:API shape presets — opt-in payload shapes that follow popular API specifications.
  8. Streaming / SSE response helperstreamResponse($generator) for server-sent events and large dataset responses.
  9. PHPStan + GitHub Actions quality gates — add static analysis (level 6+) and code style checks (PHP-CS-Fixer / Pint) to CI alongside the existing PHPUnit run.