5

GitHub - JustSteveKing/laravel-transporter: Transporter is a futuristic way to s...

 2 years ago
source link: https://github.com/JustSteveKing/laravel-transporter
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Transporter

Transporter is a futuristic way to send API requests in PHP. This is an OOP approach to handle API requests.

This package is still a work in progress

Installation

You can install the package via composer:

composer require juststeveking/laravel-transporter

Generating Request

To generate an API request to use with Transporter, you can use the Artisan make command:

php artisan make:api-request NameOfYourRequest

This will by default publish as: app/Transporter/Requests/NameOfYourRequest.php

Usage

Transporter Requests are an extention of Laravels PendingRequest so all of the methods available on a Pending Request is available to you on your requests.

Also when you send the request, you will receive a Illuminate\Http\Client\Response back, allowing you to do things such as collect($key) and json() and failed() very easily. We are simply just shifting how we send it into a class based approach.

TestRequest::build()
    ->withToken('foobar')
    ->withData([
        'title' => 'Build a package'
    ])
    ->send()
    ->json();

When building your request to send, you can override the following:

  • Request Data using withData(array $data)
  • Request Query Params using withQuery(array $query)
  • Request Path using setPath(string $path)

Faking a Request

Thanks to a fantastic PR, we can now fake requests for testing. To do so:

use JustSteveKing\Transporter\Request;

Request::fake();

TestRequest::build()
    ->withToken('foobar')
    ->withData([
        'title' => 'Build a package'
    ])
    ->withFakeData([
        'data' => 'faked'
    ])
    ->send();

Testing

To run the tests in parallel:

composer run test

To run the tests with a coverage report:

composer run test-coverage

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK