2021-06-29 14:33:36 +05:30
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
|
//
|
2023-01-02 11:53:55 +05:30
|
|
|
// Copyright (C) 2013 - 2023, The pgAdmin Development Team
|
2021-06-29 14:33:36 +05:30
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
|
//
|
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
import pgAdmin from 'sources/pgadmin';
|
|
|
|
|
import gettext from 'sources/gettext';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
|
|
/* Get the axios instance to call back end APIs.
|
|
|
|
|
Do not import axios directly, instead use this */
|
|
|
|
|
export default function getApiInstance(headers={}) {
|
2022-08-11 10:49:45 +05:30
|
|
|
return axios.create({
|
2021-06-29 14:33:36 +05:30
|
|
|
headers: {
|
|
|
|
|
'Content-type': 'application/json',
|
|
|
|
|
[pgAdmin.csrf_token_header]: pgAdmin.csrf_token,
|
|
|
|
|
...headers,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function parseApiError(error) {
|
|
|
|
|
if (error.response) {
|
|
|
|
|
// The request was made and the server responded with a status code
|
|
|
|
|
// that falls out of the range of 2xx
|
|
|
|
|
if(error.response.headers['content-type'] == 'application/json') {
|
2022-04-07 17:36:56 +05:30
|
|
|
return error.response.data.errormsg;
|
2021-06-29 14:33:36 +05:30
|
|
|
} else {
|
|
|
|
|
return error.response.statusText;
|
|
|
|
|
}
|
|
|
|
|
} else if (error.request) {
|
|
|
|
|
// The request was made but no response was received
|
|
|
|
|
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
|
|
|
|
|
// http.ClientRequest in node.js
|
|
|
|
|
return gettext('Connection to pgAdmin server has been lost');
|
2022-02-11 10:36:24 +05:30
|
|
|
} else if(error.message) {
|
2021-06-29 14:33:36 +05:30
|
|
|
// Something happened in setting up the request that triggered an Error
|
|
|
|
|
return error.message;
|
2022-11-16 14:37:54 +05:30
|
|
|
} else if(error.errormsg) {
|
|
|
|
|
// Received response JSON in socket handle
|
|
|
|
|
return error.errormsg;
|
2022-02-11 10:36:24 +05:30
|
|
|
} else {
|
|
|
|
|
return error;
|
2021-06-29 14:33:36 +05:30
|
|
|
}
|
|
|
|
|
}
|
2023-03-20 18:20:48 +05:30
|
|
|
|
|
|
|
|
export function callFetch(url, options, headers={}) {
|
|
|
|
|
return fetch(url, {
|
|
|
|
|
...options,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-type': 'application/json',
|
|
|
|
|
[pgAdmin.csrf_token_header]: pgAdmin.csrf_token,
|
|
|
|
|
...headers,
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|