@plyaz/types 1.11.0 → 1.11.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.
@@ -4,4 +4,4 @@
4
4
  *
5
5
  * @module core
6
6
  */
7
- export type { ApiEnvironmentConfig } from './services';
7
+ export type { ApiEnvironmentConfig, ApiProviderProps } from './services';
@@ -2,6 +2,8 @@
2
2
  * Service Layer Types for @plyaz/core
3
3
  * Types specific to core package services
4
4
  */
5
+ import type { ReactNode } from 'react';
6
+ import type { ApiClientOptions } from '../api/client';
5
7
  /**
6
8
  * Environment configuration for API client initialization
7
9
  *
@@ -180,3 +182,216 @@ export interface ApiEnvironmentConfig {
180
182
  */
181
183
  setAsDefault?: boolean;
182
184
  }
185
+ /**
186
+ * Props for ApiProvider component
187
+ *
188
+ * React provider component that initializes the API client service for React/Next.js applications.
189
+ * Wraps your app to ensure the API client is ready before rendering children.
190
+ *
191
+ * **Features**:
192
+ * - Automatic API client initialization on mount
193
+ * - Loading state management during initialization
194
+ * - Error handling with customizable error UI
195
+ * - Success/error callbacks for integration with state management
196
+ * - Singleton pattern - only initializes once per app lifecycle
197
+ *
198
+ * @example
199
+ * ```tsx
200
+ * // Basic usage in Next.js app layout
201
+ * import { ApiProvider } from '@plyaz/core';
202
+ *
203
+ * export default function RootLayout({ children }) {
204
+ * const envConfig = {
205
+ * env: process.env.NODE_ENV as 'production',
206
+ * apiKey: process.env.NEXT_PUBLIC_API_KEY,
207
+ * };
208
+ *
209
+ * const apiConfig = {
210
+ * baseURL: process.env.NEXT_PUBLIC_API_URL!,
211
+ * encryption: {
212
+ * key: {
213
+ * id: 'prod-key-v1',
214
+ * key: process.env.NEXT_PUBLIC_ENCRYPTION_KEY!,
215
+ * algorithm: 'AES-GCM',
216
+ * format: 'raw'
217
+ * }
218
+ * },
219
+ * };
220
+ *
221
+ * return (
222
+ * <ApiProvider envConfig={envConfig} apiConfig={apiConfig}>
223
+ * {children}
224
+ * </ApiProvider>
225
+ * );
226
+ * }
227
+ * ```
228
+ *
229
+ * @example
230
+ * ```tsx
231
+ * // With custom loading and error components
232
+ * <ApiProvider
233
+ * envConfig={envConfig}
234
+ * apiConfig={apiConfig}
235
+ * loadingComponent={
236
+ * <div className="loading">
237
+ * <Spinner />
238
+ * <p>Initializing API client...</p>
239
+ * </div>
240
+ * }
241
+ * errorComponent={(error) => (
242
+ * <div className="error">
243
+ * <h2>API Initialization Failed</h2>
244
+ * <p>{error.message}</p>
245
+ * <button onClick={() => window.location.reload()}>Retry</button>
246
+ * </div>
247
+ * )}
248
+ * onInitialized={() => {
249
+ * console.log('[App] API client ready');
250
+ * }}
251
+ * onError={(error) => {
252
+ * console.error('[App] API initialization error:', error);
253
+ * }}
254
+ * >
255
+ * {children}
256
+ * </ApiProvider>
257
+ * ```
258
+ *
259
+ * @example
260
+ * ```tsx
261
+ * // With state management integration
262
+ * <ApiProvider
263
+ * envConfig={envConfig}
264
+ * apiConfig={{
265
+ * ...apiConfig,
266
+ * clientEvents: {
267
+ * onRequestStart: (event) => {
268
+ * apiStore.setState({ loading: true, requestId: event.data.id });
269
+ * },
270
+ * onResponseReceived: (event) => {
271
+ * apiStore.setState({ loading: false, lastResponse: event.data });
272
+ * },
273
+ * },
274
+ * }}
275
+ * onInitialized={() => {
276
+ * appStore.setState({ apiReady: true });
277
+ * }}
278
+ * >
279
+ * {children}
280
+ * </ApiProvider>
281
+ * ```
282
+ *
283
+ * @see {@link ApiEnvironmentConfig} - Environment configuration options
284
+ * @see {@link ApiClientOptions} from @plyaz/types/api - Full API configuration options
285
+ * @since 1.1.0
286
+ */
287
+ export interface ApiProviderProps {
288
+ /**
289
+ * React children to render after successful initialization
290
+ *
291
+ * Children are only rendered once the API client is fully initialized and ready.
292
+ * If initialization fails, the error component is rendered instead.
293
+ */
294
+ children: ReactNode;
295
+ /**
296
+ * Environment configuration
297
+ *
298
+ * Contains environment-level metadata (env, apiKey, rateLimit, setAsDefault).
299
+ * Determines which default configuration to apply from @plyaz/config.
300
+ *
301
+ * @see {@link ApiEnvironmentConfig} - Full configuration options
302
+ */
303
+ envConfig: ApiEnvironmentConfig;
304
+ /**
305
+ * API configuration
306
+ *
307
+ * Contains API-specific settings (baseURL, encryption, timeout, event handlers).
308
+ * All options from @plyaz/api's `ApiClientOptions` interface are supported.
309
+ *
310
+ * **Required fields**:
311
+ * - `baseURL`: API base URL
312
+ *
313
+ * **Commonly configured**:
314
+ * - `encryption`: Encryption configuration with key
315
+ * - `timeout`: Request timeout in milliseconds
316
+ * - `clientEvents`: Event handlers for monitoring
317
+ * - `retry`: Retry strategy configuration
318
+ *
319
+ * @see {@link ApiClientOptions} from @plyaz/types/api - Complete options reference
320
+ */
321
+ apiConfig: Partial<ApiClientOptions>;
322
+ /**
323
+ * Loading component to show while initializing
324
+ *
325
+ * Rendered while the API client is being initialized. If not provided,
326
+ * a default loading message is shown.
327
+ *
328
+ * @optional
329
+ *
330
+ * @example
331
+ * ```tsx
332
+ * loadingComponent={
333
+ * <div className="loading">
334
+ * <Spinner size="large" />
335
+ * <p>Setting up API connection...</p>
336
+ * </div>
337
+ * }
338
+ * ```
339
+ */
340
+ loadingComponent?: ReactNode;
341
+ /**
342
+ * Error component to show if initialization fails
343
+ *
344
+ * Rendered when API client initialization fails. Receives the error as a parameter.
345
+ * If not provided, a default error message is shown.
346
+ *
347
+ * @optional
348
+ *
349
+ * @example
350
+ * ```tsx
351
+ * errorComponent={(error) => (
352
+ * <ErrorBoundary error={error}>
353
+ * <button onClick={() => window.location.reload()}>
354
+ * Retry
355
+ * </button>
356
+ * </ErrorBoundary>
357
+ * )}
358
+ * ```
359
+ */
360
+ errorComponent?: (error: Error) => ReactNode;
361
+ /**
362
+ * Callback when initialization completes successfully
363
+ *
364
+ * Called once the API client is fully initialized and ready to use.
365
+ * Useful for tracking initialization success or triggering post-init logic.
366
+ *
367
+ * @optional
368
+ *
369
+ * @example
370
+ * ```tsx
371
+ * onInitialized={() => {
372
+ * console.log('[App] API client initialized');
373
+ * analytics.track('api_client_ready');
374
+ * appStore.setState({ apiReady: true });
375
+ * }}
376
+ * ```
377
+ */
378
+ onInitialized?: () => void;
379
+ /**
380
+ * Callback when initialization fails
381
+ *
382
+ * Called if API client initialization throws an error.
383
+ * Useful for error tracking, logging, or showing user notifications.
384
+ *
385
+ * @optional
386
+ *
387
+ * @example
388
+ * ```tsx
389
+ * onError={(error) => {
390
+ * console.error('[App] API initialization failed:', error);
391
+ * Sentry.captureException(error);
392
+ * toast.error('Failed to connect to API');
393
+ * }}
394
+ * ```
395
+ */
396
+ onError?: (error: Error) => void;
397
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plyaz/types",
3
- "version": "1.11.0",
3
+ "version": "1.11.1",
4
4
  "author": "Redeemer Pace",
5
5
  "license": "ISC",
6
6
  "description": "Provides shared TypeScript types and schema utilities for validation and parsing in the @playz ecosystem.",