@owlmeans/client-context 0.1.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/LICENSE +21 -0
- package/README.md +471 -0
- package/build/.gitkeep +0 -0
- package/build/config.d.ts +3 -0
- package/build/config.d.ts.map +1 -0
- package/build/config.js +6 -0
- package/build/config.js.map +1 -0
- package/build/consts.d.ts +2 -0
- package/build/consts.d.ts.map +1 -0
- package/build/consts.js +2 -0
- package/build/consts.js.map +1 -0
- package/build/context.d.ts +3 -0
- package/build/context.d.ts.map +1 -0
- package/build/context.js +19 -0
- package/build/context.js.map +1 -0
- package/build/index.d.ts +5 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +4 -0
- package/build/index.js.map +1 -0
- package/build/types.d.ts +13 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/package.json +45 -0
- package/src/config.ts +9 -0
- package/src/consts.ts +2 -0
- package/src/context.ts +26 -0
- package/src/index.ts +5 -0
- package/src/types.ts +15 -0
- package/tsconfig.json +16 -0
- package/tsconfig.tsbuildinfo +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 OwlMeans Common — Fullstack typescript framework
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
# @owlmeans/client-context
|
|
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.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
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.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @owlmeans/client-context
|
|
21
|
+
```
|
|
22
|
+
|
|
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
|
+
```
|
|
59
|
+
|
|
60
|
+
#### `ClientContext<C>`
|
|
61
|
+
Main client context interface extending BasicContext with client-specific functionality.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
interface ClientContext<C extends ClientConfig = ClientConfig> extends BasicContext<C>, ConfigResourceAppend {
|
|
65
|
+
serviceRoute: (alias: string, makeDefault?: boolean) => CommonServiceRoute
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
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.
|
|
77
|
+
|
|
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
|
+
```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
|
|
107
|
+
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
|
+
```
|
|
174
|
+
|
|
175
|
+
### Constants
|
|
176
|
+
|
|
177
|
+
#### `PLUGINS`
|
|
178
|
+
Constant for plugin configuration.
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
const PLUGINS = 'plugins'
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Usage Examples
|
|
185
|
+
|
|
186
|
+
### Basic Client Context Setup
|
|
187
|
+
|
|
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()
|
|
218
|
+
|
|
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
|
|
452
|
+
|
|
453
|
+
## Related Packages
|
|
454
|
+
|
|
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.
|
package/build/.gitkeep
ADDED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,YAAY,WAAW,MAAM,QAAQ,OAAO,CAAC,CAAC,CAAC,KAAG,CAIlF,CAAA"}
|
package/build/config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAG5D,MAAM,CAAC,MAAM,MAAM,GAAG,CAAyB,OAAe,EAAE,GAAgB,EAAK,EAAE;IACrF,MAAM,MAAM,GAAM,eAAe,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,CAAC,CAAA;IAEjE,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,OAAO,YAAY,CAAA"}
|
package/build/consts.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,OAAO,GAAG,SAAS,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAG7D,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,YAAY,EAAE,CAAC,SAAS,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,KAAG,CAqB9F,CAAA"}
|
package/build/context.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { makeBasicContext } from '@owlmeans/context';
|
|
2
|
+
import { appendApiClient } from '@owlmeans/api';
|
|
3
|
+
export const makeClientContext = (cfg) => {
|
|
4
|
+
const context = makeBasicContext(cfg);
|
|
5
|
+
context.serviceRoute = (alias, makeDefault) => {
|
|
6
|
+
const service = context.cfg.services[alias];
|
|
7
|
+
if (service == null) {
|
|
8
|
+
throw new SyntaxError(`Service not found ${alias}`);
|
|
9
|
+
}
|
|
10
|
+
if (typeof makeDefault === 'boolean') {
|
|
11
|
+
service.default = makeDefault;
|
|
12
|
+
}
|
|
13
|
+
return service;
|
|
14
|
+
};
|
|
15
|
+
appendApiClient(context);
|
|
16
|
+
context.makeContext = makeClientContext;
|
|
17
|
+
return context;
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AAEpD,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAA;AAE/C,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAqD,GAAM,EAAK,EAAE;IACjG,MAAM,OAAO,GAAG,gBAAgB,CAAC,GAAG,CAAM,CAAA;IAE1C,OAAO,CAAC,YAAY,GAAG,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE;QAC5C,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC3C,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;YACpB,MAAM,IAAI,WAAW,CAAC,qBAAqB,KAAK,EAAE,CAAC,CAAA;QACrD,CAAC;QAED,IAAI,OAAO,WAAW,KAAK,SAAS,EAAE,CAAC;YACrC,OAAO,CAAC,OAAO,GAAG,WAAW,CAAA;QAC/B,CAAC;QAED,OAAO,OAAO,CAAA;IAChB,CAAC,CAAA;IAED,eAAe,CAAO,OAAO,CAAC,CAAA;IAE9B,OAAO,CAAC,WAAW,GAAG,iBAA+C,CAAA;IAErE,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,aAAa,CAAA"}
|
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { BasicClientConfig } from '@owlmeans/client-config';
|
|
2
|
+
import type { BasicContext } from '@owlmeans/context';
|
|
3
|
+
import type { CommonServiceRoute } from '@owlmeans/route';
|
|
4
|
+
import type { I18nConfig } from '@owlmeans/i18n';
|
|
5
|
+
import type { ConfigResourceAppend } from '@owlmeans/config';
|
|
6
|
+
export interface ClientConfig extends BasicClientConfig {
|
|
7
|
+
services: Record<string, CommonServiceRoute>;
|
|
8
|
+
i18n?: I18nConfig;
|
|
9
|
+
}
|
|
10
|
+
export interface ClientContext<C extends ClientConfig = ClientConfig> extends BasicContext<C>, ConfigResourceAppend {
|
|
11
|
+
serviceRoute: (alias: string, makeDefault?: boolean) => CommonServiceRoute;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAA;AAChE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AACzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAChD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAE5D,MAAM,WAAW,YAAa,SAAQ,iBAAiB;IACrD,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAA;IAC5C,IAAI,CAAC,EAAE,UAAU,CAAA;CAClB;AAED,MAAM,WAAW,aAAa,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,CAAE,SAAQ,YAAY,CAAC,CAAC,CAAC,EAC3F,oBAAoB;IACpB,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,KAAK,kBAAkB,CAAA;CAC3E"}
|
package/build/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@owlmeans/client-context",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc -b",
|
|
7
|
+
"dev": "sleep 78 && nodemon -e ts,tsx,json --watch src --exec \"tsc -p ./tsconfig.json\"",
|
|
8
|
+
"watch": "tsc -b -w --preserveWatchOutput --pretty"
|
|
9
|
+
},
|
|
10
|
+
"main": "build/index.js",
|
|
11
|
+
"module": "build/index.js",
|
|
12
|
+
"types": "build/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./build/index.js",
|
|
16
|
+
"require": "./build/index.js",
|
|
17
|
+
"default": "./build/index.js",
|
|
18
|
+
"module": "./build/index.js",
|
|
19
|
+
"types": "./build/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./utils": {
|
|
22
|
+
"import": "./build/utils/index.js",
|
|
23
|
+
"require": "./build/utils/index.js",
|
|
24
|
+
"default": "./build/utils/index.js",
|
|
25
|
+
"module": "./build/utils/index.js",
|
|
26
|
+
"types": "./build/utils/index.d.ts"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@owlmeans/api": "0.1.0",
|
|
31
|
+
"@owlmeans/client-config": "0.1.0",
|
|
32
|
+
"@owlmeans/config": "0.1.0",
|
|
33
|
+
"@owlmeans/context": "0.1.0",
|
|
34
|
+
"@owlmeans/i18n": "0.1.0",
|
|
35
|
+
"@owlmeans/route": "0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"nodemon": "^3.1.7",
|
|
39
|
+
"typescript": "^5.6.3"
|
|
40
|
+
},
|
|
41
|
+
"private": false,
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
import { AppType, makeBasicConfig } from '@owlmeans/context'
|
|
3
|
+
import type { ClientConfig } from './types.js'
|
|
4
|
+
|
|
5
|
+
export const config = <C extends ClientConfig>(service: string, cfg?: Partial<C>): C => {
|
|
6
|
+
const config: C = makeBasicConfig(AppType.Frontend, service, cfg)
|
|
7
|
+
|
|
8
|
+
return config
|
|
9
|
+
}
|
package/src/consts.ts
ADDED
package/src/context.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { makeBasicContext } from '@owlmeans/context'
|
|
2
|
+
import type { ClientConfig, ClientContext } from './types.js'
|
|
3
|
+
import { appendApiClient } from '@owlmeans/api'
|
|
4
|
+
|
|
5
|
+
export const makeClientContext = <C extends ClientConfig, T extends ClientContext<C>>(cfg: C): T => {
|
|
6
|
+
const context = makeBasicContext(cfg) as T
|
|
7
|
+
|
|
8
|
+
context.serviceRoute = (alias, makeDefault) => {
|
|
9
|
+
const service = context.cfg.services[alias]
|
|
10
|
+
if (service == null) {
|
|
11
|
+
throw new SyntaxError(`Service not found ${alias}`)
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (typeof makeDefault === 'boolean') {
|
|
15
|
+
service.default = makeDefault
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return service
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
appendApiClient<C, T>(context)
|
|
22
|
+
|
|
23
|
+
context.makeContext = makeClientContext as typeof context.makeContext
|
|
24
|
+
|
|
25
|
+
return context
|
|
26
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BasicClientConfig } from '@owlmeans/client-config'
|
|
2
|
+
import type { BasicContext } from '@owlmeans/context'
|
|
3
|
+
import type { CommonServiceRoute } from '@owlmeans/route'
|
|
4
|
+
import type { I18nConfig } from '@owlmeans/i18n'
|
|
5
|
+
import type { ConfigResourceAppend } from '@owlmeans/config'
|
|
6
|
+
|
|
7
|
+
export interface ClientConfig extends BasicClientConfig {
|
|
8
|
+
services: Record<string, CommonServiceRoute>
|
|
9
|
+
i18n?: I18nConfig
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ClientContext<C extends ClientConfig = ClientConfig> extends BasicContext<C>,
|
|
13
|
+
ConfigResourceAppend {
|
|
14
|
+
serviceRoute: (alias: string, makeDefault?: boolean) => CommonServiceRoute
|
|
15
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"../tsconfig.default.json",
|
|
4
|
+
"../tsconfig.react.json",
|
|
5
|
+
],
|
|
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"
|
|
10
|
+
},
|
|
11
|
+
"exclude": [
|
|
12
|
+
"./dist/**/*",
|
|
13
|
+
"./build/**/*",
|
|
14
|
+
"./*.ts"
|
|
15
|
+
]
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/config.ts","./src/consts.ts","./src/context.ts","./src/index.ts","./src/types.ts"],"version":"5.6.3"}
|