Skip to content

Commit

Permalink
yes
Browse files Browse the repository at this point in the history
  • Loading branch information
PurpleCreativity committed Dec 14, 2024
1 parent af54b10 commit 0e1fc02
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import axios, { AxiosError, type AxiosResponse, type AxiosInstance, type AxiosRequestConfig } from "axios";
import axios, { AxiosError, type AxiosInstance, type AxiosRequestConfig } from "axios";
import type {
HttpMethod,
LegacyFetchOptions,
LegacyListFetchOptions,
PaginatedResponse,
} from "../../types/fetchHandler.js";
} from "../../types/internal/LegacyFetchHandler.js";
import CacheManager from "./cacheManager.js";

type credentials = {
cookie?: string;
csrf?: string;
key?: string;
};

export default class FetchHandler {
export default class LegacyFetchHandler {
private client: AxiosInstance;
private cache = new CacheManager<string, unknown>();

Expand Down Expand Up @@ -71,7 +70,7 @@ export default class FetchHandler {
this.credentials = { ...this.credentials, ...credentials };
};

async fetchLegacy<T = unknown>(
async fetch<T = unknown>(
method: HttpMethod,
API: keyof typeof this.LegacyAPI,
route: string,
Expand Down Expand Up @@ -99,7 +98,7 @@ export default class FetchHandler {
this.credentials.csrf = response.headers["x-csrf-token"];

if (response.status === 403) {
return await this.fetchLegacy(method, API, route, options);
return await this.fetch(method, API, route, options);
}
}

Expand All @@ -108,7 +107,7 @@ export default class FetchHandler {
return response.data;
}

async fetchLegacyList<T = unknown>(
async fetchList<T = unknown>(
method: HttpMethod,
API: keyof typeof this.LegacyAPI,
route: string,
Expand All @@ -119,7 +118,7 @@ export default class FetchHandler {

while (true) {
try {
const response = await this.fetchLegacy<PaginatedResponse<T>>(
const response = await this.fetch<PaginatedResponse<T>>(
method,
API,
`${route}?limit=${options.maxResults || 100}&cursor=${cursor}`,
Expand Down
4 changes: 2 additions & 2 deletions src/functions/legacy/presence/v1/fetchUsersPresence.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyFetchOptions } from "../../../../types/fetchHandler.js";
import type { LegacyFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";
import type { UserPresence } from "../../../../types/legacy/Presence.js";

export type UsersPresenceResponse = {
Expand All @@ -22,7 +22,7 @@ export default async function (
fetchOptions?: Partial<LegacyFetchOptions>,
): Promise<UserPresence[]> {
const data = (
await this.fetchHandler.fetchLegacy<UsersPresenceResponse>("POST", "PresenceV1", "/presence/users", {
await this.LegacyFetchHandler.fetch<UsersPresenceResponse>("POST", "PresenceV1", "/presence/users", {
body: { userIds: Array.isArray(userIds) ? userIds : [userIds] },
params: {},
useCache: fetchOptions?.useCache ?? true,
Expand Down
45 changes: 22 additions & 23 deletions src/functions/legacy/thumbnails/v1/fetchBadgeIcons.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyFetchOptions } from "../../../../types/fetchHandler.js";
import type {
BadgeIconImageFormat,
BadgeIconImageSize,
fetchedBadgeIcon,
} from "../../../../types/legacy/Thumbnails.js";
import type { LegacyFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";
import type { fetchedImage } from "../../../../types/legacy/Thumbnails.js";
import { BadgeIconImageFormat, BadgeIconImageSize } from "../../../../types/legacy/ThumbnailsEnums.js";

export type fetchBadgeIconsResponse = { data: fetchedBadgeIcon[] };
export type fetchBadgeIconsResponse = { data: fetchedImage[] };

export type fetchBadgeIconsType = (
this: BloxFetch,
Expand All @@ -17,27 +14,29 @@ export type fetchBadgeIconsType = (
isCircular: boolean,

fetchOptions?: Partial<LegacyFetchOptions>,
) => Promise<fetchedBadgeIcon[]>;
) => Promise<fetchedImage[]>;

export default async function (
this: BloxFetch,

badgeIds: number | number[],
size: BadgeIconImageSize,
format: BadgeIconImageFormat,
isCircular: boolean,
size: BadgeIconImageSize = BadgeIconImageSize["150x150"],
format: BadgeIconImageFormat = BadgeIconImageFormat.Png,
isCircular = false,

fetchOptions?: Partial<LegacyFetchOptions>,
): Promise<fetchedBadgeIcon[]> {
return await this.fetchHandler.fetchLegacy("GET", "ThumbnailsV1", "/v1/badges/icons", {
params: {
badgeIds: Array.isArray(badgeIds) ? badgeIds : [badgeIds],

size: size,
format: format,
isCircular: isCircular,
},
body: {},
useCache: fetchOptions?.useCache ?? true,
});
): Promise<fetchedImage[]> {
return (
await this.LegacyFetchHandler.fetch<fetchBadgeIconsResponse>("GET", "ThumbnailsV1", "/v1/badges/icons", {
params: {
badgeIds: Array.isArray(badgeIds) ? badgeIds : [badgeIds],

size: size,
format: format,
isCircular: isCircular,
},
body: {},
useCache: fetchOptions?.useCache ?? true,
})
).data;
}
42 changes: 42 additions & 0 deletions src/functions/legacy/thumbnails/v1/fetchGamepassIcons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";
import type { fetchedImage } from "../../../../types/legacy/Thumbnails.js";
import { GamePassImageFormat, GamePassImageSize } from "../../../../types/legacy/ThumbnailsEnums.js";

export type fetchGamepassIconsResponse = { data: fetchedImage[] };

export type fetchGamepassIconsType = (
this: BloxFetch,

gamePassIds: number | number[],
size: GamePassImageSize,
format: GamePassImageFormat,
isCircular: boolean,

fetchOptions?: Partial<LegacyFetchOptions>,
) => Promise<fetchedImage[]>;

export default async function (
this: BloxFetch,

gamePassIds: number | number[],
size: GamePassImageSize = GamePassImageSize["150x150"],
format: GamePassImageFormat = GamePassImageFormat.Png,
isCircular = false,

fetchOptions?: Partial<LegacyFetchOptions>,
): Promise<fetchedImage[]> {
return (
await this.LegacyFetchHandler.fetch<fetchGamepassIconsResponse>("GET", "ThumbnailsV1", "/v1/badges/icons", {
params: {
gamePassIds: Array.isArray(gamePassIds) ? gamePassIds : [gamePassIds],

size: size,
format: format,
isCircular: isCircular,
},
body: {},
useCache: fetchOptions?.useCache ?? true,
})
).data;
}
4 changes: 2 additions & 2 deletions src/functions/legacy/users/v1/fetchBirthdate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyFetchOptions } from "../../../../types/fetchHandler.js";
import type { LegacyFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";
import type { UserBirthdate } from "../../../../types/legacy/Users.js";

export type fetchBirthdateType = (
Expand All @@ -8,7 +8,7 @@ export type fetchBirthdateType = (
) => Promise<UserBirthdate>;

export default async function (this: BloxFetch, fetchOptions?: Partial<LegacyFetchOptions>): Promise<UserBirthdate> {
return await this.fetchHandler.fetchLegacy<UserBirthdate>("GET", "UsersV1", "/birthdate", {
return await this.LegacyFetchHandler.fetch<UserBirthdate>("GET", "UsersV1", "/birthdate", {
params: {},
body: {},
useCache: fetchOptions?.useCache ?? true,
Expand Down
4 changes: 2 additions & 2 deletions src/functions/legacy/users/v1/fetchUserById.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyFetchOptions } from "../../../../types/fetchHandler.js";
import type { LegacyFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";
import type { UserData, rawUserData } from "../../../../types/legacy/Users.js";

export type fetchUserByIdType = (
Expand All @@ -17,7 +17,7 @@ export default async function (

fetchOptions?: Partial<LegacyFetchOptions>,
): Promise<UserData> {
const rawdata = await this.fetchHandler.fetchLegacy<rawUserData>("GET", "UsersV1", `/users/${userId}`, {
const rawdata = await this.LegacyFetchHandler.fetch<rawUserData>("GET", "UsersV1", `/users/${userId}`, {
params: {},
body: {},
useCache: fetchOptions?.useCache ?? true,
Expand Down
4 changes: 2 additions & 2 deletions src/functions/legacy/users/v1/fetchUsernameHistory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyListFetchOptions } from "../../../../types/fetchHandler.js";
import type { LegacyListFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";

export type UsernameHistoryResponse = {
name: string;
Expand All @@ -21,7 +21,7 @@ export default async function (
fetchOptions?: Partial<LegacyListFetchOptions>,
): Promise<string[]> {
return (
await this.fetchHandler.fetchLegacyList<UsernameHistoryResponse>(
await this.LegacyFetchHandler.fetchList<UsernameHistoryResponse>(
"GET",
"UsersV1",
`/users/${userId}/username-history`,
Expand Down
4 changes: 2 additions & 2 deletions src/functions/legacy/users/v1/fetchUsersByIds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyFetchOptions } from "../../../../types/fetchHandler.js";
import type { LegacyFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";
import type { partialUserDataByIds } from "../../../../types/legacy/Users.js";

export type fetchUsersByIdsResponse = { data: partialUserDataByIds[] };
Expand All @@ -22,7 +22,7 @@ export default async function (
fetchOptions?: Partial<LegacyFetchOptions>,
): Promise<partialUserDataByIds[]> {
return (
await this.fetchHandler.fetchLegacy<fetchUsersByIdsResponse>("POST", "UsersV1", "/users", {
await this.LegacyFetchHandler.fetch<fetchUsersByIdsResponse>("POST", "UsersV1", "/users", {
params: {},
body: {
userIds: Array.isArray(userIds) ? userIds : [userIds],
Expand Down
4 changes: 2 additions & 2 deletions src/functions/legacy/users/v1/fetchUsersByUsernames.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type BloxFetch from "../../../../main.js";
import type { LegacyFetchOptions } from "../../../../types/fetchHandler.js";
import type { LegacyFetchOptions } from "../../../../types/internal/LegacyFetchHandler.js";
import type { partialUserDataByUsernames } from "../../../../types/legacy/Users.js";

export type fetchUsersByUsernamesResponse = { data: partialUserDataByUsernames[] };
Expand All @@ -22,7 +22,7 @@ export default async function (
fetchOptions?: Partial<LegacyFetchOptions>,
): Promise<partialUserDataByUsernames[]> {
return (
await this.fetchHandler.fetchLegacy<fetchUsersByUsernamesResponse>("POST", "UsersV1", "/usernames/users", {
await this.LegacyFetchHandler.fetch<fetchUsersByUsernamesResponse>("POST", "UsersV1", "/usernames/users", {
params: {},
body: {
usernames: Array.isArray(usernames) ? usernames : [usernames],
Expand Down
14 changes: 10 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import FetchHandler from "./classes/internal/fetchHandler.js";
import LegacyFetchHandler from "./classes/internal/LegacyFetchHandler.js";

import fetchUsersPresence, { type fetchUsersPresenceType } from "./functions/legacy/presence/v1/fetchUsersPresence.js";
import fetchBadgeIcons, { type fetchBadgeIconsType } from "./functions/legacy/thumbnails/v1/fetchBadgeIcons.js";
import fetchGamepassIcons, {
type fetchGamepassIconsType,
} from "./functions/legacy/thumbnails/v1/fetchGamepassIcons.js";
import fetchBirthdate, { type fetchBirthdateType } from "./functions/legacy/users/v1/fetchBirthdate.js";
import fetchUserById, { type fetchUserByIdType } from "./functions/legacy/users/v1/fetchUserById.js";
import fetchUsernameHistory, {
Expand All @@ -18,7 +21,7 @@ import fetchUsersByUsernames, {
//export type * from "./types/legacy/Games.js";
//export type * from "./types/legacy/Groups.js";

export type * from "./types/fetchHandler.js";
export type * from "./types/internal/LegacyFetchHandler.js";
export type * from "./types/shared.js";

/**
Expand All @@ -28,7 +31,7 @@ export default class BloxFetch {
/**
* Instance of the internal fetch handler responsible for making API requests.
*/
readonly fetchHandler: FetchHandler;
readonly LegacyFetchHandler: LegacyFetchHandler;

//? UsersV1

Expand Down Expand Up @@ -69,11 +72,13 @@ export default class BloxFetch {
//? ThumbnailsV1

readonly fetchBadgeIcons: fetchBadgeIconsType;

readonly fetchGamepassIcons: fetchGamepassIconsType;
/**
* Initializes a new instance of the BloxFetch class.
*/
constructor() {
this.fetchHandler = new FetchHandler({});
this.LegacyFetchHandler = new LegacyFetchHandler({});

//? UsersV1
this.fetchUsernameHistory = fetchUsernameHistory;
Expand All @@ -89,5 +94,6 @@ export default class BloxFetch {

//? ThumbnailsV1
this.fetchBadgeIcons = fetchBadgeIcons;
this.fetchGamepassIcons = fetchGamepassIcons;
}
}
File renamed without changes.
78 changes: 1 addition & 77 deletions src/types/legacy/Thumbnails.d.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,6 @@
export type fetchedBadgeIcon = {
export type fetchedImage = {
targetId: number;
state: string;
imageUrl: string;
version: string;
};

//? Enums

// User Avatar

export enum UserAvatarImageSize {
"48x48" = "48x49",
"50x50" = "50x50",
"60x60" = "60x60",
"75x75" = "75x75",
"100x100" = "100x100",
"110x110" = "110x110",
"150x150" = "150x150",
"180x180" = "180x180",
"352x352" = "352x352",
"420x420" = "420x420",
"720x720" = "720x720",
}

export enum UserAvatarImageFormat {
Png = "Png",
Jpeg = "Jpeg",
Webp = "Webp",
}

// User Avatar Bust

export enum UserAvatarBustImageSize {
"48x48" = "48x49",
"50x50" = "50x50",
"60x60" = "60x60",
"75x75" = "75x75",
"100x100" = "100x100",
"150x150" = "150x150",
"180x180" = "180x180",
"352x352" = "352x352",
"420x420" = "420x420",
}

export enum UserAvatarImageFormat {
Png = "Png",
Webp = "Webp",
}

// User Avatar Headshot

export enum UserAvatarHeadshotImageSize {
"48x48" = "48x49",
"50x50" = "50x50",
"60x60" = "60x60",
"75x75" = "75x75",
"100x100" = "100x100",
"110x110" = "110x110",
"150x150" = "150x150",
"180x180" = "180x180",
"352x352" = "352x352",
"420x420" = "420x420",
"720x720" = "720x720",
}

export enum UserAvatarHeadshotImageFormat {
Png = "Png",
Jpeg = "Jpeg",
Webp = "Webp",
}

// Badge Icon

export enum BadgeIconImageSize {
"150x150" = "150x150",
}

export enum BadgeIconImageFormat {
Png = "Png",
Webp = "Webp",
}
Loading

0 comments on commit 0e1fc02

Please sign in to comment.