Refactor FetchFilter.js code

This commit is contained in:
Yang Luo 2023-02-16 22:50:08 +08:00
parent 14fa914e6f
commit 4605938f8e
10 changed files with 21 additions and 29 deletions

View File

@ -1,4 +1,4 @@
// Copyright 2022 The Casdoor Authors. All Rights Reserved. // Copyright 2023 The Casdoor Authors. All Rights Reserved.
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -12,52 +12,44 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import {ExclamationCircleFilled} from "@ant-design/icons";
import {Modal} from "antd"; import {Modal} from "antd";
import {ExclamationCircleFilled} from "@ant-design/icons";
import i18next from "i18next"; import i18next from "i18next";
import * as Conf from "../Conf"; import * as Conf from "../Conf";
import * as Setting from "../Setting";
const {confirm} = Modal; const {confirm} = Modal;
const {fetch: originalFetch} = window; const {fetch: originalFetch} = window;
/** const demoModeCallback = async(url, option) => {
* When modify data, prompt it's read-only and ask whether to go writable site
*/
const demoModePrompt = async(url, option) => {
if (option.method === "POST") { if (option.method === "POST") {
confirm({ confirm({
title: i18next.t("general:This is a read-only demo site!"), title: i18next.t("general:This is a read-only demo site!"),
icon: <ExclamationCircleFilled />, icon: <ExclamationCircleFilled />,
content: i18next.t("general:Go Writable demo site?"), content: i18next.t("general:Go to writable demo site?"),
okText: i18next.t("user:OK"), okText: i18next.t("user:OK"),
cancelText: i18next.t("general:Cancel"), cancelText: i18next.t("general:Cancel"),
onOk() { onOk() {
const fullURL = document.location.toString(); Setting.openLink(`https://demo.casdoor.com${location.path}${location.search}?username=built-in/admin&password=123`);
window.open("https://demo.casdoor.com" + fullURL.substring(fullURL.lastIndexOf("/")) + "?username=built-in/admin&password=123", "_blank");
}, },
onCancel() {}, onCancel() {},
}); });
} }
return option; return option;
}; };
const requsetInterceptors = []; const requestFilters = [];
const responseInterceptors = []; const responseFilters = [];
// when it's in DemoMode, demoModePrompt() should run before fetch
if (Conf.IsDemoMode) { if (Conf.IsDemoMode) {
requsetInterceptors.push(demoModePrompt); requestFilters.push(demoModeCallback);
} }
/**
* rewrite fetch to support interceptors
*/
window.fetch = async(url, option = {}) => { window.fetch = async(url, option = {}) => {
for (const fn of requsetInterceptors) { requestFilters.forEach(filter => filter(url, option));
fn(url, option);
}
const response = await originalFetch(url, option); const response = await originalFetch(url, option);
responseInterceptors.forEach(fn => (response) => fn(response)); responseFilters.forEach(filter => (response) => filter(response));
return response; return response;
}; };

View File

@ -22,7 +22,7 @@ import "./App.less";
import App from "./App"; import App from "./App";
import * as serviceWorker from "./serviceWorker"; import * as serviceWorker from "./serviceWorker";
import {BrowserRouter} from "react-router-dom"; import {BrowserRouter} from "react-router-dom";
import "./backend/FetchInterceptor"; import "./backend/FetchFilter";
const container = document.getElementById("root"); const container = document.getElementById("root");

View File

@ -181,7 +181,7 @@
"First name": "First name", "First name": "First name",
"Forget URL": "URL vergessen", "Forget URL": "URL vergessen",
"Forget URL - Tooltip": "Unique string-style identifier", "Forget URL - Tooltip": "Unique string-style identifier",
"Go Writable demo site?": "Go Writable demo site?", "Go to writable demo site?": "Go to writable demo site?",
"Home": "Zuhause", "Home": "Zuhause",
"Home - Tooltip": "Application homepage", "Home - Tooltip": "Application homepage",
"ID": "ID", "ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name", "First name": "First name",
"Forget URL": "Forget URL", "Forget URL": "Forget URL",
"Forget URL - Tooltip": "Forget URL - Tooltip", "Forget URL - Tooltip": "Forget URL - Tooltip",
"Go Writable demo site?": "Go Writable demo site?", "Go to writable demo site?": "Go to writable demo site?",
"Home": "Home", "Home": "Home",
"Home - Tooltip": "Home - Tooltip", "Home - Tooltip": "Home - Tooltip",
"ID": "ID", "ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "Nombre", "First name": "Nombre",
"Forget URL": "URL de olvido", "Forget URL": "URL de olvido",
"Forget URL - Tooltip": "URL de olvido - Tooltip", "Forget URL - Tooltip": "URL de olvido - Tooltip",
"Go Writable demo site?": "Go Writable demo site?", "Go to writable demo site?": "Go to writable demo site?",
"Home": "Inicio", "Home": "Inicio",
"Home - Tooltip": "Inicio - Tooltip", "Home - Tooltip": "Inicio - Tooltip",
"ID": "ID", "ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name", "First name": "First name",
"Forget URL": "Oublier l'URL", "Forget URL": "Oublier l'URL",
"Forget URL - Tooltip": "Unique string-style identifier", "Forget URL - Tooltip": "Unique string-style identifier",
"Go Writable demo site?": "Go Writable demo site?", "Go to writable demo site?": "Go to writable demo site?",
"Home": "Domicile", "Home": "Domicile",
"Home - Tooltip": "Application homepage", "Home - Tooltip": "Application homepage",
"ID": "ID", "ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name", "First name": "First name",
"Forget URL": "URLを忘れた", "Forget URL": "URLを忘れた",
"Forget URL - Tooltip": "Unique string-style identifier", "Forget URL - Tooltip": "Unique string-style identifier",
"Go Writable demo site?": "Go Writable demo site?", "Go to writable demo site?": "Go to writable demo site?",
"Home": "ホーム", "Home": "ホーム",
"Home - Tooltip": "Application homepage", "Home - Tooltip": "Application homepage",
"ID": "ID", "ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name", "First name": "First name",
"Forget URL": "Forget URL", "Forget URL": "Forget URL",
"Forget URL - Tooltip": "Unique string-style identifier", "Forget URL - Tooltip": "Unique string-style identifier",
"Go Writable demo site?": "Go Writable demo site?", "Go to writable demo site?": "Go to writable demo site?",
"Home": "Home", "Home": "Home",
"Home - Tooltip": "Application homepage", "Home - Tooltip": "Application homepage",
"ID": "ID", "ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "Имя", "First name": "Имя",
"Forget URL": "Забыть URL", "Forget URL": "Забыть URL",
"Forget URL - Tooltip": "Unique string-style identifier", "Forget URL - Tooltip": "Unique string-style identifier",
"Go Writable demo site?": "Go Writable demo site?", "Go to writable demo site?": "Go to writable demo site?",
"Home": "Домашний", "Home": "Домашний",
"Home - Tooltip": "Application homepage", "Home - Tooltip": "Application homepage",
"ID": "ID", "ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "名字", "First name": "名字",
"Forget URL": "忘记密码URL", "Forget URL": "忘记密码URL",
"Forget URL - Tooltip": "忘记密码URL", "Forget URL - Tooltip": "忘记密码URL",
"Go Writable demo site?": "跳转至可写演示站点?", "Go to writable demo site?": "跳转至可写演示站点?",
"Home": "首页", "Home": "首页",
"Home - Tooltip": "应用的首页", "Home - Tooltip": "应用的首页",
"ID": "ID", "ID": "ID",