|
|
|
@ -11,9 +11,83 @@ import { authExchange } from "@urql/exchange-auth";
|
|
|
|
|
import { GraphQLError } from "graphql";
|
|
|
|
|
import z, { Infer } from "myzod";
|
|
|
|
|
|
|
|
|
|
const getHost = () => z.string().parse(process.env.DIRECTUS_HOST);
|
|
|
|
|
const getPort = () =>
|
|
|
|
|
z
|
|
|
|
|
.bigint()
|
|
|
|
|
.parse(
|
|
|
|
|
process.env.DIRECTUS_PORT && Number.parseInt(process.env.DIRECTUS_PORT)
|
|
|
|
|
);
|
|
|
|
|
const getGraphqlUrl = () => `http://${getHost()}:${getPort()}/graphql`;
|
|
|
|
|
|
|
|
|
|
export const createDirectusSystemClient = () => {
|
|
|
|
|
const client = createClient({
|
|
|
|
|
url: "http://localhost:8055/graphql/system",
|
|
|
|
|
url: getGraphqlUrl() + "/system",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createDirectusClient = (
|
|
|
|
|
session: Session,
|
|
|
|
|
onAuthError?: Function
|
|
|
|
|
) => {
|
|
|
|
|
const client = createClient({
|
|
|
|
|
url: getGraphqlUrl(),
|
|
|
|
|
exchanges: [
|
|
|
|
|
authExchange({
|
|
|
|
|
getAuth: async ({ authState }) => {
|
|
|
|
|
if (!authState) {
|
|
|
|
|
const token = session.get("accessToken");
|
|
|
|
|
const refreshToken = session.get("refreshToken");
|
|
|
|
|
if (token && refreshToken) {
|
|
|
|
|
return { token, refreshToken };
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
addAuthToOperation: ({ authState, operation }: any) => {
|
|
|
|
|
if (!authState || !authState.token) {
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchOptions =
|
|
|
|
|
typeof operation.context.fetchOptions === "function"
|
|
|
|
|
? operation.context.fetchOptions()
|
|
|
|
|
: operation.context.fetchOptions || {};
|
|
|
|
|
|
|
|
|
|
return makeOperation(operation.kind, operation, {
|
|
|
|
|
...operation.context,
|
|
|
|
|
fetchOptions: {
|
|
|
|
|
...fetchOptions,
|
|
|
|
|
headers: {
|
|
|
|
|
...fetchOptions.headers,
|
|
|
|
|
Authorization: `Bearer ${authState.token}`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
errorExchange({
|
|
|
|
|
onError: async (error, o) => {
|
|
|
|
|
console.log("code", error.graphQLErrors[0].extensions.code);
|
|
|
|
|
console.log(error.graphQLErrors[0].extensions.graphqlErrors);
|
|
|
|
|
const isAuthError = error.graphQLErrors.some(
|
|
|
|
|
(e: GraphQLError) =>
|
|
|
|
|
["TOKEN_EXPIRED", "FORBIDDEN"].indexOf(
|
|
|
|
|
e.extensions?.code as string
|
|
|
|
|
) >= 0
|
|
|
|
|
);
|
|
|
|
|
console.log("isAuthError", isAuthError);
|
|
|
|
|
if (onAuthError && isAuthError) {
|
|
|
|
|
onAuthError();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
fetchExchange,
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
@ -111,68 +185,3 @@ export const logout = async (options: {
|
|
|
|
|
|
|
|
|
|
return z.boolean().parse(result.data.auth_logout);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const createDirectusClient = (
|
|
|
|
|
session: Session,
|
|
|
|
|
onAuthError?: Function
|
|
|
|
|
) => {
|
|
|
|
|
const client = createClient({
|
|
|
|
|
url: "http://localhost:8055/graphql",
|
|
|
|
|
exchanges: [
|
|
|
|
|
authExchange({
|
|
|
|
|
getAuth: async ({ authState }) => {
|
|
|
|
|
if (!authState) {
|
|
|
|
|
const token = session.get("accessToken");
|
|
|
|
|
const refreshToken = session.get("refreshToken");
|
|
|
|
|
if (token && refreshToken) {
|
|
|
|
|
return { token, refreshToken };
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
addAuthToOperation: ({ authState, operation }: any) => {
|
|
|
|
|
if (!authState || !authState.token) {
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchOptions =
|
|
|
|
|
typeof operation.context.fetchOptions === "function"
|
|
|
|
|
? operation.context.fetchOptions()
|
|
|
|
|
: operation.context.fetchOptions || {};
|
|
|
|
|
|
|
|
|
|
return makeOperation(operation.kind, operation, {
|
|
|
|
|
...operation.context,
|
|
|
|
|
fetchOptions: {
|
|
|
|
|
...fetchOptions,
|
|
|
|
|
headers: {
|
|
|
|
|
...fetchOptions.headers,
|
|
|
|
|
Authorization: `Bearer ${authState.token}`,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
errorExchange({
|
|
|
|
|
onError: async (error, o) => {
|
|
|
|
|
console.log("code", error.graphQLErrors[0].extensions.code);
|
|
|
|
|
console.log(error.graphQLErrors[0].extensions.graphqlErrors);
|
|
|
|
|
const isAuthError = error.graphQLErrors.some(
|
|
|
|
|
(e: GraphQLError) =>
|
|
|
|
|
["TOKEN_EXPIRED", "FORBIDDEN"].indexOf(
|
|
|
|
|
e.extensions?.code as string
|
|
|
|
|
) >= 0
|
|
|
|
|
);
|
|
|
|
|
console.log("isAuthError", isAuthError);
|
|
|
|
|
if (onAuthError && isAuthError) {
|
|
|
|
|
onAuthError();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
fetchExchange,
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
};
|
|
|
|
|