Skip to content

Commit 6e6b636

Browse files
committed
docs(README): Types section
1 parent 42df25d commit 6e6b636

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,82 @@ Most of GitHub's paginating REST API endpoints return an array, but there are a
183183

184184
If a response is lacking the `Link` header, `octokit.paginate()` still resolves with an array, even if the response returns a single object.
185185

186+
## Types
187+
188+
The plugin also exposes some types and runtime type guards for TypeScript projects.
189+
190+
<table>
191+
<tbody valign=top align=left>
192+
<tr><th>
193+
<tr><th>
194+
Types
195+
</th><td>
196+
197+
```typescript
198+
import {
199+
PaginateInterface,
200+
PaginatingEndpoints,
201+
} from "@octokit/plugin-paginate-rest";
202+
```
203+
204+
</td></tr>
205+
<tr><th>
206+
Guards
207+
</th><td>
208+
209+
```typescript
210+
import { isPaginatingEndpoint } from "@octokit/plugin-paginate-rest";
211+
```
212+
213+
</td></tr>
214+
</tbody>
215+
</table>
216+
217+
### PaginateInterface
218+
219+
An `interface` that declares all the overloads of the `.paginate` method.
220+
221+
### PaginatingEndpoints
222+
223+
An `interface` which describes all API endpoints supported by the plugin. Some overloads of `.paginate()` method and `composePaginateRest()` function depend on `PaginatingEndpoints`, using the `keyof PaginatingEndpoints` as a type for one of its arguments.
224+
225+
```typescript
226+
import { Octokit } from "@octokit/core";
227+
import {
228+
PaginatingEndpoints,
229+
composePaginateRest,
230+
} from "@octokit/plugin-paginate-rest";
231+
232+
type DataType<T> = "data" extends keyof T ? T["data"] : unknown;
233+
234+
async function myPaginatePlugin<E extends keyof PaginatingEndpoints>(
235+
octokit: Octokit,
236+
endpoint: E,
237+
parameters?: PaginatingEndpoints[E]["parameters"]
238+
): Promise<DataType<PaginatingEndpoints[E]["response"]>> {
239+
return await composePaginateRest(octokit, endpoint, parameters);
240+
}
241+
```
242+
243+
### isPaginatingEndpoint
244+
245+
A type guard, `isPaginatingEndpoint(arg)` returns `true` iff `arg` is one of the keys in `PaginatingEndpoints` (is `keyof PaginatingEndpoints`).
246+
247+
```typescript
248+
import { Octokit } from "@octokit/core";
249+
import {
250+
isPaginatingEndpoint,
251+
composePaginateRest,
252+
} from "@octokit/plugin-paginate-rest";
253+
254+
async function myPlugin(octokit: Octokit, arg: unknown) {
255+
if (isPaginatingEndpoint(arg)) {
256+
return await composePaginateRest(octokit, arg);
257+
}
258+
// ...
259+
}
260+
```
261+
186262
## Contributing
187263

188264
See [CONTRIBUTING.md](CONTRIBUTING.md)

0 commit comments

Comments
 (0)