@chaosity/location-client-react 0.1.3 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
2
|
import { GeoPlacesClient, ClientConfig } from '@chaosity/location-client';
|
|
3
3
|
interface LocationClientContextValue {
|
|
4
4
|
config: ClientConfig | null;
|
|
5
5
|
client: GeoPlacesClient | null;
|
|
6
|
+
getToken: () => string | undefined;
|
|
7
|
+
ensureValidToken: () => Promise<void>;
|
|
6
8
|
loading: boolean;
|
|
7
9
|
error: string | null;
|
|
8
10
|
}
|
|
9
11
|
export interface LocationClientProviderProps {
|
|
10
|
-
children:
|
|
11
|
-
getConfig: () => Promise<ClientConfig
|
|
12
|
+
children: ReactNode;
|
|
13
|
+
getConfig: () => Promise<ClientConfig & {
|
|
14
|
+
expiresAt?: number;
|
|
15
|
+
}>;
|
|
16
|
+
refreshBuffer?: number;
|
|
12
17
|
}
|
|
13
|
-
export declare function LocationClientProvider({ children, getConfig }: LocationClientProviderProps):
|
|
18
|
+
export declare function LocationClientProvider({ children, getConfig, refreshBuffer }: LocationClientProviderProps): import("react/jsx-runtime").JSX.Element;
|
|
14
19
|
export declare function useLocationClient(): LocationClientContextValue;
|
|
15
20
|
export {};
|
|
@@ -1,24 +1,112 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { createContext, useContext, useEffect, useState, useRef, useCallback } from 'react';
|
|
2
4
|
import { GeoPlacesClient } from '@chaosity/location-client';
|
|
5
|
+
import debug from 'debug';
|
|
6
|
+
import { jwtDecode } from 'jwt-decode';
|
|
7
|
+
const log = debug('location-client-react:provider');
|
|
3
8
|
const LocationClientContext = createContext(undefined);
|
|
4
|
-
export function LocationClientProvider({ children, getConfig }) {
|
|
9
|
+
export function LocationClientProvider({ children, getConfig, refreshBuffer = 60 }) {
|
|
5
10
|
const [config, setConfig] = useState(null);
|
|
6
11
|
const [client, setClient] = useState(null);
|
|
7
12
|
const [loading, setLoading] = useState(true);
|
|
8
13
|
const [error, setError] = useState(null);
|
|
14
|
+
const tokenRef = useRef(undefined);
|
|
15
|
+
const expiresAtRef = useRef(null);
|
|
16
|
+
const getConfigRef = useRef(getConfig);
|
|
17
|
+
const tokenGenerationCount = useRef(0);
|
|
18
|
+
// Update ref when getConfig changes
|
|
9
19
|
useEffect(() => {
|
|
20
|
+
getConfigRef.current = getConfig;
|
|
21
|
+
}, [getConfig]);
|
|
22
|
+
// Token getter function (stable reference)
|
|
23
|
+
const getToken = useCallback(() => tokenRef.current, []);
|
|
24
|
+
// Check if token is expired or about to expire
|
|
25
|
+
const isTokenExpired = useCallback(() => {
|
|
26
|
+
if (!expiresAtRef.current)
|
|
27
|
+
return false;
|
|
28
|
+
return Date.now() >= (expiresAtRef.current - refreshBuffer * 1000);
|
|
29
|
+
}, [refreshBuffer]);
|
|
30
|
+
// Refresh token if needed
|
|
31
|
+
const ensureValidToken = useCallback(async () => {
|
|
32
|
+
if (!isTokenExpired())
|
|
33
|
+
return;
|
|
34
|
+
const timeUntilExpiry = expiresAtRef.current ? Math.floor((expiresAtRef.current - Date.now()) / 1000) : 0;
|
|
35
|
+
log('Token expired or expiring soon (expires in %ds), refreshing...', timeUntilExpiry);
|
|
36
|
+
tokenGenerationCount.current++;
|
|
37
|
+
try {
|
|
38
|
+
const cfg = await getConfigRef.current();
|
|
39
|
+
setConfig(cfg);
|
|
40
|
+
tokenRef.current = cfg.token;
|
|
41
|
+
expiresAtRef.current = null;
|
|
42
|
+
const baseClient = new GeoPlacesClient(cfg);
|
|
43
|
+
const wrappedClient = new Proxy(baseClient, {
|
|
44
|
+
get(target, prop) {
|
|
45
|
+
if (prop === 'send') {
|
|
46
|
+
return async (command) => {
|
|
47
|
+
await ensureValidToken();
|
|
48
|
+
return target.send(command);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return target[prop];
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
setClient(wrappedClient);
|
|
55
|
+
const decoded = jwtDecode(cfg.token);
|
|
56
|
+
expiresAtRef.current = decoded.exp * 1000;
|
|
57
|
+
const newExpiry = Math.floor((decoded.exp * 1000 - Date.now()) / 1000);
|
|
58
|
+
log('Token refreshed successfully (new token expires in %ds)', newExpiry);
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
log('Token refresh failed: %s', err instanceof Error ? err.message : 'Unknown error');
|
|
62
|
+
setError(err instanceof Error ? err.message : 'Failed to refresh token');
|
|
63
|
+
}
|
|
64
|
+
}, [isTokenExpired]);
|
|
65
|
+
// Initial load
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
log('Initializing LocationClientProvider');
|
|
68
|
+
tokenGenerationCount.current++;
|
|
10
69
|
getConfig()
|
|
11
70
|
.then((cfg) => {
|
|
12
71
|
setConfig(cfg);
|
|
13
|
-
|
|
72
|
+
tokenRef.current = cfg.token;
|
|
73
|
+
const baseClient = new GeoPlacesClient(cfg);
|
|
74
|
+
const wrappedClient = new Proxy(baseClient, {
|
|
75
|
+
get(target, prop) {
|
|
76
|
+
if (prop === 'send') {
|
|
77
|
+
return async (command) => {
|
|
78
|
+
await ensureValidToken();
|
|
79
|
+
return target.send(command);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return target[prop];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
setClient(wrappedClient);
|
|
86
|
+
try {
|
|
87
|
+
const decoded = jwtDecode(cfg.token);
|
|
88
|
+
expiresAtRef.current = decoded.exp * 1000;
|
|
89
|
+
const expiry = Math.floor((decoded.exp * 1000 - Date.now()) / 1000);
|
|
90
|
+
log('Client initialized (token expires in %ds)', expiry);
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
log('Failed to decode token, no expiry info');
|
|
94
|
+
}
|
|
14
95
|
setLoading(false);
|
|
15
96
|
})
|
|
16
97
|
.catch((err) => {
|
|
98
|
+
log('Initialization failed: %s', err instanceof Error ? err.message : 'Unknown error');
|
|
17
99
|
setError(err instanceof Error ? err.message : 'Failed to initialize client');
|
|
18
100
|
setLoading(false);
|
|
19
101
|
});
|
|
20
|
-
}, []);
|
|
21
|
-
|
|
102
|
+
}, [ensureValidToken]);
|
|
103
|
+
// Check token validity on every state change
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (!loading && config) {
|
|
106
|
+
ensureValidToken();
|
|
107
|
+
}
|
|
108
|
+
}, [config, loading, ensureValidToken]);
|
|
109
|
+
return (_jsx(LocationClientContext.Provider, { value: { config, client, getToken, ensureValidToken, loading, error }, children: children }));
|
|
22
110
|
}
|
|
23
111
|
export function useLocationClient() {
|
|
24
112
|
const context = useContext(LocationClientContext);
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chaosity/location-client-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"description": "React bindings for Chaosity Location Service client",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "dist/index.js",
|
|
6
7
|
"types": "dist/index.d.ts",
|
|
7
8
|
"scripts": {
|
|
@@ -27,15 +28,18 @@
|
|
|
27
28
|
"url": "https://github.com/chaosity-io/location-service-client-react/issues"
|
|
28
29
|
},
|
|
29
30
|
"dependencies": {
|
|
30
|
-
"@chaosity/location-client": "^0.1.
|
|
31
|
+
"@chaosity/location-client": "^0.1.5",
|
|
32
|
+
"debug": "^4.4.3",
|
|
33
|
+
"jwt-decode": "^4.0.0"
|
|
31
34
|
},
|
|
32
35
|
"peerDependencies": {
|
|
33
36
|
"react": "^18.0.0 || ^19.0.0"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
36
|
-
"@types/
|
|
37
|
-
"
|
|
38
|
-
"react": "^
|
|
39
|
+
"@types/debug": "^4.1.12",
|
|
40
|
+
"@types/react": "^19.0.0",
|
|
41
|
+
"react": "^19.0.0",
|
|
42
|
+
"typescript": "^5.0.0"
|
|
39
43
|
},
|
|
40
44
|
"files": [
|
|
41
45
|
"dist",
|