Skip to content

Commit d7228e1

Browse files
committed
refactor: update pagination keywords handling to support custom configuration
1 parent 0ccbbd9 commit d7228e1

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed
Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest';
22

3+
import type { Config } from '../../types/config';
34
import { getPaginationKeywordsRegExp } from '../pagination';
45

56
describe('paginationKeywordsRegExp', () => {
@@ -9,35 +10,35 @@ describe('paginationKeywordsRegExp', () => {
910
}> = [
1011
{
1112
result: true,
12-
value: 'after',
13+
value: 'page',
1314
},
1415
{
1516
result: true,
16-
value: 'before',
17+
value: 'cursor',
1718
},
1819
{
1920
result: true,
20-
value: 'cursor',
21+
value: 'after',
2122
},
2223
{
2324
result: true,
24-
value: 'offset',
25+
value: 'before',
2526
},
2627
{
2728
result: true,
28-
value: 'page',
29+
value: 'offset',
2930
},
3031
{
3132
result: true,
3233
value: 'start',
3334
},
3435
{
3536
result: false,
36-
value: 'my_start',
37+
value: 'pageSize',
3738
},
3839
{
3940
result: false,
40-
value: 'start_my',
41+
value: 'perPage',
4142
},
4243
];
4344

@@ -49,4 +50,28 @@ describe('paginationKeywordsRegExp', () => {
4950
expect(paginationRegExp.test(value)).toEqual(result);
5051
},
5152
);
53+
54+
it('uses custom keywords from config', async () => {
55+
const config: Config = {
56+
input: {
57+
pagination: {
58+
keywords: ['customPagination', 'pageSize', 'perPage'],
59+
},
60+
},
61+
} as Config;
62+
63+
const paginationRegExp = getPaginationKeywordsRegExp(config);
64+
65+
// Should match custom keywords
66+
paginationRegExp.lastIndex = 0;
67+
expect(paginationRegExp.test('customPagination')).toEqual(true);
68+
paginationRegExp.lastIndex = 0;
69+
expect(paginationRegExp.test('pageSize')).toEqual(true);
70+
paginationRegExp.lastIndex = 0;
71+
expect(paginationRegExp.test('perPage')).toEqual(true);
72+
73+
// Should not match default keywords
74+
paginationRegExp.lastIndex = 0;
75+
expect(paginationRegExp.test('page')).toEqual(false);
76+
});
5277
});

packages/openapi-ts/src/ir/pagination.ts

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import type { Config } from '../types/config';
12
import type { IR } from './types';
23

3-
const DEFAULT_PAGINATION_KEYWORDS = [
4+
export const DEFAULT_PAGINATION_KEYWORDS = [
45
'after',
56
'before',
67
'cursor',
@@ -9,31 +10,13 @@ const DEFAULT_PAGINATION_KEYWORDS = [
910
'start',
1011
];
1112

12-
let currentPaginationKeywords = DEFAULT_PAGINATION_KEYWORDS;
13-
14-
export function setPaginationKeywords(
15-
keywords: string[] = DEFAULT_PAGINATION_KEYWORDS,
16-
): void {
17-
currentPaginationKeywords = keywords;
18-
}
19-
20-
export function createPaginationKeywordsRegExp(keywords?: string[]): RegExp {
21-
const keywordsToUse = keywords || currentPaginationKeywords;
22-
const pattern = `^(${keywordsToUse.join('|')})$`;
13+
export function getPaginationKeywordsRegExp(config?: Config): RegExp {
14+
const configKeywords = config?.input?.pagination?.keywords;
15+
const keywords = configKeywords || DEFAULT_PAGINATION_KEYWORDS;
16+
const pattern = `^(${keywords.join('|')})$`;
2317
return new RegExp(pattern);
2418
}
2519

26-
let paginationKeywordsRegExp: RegExp | undefined;
27-
28-
export function getPaginationKeywordsRegExp(): RegExp {
29-
if (!paginationKeywordsRegExp) {
30-
paginationKeywordsRegExp = createPaginationKeywordsRegExp(
31-
currentPaginationKeywords,
32-
);
33-
}
34-
return paginationKeywordsRegExp;
35-
}
36-
3720
export interface Pagination {
3821
in: string;
3922
name: string;

0 commit comments

Comments
 (0)