You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
58 lines
1.5 KiB
import { SearchResults } from "../types.d.ts";
|
|
import { newSession } from "../utils/http.ts";
|
|
import Work from "./Story.ts";
|
|
|
|
export interface SearchParameters {
|
|
query: string;
|
|
isTitle?: boolean;
|
|
limit?: number;
|
|
}
|
|
|
|
export interface PrivateSearchParameters {
|
|
query: string;
|
|
isTitle: boolean;
|
|
limit: number;
|
|
}
|
|
|
|
export default class Search {
|
|
#opts: PrivateSearchParameters = {
|
|
query: "",
|
|
isTitle: false,
|
|
limit: 30,
|
|
};
|
|
#session: ReturnType<typeof newSession>;
|
|
results: Work[] = [];
|
|
|
|
constructor(
|
|
opts: SearchParameters,
|
|
session: ReturnType<typeof newSession>,
|
|
) {
|
|
this.#session = session;
|
|
Object.assign(this.#opts, opts);
|
|
}
|
|
|
|
async update(pageNum: number) {
|
|
this.results = [];
|
|
|
|
const res: SearchResults =
|
|
await (await this.#session.get("/v4/search/stories", true, {
|
|
params: new URLSearchParams({
|
|
query: this.#opts.isTitle
|
|
? `title: ${this.#opts.query}`
|
|
: this.#opts.query,
|
|
limit: this.#opts.limit.toString(),
|
|
offset: (pageNum * this.#opts.limit).toString(),
|
|
}),
|
|
})).json();
|
|
|
|
for (let i = 0; i < res.stories.length; i++) {
|
|
const work = new Work(
|
|
res.stories[i].id,
|
|
this.#session,
|
|
);
|
|
|
|
this.results.push(work);
|
|
}
|
|
}
|
|
}
|