Skip to content

Commit

Permalink
Fix JWT unauthorized/expired issue
Browse files Browse the repository at this point in the history
  • Loading branch information
octref committed Jan 16, 2025
1 parent c2327c9 commit b170159
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/util/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import { Org, Project } from "../util/types.js";
import { getSlugFromName } from "./index.js";
import chalk from "chalk";

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

const response = await fetch(url, options);
try {
const response = await fetch(url, options);

const data: any = await response.json();
return data;
if (!response.ok) {
if (response.status === 401) {
console.error(`Unauthorized. Please try ${chalk.blueBright("hyp login")} again.`);
throw new Error("Unauthorized: Invalid or expired JWT token.");
} else {
throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
}
}

const data = await response.json();
return data;
} catch (error: any) {
throw new Error(`Failed to send GraphQL request: ${error?.message}`);
}
}

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

0 comments on commit b170159

Please sign in to comment.