dragon-code/laravel-http-macros

扩展 Laravel HTTP 客户端的功能

1.3.1 2024-08-25 10:09 UTC

This package is auto-updated.

Last update: 2024-08-25 10:10:45 UTC


README

the dragon code laravel http macros

Stable Version Total Downloads Github Workflow Status License

安装

要获取最新版本的 HTTP 宏,只需使用 Composer 引入项目即可

composer require dragon-code/laravel-http-macros

配置

如果需要,您可以使用控制台命令发布配置文件

php artisan vendor:publish --provider="DragonCode\\LaravelHttpMacros\\ServiceProvider"

如果您的应用程序已经存在一个 config/http.php 文件,那么您可以直接从 配置文件 中添加一个新的 macros 键。

在这里,您可以指定用于注册宏的类列表。宏类必须继承自抽象类 DragonCode\LaravelHttpMacros\Macros\Macro

您还可以使用关联数组重新定义宏名称。例如

// Config
return [
    'macros' => [
        'request' => [
            WithLoggerMacro::class,
        ],
        'response' => [
            ToDataMacro::class,
        ],
    ],
];

// Macro
Http::withLogger('some')->get();
Http::withLogger('some')->get()->toData(...);
Http::get()->toData(...);
// Config
return [
    'macros' => [
        'request' => [
            'qwerty' => WithLoggerMacro::class,
        ],
        'response' => [
            'qwerty' => ToDataMacro::class,
        ],
    ],
];

// Macro
Http::qwerty('some')->get();
Http::qwerty('some')->get()->qwerty(...);
Http::qwerty('some')->get()->toData(...); // method not found

Http::get()->qwerty(...);
Http::get()->toData(...); // method not found

用法

可用方法

请求

响应

方法列表

withLogger()

增加了记录 HTTP 请求和响应的能力。

use Illuminate\Support\Facades\Http;

Http::withLogger('some_channel')->get();

此方法将记录 HTTP 请求和响应。

您还可以使用自己的处理器、消息格式和日志文件的路径。为此,您需要指定日志文件中的所需通道名称,并在其中定义必要的参数。

例如

// config/logging.php
return [
    // ...
    
    'channels' => [
        'some' => [
            'driver' => 'single',
            'level' => env('LOG_LEVEL', 'debug'),
            'path' => storage_path('logs/some.log'),
            'handler' => \App\Logging\SomeHandlerStack::class,
            'formatter' => \App\Logging\MessageFormatter::class,
        ],
    ],
];

// Usage
return Http::withLogger('some')->...

toData()

将返回类实例。

use Illuminate\Support\Facades\Http;

// Returns a SomeData object
return Http::get()->toData(SomeData::class);

// Will return a SomeData object generated from the JSON path
return Http::get()->toData(SomeData::class, 'data.item');

// Returns the result of the callback execution
return Http::get()->toData(
    fn (array $data) => new SomeData(
        $data['data']['item']['id'],
        $data['data']['item']['title']
    )
);

// Returns the result of the callback execution from a custom JSON path
return Http::get()->toData(
    fn (array $data) => new SomeData($data['id'], $data['title']),
    'data.item'
);

注意

如果类中存在一个 from 方法,则将调用它来构造对象。

Spatie Laravel Data 兼容。

class SomeData
{
    public function __construct(
        public int $id,
        public string $title
    ) {}
    
    public static function from(array $data): static
    {
        return new static(...$data);
    }
}

return Http::get()->toData(SomeData::class);

toDataCollection()

将返回 Illuminate\Support\Collection 对象或继承自它的对象。

use Illuminate\Support\Facades\Http;

// Returns a collection of SomeData objects
return Http::get()->toDataCollection(SomeData::class);

// Returns a collection of SomeData objects formed from the JSON path
return Http::get()->toDataCollection(SomeData::class, 'data.item');

// Returns the result of the callback execution
return Http::get()->toDataCollection(
    fn (array $data) => collect([
        new SomeData(
            $data['data']['item']['id'],
            $data['data']['item']['title']
        ),
    ])
);

// Returns the result of the callback execution from a custom JSON path
return Http::get()->toDataCollection(
    fn (array $data) => collect([
        new SomeData(...$data),
    ]),
    'data.item'
);

注意

如果类中存在一个 collect 方法,则将调用它来构造集合。

Spatie Laravel Data 兼容。

use Illuminate\Support\Collection;

class SomeData
{
    public function __construct(
        public int $id,
        public string $title
    ) {}
    
    public static function collect(array $items): Collection
    {
        return collect($items)->map(
            fn (array $item) => new static(...$item)
        );
    }
}

return Http::get()->toDataCollection(SomeData::class);

生成 IDE 辅助文件

您可以使用控制台命令为 IDE 生成辅助文件

php artisan http:macros-helper

这将帮助您的 IDE 提示方法。

IDE Helper

许可证

本软件包根据 MIT 许可证 授权。