Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/common/interfaces/list-option.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@ export interface List<T> {
readonly object: 'list';
data: T[];
}

export interface PaginatedList<T> extends List<T> {
has_more: boolean;
}

export interface PaginationOptions {
before?: string | null;
after?: string | null;
limit?: number | null;
}
38 changes: 38 additions & 0 deletions src/common/utils/get-pagination-query-properties.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getPaginationQueryProperties } from './get-pagination-query-properties';

describe('getPaginationQueryProperties', () => {
it('returns empty string when no options provided', () => {
expect(getPaginationQueryProperties()).toBe('');
expect(getPaginationQueryProperties({})).toBe('');
});

it('builds query string with single parameter', () => {
expect(getPaginationQueryProperties({ before: 'cursor1' })).toBe(
'?before=cursor1',
);
expect(getPaginationQueryProperties({ after: 'cursor2' })).toBe(
'?after=cursor2',
);
expect(getPaginationQueryProperties({ limit: 10 })).toBe('?limit=10');
});

it('builds query string with multiple parameters', () => {
const result = getPaginationQueryProperties({
before: 'cursor1',
after: 'cursor2',
limit: 25,
});

expect(result).toBe('?before=cursor1&after=cursor2&limit=25');
});

it('ignores undefined/null values', () => {
expect(
getPaginationQueryProperties({
before: undefined,
after: 'cursor2',
limit: null,
}),
).toBe('?after=cursor2');
});
});
13 changes: 13 additions & 0 deletions src/common/utils/get-pagination-query-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { PaginationOptions } from '../interfaces';

export function getPaginationQueryProperties(
options: PaginationOptions = {},
): string {
const query = new URLSearchParams();

if (options.before) query.set('before', options.before);
if (options.after) query.set('after', options.after);
if (options.limit) query.set('limit', options.limit.toString());

return query.size > 0 ? `?${query.toString()}` : '';
}
28 changes: 28 additions & 0 deletions src/templates/interfaces/list-templates.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { PaginatedList } from '../../common/interfaces';
import type { ErrorResponse } from '../../interfaces';
import type { Template } from './template';

interface TemplateListItem
extends Pick<
Template,
| 'id'
| 'name'
| 'created_at'
| 'updated_at'
| 'status'
| 'published_at'
| 'alias'
> {}

export interface ListTemplatesResponseSuccess
extends PaginatedList<TemplateListItem> {}

export type ListTemplatesResponse =
| {
data: ListTemplatesResponseSuccess;
error: null;
}
| {
data: null;
error: ErrorResponse;
};
Loading