22 lines
829 B
TypeScript
22 lines
829 B
TypeScript
|
import GelbooruServiceResponse from "../models/GelbooruServiceResponse";
|
||
|
import GelbooruApiResponse from "../models/GelbooruApiResponse";
|
||
|
|
||
|
class GelbooruApiService {
|
||
|
async get(): Promise<GelbooruServiceResponse> {
|
||
|
const limit = 100;
|
||
|
const url: string = `https://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=${limit}&json=1&tags=2girls+sleeping`
|
||
|
|
||
|
const response: GelbooruApiResponse = await fetch(url)
|
||
|
.then(res => {
|
||
|
if (!res.ok) {
|
||
|
throw new Error("Error fetching images");
|
||
|
} else {
|
||
|
res.json();
|
||
|
}
|
||
|
}) as GelbooruApiResponse;
|
||
|
|
||
|
return { posts: response.post.map(post => ({ url: post.file_url, tags: post.tags.split(" ") })) };
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default new GelbooruApiService();
|