Files
OSIT-AE-App-Svelte/src/lib/api_patch_object.ts
2024-06-24 14:42:14 -04:00

61 lines
1.4 KiB
TypeScript

import axios from 'axios';
// Updated 2024-05-23
export let patch_object = async function patch_object(
{
api_cfg=null,
endpoint='',
params={},
data={},
return_meta=false,
log_lvl=0
}: {
api_cfg: any,
endpoint: string,
params?: any,
data?: any,
return_meta?: boolean,
log_lvl?: number
}
) {
console.log('*** patch_object() ***');
if (log_lvl) {
// console.log(api_cfg);
console.log(endpoint);
console.log(params);
if (log_lvl > 1) {
console.log(data);
}
// console.log(return_meta);
// console.log(as_list);
}
let axios_api = axios.create({
baseURL: api_cfg['base_url'],
/* other custom settings */
});
axios_api.defaults.headers = api_cfg['headers'];
let response_data = await axios_api.patch(endpoint, data, { params: params })
.then(function (response) {
console.log(response.data);
return response.data['data'];
//return response.data;
})
.catch(function (error) {
if (error.response && error.response.status === 404) {
return null; // Returning null since there were no results
}
console.log(error);
return false; // Returning false since something may have gone wrong. Also more in line with what the API returns.
// return error;
});
if (log_lvl > 1) {
console.log(response_data);
}
return response_data;
}