mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: init routes protection
This commit is contained in:
parent
49b6d1dfe7
commit
1ad8ee12b1
25 changed files with 82 additions and 64 deletions
|
|
@ -1,53 +0,0 @@
|
|||
export const RequestBody = (method: string, data: any) => {
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
if (data) {
|
||||
options.body = JSON.stringify(data);
|
||||
}
|
||||
return options;
|
||||
};
|
||||
|
||||
export const RequestBodyForm = (method: string, data: any) => {
|
||||
let HeadersConfig = new Headers({});
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
body: data,
|
||||
};
|
||||
return options;
|
||||
};
|
||||
|
||||
export const swrFetcher = async (url: string, body: any) => {
|
||||
// Create the request options
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let options: any = {
|
||||
method: "GET",
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
|
||||
// If there is a body, add it to the request options
|
||||
if (body) {
|
||||
options.body = JSON.stringify(body);
|
||||
}
|
||||
|
||||
// Fetch the data
|
||||
const res = await fetch(url, options);
|
||||
|
||||
// If the response is not in the 200 range, throw an error
|
||||
if (!res.ok) {
|
||||
const error = new Error("An error occurred while fetching the data.");
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Return the data
|
||||
return res.json();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue