| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import pgAdmin from 'sources/pgadmin'; |
| import gettext from 'sources/gettext'; |
| import axios from 'axios'; |
|
|
| |
| |
| export default function getApiInstance(headers={}) { |
| return axios.create({ |
| headers: { |
| 'Content-type': 'application/json', |
| [pgAdmin.csrf_token_header]: pgAdmin.csrf_token, |
| ...headers, |
| } |
| }); |
| } |
|
|
| export function parseApiError(error, withData=false) { |
| if (error.response) { |
| |
| |
| if(error.response.headers['content-type'] == 'application/json') { |
| let err_resp_data = error.response.data; |
| if (err_resp_data.response != undefined && Array.isArray(err_resp_data.response.errors)) { |
| return err_resp_data.response.errors[0]; |
| } else { |
| let errormsg = err_resp_data.errormsg; |
| let data = error.response.data.data; |
| |
| |
| return withData ? {errormsg, data} : errormsg; |
| } |
| } else { |
| return error.response.statusText; |
| } |
| } else if (error.request) { |
| |
| |
| |
| return gettext('Connection to pgAdmin server has been lost'); |
| } else if(error.message) { |
| |
| return error.message; |
| } else if(error.errormsg) { |
| |
| return error.errormsg; |
| } else { |
| return error; |
| } |
| } |
|
|
| export function callFetch(url, options, headers={}) { |
| return fetch(url, { |
| ...options, |
| headers: { |
| 'Content-type': 'application/json', |
| [pgAdmin.csrf_token_header]: pgAdmin.csrf_token, |
| ...headers, |
| } |
| }); |
| } |
|
|