forked from creations/profilePage
first commit
This commit is contained in:
commit
b11c87a506
26 changed files with 1782 additions and 0 deletions
46
src/helpers/lanyard.ts
Normal file
46
src/helpers/lanyard.ts
Normal file
|
@ -0,0 +1,46 @@
|
|||
import { lanyardConfig } from "@config/environment";
|
||||
import { fetch } from "bun";
|
||||
|
||||
export async function getLanyardData(id?: string): Promise<LanyardResponse> {
|
||||
let instance: string = lanyardConfig.instance;
|
||||
|
||||
if (instance.endsWith("/")) {
|
||||
instance = instance.slice(0, -1);
|
||||
}
|
||||
|
||||
if (!instance.startsWith("http://") && !instance.startsWith("https://")) {
|
||||
instance = `https://${instance}`;
|
||||
}
|
||||
|
||||
const url: string = `${instance}/v1/users/${id || lanyardConfig.userId}`;
|
||||
const res: Response = await fetch(url, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
return {
|
||||
success: false,
|
||||
error: {
|
||||
code: "API_ERROR",
|
||||
message: `Lanyard API responded with status ${res.status}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const data: LanyardResponse = (await res.json()) as LanyardResponse;
|
||||
|
||||
if (!data.success) {
|
||||
return {
|
||||
success: false,
|
||||
error: {
|
||||
code: "API_ERROR",
|
||||
message: "Failed to fetch valid Lanyard data",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue