From 2f973f910ba236542e99765c3544064d2c0d388c Mon Sep 17 00:00:00 2001 From: Volpeon Date: Mon, 18 Oct 2021 13:57:48 +0200 Subject: Better interfaces and function responsibilities --- src/api/e621/index.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/api/e621/index.ts') diff --git a/src/api/e621/index.ts b/src/api/e621/index.ts index 411edae..2fc0d2e 100644 --- a/src/api/e621/index.ts +++ b/src/api/e621/index.ts @@ -1,6 +1,11 @@ import got from "got"; import config from "../../config"; +export interface GetPostQuery { + tags: readonly string[]; + maxPage: number; +} + export interface Post { id: number; file: { @@ -26,7 +31,7 @@ export const client = got.extend({ }, }); -export async function getPost(id: number) { +export async function getPostById(id: number) { const response = await client .get("https://e926.net/posts.json", { searchParams: { @@ -42,9 +47,7 @@ export async function getPost(id: number) { return response.posts[0]; } -export async function getRandomPost() { - const queryIndex = Math.floor(Math.random() * config.e621.queries.length); - const query = config.e621.queries[queryIndex]; +export async function getRandomPost(query: GetPostQuery) { const page = Math.floor(Math.random() * (query.maxPage - 1)) + 1; const response = await client @@ -64,5 +67,5 @@ export async function getRandomPost() { const postIndex = Math.floor(Math.random() * response.posts.length); const post = response.posts[postIndex]; - return { queryIndex, post }; + return post; } -- cgit v1.2.3-70-g09d2