shared-library/meilisearch/sdk.ts

43 lines
932 B
TypeScript
Raw Normal View History

2024-07-13 10:47:50 +00:00
import { MeiliDocument, SearchRequest, SearchResponse } from "./types";
export class MeilisearchSDK {
constructor(
private readonly apiURL: string,
private readonly bearer: string
) {}
async search({
q,
page,
types,
}: SearchRequest): Promise<SearchResponse<MeiliDocument>> {
const filter: string[] = [];
if (types) {
if (typeof types === "string") {
filter.push(`type = ${types}`);
} else {
filter.push(`type IN [${types.join(", ")}]`);
}
}
const body = {
q,
page,
hitsPerPage: 25,
filter,
sort: ["updatedAt:desc"],
};
const result = await fetch(`${this.apiURL}/indexes/DOCUMENT/search`, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.bearer}`,
},
});
return await result.json();
}
}