Skip to content

Commit ee47e9f

Browse files
authored
Fix JWT unauthorized/expired issue (#73)
1 parent c2327c9 commit ee47e9f

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/util/graphql.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import { Org, Project } from "../util/types.js";
1111
import { getSlugFromName } from "./index.js";
12+
import chalk from "chalk";
1213

1314
export async function sendGraphQLReqToHypermode(jwt: string, query: string): Promise<any> {
1415
const url = "https://api.hypermode.com/graphql";
@@ -22,10 +23,23 @@ export async function sendGraphQLReqToHypermode(jwt: string, query: string): Pro
2223
method: "POST",
2324
};
2425

25-
const response = await fetch(url, options);
26+
try {
27+
const response = await fetch(url, options);
2628

27-
const data: any = await response.json();
28-
return data;
29+
if (!response.ok) {
30+
if (response.status === 401) {
31+
console.error(`Unauthorized. Please try ${chalk.blueBright("hyp login")} again.`);
32+
throw new Error("Unauthorized: Invalid or expired JWT token.");
33+
} else {
34+
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
35+
}
36+
}
37+
38+
const data = await response.json();
39+
return data;
40+
} catch (error: any) {
41+
throw new Error(`Failed to send GraphQL request: ${error?.message}`);
42+
}
2943
}
3044

3145
export async function sendMapRepoAndFinishProjectCreationReq(jwt: string, id: string, repoId: string, repoName: string): Promise<Project> {

0 commit comments

Comments
 (0)