Skip to content

Commit b5aec6b

Browse files
committed
implemented API interception
1 parent 10b8a91 commit b5aec6b

6 files changed

Lines changed: 53 additions & 9 deletions

File tree

package-lock.json

Lines changed: 17 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "traccar",
2+
"name": "@tang/traccar",
33
"version": "6.5.0",
44
"type": "module",
55
"private": true,
@@ -21,6 +21,7 @@
2121
"@vitejs/plugin-react": "^4.3.1",
2222
"dayjs": "^1.11.13",
2323
"events": "^3.3.0",
24+
"fetch-intercept": "^2.4.0",
2425
"mapbox-gl": "^1.13.3",
2526
"maplibre-gl": "^4.7.0",
2627
"maplibre-google-maps": "^1.1.0",
@@ -40,11 +41,15 @@
4041
"vite": "^5.4.6",
4142
"vite-plugin-pwa": "^0.20.5",
4243
"vite-plugin-svgr": "^4.2.0",
43-
"wellknown": "^0.5.0"
44+
"wellknown": "^0.5.0",
45+
"whatwg-fetch": "^3.6.20"
4446
},
4547
"scripts": {
4648
"start": "vite",
4749
"build": "vite build",
50+
"start:custom": "VITE_SERVER_PROTOCOL=https: VITE_SERVER_URL=demo.traccar.org vite",
51+
"dev": "npm run start:custom",
52+
"build:app": "npm run start:custom build",
4853
"generate-pwa-assets": "pwa-assets-generator --preset minimal public/logo.svg",
4954
"lint": "eslint .",
5055
"lint:fix": "eslint --fix ."

src/SocketController.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ const SocketController = () => {
3333
const features = useFeatures();
3434

3535
const connectSocket = () => {
36-
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
37-
const socket = new WebSocket(`${protocol}//${window.location.host}/api/socket`);
36+
const protocol = import.meta.env.VITE_SERVER_PROTOCOL ?? window.location.protocol === 'https:' ? 'wss:' : 'ws:';
37+
const host = import.meta.env.VITE_SERVER_URL ?? window.location.host;
38+
const socket = new WebSocket(`${protocol}//${host}/api/socket`);
3839
socketRef.current = socket;
3940

4041
socket.onopen = () => {

src/index.jsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,29 @@ import NativeInterface from './common/components/NativeInterface';
1212
import ServerProvider from './ServerProvider';
1313
import ErrorBoundary from './ErrorBoundary';
1414
import AppThemeProvider from './AppThemeProvider';
15+
import fetchIntercept from 'fetch-intercept';
1516

1617
preloadImages();
1718

19+
/** Setup API inteception */
20+
fetchIntercept.register({
21+
request: function (url, config) {
22+
const serverUrl = import.meta.env.VITE_SERVER_URL;
23+
const apiUrl = url.length > 0 ? (url.startsWith('/api') ? url : null) : null;
24+
if (serverUrl && apiUrl) {
25+
const serverProtocol = import.meta.env.VITE_SERVER_PROTOCOL ?? window.location.protocol;
26+
url = `${serverProtocol}//${serverUrl}${url}`
27+
config = {
28+
...config,
29+
credentials: 'include',
30+
mode: 'cors',
31+
}
32+
}
33+
34+
return [url, config];
35+
},
36+
});
37+
1838
const root = createRoot(document.getElementById('root'));
1939
root.render(
2040
<ErrorBoundary>

src/login/ChangeServerPage.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
import { useNavigate } from 'react-router-dom';
88
import { useTranslation } from '../common/components/LocalizationProvider';
99

10-
const currentServer = `${window.location.protocol}//${window.location.host}`;
10+
const protocol = import.meta.env.VITE_SERVER_PROTOCOL ?? window.location.protocol
11+
const serverUrl = import.meta.env.VITE_SERVER_URL ?? window.location.host;
12+
const currentServer = `${protocol}//${serverUrl}`;
1113

1214
const officialServers = [
1315
currentServer,

src/other/EmulatorPage.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ const EmulatorPage = () => {
8585
params.append('id', devices[deviceId].uniqueId);
8686
params.append('lat', latitude);
8787
params.append('lon', longitude);
88-
response = await fetch(`http://${window.location.hostname}:5055?${params.toString()}`, {
88+
const protocol = import.meta.env.VITE_SERVER_PROTOCOL ?? window.location.protocol
89+
const serverUrl = import.meta.env.VITE_SERVER_URL ?? window.location.host;
90+
response = await fetch(`${protocol}//${serverUrl}:5055?${params.toString()}`, {
8991
method: 'POST',
9092
mode: 'no-cors',
9193
});

0 commit comments

Comments
 (0)