{"id":14974639,"url":"https://github.com/thedragoncode/laravel-http-macros","last_synced_at":"2025-10-27T09:31:27.563Z","repository":{"id":254304300,"uuid":"846133014","full_name":"TheDragonCode/laravel-http-macros","owner":"TheDragonCode","description":"Extending the functionality of the Laravel HTTP client","archived":false,"fork":false,"pushed_at":"2025-01-02T16:42:20.000Z","size":103,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-01T04:41:18.398Z","etag":null,"topics":["http","http-client","laravel","laravel-framework","macros"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TheDragonCode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://boosty.to/dragon-code","https://yoomoney.ru/to/410012608840929","https://www.donationalerts.com/r/dragon_code"]}},"created_at":"2024-08-22T15:40:21.000Z","updated_at":"2025-01-02T16:42:22.000Z","dependencies_parsed_at":"2025-01-02T17:34:13.184Z","dependency_job_id":"1bf71dc3-1268-4942-a957-6678a9606451","html_url":"https://github.com/TheDragonCode/laravel-http-macros","commit_stats":{"total_commits":23,"total_committers":1,"mean_commits":23.0,"dds":0.0,"last_synced_commit":"e9c7f1912c890351bc3a46f5d27e7cb5a02a65be"},"previous_names":["thedragoncode/laravel-http-macros"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDragonCode%2Flaravel-http-macros","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDragonCode%2Flaravel-http-macros/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDragonCode%2Flaravel-http-macros/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheDragonCode%2Flaravel-http-macros/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheDragonCode","download_url":"https://codeload.github.com/TheDragonCode/laravel-http-macros/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238472013,"owners_count":19478144,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["http","http-client","laravel","laravel-framework","macros"],"created_at":"2024-09-24T13:50:50.808Z","updated_at":"2025-10-27T09:31:27.191Z","avatar_url":"https://github.com/TheDragonCode.png","language":"PHP","funding_links":["https://boosty.to/dragon-code","https://yoomoney.ru/to/410012608840929","https://www.donationalerts.com/r/dragon_code"],"categories":[],"sub_categories":[],"readme":"# HTTP Macros for Laravel\n\n![the dragon code laravel http macros](https://preview.dragon-code.pro/the-dragon-code/http-macros.svg?brand=laravel\u0026mode=dark)\n\n[![Stable Version][badge_stable]][link_packagist]\n[![Total Downloads][badge_downloads]][link_packagist]\n[![Github Workflow Status][badge_build]][link_build]\n[![License][badge_license]][link_license]\n\n## Installation\n\nTo get the latest version of `HTTP Macros`, simply require the project using [Composer](https://getcomposer.org):\n\n```Bash\ncomposer require dragon-code/laravel-http-macros\n```\n\n## Configuration\n\nIf desired, you can publish the configuration file using the console command:\n\n```bash\nphp artisan vendor:publish --provider=\"DragonCode\\\\LaravelHttpMacros\\\\ServiceProvider\"\n```\n\nIf your application already has a `config/http.php` file, then you can simply add a new `macros` key from the\n[configuration](config/http.php) file to it.\n\nHere you can specify a list of your classes for registering macros.\nMacro classes must inherit from the abstract class `DragonCode\\LaravelHttpMacros\\Macros\\Macro`.\n\nYou can also redefine macro names using an associative array. For example:\n\n```php\n// Config\nreturn [\n    'macros' =\u003e [\n        'request' =\u003e [\n            WithLoggerMacro::class,\n        ],\n        'response' =\u003e [\n            ToDataMacro::class,\n        ],\n    ],\n];\n\n// Macro\nHttp::withLogger('some')-\u003eget();\nHttp::withLogger('some')-\u003eget()-\u003etoData(...);\nHttp::get()-\u003etoData(...);\n```\n```php\n// Config\nreturn [\n    'macros' =\u003e [\n        'request' =\u003e [\n            'qwerty' =\u003e WithLoggerMacro::class,\n        ],\n        'response' =\u003e [\n            'qwerty' =\u003e ToDataMacro::class,\n        ],\n    ],\n];\n\n// Macro\nHttp::qwerty('some')-\u003eget();\nHttp::qwerty('some')-\u003eget()-\u003eqwerty(...);\nHttp::qwerty('some')-\u003eget()-\u003etoData(...); // method not found\n\nHttp::get()-\u003eqwerty(...);\nHttp::get()-\u003etoData(...); // method not found\n```\n\n## Usage\n\n### Available Methods\n\n#### Request\n\n- [withLogger](#withlogger)\n\n#### Response\n\n- [toData](#todata)\n- [toDataCollection](#todatacollection)\n\n### Method Listing\n\n#### withLogger()\n\nAdds the ability to log HTTP requests and responses.\n\n```php\nuse Illuminate\\Support\\Facades\\Http;\n\nHttp::withLogger('some_channel')-\u003eget();\n```\n\nThis method will log HTTP requests and responses.\n\nIt is also possible to use your own handler, message formatting and path to the log file.\nTo do this, you need to specify the desired channel name from the log file and define the necessary parameters in it.\n\nFor example:\n\n```php\n// config/logging.php\nreturn [\n    // ...\n    \n    'channels' =\u003e [\n        'some' =\u003e [\n            'driver' =\u003e 'single',\n            'level' =\u003e env('LOG_LEVEL', 'debug'),\n            'path' =\u003e storage_path('logs/some.log'),\n            'handler' =\u003e \\App\\Logging\\SomeHandlerStack::class,\n            'formatter' =\u003e \\App\\Logging\\MessageFormatter::class,\n        ],\n    ],\n];\n\n// Usage\nreturn Http::withLogger('some')-\u003e...\n```\n\n#### toData()\n\nThe class instance will be returned.\n\n```php\nuse Illuminate\\Support\\Facades\\Http;\n\n// Returns a SomeData object\nreturn Http::get()-\u003etoData(SomeData::class);\n\n// Will return a SomeData object generated from the JSON path\nreturn Http::get()-\u003etoData(SomeData::class, 'data.item');\n\n// Returns the result of the callback execution\nreturn Http::get()-\u003etoData(\n    fn (array $data) =\u003e new SomeData(\n        $data['data']['item']['id'],\n        $data['data']['item']['title']\n    )\n);\n\n// Returns the result of the callback execution from a custom JSON path\nreturn Http::get()-\u003etoData(\n    fn (array $data) =\u003e new SomeData($data['id'], $data['title']),\n    'data.item'\n);\n```\n\n\u003e Note\n\u003e\n\u003e If a `from` method exists in a class, then it will be called to construct the object.\n\u003e\n\u003e Compatible with [Spatie Laravel Data](https://spatie.be/docs/laravel-data).\n\n```php\nclass SomeData\n{\n    public function __construct(\n        public int $id,\n        public string $title\n    ) {}\n    \n    public static function from(array $data): static\n    {\n        return new static(...$data);\n    }\n}\n\nreturn Http::get()-\u003etoData(SomeData::class);\n```\n\n#### toDataCollection()\n\nThe `Illuminate\\Support\\Collection` object or an object inherited from it will be returned.\n\n```php\nuse Illuminate\\Support\\Facades\\Http;\n\n// Returns a collection of SomeData objects\nreturn Http::get()-\u003etoDataCollection(SomeData::class);\n\n// Returns a collection of SomeData objects formed from the JSON path\nreturn Http::get()-\u003etoDataCollection(SomeData::class, 'data.item');\n\n// Returns the result of the callback execution\nreturn Http::get()-\u003etoDataCollection(\n    fn (array $data) =\u003e collect([\n        new SomeData(\n            $data['data']['item']['id'],\n            $data['data']['item']['title']\n        ),\n    ])\n);\n\n// Returns the result of the callback execution from a custom JSON path\nreturn Http::get()-\u003etoDataCollection(\n    fn (array $data) =\u003e collect([\n        new SomeData(...$data),\n    ]),\n    'data.item'\n);\n```\n\n\u003e Note\n\u003e\n\u003e If a `collect` method exists in a class, then it will be called to construct the collection.\n\u003e\n\u003e Compatible with [Spatie Laravel Data](https://spatie.be/docs/laravel-data).\n\n```php\nuse Illuminate\\Support\\Collection;\n\nclass SomeData\n{\n    public function __construct(\n        public int $id,\n        public string $title\n    ) {}\n    \n    public static function collect(array $items): Collection\n    {\n        return collect($items)-\u003emap(\n            fn (array $item) =\u003e new static(...$item)\n        );\n    }\n}\n\nreturn Http::get()-\u003etoDataCollection(SomeData::class);\n```\n\n### Generate IDE Helper files\n\nYou can generate helper files for the IDE using the console command:\n\n```Bash\nphp artisan http:macros-helper\n```\n\nThis will help your IDE suggest methods.\n\n![IDE Helper](.github/images/ide-helper.png)\n\n## License\n\nThis package is licensed under the [MIT License](LICENSE).\n\n\n[badge_build]:          https://img.shields.io/github/actions/workflow/status/TheDragonCode/laravel-http-macros/phpunit.yml?style=flat-square\n\n[badge_downloads]:      https://img.shields.io/packagist/dt/dragon-code/laravel-http-macros.svg?style=flat-square\n\n[badge_license]:        https://img.shields.io/packagist/l/dragon-code/laravel-http-macros.svg?style=flat-square\n\n[badge_stable]:         https://img.shields.io/github/v/release/TheDragonCode/laravel-http-macros?label=packagist\u0026style=flat-square\n\n[link_build]:           https://github.com/TheDragonCode/laravel-http-macros/actions\n\n[link_license]:         LICENSE\n\n[link_packagist]:       https://packagist.org/packages/dragon-code/laravel-http-macros\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedragoncode%2Flaravel-http-macros","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthedragoncode%2Flaravel-http-macros","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthedragoncode%2Flaravel-http-macros/lists"}