space-react-client 0.1.0 → 0.2.0
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.d.ts +14 -3
- package/dist/index.js +27 -38
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -108,9 +108,8 @@ interface SpaceClientContext{
|
|
|
108
108
|
/**
|
|
109
109
|
* SpaceProvider initializes and provides the SpaceClient instance to children.
|
|
110
110
|
*/
|
|
111
|
-
declare const SpaceProvider: ({ config,
|
|
111
|
+
declare const SpaceProvider: ({ config, children, }: {
|
|
112
112
|
config: SpaceConfiguration;
|
|
113
|
-
loader?: React.ReactNode;
|
|
114
113
|
children: React.ReactNode;
|
|
115
114
|
}) => JSX.Element;
|
|
116
115
|
|
|
@@ -130,7 +129,19 @@ interface FeatureProps {
|
|
|
130
129
|
id: string;
|
|
131
130
|
children: React.ReactNode;
|
|
132
131
|
}
|
|
132
|
+
declare function On({ children }: {
|
|
133
|
+
children: React.ReactNode;
|
|
134
|
+
}): JSX.Element;
|
|
135
|
+
declare function Default({ children }: {
|
|
136
|
+
children: React.ReactNode;
|
|
137
|
+
}): JSX.Element;
|
|
138
|
+
declare function Loading({ children }: {
|
|
139
|
+
children: React.ReactNode;
|
|
140
|
+
}): JSX.Element;
|
|
141
|
+
declare function ErrorFallback({ children }: {
|
|
142
|
+
children: React.ReactNode;
|
|
143
|
+
}): JSX.Element;
|
|
133
144
|
declare const Feature: ({ id, children }: FeatureProps) => JSX.Element;
|
|
134
145
|
|
|
135
|
-
export { Feature, SpaceClient$1 as SpaceClient, SpaceProvider, TokenService$1 as TokenService, usePricingToken, useSpaceClient };
|
|
146
|
+
export { Default, ErrorFallback, Feature, Loading, On, SpaceClient$1 as SpaceClient, SpaceProvider, TokenService$1 as TokenService, usePricingToken, useSpaceClient };
|
|
136
147
|
export type { EventMessage, SpaceClientContext, SpaceConfiguration, SpaceEvents };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
2
2
|
import React, { createContext, useMemo, useContext, useState, useEffect } from 'react';
|
|
3
3
|
import { TinyEmitter } from 'tiny-emitter';
|
|
4
4
|
import axios from 'axios';
|
|
@@ -165,7 +165,8 @@ class SpaceClient {
|
|
|
165
165
|
throw new Error('User ID must be a non-empty string.');
|
|
166
166
|
}
|
|
167
167
|
this.userId = userId;
|
|
168
|
-
|
|
168
|
+
const userPricingToken = await this.generateUserPricingToken();
|
|
169
|
+
this.tokenService.updatePricingToken(userPricingToken);
|
|
169
170
|
}
|
|
170
171
|
/**
|
|
171
172
|
* Performs a request to SPACE to retrieve a new pricing token for the user with the given userId.
|
|
@@ -178,7 +179,7 @@ class SpaceClient {
|
|
|
178
179
|
throw new Error('User ID is not set. Please set the user ID with `setUserId(userId)` before trying to generate a pricing token.');
|
|
179
180
|
}
|
|
180
181
|
return this.axios
|
|
181
|
-
.post(`/features/${this.userId}`)
|
|
182
|
+
.post(`/features/${this.userId}/pricing-token`)
|
|
182
183
|
.then((response) => {
|
|
183
184
|
return response.data.pricingToken;
|
|
184
185
|
})
|
|
@@ -194,7 +195,7 @@ const SpaceContext = createContext(undefined);
|
|
|
194
195
|
/**
|
|
195
196
|
* SpaceProvider initializes and provides the SpaceClient instance to children.
|
|
196
197
|
*/
|
|
197
|
-
const SpaceProvider = ({ config,
|
|
198
|
+
const SpaceProvider = ({ config, children, }) => {
|
|
198
199
|
// Memorize the client to avoid unnecessary re-instantiation
|
|
199
200
|
const context = useMemo(() => {
|
|
200
201
|
const client = config.allowConnectionWithSpace ? new SpaceClient(config) : undefined;
|
|
@@ -210,29 +211,6 @@ const SpaceProvider = ({ config, loader, children, }) => {
|
|
|
210
211
|
tokenService: tokenService,
|
|
211
212
|
};
|
|
212
213
|
}, [config.url, config.apiKey]);
|
|
213
|
-
const [connected, setConnected] = React.useState(false);
|
|
214
|
-
React.useEffect(() => {
|
|
215
|
-
if (!context.client) {
|
|
216
|
-
setConnected(true); // No connection needed if client is undefined
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
const handleSync = () => setConnected(true);
|
|
220
|
-
context.client.on('synchronized', handleSync);
|
|
221
|
-
}, [context.client]);
|
|
222
|
-
if (!connected)
|
|
223
|
-
return (jsxs("div", { style: { display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh', width: '100%' }, children: [loader ?? (jsx("div", { style: {
|
|
224
|
-
border: '4px solid #e0e0e0',
|
|
225
|
-
borderTop: '4px solid #1976d2',
|
|
226
|
-
borderRadius: '50%',
|
|
227
|
-
width: 48,
|
|
228
|
-
height: 48,
|
|
229
|
-
animation: 'spin 1s linear infinite',
|
|
230
|
-
} })), jsx("style", { children: `
|
|
231
|
-
@keyframes spin {
|
|
232
|
-
0% { transform: rotate(0deg); }
|
|
233
|
-
100% { transform: rotate(360deg); }
|
|
234
|
-
}
|
|
235
|
-
` })] }));
|
|
236
214
|
return jsx(SpaceContext.Provider, { value: context, children: children });
|
|
237
215
|
};
|
|
238
216
|
|
|
@@ -266,10 +244,23 @@ function usePricingToken() {
|
|
|
266
244
|
return spaceContext.tokenService;
|
|
267
245
|
}
|
|
268
246
|
|
|
269
|
-
//
|
|
270
|
-
function
|
|
271
|
-
|
|
272
|
-
|
|
247
|
+
// Generic wrapper for feature children
|
|
248
|
+
function On({ children }) {
|
|
249
|
+
return jsx(Fragment, { children: children });
|
|
250
|
+
}
|
|
251
|
+
function Default({ children }) {
|
|
252
|
+
return jsx(Fragment, { children: children });
|
|
253
|
+
}
|
|
254
|
+
function Loading({ children }) {
|
|
255
|
+
return jsx(Fragment, { children: children });
|
|
256
|
+
}
|
|
257
|
+
function ErrorFallback({ children }) {
|
|
258
|
+
return jsx(Fragment, { children: children });
|
|
259
|
+
}
|
|
260
|
+
// Helper to get the children of a specific subcomponent type
|
|
261
|
+
function getChildrenOfType(children, type) {
|
|
262
|
+
const match = React.Children.toArray(children).find((child) => React.isValidElement(child) && child.type === type);
|
|
263
|
+
return match ? match.props.children : null;
|
|
273
264
|
}
|
|
274
265
|
const Feature = ({ id, children }) => {
|
|
275
266
|
const tokenService = usePricingToken();
|
|
@@ -279,12 +270,10 @@ const Feature = ({ id, children }) => {
|
|
|
279
270
|
const isValidId = useMemo(() => id.includes('-'), [id]);
|
|
280
271
|
useEffect(() => {
|
|
281
272
|
if (!isValidId) {
|
|
282
|
-
console.error(`Invalid feature ID: ‘${id}’. A valid feature ID must contain a hyphen (’-’) and follow the format: ‘{serviceName in lowercase}-{featureName as defined in the pricing}’.`);
|
|
283
273
|
setStatus('error');
|
|
284
274
|
return;
|
|
285
275
|
}
|
|
286
276
|
if (tokenService.getPricingToken() === null) {
|
|
287
|
-
console.error(`Pricing token is either not set or expired. Please ensure the token is initialized and not expired before using the Feature component.`);
|
|
288
277
|
setStatus('error');
|
|
289
278
|
return;
|
|
290
279
|
}
|
|
@@ -300,18 +289,18 @@ const Feature = ({ id, children }) => {
|
|
|
300
289
|
}
|
|
301
290
|
}, [id, isValidId]);
|
|
302
291
|
if (status === 'loading') {
|
|
303
|
-
return
|
|
292
|
+
return jsx(Fragment, { children: getChildrenOfType(children, Loading) });
|
|
304
293
|
}
|
|
305
294
|
if (status === 'error') {
|
|
306
|
-
return
|
|
295
|
+
return jsx(Fragment, { children: getChildrenOfType(children, ErrorFallback) });
|
|
307
296
|
}
|
|
308
297
|
if (status === 'success' && result === true) {
|
|
309
|
-
return
|
|
298
|
+
return jsx(Fragment, { children: getChildrenOfType(children, On) });
|
|
310
299
|
}
|
|
311
300
|
if (status === 'success' && result === false) {
|
|
312
|
-
return
|
|
301
|
+
return jsx(Fragment, { children: getChildrenOfType(children, Default) });
|
|
313
302
|
}
|
|
314
303
|
return jsx(Fragment, {});
|
|
315
304
|
};
|
|
316
305
|
|
|
317
|
-
export { Feature, SpaceProvider, usePricingToken, useSpaceClient };
|
|
306
|
+
export { Default, ErrorFallback, Feature, Loading, On, SpaceProvider, usePricingToken, useSpaceClient };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "space-react-client",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.2.0",
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"module": "dist/index.js",
|
|
@@ -28,13 +28,14 @@
|
|
|
28
28
|
"license": "ISC",
|
|
29
29
|
"packageManager": "pnpm@10.10.0",
|
|
30
30
|
"peerDependencies": {
|
|
31
|
-
"react": "^
|
|
31
|
+
"react": "^18.3.1",
|
|
32
|
+
"react-dom": "^18.3.1"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"@rollup/plugin-alias": "^5.1.1",
|
|
35
36
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
36
37
|
"@testing-library/react": "^16.3.0",
|
|
37
|
-
"@types/react": "^
|
|
38
|
+
"@types/react": "^18.3.1",
|
|
38
39
|
"@vitejs/plugin-react": "^4.5.0",
|
|
39
40
|
"eslint": "^9.28.0",
|
|
40
41
|
"eslint-config-prettier": "^10.1.5",
|