mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-05 05:50:19 +08:00
Refactor FetchFilter.js code
This commit is contained in:
@ -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;
|
||||
};
|
Reference in New Issue
Block a user