Skip to content

Commit a584aa0

Browse files
committed
- Added pagination to API docs
1 parent d499616 commit a584aa0

1 file changed

Lines changed: 45 additions & 2 deletions

File tree

src/Concerns/ApiDocHelpers.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,39 @@ public static function apiDocAllowedSorts(): array
7171
return $new_instance->getAllowedSorts();
7272
}
7373

74+
public static function apiDocDefaultPerPage(): int
75+
{
76+
/** @var self $new_instance */
77+
$new_instance = app(static::class);
78+
79+
return $new_instance->getDefaultPerPage();
80+
}
81+
82+
public static function apiDocMaxPerPage(): int
83+
{
84+
/** @var self $new_instance */
85+
$new_instance = app(static::class);
86+
87+
return $new_instance->getMaxPerPage();
88+
}
89+
90+
public function apiDocAllowUnlimitedResultsPerPage(): bool
91+
{
92+
/** @var self $new_instance */
93+
$new_instance = app(static::class);
94+
95+
return $new_instance->allowUnlimitedResultsPerPage();
96+
}
97+
7498
public static function apiDocDefaultQueryParameters(
7599
array $fields = [],
76100
array $sorts = [],
77101
string $default_sort = '',
78102
array $appends = [],
79103
array $includes = [],
80104
array $filters = [],
81-
array $filter_metadata = []
105+
array $filter_metadata = [],
106+
bool $include_pagination = false
82107
): array
83108
{
84109
$params = [];
@@ -134,6 +159,23 @@ public static function apiDocDefaultQueryParameters(
134159
];
135160
}
136161

162+
if ($include_pagination) {
163+
$params['per_page'] = [
164+
'type' => 'integer',
165+
'description' => 'How many results to return per page. '.
166+
(static::apiDocAllowUnlimitedResultsPerPage() ? '<br></br>To return all results, set `per_page` to `-1`' : '').
167+
'<br>**Max per page:** ' . static::apiDocMaxPerPage() .
168+
'<br>**Default per page:** ' . static::apiDocDefaultPerPage(),
169+
'example' => static::apiDocDefaultPerPage(),
170+
];
171+
172+
$params['page'] = [
173+
'type' => 'integer',
174+
'description' => 'For paginated results, which page to return.' ,
175+
'example' => 1,
176+
];
177+
}
178+
137179
$singular_resource_name = static::apiDocResourceNameSingularLower();
138180

139181
if ($filters) {
@@ -171,7 +213,8 @@ public static function apiDocDefaultIndexQueryParameters(): array
171213
static::apiDocIndexAllowedAppends(),
172214
static::apiDocAllowedIncludes(),
173215
static::apiDocAllowedFilters(),
174-
static::apiDocFilterMetadata()
216+
static::apiDocFilterMetadata(),
217+
true
175218
);
176219
}
177220

0 commit comments

Comments
 (0)