feat(pagination): add generic typed pagination with ordering to all list endpoints
- add PaginationQueryDto<TOrderBy> generic with orderBy and orderDir fields - add SessionOrderBy, EnvironmentOrderBy, ScenarioOrderBy type aliases - update session, environment, scenario services and controllers to use typed pagination - update MCP list_sessions, list_environments, list_scenarios tools to expose orderBy/orderDir - update tests for all list endpoints to cover page, limit, ordering, and invalid param rejection
This commit is contained in:
1 parent
d9cdb64ee2
commit
36f8b8c0ca
16 files changed
+337
-81
No files matched your search
@@ -43,7 +43,7 @@ export class AuthService {
|
||||
async login(keyId: string, environmentName: string, sessionName?: string): Promise<{ token: string; sessionName: string }> {
|
||||
const resolvedSession = sessionName ?? crypto.randomUUID();
|
||||
|
||||
const env = await this.environmentService.findAll().then(all => all.find(e => e.name === environmentName));
|
||||
const env = await this.environmentService.findAll().then(({ data }) => data.find(e => e.name === environmentName));
|
||||
if (!env) throw new NotFoundException(`Environment "${environmentName}" not found`);
|
||||
|
||||
const loginUrl = env.urls.id_url;
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsIn, IsInt, IsOptional, IsString, Min } from 'class-validator';
|
||||
|
||||
export class PaginationQueryDto<TOrderBy extends string = string> {
|
||||
@ApiPropertyOptional({ example: 1, default: 1 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
page?: number = 1;
|
||||
|
||||
@ApiPropertyOptional({ example: 20, default: 20 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
limit?: number = 20;
|
||||
|
||||
@ApiPropertyOptional({ example: 'id', default: 'id', description: 'Field to order by' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
orderBy?: TOrderBy;
|
||||
|
||||
@ApiPropertyOptional({ enum: ['ASC', 'DESC'], default: 'ASC' })
|
||||
@IsOptional()
|
||||
@IsIn(['ASC', 'DESC'])
|
||||
orderDir?: 'ASC' | 'DESC' = 'ASC';
|
||||
}
|
||||
|
||||
export interface PaginatedResult<T> {
|
||||
data: T[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
}
|
||||
@@ -8,11 +8,14 @@ import {
|
||||
ParseIntPipe,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
} from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { EnvironmentService } from './environment.service';
|
||||
import { CreateEnvironmentDto } from './dto/create-environment.dto';
|
||||
import { UpdateEnvironmentDto } from './dto/update-environment.dto';
|
||||
import { PaginationQueryDto } from '../common/dto/pagination.dto';
|
||||
import { EnvironmentOrderBy } from './environment.service';
|
||||
|
||||
@ApiTags('environments')
|
||||
@Controller('environments')
|
||||
@@ -28,10 +31,10 @@ export class EnvironmentController {
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all environments' })
|
||||
@ApiResponse({ status: 200, description: 'Array of environments' })
|
||||
findAll() {
|
||||
return this.environmentService.findAll();
|
||||
@ApiOperation({ summary: 'List all environments (paginated)' })
|
||||
@ApiResponse({ status: 200, description: 'Paginated environments' })
|
||||
findAll(@Query() query: PaginationQueryDto<EnvironmentOrderBy>) {
|
||||
return this.environmentService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
|
||||
@@ -4,6 +4,9 @@ import { Repository } from 'typeorm';
|
||||
import { EnvironmentEntity } from './environment.entity';
|
||||
import { CreateEnvironmentDto } from './dto/create-environment.dto';
|
||||
import { UpdateEnvironmentDto } from './dto/update-environment.dto';
|
||||
import { PaginationQueryDto, PaginatedResult } from '../common/dto/pagination.dto';
|
||||
|
||||
export type EnvironmentOrderBy = 'id' | 'name' | 'createdAt' | 'updatedAt';
|
||||
|
||||
@Injectable()
|
||||
export class EnvironmentService {
|
||||
@@ -20,8 +23,17 @@ export class EnvironmentService {
|
||||
return this.repo.save(this.repo.create(dto));
|
||||
}
|
||||
|
||||
findAll(): Promise<EnvironmentEntity[]> {
|
||||
return this.repo.find();
|
||||
async findAll(query: PaginationQueryDto<EnvironmentOrderBy> = {}): Promise<PaginatedResult<EnvironmentEntity>> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 20;
|
||||
const orderBy = query.orderBy ?? 'id';
|
||||
const orderDir = query.orderDir ?? 'ASC';
|
||||
const [data, total] = await this.repo.findAndCount({
|
||||
order: { [orderBy]: orderDir },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
});
|
||||
return { data, total, page, limit };
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<EnvironmentEntity> {
|
||||
|
||||
+28
-10
@@ -55,9 +55,17 @@ export class McpService {
|
||||
|
||||
server.registerTool(
|
||||
'list_sessions',
|
||||
{ description: 'List all stored sessions (id, sessionName, createdAt, updatedAt)' },
|
||||
async () => {
|
||||
const sessions = await this.sessionService.findAll();
|
||||
{
|
||||
description: 'List all stored sessions (id, sessionName, createdAt, updatedAt), paginated',
|
||||
inputSchema: {
|
||||
page: z.number().int().min(1).optional().describe('Page number (default 1)'),
|
||||
limit: z.number().int().min(1).optional().describe('Items per page (default 20)'),
|
||||
orderBy: z.enum(['id', 'sessionName', 'createdAt', 'updatedAt']).optional().describe('Field to order by (default id)'),
|
||||
orderDir: z.enum(['ASC', 'DESC']).optional().describe('Sort direction (default ASC)'),
|
||||
},
|
||||
},
|
||||
async ({ page, limit, orderBy, orderDir }) => {
|
||||
const sessions = await this.sessionService.findAll({ page, limit, orderBy, orderDir });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(sessions) }] };
|
||||
},
|
||||
);
|
||||
@@ -71,8 +79,8 @@ export class McpService {
|
||||
},
|
||||
},
|
||||
async ({ id }) => {
|
||||
const sessions = await this.sessionService.findAll();
|
||||
if (!sessions.find(s => s.id === id)) {
|
||||
const { data } = await this.sessionService.findAll();
|
||||
if (!data.find(s => s.id === id)) {
|
||||
return { isError: true, content: [{ type: 'text' as const, text: `Session ${id} not found` }] };
|
||||
}
|
||||
await this.sessionService.remove(id);
|
||||
@@ -84,9 +92,17 @@ export class McpService {
|
||||
|
||||
server.registerTool(
|
||||
'list_environments',
|
||||
{ description: 'List all environments' },
|
||||
async () => {
|
||||
const envs = await this.environmentService.findAll();
|
||||
{
|
||||
description: 'List all environments, paginated',
|
||||
inputSchema: {
|
||||
page: z.number().int().min(1).optional().describe('Page number (default 1)'),
|
||||
limit: z.number().int().min(1).optional().describe('Items per page (default 20)'),
|
||||
orderBy: z.enum(['id', 'name', 'createdAt', 'updatedAt']).optional().describe('Field to order by (default id)'),
|
||||
orderDir: z.enum(['ASC', 'DESC']).optional().describe('Sort direction (default ASC)'),
|
||||
},
|
||||
},
|
||||
async ({ page, limit, orderBy, orderDir }) => {
|
||||
const envs = await this.environmentService.findAll({ page, limit, orderBy, orderDir });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(envs) }] };
|
||||
},
|
||||
);
|
||||
@@ -218,10 +234,12 @@ export class McpService {
|
||||
inputSchema: {
|
||||
page: z.number().int().min(1).optional().describe('Page number (default 1)'),
|
||||
limit: z.number().int().min(1).optional().describe('Items per page (default 20)'),
|
||||
orderBy: z.enum(['id', 'name', 'createdAt', 'updatedAt']).optional().describe('Field to order by (default id)'),
|
||||
orderDir: z.enum(['ASC', 'DESC']).optional().describe('Sort direction (default ASC)'),
|
||||
},
|
||||
},
|
||||
async ({ page, limit }) => {
|
||||
const result = await this.scenarioService.findAll({ page, limit });
|
||||
async ({ page, limit, orderBy, orderDir }) => {
|
||||
const result = await this.scenarioService.findAll({ page, limit, orderBy, orderDir });
|
||||
return { content: [{ type: 'text' as const, text: JSON.stringify(result) }] };
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,19 +1 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
import { IsInt, IsOptional, Min } from 'class-validator';
|
||||
|
||||
export class PaginationQueryDto {
|
||||
@ApiPropertyOptional({ example: 1, default: 1 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
page?: number = 1;
|
||||
|
||||
@ApiPropertyOptional({ example: 20, default: 20 })
|
||||
@IsOptional()
|
||||
@Type(() => Number)
|
||||
@IsInt()
|
||||
@Min(1)
|
||||
limit?: number = 20;
|
||||
}
|
||||
export { PaginationQueryDto } from '../../common/dto/pagination.dto';
|
||||
@@ -17,6 +17,7 @@ import { UpdateScenarioDto } from './dto/update-scenario.dto';
|
||||
import { CreateScenarioStepDto } from './dto/create-scenario-step.dto';
|
||||
import { UpdateScenarioStepDto } from './dto/update-scenario-step.dto';
|
||||
import { PaginationQueryDto } from './dto/pagination-query.dto';
|
||||
import { ScenarioOrderBy } from './scenario.service';
|
||||
import { RunsQueryDto } from './dto/runs-query.dto';
|
||||
|
||||
@ApiTags('scenarios')
|
||||
@@ -36,7 +37,7 @@ export class ScenarioController {
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all scenarios (paginated)' })
|
||||
@ApiResponse({ status: 200 })
|
||||
findAll(@Query() query: PaginationQueryDto) {
|
||||
findAll(@Query() query: PaginationQueryDto<ScenarioOrderBy>) {
|
||||
return this.scenarioService.findAll(query);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,15 +9,11 @@ import { CreateScenarioDto } from './dto/create-scenario.dto';
|
||||
import { UpdateScenarioDto } from './dto/update-scenario.dto';
|
||||
import { CreateScenarioStepDto } from './dto/create-scenario-step.dto';
|
||||
import { UpdateScenarioStepDto } from './dto/update-scenario-step.dto';
|
||||
import { PaginationQueryDto } from './dto/pagination-query.dto';
|
||||
import { PaginationQueryDto, PaginatedResult } from '../common/dto/pagination.dto';
|
||||
import { RunsQueryDto } from './dto/runs-query.dto';
|
||||
|
||||
export interface PaginatedResult<T> {
|
||||
data: T[];
|
||||
total: number;
|
||||
page: number;
|
||||
limit: number;
|
||||
}
|
||||
export { PaginatedResult } from '../common/dto/pagination.dto';
|
||||
export type ScenarioOrderBy = 'id' | 'name' | 'createdAt' | 'updatedAt';
|
||||
|
||||
@Injectable()
|
||||
export class ScenarioService {
|
||||
@@ -38,11 +34,13 @@ export class ScenarioService {
|
||||
return this.scenarioRepo.save(this.scenarioRepo.create(dto));
|
||||
}
|
||||
|
||||
async findAll(query: PaginationQueryDto): Promise<PaginatedResult<ScenarioEntity>> {
|
||||
async findAll(query: PaginationQueryDto<ScenarioOrderBy>): Promise<PaginatedResult<ScenarioEntity>> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 20;
|
||||
const orderBy = query.orderBy ?? 'id';
|
||||
const orderDir = query.orderDir ?? 'ASC';
|
||||
const [data, total] = await this.scenarioRepo.findAndCount({
|
||||
order: { id: 'ASC' },
|
||||
order: { [orderBy]: orderDir },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
});
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { Controller, Delete, Get, NotFoundException, Param, ParseIntPipe } from '@nestjs/common';
|
||||
import { Controller, Delete, Get, NotFoundException, Param, ParseIntPipe, Query } from '@nestjs/common';
|
||||
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
import { SessionService } from './session.service';
|
||||
import { PaginationQueryDto } from '../common/dto/pagination.dto';
|
||||
import { SessionOrderBy } from './session.service';
|
||||
|
||||
@ApiTags('sessions')
|
||||
@Controller('sessions')
|
||||
@@ -8,24 +10,10 @@ export class SessionController {
|
||||
constructor(private readonly sessionService: SessionService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: 'List all stored sessions' })
|
||||
@ApiResponse({
|
||||
status: 200,
|
||||
description: 'Array of sessions',
|
||||
schema: {
|
||||
type: 'array',
|
||||
items: {
|
||||
properties: {
|
||||
id: { type: 'number' },
|
||||
sessionName: { type: 'string' },
|
||||
createdAt: { type: 'string', format: 'date-time' },
|
||||
updatedAt: { type: 'string', format: 'date-time' },
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
findAll() {
|
||||
return this.sessionService.findAll();
|
||||
@ApiOperation({ summary: 'List all stored sessions (paginated)' })
|
||||
@ApiResponse({ status: 200, description: 'Paginated sessions' })
|
||||
findAll(@Query() query: PaginationQueryDto<SessionOrderBy>) {
|
||||
return this.sessionService.findAll(query);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
@@ -34,7 +22,7 @@ export class SessionController {
|
||||
@ApiResponse({ status: 404, description: 'Session not found' })
|
||||
async remove(@Param('id', ParseIntPipe) id: number): Promise<void> {
|
||||
const sessions = await this.sessionService.findAll();
|
||||
if (!sessions.find(s => s.id === id)) {
|
||||
if (!sessions.data.find(s => s.id === id)) {
|
||||
throw new NotFoundException(`Session ${id} not found`);
|
||||
}
|
||||
await this.sessionService.remove(id);
|
||||
|
||||
@@ -3,6 +3,9 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { SessionEntity } from './session.entity';
|
||||
import type { Cookie } from 'playwright';
|
||||
import { PaginationQueryDto, PaginatedResult } from '../common/dto/pagination.dto';
|
||||
|
||||
export type SessionOrderBy = 'id' | 'sessionName' | 'createdAt' | 'updatedAt';
|
||||
|
||||
@Injectable()
|
||||
export class SessionService {
|
||||
@@ -40,8 +43,18 @@ export class SessionService {
|
||||
return this.repo.findOneBy({ sessionName });
|
||||
}
|
||||
|
||||
findAll(): Promise<Pick<SessionEntity, 'id' | 'sessionName' | 'createdAt' | 'updatedAt'>[]> {
|
||||
return this.repo.find({ select: ['id', 'sessionName', 'createdAt', 'updatedAt'] });
|
||||
async findAll(query: PaginationQueryDto<SessionOrderBy> = {}): Promise<PaginatedResult<Pick<SessionEntity, 'id' | 'sessionName' | 'createdAt' | 'updatedAt'>>> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 20;
|
||||
const orderBy = query.orderBy ?? 'id';
|
||||
const orderDir = query.orderDir ?? 'ASC';
|
||||
const [data, total] = await this.repo.findAndCount({
|
||||
select: ['id', 'sessionName', 'createdAt', 'updatedAt'],
|
||||
order: { [orderBy]: orderDir },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
});
|
||||
return { data, total, page, limit };
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
|
||||
Reference in new issue
Block a user