Fix apps/trust lint issues
Signed-off-by: Émile Ré <emile@getprobo.com>
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import {
|
||||
Environment,
|
||||
type FetchFunction,
|
||||
Network,
|
||||
RecordSource,
|
||||
Store,
|
||||
} from "relay-runtime";
|
||||
import { GraphQLError } from "graphql";
|
||||
import type { PropsWithChildren } from "react";
|
||||
import { RelayEnvironmentProvider } from "react-relay";
|
||||
import { getPathPrefix } from "/utils/pathPrefix";
|
||||
import { makeFetchQuery } from "@probo/relay";
|
||||
|
||||
export class UnAuthenticatedError extends Error {
|
||||
constructor() {
|
||||
@@ -43,8 +42,8 @@ export function buildEndpoint(): string {
|
||||
host = window.location.origin;
|
||||
}
|
||||
|
||||
const formattedHost =
|
||||
host.startsWith("http://") || host.startsWith("https://")
|
||||
const formattedHost
|
||||
= host.startsWith("http://") || host.startsWith("https://")
|
||||
? host
|
||||
: `https://${host}`;
|
||||
|
||||
@@ -63,99 +62,6 @@ export function buildEndpoint(): string {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
const hasUnauthenticatedError = (error: GraphQLError) =>
|
||||
error.extensions?.code == "UNAUTHENTICATED";
|
||||
|
||||
const hasInvalidError = (error: GraphQLError) =>
|
||||
error.extensions?.code == "INVALID_REQUEST";
|
||||
|
||||
const fetchRelay: FetchFunction = async (
|
||||
request,
|
||||
variables,
|
||||
_,
|
||||
uploadables,
|
||||
) => {
|
||||
const requestInit: RequestInit = {
|
||||
method: "POST",
|
||||
credentials: "include",
|
||||
headers: {},
|
||||
};
|
||||
|
||||
if (uploadables) {
|
||||
const formData = new FormData();
|
||||
formData.append(
|
||||
"operations",
|
||||
JSON.stringify({
|
||||
operationName: request.name,
|
||||
query: request.text,
|
||||
variables: variables,
|
||||
}),
|
||||
);
|
||||
|
||||
const uploadableMap: {
|
||||
[key: string]: string[];
|
||||
} = {};
|
||||
|
||||
Object.keys(uploadables).forEach((key, index) => {
|
||||
uploadableMap[index] = [`variables.${key}`];
|
||||
});
|
||||
|
||||
formData.append("map", JSON.stringify(uploadableMap));
|
||||
|
||||
Object.keys(uploadables).forEach((key, index) => {
|
||||
formData.append(index.toString(), uploadables[key]);
|
||||
});
|
||||
|
||||
requestInit.body = formData;
|
||||
} else {
|
||||
// Extract slug from URL if present for slug-based routing
|
||||
const slugMatch = window.location.pathname.match(/^\/trust\/([^/]+)/);
|
||||
const slug = slugMatch ? slugMatch[1] : null;
|
||||
|
||||
requestInit.headers = {
|
||||
Accept:
|
||||
"application/graphql-response+json; charset=utf-8, application/json; charset=utf-8",
|
||||
"Content-Type": "application/json",
|
||||
...(slug ? { "X-Trust-Slug": slug } : {}),
|
||||
};
|
||||
|
||||
requestInit.body = JSON.stringify({
|
||||
operationName: request.name,
|
||||
query: request.text,
|
||||
variables,
|
||||
});
|
||||
}
|
||||
|
||||
const response = await fetch(buildEndpoint(), requestInit);
|
||||
|
||||
if (response.status === 500) {
|
||||
throw new InternalServerError();
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
|
||||
if (json.errors) {
|
||||
const errors = json.errors as GraphQLError[];
|
||||
|
||||
if (errors.find(hasUnauthenticatedError)) {
|
||||
throw new UnAuthenticatedError();
|
||||
}
|
||||
|
||||
const invalidError = errors.find(hasInvalidError);
|
||||
if (invalidError) {
|
||||
throw new InvalidError(
|
||||
invalidError.message,
|
||||
(invalidError.extensions.field as string) ?? "",
|
||||
(invalidError.extensions.cause as string) ?? "",
|
||||
);
|
||||
}
|
||||
|
||||
throw new Error(`Error fetching GraphQL query '${request.name}'`);
|
||||
}
|
||||
|
||||
return json;
|
||||
};
|
||||
|
||||
const source = new RecordSource();
|
||||
const store = new Store(source, {
|
||||
queryCacheExpirationTime: 1 * 60 * 1000,
|
||||
@@ -164,7 +70,7 @@ const store = new Store(source, {
|
||||
|
||||
export const consoleEnvironment = new Environment({
|
||||
configName: "trust",
|
||||
network: Network.create(fetchRelay),
|
||||
network: Network.create(makeFetchQuery(buildEndpoint())),
|
||||
store,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user