|
| 1 | +// Copyright 2023 The casbin Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import {Modal} from "antd"; |
| 16 | +import {ExclamationCircleFilled} from "@ant-design/icons"; |
| 17 | +import i18next from "i18next"; |
| 18 | +import * as Conf from "../Conf"; |
| 19 | +import * as Setting from "../Setting"; |
| 20 | + |
| 21 | +const {confirm} = Modal; |
| 22 | +const {fetch: originalFetch} = window; |
| 23 | + |
| 24 | +const demoModeCallback = (res) => { |
| 25 | + res.json().then(data => { |
| 26 | + if (Setting.isResponseDenied(data)) { |
| 27 | + confirm({ |
| 28 | + title: i18next.t("general:This is a read-only demo site!"), |
| 29 | + icon: <ExclamationCircleFilled />, |
| 30 | + content: i18next.t("general:Go to writable demo site?"), |
| 31 | + okText: i18next.t("general:OK"), |
| 32 | + cancelText: i18next.t("general:Cancel"), |
| 33 | + onOk() { |
| 34 | + Setting.openLink(`https://demo.ai.casbin.com/${location.pathname}${location.search}?username=built-in/admin&password=123`); |
| 35 | + }, |
| 36 | + onCancel() {}, |
| 37 | + }); |
| 38 | + } |
| 39 | + }); |
| 40 | +}; |
| 41 | + |
| 42 | +const requestFilters = []; |
| 43 | +const responseFilters = []; |
| 44 | + |
| 45 | +if (Conf.IsDemoMode) { |
| 46 | + responseFilters.push(demoModeCallback); |
| 47 | +} |
| 48 | + |
| 49 | +window.fetch = async(url, option = {}) => { |
| 50 | + requestFilters.forEach(filter => filter(url, option)); |
| 51 | + return new Promise((resolve, reject) => { |
| 52 | + originalFetch(url, option).then(res => { |
| 53 | + // eslint-disable-next-line no-console |
| 54 | + console.log(res.clone()); |
| 55 | + responseFilters.forEach(filter => filter(res.clone())); |
| 56 | + resolve(res); |
| 57 | + }); |
| 58 | + }); |
| 59 | +}; |
0 commit comments