mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-19 04:19:25 +00:00
feat: format with prettier
This commit is contained in:
parent
03fb09c3d6
commit
a147ad6610
164 changed files with 11257 additions and 8154 deletions
|
|
@ -1,12 +1,12 @@
|
|||
import { AppRouterInstance } from "next/dist/shared/lib/app-router-context.shared-runtime";
|
||||
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'
|
||||
|
||||
export const denyAccessToUser = (error: any, router: AppRouterInstance) => {
|
||||
if (error.status === 401) {
|
||||
router.push("/login");
|
||||
router.push('/login')
|
||||
}
|
||||
|
||||
if (error.status === 403) {
|
||||
router.push("/login");
|
||||
router.push('/login')
|
||||
// TODO : add a message to the user to tell him he is not allowed to access this page, route to /error
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,100 +1,120 @@
|
|||
import { getUriWithOrg } from "@services/config/config";
|
||||
import { getUriWithOrg } from '@services/config/config'
|
||||
|
||||
export const RequestBody = (method: string, data: any, next: any) => {
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let HeadersConfig = new Headers({ 'Content-Type': 'application/json' })
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
redirect: 'follow',
|
||||
credentials: 'include',
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
if (data) {
|
||||
options.body = JSON.stringify(data);
|
||||
}
|
||||
return options;
|
||||
};
|
||||
if (data) {
|
||||
options.body = JSON.stringify(data)
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
export const RequestBodyWithAuthHeader = (method: string, data: any, next: any, token: string) => {
|
||||
let HeadersConfig = new Headers(token ? { "Content-Type": "application/json", Authorization: `Bearer ${token}` } : { "Content-Type": "application/json" });
|
||||
export const RequestBodyWithAuthHeader = (
|
||||
method: string,
|
||||
data: any,
|
||||
next: any,
|
||||
token: string
|
||||
) => {
|
||||
let HeadersConfig = new Headers(
|
||||
token
|
||||
? { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }
|
||||
: { 'Content-Type': 'application/json' }
|
||||
)
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
redirect: 'follow',
|
||||
credentials: 'include',
|
||||
body: data,
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
return options;
|
||||
};
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
export const RequestBodyForm = (method: string, data: any, next: any) => {
|
||||
let HeadersConfig = new Headers({});
|
||||
let HeadersConfig = new Headers({})
|
||||
let options: any = {
|
||||
method: method,
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
redirect: 'follow',
|
||||
credentials: 'include',
|
||||
body: data,
|
||||
// Next.js
|
||||
next: next,
|
||||
};
|
||||
return options;
|
||||
};
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
export const swrFetcher = async (url: string) => {
|
||||
// Create the request options
|
||||
let HeadersConfig = new Headers({ "Content-Type": "application/json" });
|
||||
let HeadersConfig = new Headers({ 'Content-Type': 'application/json' })
|
||||
let options: any = {
|
||||
method: "GET",
|
||||
method: 'GET',
|
||||
headers: HeadersConfig,
|
||||
redirect: "follow",
|
||||
credentials: "include",
|
||||
};
|
||||
redirect: 'follow',
|
||||
credentials: 'include',
|
||||
}
|
||||
|
||||
try {
|
||||
// Fetch the data
|
||||
const request = await fetch(url, options);
|
||||
let res = errorHandling(request);
|
||||
const request = await fetch(url, options)
|
||||
let res = errorHandling(request)
|
||||
|
||||
// Return the data
|
||||
return res;
|
||||
return res
|
||||
} catch (error: any) {
|
||||
throw error;
|
||||
throw error
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const errorHandling = (res: any) => {
|
||||
if (!res.ok) {
|
||||
const error: any = new Error(`${res.statusText}`);
|
||||
error.status = res.status;
|
||||
throw error;
|
||||
const error: any = new Error(`${res.statusText}`)
|
||||
error.status = res.status
|
||||
throw error
|
||||
}
|
||||
return res.json();
|
||||
};
|
||||
|
||||
return res.json()
|
||||
}
|
||||
|
||||
type CustomResponseTyping = {
|
||||
success: boolean;
|
||||
data: any;
|
||||
status: number;
|
||||
HTTPmessage: string;
|
||||
};
|
||||
success: boolean
|
||||
data: any
|
||||
status: number
|
||||
HTTPmessage: string
|
||||
}
|
||||
|
||||
export const getResponseMetadata = async (fetch_result: any): Promise<CustomResponseTyping> => {
|
||||
const json = await fetch_result.json();
|
||||
export const getResponseMetadata = async (
|
||||
fetch_result: any
|
||||
): Promise<CustomResponseTyping> => {
|
||||
const json = await fetch_result.json()
|
||||
if (fetch_result.status === 200) {
|
||||
return { success: true, data: json, status: fetch_result.status, HTTPmessage: fetch_result.statusText };
|
||||
return {
|
||||
success: true,
|
||||
data: json,
|
||||
status: fetch_result.status,
|
||||
HTTPmessage: fetch_result.statusText,
|
||||
}
|
||||
} else {
|
||||
return { success: false, data: json, status: fetch_result.status, HTTPmessage: fetch_result.statusText };
|
||||
return {
|
||||
success: false,
|
||||
data: json,
|
||||
status: fetch_result.status,
|
||||
HTTPmessage: fetch_result.statusText,
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export const revalidateTags = async (tags: string[], orgslug: string) => {
|
||||
const url = getUriWithOrg(orgslug, "");
|
||||
const url = getUriWithOrg(orgslug, '')
|
||||
tags.forEach((tag) => {
|
||||
fetch(`${url}/api/revalidate?tag=${tag}`);
|
||||
});
|
||||
};
|
||||
fetch(`${url}/api/revalidate?tag=${tag}`)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue