Files
mixtape/zero.Web.UI/app/helpers/request.ts
T
2021-03-24 20:26:55 +01:00

114 lines
3.3 KiB
TypeScript

import axios from 'axios';
const getConfig = config =>
{
config = config || {};
if (config.scope)
{
config.params = config.params || {};
config.params.scope = config.scope;
}
return config;
};
export async function get(url, config: any = null)
{
return await send({ method: 'get', url, ...config });
}
export async function post(url, data: any = null, config: any = null)
{
return await send({ method: 'post', url, data, ...config });
}
export async function del(url, config: any = null)
{
return await send({ method: 'delete', url, ...config });
}
export async function put(url, data: any = null, config: any = null)
{
return await send({ method: 'put', url, data, ...config });
}
export async function patch(url, data: any = null, config: any = null)
{
return await send({ method: 'patch', url, data, ...config });
}
export async function send(config)
{
config = getConfig(config);
try
{
const result = await axios(config);
return result.data;
}
catch (err)
{
// TODO handle errors
}
}
export function collection(base)
{
return {
getById: async (id, config) => await get(base + 'getById', { ...config, params: { id } }),
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 }),
save: async (model, config) => await post(base + 'save', model, { ...config }),
delete: async (id, config) => await del(base + 'delete', { ...config, params: { id } })
};
}
export function download(response)
{
let filename = response.headers["zero-filename"] || 'download.bin';
// code from: https://github.com/kennethjiang/js-file-download/blob/master/file-download.js
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);
// 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');
}
document.body.appendChild(tempLink);
tempLink.click();
// Fixes "webkit blob resource error 1"
setTimeout(function ()
{
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}, 200)
}
};