refactor: introduce domain model classes and mapper layer
Replace ad-hoc inline row casting (snake_case → camelCase) spread across services, routes, and the indexing pipeline with explicit model classes (Repository, IndexingJob, RepositoryVersion, Snippet, SearchResult) and dedicated mapper classes that own the DB → domain conversion. - Add src/lib/server/models/ with typed model classes for all domain entities - Add src/lib/server/mappers/ with mapper classes per entity - Remove duplicated RawRow interfaces and inline map functions from job-queue, repository.service, indexing.pipeline, and all API routes - Add dtoJsonResponse helper to standardise JSON responses via SvelteKit json() - Add api-contract.integration.test.ts as a regression baseline Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
99
src/lib/server/models/context-response.ts
Normal file
99
src/lib/server/models/context-response.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
export class LibrarySearchJsonResultDto {
|
||||
id: string;
|
||||
title: string;
|
||||
description: string | null;
|
||||
branch: string | null;
|
||||
lastUpdateDate: string | null;
|
||||
state: 'initial' | 'finalized' | 'error';
|
||||
totalTokens: number | null;
|
||||
totalSnippets: number | null;
|
||||
stars: number | null;
|
||||
trustScore: number | null;
|
||||
benchmarkScore: number | null;
|
||||
versions: string[];
|
||||
source: string;
|
||||
|
||||
constructor(props: LibrarySearchJsonResultDto) {
|
||||
this.id = props.id;
|
||||
this.title = props.title;
|
||||
this.description = props.description;
|
||||
this.branch = props.branch;
|
||||
this.lastUpdateDate = props.lastUpdateDate;
|
||||
this.state = props.state;
|
||||
this.totalTokens = props.totalTokens;
|
||||
this.totalSnippets = props.totalSnippets;
|
||||
this.stars = props.stars;
|
||||
this.trustScore = props.trustScore;
|
||||
this.benchmarkScore = props.benchmarkScore;
|
||||
this.versions = props.versions;
|
||||
this.source = props.source;
|
||||
}
|
||||
}
|
||||
|
||||
export class LibrarySearchJsonResponseDto {
|
||||
results: LibrarySearchJsonResultDto[];
|
||||
|
||||
constructor(results: LibrarySearchJsonResultDto[]) {
|
||||
this.results = results;
|
||||
}
|
||||
}
|
||||
|
||||
export class CodeListItemDto {
|
||||
language: string;
|
||||
code: string;
|
||||
|
||||
constructor(props: CodeListItemDto) {
|
||||
this.language = props.language;
|
||||
this.code = props.code;
|
||||
}
|
||||
}
|
||||
|
||||
export class CodeSnippetJsonDto {
|
||||
type: 'code' = 'code';
|
||||
title: string | null;
|
||||
description: string | null;
|
||||
language: string | null;
|
||||
codeList: CodeListItemDto[];
|
||||
id: string;
|
||||
tokenCount: number | null;
|
||||
pageTitle: string | null;
|
||||
|
||||
constructor(props: Omit<CodeSnippetJsonDto, 'type'>) {
|
||||
this.title = props.title;
|
||||
this.description = props.description;
|
||||
this.language = props.language;
|
||||
this.codeList = props.codeList;
|
||||
this.id = props.id;
|
||||
this.tokenCount = props.tokenCount;
|
||||
this.pageTitle = props.pageTitle;
|
||||
}
|
||||
}
|
||||
|
||||
export class InfoSnippetJsonDto {
|
||||
type: 'info' = 'info';
|
||||
text: string;
|
||||
breadcrumb: string | null;
|
||||
pageId: string;
|
||||
tokenCount: number | null;
|
||||
|
||||
constructor(props: Omit<InfoSnippetJsonDto, 'type'>) {
|
||||
this.text = props.text;
|
||||
this.breadcrumb = props.breadcrumb;
|
||||
this.pageId = props.pageId;
|
||||
this.tokenCount = props.tokenCount;
|
||||
}
|
||||
}
|
||||
|
||||
export type SnippetJsonDto = CodeSnippetJsonDto | InfoSnippetJsonDto;
|
||||
|
||||
export class ContextJsonResponseDto {
|
||||
snippets: SnippetJsonDto[];
|
||||
rules: string[];
|
||||
totalTokens: number;
|
||||
|
||||
constructor(props: ContextJsonResponseDto) {
|
||||
this.snippets = props.snippets;
|
||||
this.rules = props.rules;
|
||||
this.totalTokens = props.totalTokens;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user