sourcebot/packages/web/src/lib/schemas.ts

160 lines
4.8 KiB
TypeScript
Raw Normal View History

import { z } from "zod";
export const searchRequestSchema = z.object({
query: z.string(),
maxMatchDisplayCount: z.number(),
whole: z.boolean().optional(),
});
2024-09-29 21:39:17 +00:00
// @see : https://github.com/sourcebot-dev/zoekt/blob/main/api.go#L212
export const locationSchema = z.object({
2024-09-11 04:55:00 +00:00
// 0-based byte offset from the beginning of the file
ByteOffset: z.number(),
2024-09-11 04:55:00 +00:00
// 1-based line number from the beginning of the file
LineNumber: z.number(),
2024-09-11 04:55:00 +00:00
// 1-based column number (in runes) from the beginning of line
Column: z.number(),
});
export const rangeSchema = z.object({
Start: locationSchema,
End: locationSchema,
});
2024-09-29 21:39:17 +00:00
// @see : https://github.com/sourcebot-dev/zoekt/blob/3780e68cdb537d5a7ed2c84d9b3784f80c7c5d04/api.go#L350
2024-09-17 04:37:34 +00:00
export const searchResponseStats = {
ContentBytesLoaded: z.number(),
IndexBytesLoaded: z.number(),
Crashes: z.number(),
Duration: z.number(),
FileCount: z.number(),
ShardFilesConsidered: z.number(),
FilesConsidered: z.number(),
FilesLoaded: z.number(),
FilesSkipped: z.number(),
ShardsScanned: z.number(),
ShardsSkipped: z.number(),
ShardsSkippedFilter: z.number(),
MatchCount: z.number(),
NgramMatches: z.number(),
NgramLookups: z.number(),
Wait: z.number(),
MatchTreeConstruction: z.number(),
MatchTreeSearch: z.number(),
RegexpsConsidered: z.number(),
FlushReason: z.number(),
}
2024-11-28 21:26:27 +00:00
export const symbolSchema = z.object({
Sym: z.string(),
Kind: z.string(),
Parent: z.string(),
ParentKind: z.string(),
});
2024-09-29 21:39:17 +00:00
// @see : https://github.com/sourcebot-dev/zoekt/blob/3780e68cdb537d5a7ed2c84d9b3784f80c7c5d04/api.go#L497
2024-11-07 02:28:10 +00:00
export const zoektSearchResponseSchema = z.object({
Result: z.object({
2024-09-17 04:37:34 +00:00
...searchResponseStats,
Files: z.array(z.object({
FileName: z.string(),
Repository: z.string(),
2024-11-01 17:51:14 +00:00
Version: z.string().optional(),
Language: z.string(),
2024-11-01 17:51:14 +00:00
Branches: z.array(z.string()).optional(),
ChunkMatches: z.array(z.object({
Content: z.string(),
Ranges: z.array(rangeSchema),
FileName: z.boolean(),
ContentStart: locationSchema,
Score: z.number(),
2024-11-28 21:26:27 +00:00
SymbolInfo: z.array(symbolSchema).nullable(),
})),
Checksum: z.string(),
Score: z.number(),
// Set if `whole` is true.
Content: z.string().optional(),
})).nullable(),
RepoURLs: z.record(z.string(), z.string()),
}),
});
2024-11-07 02:28:10 +00:00
export const searchResponseSchema = z.object({
...zoektSearchResponseSchema.shape,
// Flag when a branch filter was used (e.g., `branch:`, `revision:`, etc.).
isBranchFilteringEnabled: z.boolean(),
});
export const fileSourceRequestSchema = z.object({
fileName: z.string(),
2024-11-07 02:28:10 +00:00
repository: z.string(),
branch: z.string().optional(),
});
export const fileSourceResponseSchema = z.object({
source: z.string(),
2025-01-07 18:27:42 +00:00
language: z.string(),
2024-09-11 04:55:00 +00:00
});
2024-09-29 21:39:17 +00:00
// @see : https://github.com/sourcebot-dev/zoekt/blob/3780e68cdb537d5a7ed2c84d9b3784f80c7c5d04/api.go#L728
2024-09-17 04:37:34 +00:00
const repoStatsSchema = z.object({
2024-09-11 04:55:00 +00:00
Repos: z.number(),
Shards: z.number(),
Documents: z.number(),
IndexBytes: z.number(),
ContentBytes: z.number(),
NewLinesCount: z.number(),
DefaultBranchNewLinesCount: z.number(),
OtherBranchesNewLinesCount: z.number(),
});
2024-09-29 21:39:17 +00:00
// @see : https://github.com/sourcebot-dev/zoekt/blob/3780e68cdb537d5a7ed2c84d9b3784f80c7c5d04/api.go#L716
2024-09-17 04:37:34 +00:00
const indexMetadataSchema = z.object({
2024-09-11 04:55:00 +00:00
IndexFormatVersion: z.number(),
IndexFeatureVersion: z.number(),
IndexMinReaderVersion: z.number(),
IndexTime: z.string(),
PlainASCII: z.boolean(),
LanguageMap: z.record(z.string(), z.number()),
ZoektVersion: z.string(),
ID: z.string(),
});
2024-09-29 21:39:17 +00:00
// @see : https://github.com/sourcebot-dev/zoekt/blob/3780e68cdb537d5a7ed2c84d9b3784f80c7c5d04/api.go#L555
export const repositorySchema = z.object({
2024-09-11 04:55:00 +00:00
Name: z.string(),
URL: z.string(),
Source: z.string(),
Branches: z.array(z.object({
Name: z.string(),
Version: z.string(),
2024-11-01 17:51:14 +00:00
})).nullable(),
2024-09-11 04:55:00 +00:00
CommitURLTemplate: z.string(),
FileURLTemplate: z.string(),
LineFragmentTemplate: z.string(),
2024-11-01 17:51:14 +00:00
RawConfig: z.record(z.string(), z.string()).nullable(),
2024-09-11 04:55:00 +00:00
Rank: z.number(),
IndexOptions: z.string(),
HasSymbols: z.boolean(),
Tombstone: z.boolean(),
LatestCommitDate: z.string(),
FileTombstones: z.string().optional(),
});
export const listRepositoriesResponseSchema = z.object({
List: z.object({
Repos: z.array(z.object({
Repository: repositorySchema,
IndexMetadata: indexMetadataSchema,
2024-09-17 04:37:34 +00:00
Stats: repoStatsSchema,
2024-09-11 04:55:00 +00:00
})),
2024-09-17 04:37:34 +00:00
Stats: repoStatsSchema,
2024-09-11 04:55:00 +00:00
})
});
export const getVersionResponseSchema = z.object({
version: z.string(),
});