space-react-client 0.2.5 → 0.3.1

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.
package/dist/index.js CHANGED
@@ -28,7 +28,7 @@ class TokenService {
28
28
  * Retrieves the stored pricing token's payload.
29
29
  * @returns The stored pricing token payload.
30
30
  */
31
- getPricingToken() {
31
+ getPayload() {
32
32
  if (!this._validToken()) {
33
33
  return null;
34
34
  }
@@ -39,7 +39,7 @@ class TokenService {
39
39
  * @param key - A key of the stored pricing token whose is going to be retrieved.
40
40
  * @return The value of the key in the stored pricing token payload.
41
41
  */
42
- getFromToken(key) {
42
+ getKey(key) {
43
43
  if (!this._validToken()) {
44
44
  return null;
45
45
  }
@@ -49,7 +49,7 @@ class TokenService {
49
49
  * Updates the stored pricing token with the payload of a new one.
50
50
  * @param token - Pricing token string
51
51
  */
52
- updatePricingToken(token) {
52
+ update(token) {
53
53
  const parsedToken = parseJwt(token);
54
54
  this.tokenPayload = parsedToken;
55
55
  this._notify();
@@ -69,7 +69,7 @@ class TokenService {
69
69
  }
70
70
  _validToken() {
71
71
  if (!this.tokenPayload) {
72
- console.warn('Token payload is not set. Please call updateLocalPricingToken first.');
72
+ console.warn('Token payload is not set. Please call TokenService.update first.');
73
73
  return false;
74
74
  }
75
75
  if (isTokenExpired(this.tokenPayload)) {
@@ -118,7 +118,7 @@ class SpaceClient {
118
118
  this.pricingSocketNamespace = this.socketClient.io.socket('/pricings');
119
119
  this.apiKey = config.apiKey;
120
120
  this.emitter = new TinyEmitter();
121
- this.tokenService = new TokenService();
121
+ this.token = new TokenService();
122
122
  this.axios = axios.create({
123
123
  baseURL: this.httpUrl,
124
124
  headers: {
@@ -211,7 +211,7 @@ class SpaceClient {
211
211
  }
212
212
  this.userId = userId;
213
213
  const userPricingToken = await this.generateUserPricingToken();
214
- this.tokenService.updatePricingToken(userPricingToken);
214
+ this.token.update(userPricingToken);
215
215
  }
216
216
  /**
217
217
  * Performs a request to SPACE to retrieve a new pricing token for the user with the given userId.
@@ -250,7 +250,7 @@ const SpaceProvider = ({ config, children, }) => {
250
250
  tokenService = new TokenService();
251
251
  }
252
252
  else {
253
- tokenService = client.tokenService;
253
+ tokenService = client.token;
254
254
  }
255
255
  return {
256
256
  client: client,
@@ -289,14 +289,33 @@ function useSpaceClient() {
289
289
  * Custom hook to access the service that manages the pricing token.
290
290
  * Throws an error if used outside of SpaceProvider.
291
291
  */
292
- function usePricingToken() {
292
+ function useTokenService() {
293
293
  const spaceContext = useContext(SpaceContext);
294
294
  if (!spaceContext) {
295
- throw new Error('usePricingToken must be used within a SpaceProvider');
295
+ throw new Error('useTokenService must be used within a SpaceProvider');
296
296
  }
297
297
  return spaceContext.tokenService;
298
298
  }
299
299
 
300
+ /**
301
+ * React hook that returns the current pricing token payload and
302
+ * re-renders when the token changes.
303
+ */
304
+ function usePricingTokenPayload() {
305
+ const tokenService = useTokenService();
306
+ const [payload, setPayload] = useState(() => tokenService.getPayload());
307
+ useEffect(() => {
308
+ // Ensure latest value on mount
309
+ setPayload(tokenService.getPayload());
310
+ // Subscribe to token updates
311
+ const unsubscribe = tokenService.subscribe(() => {
312
+ setPayload(tokenService.getPayload());
313
+ });
314
+ return () => unsubscribe();
315
+ }, [tokenService]);
316
+ return payload;
317
+ }
318
+
300
319
  // Generic wrapper for feature children
301
320
  function On({ children }) {
302
321
  return jsx(Fragment, { children: children });
@@ -316,7 +335,8 @@ function getChildrenOfType(children, type) {
316
335
  return match ? match.props.children : null;
317
336
  }
318
337
  const Feature = ({ id, children }) => {
319
- const tokenService = usePricingToken();
338
+ const tokenService = useTokenService();
339
+ const tokenPayload = usePricingTokenPayload();
320
340
  const [status, setStatus] = useState('loading');
321
341
  const [result, setResult] = useState(null);
322
342
  // Validate id
@@ -327,7 +347,7 @@ const Feature = ({ id, children }) => {
327
347
  setStatus('error');
328
348
  return;
329
349
  }
330
- if (tokenService.getPricingToken() === null) {
350
+ if (tokenService.getPayload() === null) {
331
351
  setStatus('error');
332
352
  return;
333
353
  }
@@ -351,7 +371,7 @@ const Feature = ({ id, children }) => {
351
371
  return () => {
352
372
  unsubscribe();
353
373
  };
354
- }, [id, isValidId, tokenService]);
374
+ }, [id, isValidId, tokenPayload, tokenService]);
355
375
  if (status === 'loading') {
356
376
  return jsx(Fragment, { children: getChildrenOfType(children, Loading) });
357
377
  }
@@ -367,4 +387,4 @@ const Feature = ({ id, children }) => {
367
387
  return jsx(Fragment, {});
368
388
  };
369
389
 
370
- export { Default, ErrorFallback, Feature, Loading, On, SpaceProvider, usePricingToken, useSpaceClient };
390
+ export { Default, ErrorFallback, Feature, Loading, On, SpaceProvider, usePricingTokenPayload, useSpaceClient, useTokenService };
package/package.json CHANGED
@@ -1,13 +1,18 @@
1
1
  {
2
2
  "name": "space-react-client",
3
3
  "type": "module",
4
- "version": "0.2.5",
4
+ "version": "0.3.1",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/Alex-GF/space-react-client"
12
+ },
9
13
  "exports": {
10
14
  ".": {
15
+ "types": "./dist/index.d.ts",
11
16
  "import": "./dist/index.js",
12
17
  "require": "./dist/index.js"
13
18
  }