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");
// 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
// limitations under the License.
import {ExclamationCircleFilled} from "@ant-design/icons";
import {Modal} from "antd";
import {ExclamationCircleFilled} from "@ant-design/icons";
import i18next from "i18next";
import * as Conf from "../Conf";
import * as Setting from "../Setting";
const {confirm} = Modal;
const {fetch: originalFetch} = window;
/**
* When modify data, prompt it's read-only and ask whether to go writable site
*/
const demoModePrompt = async(url, option) => {
const demoModeCallback = async(url, option) => {
if (option.method === "POST") {
confirm({
title: i18next.t("general:This is a read-only demo site!"),
icon: <ExclamationCircleFilled />,
content: i18next.t("general:Go Writable demo site?"),
content: i18next.t("general:Go to writable demo site?"),
okText: i18next.t("user:OK"),
cancelText: i18next.t("general:Cancel"),
onOk() {
const fullURL = document.location.toString();
window.open("https://demo.casdoor.com" + fullURL.substring(fullURL.lastIndexOf("/")) + "?username=built-in/admin&password=123", "_blank");
Setting.openLink(`https://demo.casdoor.com${location.path}${location.search}?username=built-in/admin&password=123`);
},
onCancel() {},
});
}
return option;
};
const requsetInterceptors = [];
const responseInterceptors = [];
const requestFilters = [];
const responseFilters = [];
// when it's in DemoMode, demoModePrompt() should run before fetch
if (Conf.IsDemoMode) {
requsetInterceptors.push(demoModePrompt);
requestFilters.push(demoModeCallback);
}
/**
* rewrite fetch to support interceptors
*/
window.fetch = async(url, option = {}) => {
for (const fn of requsetInterceptors) {
fn(url, option);
}
requestFilters.forEach(filter => filter(url, option));
const response = await originalFetch(url, option);
responseInterceptors.forEach(fn => (response) => fn(response));
responseFilters.forEach(filter => (response) => filter(response));
return response;
};

View File

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

View File

@ -181,7 +181,7 @@
"First name": "First name",
"Forget URL": "URL vergessen",
"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 - Tooltip": "Application homepage",
"ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name",
"Forget URL": "Forget URL",
"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 - Tooltip": "Home - Tooltip",
"ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "Nombre",
"Forget URL": "URL de olvido",
"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 - Tooltip": "Inicio - Tooltip",
"ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name",
"Forget URL": "Oublier l'URL",
"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 - Tooltip": "Application homepage",
"ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name",
"Forget URL": "URLを忘れた",
"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 - Tooltip": "Application homepage",
"ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "First name",
"Forget URL": "Forget URL",
"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",
"ID": "ID",

View File

@ -181,7 +181,7 @@
"First name": "Имя",
"Forget URL": "Забыть URL",
"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 - Tooltip": "Application homepage",
"ID": "ID",

View File

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