saloonphp / saloon

šŸ¤  Build beautiful API integrations and SDKs with Saloon

Home Page:https://docs.saloon.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Body-based Pagination

faustbrian opened this issue Ā· comments

Hey, is it currently possible to create a paginator that adds parameters to the body instead of headers and query? I would basically need a slightly adjusted version of what is shown on this page.

public function paginate(Request $request): CursorPaginator
{
    return new class(connector: $this, request: $request) extends CursorPaginator
    {
        protected function getNextCursor(Response $response): int|string
        {
            return $response->json('result.page.next');
        }
    
        protected function isLastPage(Response $response): bool
        {
            return is_null($response->json('result.page.next'));
        }
        
        protected function getPageItems(Response $response, Request $request): array
        {
            return $response->json('result.data');
        }
        
        protected function applyPagination(Request $request): Request
        {
            if ($this->currentResponse instanceof Response) {
				// Pseudo Code
                $request->body()->add('params.page.cursor', $this->getNextCursor($this->currentResponse));
            }
    
            if (isset($this->perPageLimit)) {
				// Pseudo Code
                $request->body()->add('params.page.size', $this->perPageLimit);
            }
    
            return $request;
        }
    };
}

This seems to do the trick, didn't think of checking if HasJsonBody is used. Though it doesn't seem to be possible to set nested keys via dot-notation so merge has to be used.

protected function applyPagination(Request $request): Request
{
    if (!\in_array(HasJsonBody::class, \class_uses($request), true)) {
        return $request;
    }

    if ($this->currentResponse instanceof Response) {
        // @phpstan-ignore-next-line
        $request->body()->merge([
            'params' => [
                'page' => [
                    'cursor' => $this->getNextCursor($this->currentResponse),
                    'size' => $this->perPageLimit,
                ],
            ],
        ]);
    }

    return $request;
}