@owlmeans/client-context 0.1.2 → 0.1.3

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.
Files changed (3) hide show
  1. package/README.md +22 -443
  2. package/package.json +9 -8
  3. package/tsconfig.json +6 -11
package/README.md CHANGED
@@ -1,471 +1,50 @@
1
1
  # @owlmeans/client-context
2
2
 
3
- Client-side context management for OwlMeans Common applications. This package extends the basic context system with client-specific functionality including service routing, API client integration, and configuration management designed for frontend applications.
3
+ Client-side context factory extending `BasicContext` with API client and service routing.
4
4
 
5
5
  ## Overview
6
6
 
7
- The `@owlmeans/client-context` package provides client-side context management for the OwlMeans ecosystem, offering:
8
-
9
- - **Client Context Factory**: Creates contexts specifically configured for frontend applications
10
- - **Service Route Management**: Manages service routes and API endpoints for client-server communication
11
- - **API Client Integration**: Automatic integration with the OwlMeans API client system
12
- - **Configuration Management**: Client-specific configuration with service definitions and i18n support
13
- - **Frontend Context Utilities**: Helper functions for managing client application contexts
14
-
15
- This package follows the OwlMeans "quadra" pattern as a client-side implementation extending the basic `@owlmeans/context` package.
7
+ - `makeClientContext(cfg)` creates a client context with HTTP API client pre-configured
8
+ - Adds `ClientContext` capabilities: service URL resolution, API route calls
9
+ - Base for higher-level context factories (`@owlmeans/web-client`'s `makeContext`)
10
+ - Usually not used directly use `makeContext` from your platform-specific package
16
11
 
17
12
  ## Installation
18
13
 
19
14
  ```bash
20
- npm install @owlmeans/client-context
15
+ bun add @owlmeans/client-context
21
16
  ```
22
17
 
23
- ## Dependencies
24
-
25
- This package requires and integrates with:
26
- - `@owlmeans/context`: Base context management system
27
- - `@owlmeans/client-config`: Client configuration management
28
- - `@owlmeans/api`: API client integration
29
- - `@owlmeans/route`: Service route definitions
30
- - `@owlmeans/i18n`: Internationalization support
31
-
32
- ## Core Concepts
33
-
34
- ### Client Context
35
-
36
- A client context is a specialized application context that manages client-side dependencies, service routes, and API communication. It extends the basic context with client-specific functionality.
37
-
38
- ### Service Routes
39
-
40
- Service routes define how the client communicates with backend services, including endpoint definitions, authentication requirements, and communication protocols.
41
-
42
- ### Configuration Management
43
-
44
- Client contexts handle configuration specific to frontend applications, including service definitions, internationalization settings, and client-specific parameters.
45
-
46
- ## API Reference
47
-
48
- ### Types
49
-
50
- #### `ClientConfig`
51
- Configuration interface for client contexts extending BasicClientConfig.
52
-
53
- ```typescript
54
- interface ClientConfig extends BasicClientConfig {
55
- services: Record<string, CommonServiceRoute> // Service route definitions
56
- i18n?: I18nConfig // Internationalization configuration
57
- }
58
- ```
18
+ ## Usage
59
19
 
60
- #### `ClientContext<C>`
61
- Main client context interface extending BasicContext with client-specific functionality.
20
+ Typically called through a higher-level factory:
62
21
 
63
22
  ```typescript
64
- interface ClientContext<C extends ClientConfig = ClientConfig> extends BasicContext<C>, ConfigResourceAppend {
65
- serviceRoute: (alias: string, makeDefault?: boolean) => CommonServiceRoute
66
- }
23
+ // Via @owlmeans/web-client
24
+ import { makeContext } from '@owlmeans/web-client'
25
+ const context = makeContext(appConfig)
67
26
  ```
68
27
 
69
- **Methods:**
70
- - **`serviceRoute(alias: string, makeDefault?: boolean): CommonServiceRoute`**: Retrieves service route by alias and optionally sets as default
71
-
72
- ### Factory Functions
73
-
74
- #### `makeClientContext<C, T>(cfg: C): T`
75
-
76
- Creates a client context instance with automatic API client integration.
28
+ Direct usage:
77
29
 
78
- **Parameters:**
79
- - `cfg`: Client configuration object
80
-
81
- **Returns:** ClientContext instance
82
-
83
- **Features:**
84
- - Automatically integrates API client
85
- - Sets up service route management
86
- - Configures context for frontend operation
87
-
88
- **Example:**
89
30
  ```typescript
90
- import { makeClientContext, config } from '@owlmeans/client-context'
91
-
92
- // Create client configuration
93
- const clientConfig = config('my-app', {
94
- services: {
95
- 'user-service': {
96
- alias: 'user-service',
97
- route: {
98
- alias: 'users',
99
- path: '/api/users',
100
- service: 'user-service'
101
- }
102
- }
103
- }
104
- })
105
-
106
- // Create client context
31
+ import { makeClientContext } from '@owlmeans/client-context'
107
32
  const context = makeClientContext(clientConfig)
108
-
109
- await context.configure().init()
110
-
111
- // Access service route
112
- const userService = context.serviceRoute('user-service')
113
- ```
114
-
115
- #### `config<C>(service: string, cfg?: Partial<C>): C`
116
-
117
- Creates a client configuration with default frontend settings.
118
-
119
- **Parameters:**
120
- - `service`: Service name identifier
121
- - `cfg` (optional): Additional configuration options
122
-
123
- **Returns:** ClientConfig object configured for frontend
124
-
125
- **Example:**
126
- ```typescript
127
- import { config } from '@owlmeans/client-context'
128
-
129
- const clientConfig = config('frontend-app', {
130
- layer: Layer.Service,
131
- services: {
132
- 'api': {
133
- alias: 'api',
134
- route: {
135
- alias: 'api',
136
- path: '/api',
137
- service: 'backend'
138
- }
139
- }
140
- },
141
- i18n: {
142
- defaultLng: 'en',
143
- defaultNs: 'translation'
144
- }
145
- })
146
- ```
147
-
148
- ### Service Route Management
149
-
150
- #### `serviceRoute(alias: string, makeDefault?: boolean): CommonServiceRoute`
151
-
152
- Retrieves and manages service routes for API communication.
153
-
154
- **Parameters:**
155
- - `alias`: Service route alias
156
- - `makeDefault` (optional): Whether to mark this route as default
157
-
158
- **Returns:** CommonServiceRoute object
159
-
160
- **Throws:** `SyntaxError` if service route not found
161
-
162
- **Example:**
163
- ```typescript
164
- // Get service route
165
- const apiRoute = context.serviceRoute('api')
166
-
167
- // Set as default service
168
- const defaultRoute = context.serviceRoute('main-api', true)
169
-
170
- // Use route for API calls
171
- const apiClient = context.service('api-client')
172
- await apiClient.call(apiRoute, 'users', { method: 'GET' })
173
33
  ```
174
34
 
175
- ### Constants
35
+ ## API
176
36
 
177
- #### `PLUGINS`
178
- Constant for plugin configuration.
37
+ ### `makeClientContext<C, T>(cfg): T`
179
38
 
180
- ```typescript
181
- const PLUGINS = 'plugins'
182
- ```
183
-
184
- ## Usage Examples
185
-
186
- ### Basic Client Context Setup
39
+ Creates a client context configured for API calls.
187
40
 
188
- ```typescript
189
- import { makeClientContext, config } from '@owlmeans/client-context'
190
- import { Layer } from '@owlmeans/context'
191
-
192
- // Create configuration with service routes
193
- const clientConfig = config('web-app', {
194
- layer: Layer.User,
195
- services: {
196
- 'auth-service': {
197
- alias: 'auth',
198
- route: {
199
- alias: 'auth',
200
- path: '/api/auth',
201
- service: 'auth-backend'
202
- }
203
- },
204
- 'user-service': {
205
- alias: 'users',
206
- route: {
207
- alias: 'users',
208
- path: '/api/users',
209
- service: 'user-backend'
210
- }
211
- }
212
- }
213
- })
214
-
215
- // Create and initialize context
216
- const context = makeClientContext(clientConfig)
217
- await context.configure().init()
41
+ ### `ClientContext<C>`
218
42
 
219
- // Access services and routes
220
- const authRoute = context.serviceRoute('auth-service')
221
- const userRoute = context.serviceRoute('user-service')
222
- ```
223
-
224
- ### API Integration
225
-
226
- ```typescript
227
- // Context automatically includes API client
228
- const apiClient = context.service('api-client')
229
-
230
- // Use service routes for API calls
231
- const authRoute = context.serviceRoute('auth-service')
232
- const response = await apiClient.call(authRoute, 'login', {
233
- method: 'POST',
234
- body: { username: 'user', password: 'pass' }
235
- })
236
-
237
- // Handle different services
238
- const userRoute = context.serviceRoute('user-service', true) // Set as default
239
- const users = await apiClient.call(userRoute, 'list', { method: 'GET' })
240
- ```
241
-
242
- ### Internationalization Integration
243
-
244
- ```typescript
245
- const clientConfig = config('i18n-app', {
246
- services: { /* service definitions */ },
247
- i18n: {
248
- defaultLng: 'en',
249
- defaultNs: 'app',
250
- resources: {
251
- en: {
252
- app: {
253
- title: 'My Application',
254
- welcome: 'Welcome to our app'
255
- }
256
- }
257
- }
258
- }
259
- })
260
-
261
- const context = makeClientContext(clientConfig)
262
- await context.configure().init()
263
-
264
- // i18n is automatically configured and available
265
- ```
266
-
267
- ### Multi-Service Configuration
268
-
269
- ```typescript
270
- const multiServiceConfig = config('complex-app', {
271
- services: {
272
- 'auth': {
273
- alias: 'auth',
274
- route: {
275
- alias: 'auth',
276
- path: '/api/auth',
277
- service: 'auth-service'
278
- }
279
- },
280
- 'users': {
281
- alias: 'users',
282
- route: {
283
- alias: 'users',
284
- path: '/api/users',
285
- service: 'user-service'
286
- }
287
- },
288
- 'orders': {
289
- alias: 'orders',
290
- route: {
291
- alias: 'orders',
292
- path: '/api/orders',
293
- service: 'order-service'
294
- }
295
- }
296
- }
297
- })
298
-
299
- const context = makeClientContext(multiServiceConfig)
300
- await context.configure().init()
301
-
302
- // Access different service routes
303
- const authRoute = context.serviceRoute('auth')
304
- const userRoute = context.serviceRoute('users')
305
- const orderRoute = context.serviceRoute('orders')
306
-
307
- // Use with API client
308
- const apiClient = context.service('api-client')
309
- await apiClient.call(authRoute, 'me')
310
- await apiClient.call(userRoute, 'profile')
311
- await apiClient.call(orderRoute, 'recent')
312
- ```
313
-
314
- ### Context with Resource Configuration
315
-
316
- ```typescript
317
- import { config } from '@owlmeans/client-context'
318
-
319
- const resourceConfig = config('resource-app', {
320
- services: {
321
- 'storage': {
322
- alias: 'storage',
323
- route: {
324
- alias: 'storage',
325
- path: '/api/storage',
326
- service: 'storage-service'
327
- }
328
- }
329
- },
330
- // Additional resource configuration
331
- resources: [{
332
- alias: 'local-storage',
333
- type: 'storage',
334
- config: {
335
- provider: 'localStorage'
336
- }
337
- }]
338
- })
339
-
340
- const context = makeClientContext(resourceConfig)
341
- await context.configure().init()
342
-
343
- // Access both service routes and resources
344
- const storageRoute = context.serviceRoute('storage')
345
- const localStorage = context.resource('local-storage')
346
- ```
347
-
348
- ### Service Route Error Handling
349
-
350
- ```typescript
351
- try {
352
- const route = context.serviceRoute('non-existent-service')
353
- } catch (error) {
354
- if (error instanceof SyntaxError && error.message.includes('Service not found')) {
355
- console.error('Service route not configured')
356
- // Handle missing service configuration
357
- }
358
- }
359
-
360
- // Safe service route access
361
- const hasUserService = 'user-service' in context.cfg.services
362
- if (hasUserService) {
363
- const userRoute = context.serviceRoute('user-service')
364
- // Use the route safely
365
- }
366
- ```
367
-
368
- ### Default Service Management
369
-
370
- ```typescript
371
- // Set a service as default
372
- const primaryApi = context.serviceRoute('primary-api', true)
373
-
374
- // The service is now marked as default
375
- console.log(primaryApi.default) // true
376
-
377
- // Other services remain non-default unless explicitly set
378
- const secondaryApi = context.serviceRoute('secondary-api')
379
- console.log(secondaryApi.default) // false or undefined
380
- ```
381
-
382
- ## Configuration
383
-
384
- Client configuration supports various options for frontend applications:
385
-
386
- ```typescript
387
- interface ClientConfig extends BasicClientConfig {
388
- // Service route definitions
389
- services: Record<string, CommonServiceRoute>
390
-
391
- // Internationalization settings
392
- i18n?: {
393
- defaultLng?: string
394
- defaultNs?: string
395
- resources?: Record<string, Record<string, any>>
396
- }
397
-
398
- // Additional client-specific configuration
399
- // (inherited from BasicClientConfig)
400
- }
401
- ```
402
-
403
- ### Service Route Configuration
404
-
405
- ```typescript
406
- interface CommonServiceRoute {
407
- alias: string // Service route alias
408
- route: {
409
- alias: string // Route alias
410
- path: string // API endpoint path
411
- service: string // Backend service identifier
412
- method?: string // HTTP method
413
- // Additional route configuration
414
- }
415
- default?: boolean // Whether this is the default service
416
- }
417
- ```
418
-
419
- ## Error Handling
420
-
421
- The package provides descriptive error messages:
422
-
423
- - **Service not found**: Thrown when accessing non-configured service routes
424
- - **Configuration errors**: Thrown during context initialization with invalid config
425
-
426
- ```typescript
427
- try {
428
- const context = makeClientContext(invalidConfig)
429
- await context.configure().init()
430
- } catch (error) {
431
- console.error('Context initialization failed:', error)
432
- }
433
- ```
434
-
435
- ## Integration with OwlMeans Ecosystem
436
-
437
- This package integrates with:
438
-
439
- - **@owlmeans/context**: Base context management system
440
- - **@owlmeans/api**: API client for service communication
441
- - **@owlmeans/client-config**: Client configuration management
442
- - **@owlmeans/route**: Service route definitions
443
- - **@owlmeans/i18n**: Internationalization support
444
-
445
- ## Best Practices
446
-
447
- 1. **Define all service routes** in configuration for proper API communication
448
- 2. **Use meaningful service aliases** for easy identification
449
- 3. **Configure i18n settings** for internationalized applications
450
- 4. **Handle service route errors** gracefully with try-catch blocks
451
- 5. **Set default services** appropriately for primary API endpoints
43
+ Extends `BasicContext<C>` with:
44
+ - Service URL resolution for configured service routes
452
45
 
453
46
  ## Related Packages
454
47
 
455
- - **@owlmeans/server-context**: Server-side context management
456
- - **@owlmeans/context**: Base context system
457
- - **@owlmeans/api**: API client integration
458
- - **@owlmeans/client-config**: Client configuration
459
- - **@owlmeans/route**: Route management
460
-
461
- ## Utilities
462
-
463
- The package also provides utility functions in the `/utils` subpackage for advanced context management and configuration operations.
464
-
465
- ```typescript
466
- import { /* utility functions */ } from '@owlmeans/client-context/utils'
467
- ```
468
-
469
- ## TypeScript Support
470
-
471
- The package is written in TypeScript and provides comprehensive type definitions for all client context operations, ensuring type safety for service routes, configuration, and API integration.
48
+ - [`@owlmeans/context`](../context) `BasicContext` base
49
+ - [`@owlmeans/web-client`](../web-client) wraps this with React Router integration
50
+ - [`@owlmeans/client-config`](../client-config) `addWebService` for service URL config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/client-context",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -28,16 +28,17 @@
28
28
  }
29
29
  },
30
30
  "dependencies": {
31
- "@owlmeans/api": "^0.1.2",
32
- "@owlmeans/client-config": "^0.1.2",
33
- "@owlmeans/config": "^0.1.2",
34
- "@owlmeans/context": "^0.1.2",
35
- "@owlmeans/i18n": "^0.1.2",
36
- "@owlmeans/route": "^0.1.2"
31
+ "@owlmeans/api": "^0.1.3",
32
+ "@owlmeans/client-config": "^0.1.3",
33
+ "@owlmeans/config": "^0.1.3",
34
+ "@owlmeans/context": "^0.1.3",
35
+ "@owlmeans/i18n": "^0.1.3",
36
+ "@owlmeans/route": "^0.1.3"
37
37
  },
38
38
  "devDependencies": {
39
+ "@owlmeans/dep-config": "workspace:*",
39
40
  "nodemon": "^3.1.11",
40
- "typescript": "^5.8.3"
41
+ "typescript": "^6.0.2"
41
42
  },
42
43
  "publishConfig": {
43
44
  "access": "public"
package/tsconfig.json CHANGED
@@ -1,16 +1,11 @@
1
1
  {
2
2
  "extends": [
3
- "../tsconfig.default.json",
4
- "../tsconfig.react.json",
3
+ "@owlmeans/dep-config/tsconfig.base.json",
4
+ "@owlmeans/dep-config/tsconfig.react.json"
5
5
  ],
6
6
  "compilerOptions": {
7
- "rootDir": "./src/", /* Specify the root folder within your source files. */
8
- "outDir": "./build/", /* Specify an output folder for all emitted files. */
9
- "moduleResolution": "Bundler"
7
+ "rootDir": "./src/",
8
+ "outDir": "./build/"
10
9
  },
11
- "exclude": [
12
- "./dist/**/*",
13
- "./build/**/*",
14
- "./*.ts"
15
- ]
16
- }
10
+ "exclude": ["./dist/**/*", "./build/**/*", "./*.ts"]
11
+ }