fix: use buildUrl utility in search module
This commit is contained in:
parent
dac0b7cc34
commit
8016e366cd
1 changed files with 21 additions and 80 deletions
|
|
@ -1,16 +1,18 @@
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { githubRequest, buildUrl } from "../common/utils.js";
|
import { githubRequest, buildUrl } from "../common/utils.js";
|
||||||
|
|
||||||
// Schema definitions
|
export const SearchOptions = z.object({
|
||||||
export const SearchCodeSchema = z.object({
|
q: z.string(),
|
||||||
q: z.string().describe("Search query. See GitHub code search syntax: https://docs.github.com/en/search-github/searching-on-github/searching-code"),
|
order: z.enum(["asc", "desc"]).optional(),
|
||||||
order: z.enum(["asc", "desc"]).optional().describe("Sort order (asc or desc)"),
|
page: z.number().min(1).optional(),
|
||||||
per_page: z.number().min(1).max(100).optional().describe("Results per page (max 100)"),
|
per_page: z.number().min(1).max(100).optional(),
|
||||||
page: z.number().min(1).optional().describe("Page number"),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const SearchIssuesSchema = z.object({
|
export const SearchUsersOptions = SearchOptions.extend({
|
||||||
q: z.string().describe("Search query. See GitHub issues search syntax: https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests"),
|
sort: z.enum(["followers", "repositories", "joined"]).optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export const SearchIssuesOptions = SearchOptions.extend({
|
||||||
sort: z.enum([
|
sort: z.enum([
|
||||||
"comments",
|
"comments",
|
||||||
"reactions",
|
"reactions",
|
||||||
|
|
@ -23,82 +25,21 @@ export const SearchIssuesSchema = z.object({
|
||||||
"interactions",
|
"interactions",
|
||||||
"created",
|
"created",
|
||||||
"updated",
|
"updated",
|
||||||
]).optional().describe("Sort field"),
|
]).optional(),
|
||||||
order: z.enum(["asc", "desc"]).optional().describe("Sort order (asc or desc)"),
|
|
||||||
per_page: z.number().min(1).max(100).optional().describe("Results per page (max 100)"),
|
|
||||||
page: z.number().min(1).optional().describe("Page number"),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const SearchUsersSchema = z.object({
|
export const SearchCodeSchema = SearchOptions;
|
||||||
q: z.string().describe("Search query. See GitHub users search syntax: https://docs.github.com/en/search-github/searching-on-github/searching-users"),
|
export const SearchUsersSchema = SearchUsersOptions;
|
||||||
sort: z.enum(["followers", "repositories", "joined"]).optional().describe("Sort field"),
|
export const SearchIssuesSchema = SearchIssuesOptions;
|
||||||
order: z.enum(["asc", "desc"]).optional().describe("Sort order (asc or desc)"),
|
|
||||||
per_page: z.number().min(1).max(100).optional().describe("Results per page (max 100)"),
|
|
||||||
page: z.number().min(1).optional().describe("Page number"),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Response schemas
|
export async function searchCode(params: z.infer<typeof SearchCodeSchema>) {
|
||||||
export const SearchCodeItemSchema = z.object({
|
return githubRequest(buildUrl("https://api.github.com/search/code", params));
|
||||||
name: z.string().describe("The name of the file"),
|
|
||||||
path: z.string().describe("The path to the file in the repository"),
|
|
||||||
sha: z.string().describe("The SHA hash of the file"),
|
|
||||||
url: z.string().describe("The API URL for this file"),
|
|
||||||
git_url: z.string().describe("The Git URL for this file"),
|
|
||||||
html_url: z.string().describe("The HTML URL to view this file on GitHub"),
|
|
||||||
repository: z.object({
|
|
||||||
full_name: z.string(),
|
|
||||||
description: z.string().nullable(),
|
|
||||||
url: z.string(),
|
|
||||||
html_url: z.string(),
|
|
||||||
}).describe("The repository where this file was found"),
|
|
||||||
score: z.number().describe("The search result score"),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const SearchCodeResponseSchema = z.object({
|
|
||||||
total_count: z.number().describe("Total number of matching results"),
|
|
||||||
incomplete_results: z.boolean().describe("Whether the results are incomplete"),
|
|
||||||
items: z.array(SearchCodeItemSchema).describe("The search results"),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const SearchUsersResponseSchema = z.object({
|
|
||||||
total_count: z.number().describe("Total number of matching results"),
|
|
||||||
incomplete_results: z.boolean().describe("Whether the results are incomplete"),
|
|
||||||
items: z.array(z.object({
|
|
||||||
login: z.string().describe("The username of the user"),
|
|
||||||
id: z.number().describe("The ID of the user"),
|
|
||||||
node_id: z.string().describe("The Node ID of the user"),
|
|
||||||
avatar_url: z.string().describe("The avatar URL of the user"),
|
|
||||||
gravatar_id: z.string().describe("The Gravatar ID of the user"),
|
|
||||||
url: z.string().describe("The API URL for this user"),
|
|
||||||
html_url: z.string().describe("The HTML URL to view this user on GitHub"),
|
|
||||||
type: z.string().describe("The type of this user"),
|
|
||||||
site_admin: z.boolean().describe("Whether this user is a site administrator"),
|
|
||||||
score: z.number().describe("The search result score"),
|
|
||||||
})).describe("The search results"),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Type exports
|
|
||||||
export type SearchCodeParams = z.infer<typeof SearchCodeSchema>;
|
|
||||||
export type SearchIssuesParams = z.infer<typeof SearchIssuesSchema>;
|
|
||||||
export type SearchUsersParams = z.infer<typeof SearchUsersSchema>;
|
|
||||||
export type SearchCodeResponse = z.infer<typeof SearchCodeResponseSchema>;
|
|
||||||
export type SearchUsersResponse = z.infer<typeof SearchUsersResponseSchema>;
|
|
||||||
|
|
||||||
// Function implementations
|
|
||||||
export async function searchCode(params: SearchCodeParams): Promise<SearchCodeResponse> {
|
|
||||||
const url = buildUrl("https://api.github.com/search/code", params);
|
|
||||||
const response = await githubRequest(url);
|
|
||||||
return SearchCodeResponseSchema.parse(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function searchIssues(params: SearchIssuesParams) {
|
export async function searchIssues(params: z.infer<typeof SearchIssuesSchema>) {
|
||||||
const url = buildUrl("https://api.github.com/search/issues", params);
|
return githubRequest(buildUrl("https://api.github.com/search/issues", params));
|
||||||
const response = await githubRequest(url);
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function searchUsers(params: SearchUsersParams): Promise<SearchUsersResponse> {
|
export async function searchUsers(params: z.infer<typeof SearchUsersSchema>) {
|
||||||
const url = buildUrl("https://api.github.com/search/users", params);
|
return githubRequest(buildUrl("https://api.github.com/search/users", params));
|
||||||
const response = await githubRequest(url);
|
}
|
||||||
return SearchUsersResponseSchema.parse(response);
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue