Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | Add HasEndpointPlaceholders trait for Request endpoints syntax sugar #381

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from

Conversation

onlime
Copy link

@onlime onlime commented Mar 5, 2024

This adds a new HasEndpointPlaceholders trait which can be (optionally) used on any Saloon Request as syntax sugar. So instead of overriding resolveEndpoint() like so:

class GetUser extends Request
{
    protected Method $method = Method::GET;

    public function __construct(protected int $id) {}

    public function resolveEndpoint(): string
    {
        return "/user/{$this->id}";
    }

... you could use:

class GetUser extends Request
{
    use HasEndpointPlaceholders;

    protected Method $method = Method::GET;

    protected string $endpoint = '/user/{id}';

    public function __construct(protected int $id) {}

This especially makes sense when you use some custom AbstractRequest that extends from Saloon\Http\Request, so you would only need to use HasEndpointPlaceholders once. It also makes sense if you have a lot of different constructor params – the HasEndpointPlaceholders will pick the ones found in your $endpoint template by reflection, supporting int, string, bool, or enum types.

I use this in like 80% of my integrations/requests and it saved me tons of lines, also greatly increased readability.

Cheers,
Pipo

@onlime onlime changed the title Add HasEndpointPlaceholders trait for Request endpoints syntax sugar Feature | Add HasEndpointPlaceholders trait for Request endpoints syntax sugar Mar 5, 2024
@Sammyjo20
Copy link
Collaborator

Hey @onlime I'm very sorry for not replying to this PR sooner - when I get a chance over the next day or two I will review this properly - from the outset I do quite like this idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants