Skip to main content

Implement Pagination

Konfig supports pagination out-of-the-box with an incredibly simple implementation process.

  1. Configure konfig.yaml for pagination
  2. Implement stubbed methods
  3. Add implementation to .konfigignore

1) Configure konfig.yaml for pagination


_20
# konfig.yaml
_20
_20
outputDirectory: /tmp/acme-sdks-out
_20
specPath: openapi.json
_20
generators:
_20
typescript:
_20
version: 1.0.0
_20
npmName: acme-typescript-sdk
_20
outputDirectory: typescript
_20
git:
_20
userId: konfig-dev
_20
repoId: acme-sdks/tree/main/typescript
_20
pagination:
_20
parameters: # parameter names to match
_20
- page_size
_20
- page
_20
response: # response property names to match
_20
- page_size
_20
- page
_20
- total_pages

2) Implement stubbed methods


_56
// page.ts
_56
_56
import { Pageable, PageParametersBase } from "./pageable";
_56
_56
/**
_56
* The set of parameters that appear in a paginated operation (requestBody or parameters)
_56
*/
_56
export type PageParameterProperties = {
_56
// stubbed as: [key: string]: any
_56
page?: number;
_56
pageSize?: number;
_56
};
_56
_56
export type PageParameters = PageParametersBase<PageParameterProperties>;
_56
_56
/**
_56
* The set of properties that appear in a paginated operation's response
_56
*/
_56
export interface PageInfo {
_56
// stubbed as: [key: string]: any
_56
total_pages?: number;
_56
page?: number;
_56
page_size?: number;
_56
}
_56
_56
export class Page<
_56
Data extends PageInfo,
_56
Parameters extends PageParameters
_56
> extends Pageable<Data, Parameters> {
_56
protected get previousParameters(): PageParameterProperties | null {
_56
// stubbed as: throw Error("Stub");
_56
if (this.data.page === undefined) return null;
_56
return {
_56
page: this.data.page - 1,
_56
};
_56
}
_56
_56
protected get nextParameters(): PageParameterProperties | null {
_56
// stubbed as: throw Error("Stub");
_56
if (this.data.page === undefined) return null;
_56
return {
_56
page: this.data.page + 1,
_56
};
_56
}
_56
hasPrevious(): boolean {
_56
// stubbed as: throw Error("Stub");
_56
return this.data.page === undefined ? false : this.data.page > 0;
_56
}
_56
_56
hasNext(): boolean {
_56
// stubbed as: throw Error("Stub");
_56
if (this.data.page === undefined) return false;
_56
if (this.data.total_pages === undefined) return false;
_56
return this.data.page < this.data.total_pages;
_56
}
_56
}

3) Add custom implementation to .konfigignore


_10
❯ konfig ignore -g "typescript/pagination/page.ts"
_10
Adding pagination/page.ts to typescript/.konfigignore