aboutsummaryrefslogtreecommitdiff
path: root/174bg/discord-bot/source/controllers/uex.js
blob: 827013ae1db1aab0c9521e05d7d4fcf7ec262992 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { get } from "./http.js";
import { cached } from "../utilities/cache.js";

export async function categories() {
    return await cached("uex", "categories", 60 * 60 * 1000 * 24, () =>
        get("https://api.uexcorp.uk/2.0/categories"),
    );
}

export async function items(id_category) {
    let items = [];

    if (!id_category) {
        const { data: allCategories } = await categories();

        for (const category of allCategories) {
            const { data: categoryItems } = await cached(
                "uex",
                `items:${category.id}`,
                60 * 60 * 1000 * 24,
                () => get(`https://api.uexcorp.uk/2.0/items?id_category=${category.id}`),
            );

            if (!categoryItems) continue;

            items = items.concat(categoryItems);
        }
    } else {
        const { data } = await cached("uex", `items:${id_category}`, 60 * 60 * 1000 * 24, () =>
            get(`https://api.uexcorp.uk/2.0/items?category=${id_category}`),
        );

        items = data;
    }

    return items;
}

export default { categories, items };