2022-04-01 04:29:09 +08:00
|
|
|
import { urqlClient } from '../urql';
|
2023-04-10 09:29:10 +08:00
|
|
|
import { ref } from 'vue';
|
2022-04-04 02:54:31 +08:00
|
|
|
import { RequestPolicy } from '@urql/vue';
|
2023-04-10 09:29:10 +08:00
|
|
|
import useNotifyToast from './alert_toast';
|
2022-04-01 04:29:09 +08:00
|
|
|
|
2023-04-10 09:29:10 +08:00
|
|
|
const { toastSuccess, toastInfo, toastWarning, toastError, swalSuccess, swalInfo, swalWarning, swalError } = useNotifyToast();
|
2022-04-01 04:29:09 +08:00
|
|
|
|
2022-04-23 14:20:33 +08:00
|
|
|
const errors = ref<any[]>([]);
|
|
|
|
|
2023-04-10 09:29:10 +08:00
|
|
|
export default function useApiUtil() {
|
2022-04-01 04:29:09 +08:00
|
|
|
// Automatic Error handling from Graphql backend
|
|
|
|
const gqlErrorHandler = (error: any) => {
|
2023-04-10 09:29:10 +08:00
|
|
|
if (typeof error == 'object') {
|
|
|
|
if (error.graphQLErrors) {
|
|
|
|
const gErrors = new Set();
|
|
|
|
error.graphQLErrors?.forEach((err: any) => gErrors.add(err.message));
|
|
|
|
gErrors?.forEach((err: any) => toastError(err));
|
|
|
|
}
|
|
|
|
if (error.networkError) {
|
|
|
|
toastError(error.networkError.message);
|
|
|
|
swalError('!!OOPS!!: Something just hapenned Please login again :)');
|
|
|
|
}
|
2022-04-01 04:29:09 +08:00
|
|
|
}
|
2023-04-10 09:29:10 +08:00
|
|
|
};
|
2022-04-01 04:29:09 +08:00
|
|
|
|
2023-04-10 09:29:10 +08:00
|
|
|
const gqlResponseHandler = (res: any): any => {
|
|
|
|
if (res.error) {
|
|
|
|
errors.value.unshift(res.error);
|
|
|
|
gqlErrorHandler(res.error);
|
|
|
|
}
|
|
|
|
return res.data;
|
|
|
|
};
|
|
|
|
|
|
|
|
const gqlOpertionalErrorHandler = (payload: any, key: string): any => {
|
|
|
|
if (payload.hasOwnProperty(key)) {
|
|
|
|
const res = payload[key];
|
|
|
|
if (res?.__typename && res?.__typename === 'OperationError') {
|
|
|
|
errors.value.unshift(res);
|
|
|
|
console.log('swalError');
|
|
|
|
swalError(res.error + '\n' + res.suggestion);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// toastInfo("operation success");
|
|
|
|
// instread of this which is not good. maybe create some dots status bar that appear green for success and red for error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return payload;
|
|
|
|
};
|
2022-04-01 04:29:09 +08:00
|
|
|
|
2023-04-10 09:29:10 +08:00
|
|
|
const GQLResponseInterceptor = (res: any, key: string): any => {
|
|
|
|
return gqlOpertionalErrorHandler(gqlResponseHandler(res), key);
|
|
|
|
};
|
2022-04-01 04:29:09 +08:00
|
|
|
|
2022-04-04 02:54:31 +08:00
|
|
|
async function withClientMutation(query, payload, dataKey): Promise<any> {
|
2023-04-10 09:29:10 +08:00
|
|
|
return await urqlClient
|
|
|
|
.mutation(query, payload)
|
|
|
|
.toPromise()
|
|
|
|
.then(result => {
|
|
|
|
const data = GQLResponseInterceptor(result, dataKey);
|
|
|
|
if (dataKey) {
|
|
|
|
return data[dataKey];
|
|
|
|
} else {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
});
|
2022-04-01 04:29:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-10 09:29:10 +08:00
|
|
|
async function withClientQuery(query, variables, dataKey, requestPolicy: RequestPolicy = 'cache-first'): Promise<any> {
|
|
|
|
// cache-and-network
|
|
|
|
return await urqlClient
|
|
|
|
.query(query, variables, { requestPolicy })
|
|
|
|
.toPromise()
|
|
|
|
.then(result => {
|
|
|
|
const data = GQLResponseInterceptor(result, dataKey);
|
|
|
|
if (dataKey) {
|
|
|
|
return data[dataKey];
|
|
|
|
} else {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
});
|
2022-04-01 04:29:09 +08:00
|
|
|
}
|
|
|
|
|
2023-04-10 09:29:10 +08:00
|
|
|
// --
|
|
|
|
return {
|
|
|
|
gqlResponseHandler,
|
|
|
|
gqlErrorHandler,
|
|
|
|
gqlOpertionalErrorHandler,
|
|
|
|
GQLResponseInterceptor,
|
|
|
|
withClientMutation,
|
|
|
|
withClientQuery,
|
|
|
|
};
|
2022-04-01 04:29:09 +08:00
|
|
|
}
|