Files
mixtape/zero.Backoffice.UI/app/services/request.ts
T

120 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-12-06 15:06:38 +01:00
import axios from 'axios';
2021-12-13 14:28:25 +01:00
import { paths } from '../options';
2021-12-14 11:54:36 +01:00
import { ApiRequestConfig } from './request.types';
2021-03-24 20:26:55 +01:00
2021-12-14 11:54:36 +01:00
export * from './request.types';
2021-12-07 15:59:29 +01:00
2021-12-14 12:16:27 +01:00
export function get(url: string, config?: ApiRequestConfig)
2021-03-24 20:26:55 +01:00
{
2021-12-14 12:16:27 +01:00
return send({ method: 'get', url, ...config });
2021-03-24 20:26:55 +01:00
}
2021-12-14 12:16:27 +01:00
export function post(url: string, data: any, config?: ApiRequestConfig)
2021-03-24 20:26:55 +01:00
{
2021-12-14 12:16:27 +01:00
return send({ method: 'post', url, data, ...config });
2021-03-24 20:26:55 +01:00
}
2021-12-16 16:59:27 +01:00
export function del(url: string, data: any, config?: ApiRequestConfig)
2021-03-24 20:26:55 +01:00
{
2021-12-16 16:59:27 +01:00
return send({ method: 'delete', url, data, ...config });
2021-03-24 20:26:55 +01:00
}
2021-12-14 12:16:27 +01:00
export function put(url: string, data: any, config?: ApiRequestConfig)
2021-03-24 20:26:55 +01:00
{
2021-12-14 12:16:27 +01:00
return send({ method: 'put', url, data, ...config });
2021-03-24 20:26:55 +01:00
}
2021-12-14 12:16:27 +01:00
export function patch(url: string, data: any, config?: ApiRequestConfig)
2021-03-24 20:26:55 +01:00
{
2021-12-14 12:16:27 +01:00
return send({ method: 'patch', url, data, ...config });
2021-03-24 20:26:55 +01:00
}
2021-12-14 11:54:36 +01:00
export async function send(config: ApiRequestConfig)
2021-03-24 20:26:55 +01:00
{
2021-12-15 15:34:05 +01:00
if (!config.raw)
{
config.baseURL = paths.api;
}
if (config.system)
{
config.params['zero.system'] = true;
}
2021-12-09 14:18:38 +01:00
const result = await axios(config);
return result.data;
2021-03-24 20:26:55 +01:00
2021-12-09 14:18:38 +01:00
//try
//{
// const result = await axios(config);
// return result.data;
//}
//catch (err)
//{
// console.error('axios err: ' + err);
// // TODO handle errors
//}
2021-03-24 20:26:55 +01:00
}
2021-12-07 15:59:29 +01:00
//export function collection(base)
//{
// return {
// getById: async (id, changeVector, config) => await get(base + 'getById', { ...config, params: { id, changeVector } }),
// getByIds: async (ids, config) => await get(base + 'getByIds', { ...config, params: { ids } }),
// getEmpty: async config => await get(base + 'getEmpty', { ...config }),
// getByQuery: async (query, config) => await get(base + 'getByQuery', { ...config, params: { query } }),
// getAll: async (config) => await get(base + 'getAll', { ...config }),
// getPreviews: async (ids, config) => await get(base + 'getPreviews', { ...config, params: { ids } }),
// getForPicker: async (config) => await get(base + 'getForPicker', { ...config }),
// getRevisions: async (id, query, config) => await get(base + 'getRevisions', { ...config, params: { id, query } }),
// save: async (model, config) => await post(base + 'save', model, { ...config }),
// delete: async (id, config) => await del(base + 'delete', { ...config, params: { id } })
// };
//}
2021-03-24 20:26:55 +01:00
2021-12-07 15:59:29 +01:00
//export function download(response)
//{
// let filename = response.headers["zero-filename"] || 'download.bin';
2021-03-24 20:26:55 +01:00
2021-12-07 15:59:29 +01:00
// // code from: https://github.com/kennethjiang/js-file-download/blob/master/file-download.js
2021-03-24 20:26:55 +01:00
2021-12-07 15:59:29 +01:00
// var blob = response.data;
// if (typeof window.navigator.msSaveBlob !== 'undefined')
// {
// // IE workaround for "HTML7007: One or more blob URLs were
// // revoked by closing the blob for which they were created.
// // These URLs will no longer resolve as the data backing
// // the URL has been freed."
// window.navigator.msSaveBlob(blob, filename);
// }
// else
// {
// var blobURL = (window.URL && window.URL.createObjectURL) ? window.URL.createObjectURL(blob) : window.webkitURL.createObjectURL(blob);
// var tempLink = document.createElement('a');
// tempLink.style.display = 'none';
// tempLink.href = blobURL;
// tempLink.setAttribute('download', filename);
2021-03-24 20:26:55 +01:00
2021-12-07 15:59:29 +01:00
// // Safari thinks _blank anchor are pop ups. We only want to set _blank
// // target if the browser does not support the HTML5 download attribute.
// // This allows you to download files in desktop safari if pop up blocking
// // is enabled.
// if (typeof tempLink.download === 'undefined')
// {
// tempLink.setAttribute('target', '_blank');
// }
2021-03-24 20:26:55 +01:00
2021-12-07 15:59:29 +01:00
// document.body.appendChild(tempLink);
// tempLink.click();
2021-03-24 20:26:55 +01:00
2021-12-07 15:59:29 +01:00
// // Fixes "webkit blob resource error 1"
// setTimeout(function ()
// {
// document.body.removeChild(tempLink);
// window.URL.revokeObjectURL(blobURL);
// }, 200)
// }
//};