parent
8dae161d7c
commit
5fc85b610f
@ -0,0 +1,65 @@
|
|||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
const searchProjectsResponse = z.object({
|
||||||
|
hits: z.object({
|
||||||
|
slug: z.string().regex(/^[\w!@$()`.+,"\-']{3,64}$/),
|
||||||
|
title: z.string(),
|
||||||
|
description: z.string(),
|
||||||
|
categories: z.string().array().default([]),
|
||||||
|
client_side: z.enum(["required", "optional", "unsupported"]),
|
||||||
|
server_side: z.enum(["required", "optional", "unsupported"]),
|
||||||
|
project_type: z.enum(["mod", "modpack", "resourcepack", "shader"]),
|
||||||
|
downloads: z.number(),
|
||||||
|
icon_url: z.string().url(),
|
||||||
|
color: z.number(),
|
||||||
|
project_id: z.string(),
|
||||||
|
author: z.string(),
|
||||||
|
display_categories: z.string().array().default([]),
|
||||||
|
versions: z.string().array(),
|
||||||
|
follows: z.number(),
|
||||||
|
date_created: z.string().datetime({offset: true}),
|
||||||
|
date_modified: z.string().datetime({offset: true}),
|
||||||
|
latest_version: z.string().optional(),
|
||||||
|
license: z.string(),
|
||||||
|
gallery: z.string().array().default([]),
|
||||||
|
featured_gallery: z.string().optional(),
|
||||||
|
}).array(),
|
||||||
|
offset: z.number(),
|
||||||
|
limit: z.number(),
|
||||||
|
total_hits: z.number()
|
||||||
|
});
|
||||||
|
|
||||||
|
const searchProjectsRequest = z.object({
|
||||||
|
query: z.string().optional(),
|
||||||
|
facets: z.string().regex(/^(categories|versions|license|project_type):(.+)$/).array().array().default([]),
|
||||||
|
index: z.enum(["relevance", "downloads", "follows", "newest", "updated"]).default("relevance"),
|
||||||
|
offset: z.number().default(0),
|
||||||
|
limit: z.number().default(10),
|
||||||
|
filters: z.string().optional()
|
||||||
|
});
|
||||||
|
|
||||||
|
function searchProjectsRequestToUrlParams(request: z.infer<typeof searchProjectsRequest>): URLSearchParams {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
if (typeof request.query != 'undefined') {
|
||||||
|
params.set("query", request.query);
|
||||||
|
}
|
||||||
|
params.set("facets", JSON.stringify(request.facets));
|
||||||
|
params.set("index", request.index);
|
||||||
|
params.set("offset", request.offset.toString());
|
||||||
|
params.set("limit", request.limit.toString());
|
||||||
|
if (typeof request.filters != 'undefined') {
|
||||||
|
params.set("filters", request.filters);
|
||||||
|
}
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function searchProjects(request: z.input<typeof searchProjectsRequest>): Promise<z.infer<typeof searchProjectsResponse>> {
|
||||||
|
const parsedRequest = searchProjectsRequest.parse(request);
|
||||||
|
const params = searchProjectsRequestToUrlParams(parsedRequest);
|
||||||
|
const response = await fetch("https://api.modrinth.com/v2/search?" + params.toString(), {
|
||||||
|
headers: {
|
||||||
|
"User-Agent": "NotModdermore/noversion (+https://git.skye.vg/me/not-moddermore/)"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return searchProjectsResponse.parse(await response.json());
|
||||||
|
}
|
Loading…
Reference in new issue