78 lines
2.1 KiB
JavaScript
78 lines
2.1 KiB
JavaScript
async function AddCollection(catalog, data, expire = "1 year")
|
|
{
|
|
if(typeof data != "object") throw new Error("data must be object");
|
|
if(typeof catalog != "string") throw new Error("catalog must be string");
|
|
|
|
let pack = {
|
|
data: data,
|
|
domain: window.location.host,
|
|
expire: expire
|
|
};
|
|
|
|
let serializedData = JSON.stringify(pack);
|
|
|
|
let url = new URL(`/collection/${catalog}/add`,window.location);
|
|
|
|
let request = await fetch(url,{
|
|
method: "post",
|
|
headers: {
|
|
'accept': "text/json, application/json",
|
|
'content-type': 'application/json'
|
|
},
|
|
body: serializedData,
|
|
cache: "no-cache",
|
|
priority: "low",
|
|
referrerPolicy: "strict-origin",
|
|
redirect: "error"
|
|
});
|
|
|
|
if(request.ok == false)
|
|
{
|
|
throw await request.json()
|
|
}
|
|
|
|
let response = await request.json();
|
|
|
|
if(response.status != "success")
|
|
{
|
|
throw new Error("Bir sorun oluştu ve veri eklenemedi")
|
|
}
|
|
}
|
|
|
|
async function ReadCollection(catalog, order = "asc", length = 100, page = 1)
|
|
{
|
|
if(typeof catalog != "string") throw new Error("catalog must be object");
|
|
if(typeof order != "string") throw new Error("order must be string");
|
|
if(typeof length != "number") throw new Error("length must be object");
|
|
if(typeof page != "number") throw new Error("page must be object");
|
|
|
|
let serializedData = JSON.stringify({
|
|
"order": order,
|
|
"length": length,
|
|
"page": page
|
|
});
|
|
|
|
let url = new URL(`/collection/${catalog}/data.json`,window.location);
|
|
|
|
let request = await fetch(url,{
|
|
method: "post",
|
|
headers: {
|
|
'accept': "text/json, application/json",
|
|
'content-type': 'application/json'
|
|
},
|
|
body: serializedData,
|
|
cache: "no-cache",
|
|
priority: "low",
|
|
referrerPolicy: "strict-origin",
|
|
redirect: "error"
|
|
});
|
|
|
|
if(request.ok == false || request.status != 200)
|
|
{
|
|
throw await request.json()
|
|
}
|
|
|
|
let response = await request.json();
|
|
|
|
return response;
|
|
} |