@owlmeans/client-config 0.1.2 → 0.1.4

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 +20 -352
  2. package/package.json +4 -3
  3. package/tsconfig.json +5 -9
package/README.md CHANGED
@@ -1,373 +1,41 @@
1
1
  # @owlmeans/client-config
2
2
 
3
- Client-side configuration management library for OwlMeans Common applications. This package extends the base `@owlmeans/config` package with client-specific configuration capabilities including web service management, primary host/port settings, and service aliases.
4
-
5
- ## Installation
6
-
7
- ```bash
8
- npm install @owlmeans/client-config
9
- ```
10
-
11
- **Note**: This package depends on `@owlmeans/config` and extends its functionality. Make sure you have the proper OwlMeans Common ecosystem set up in your project.
3
+ Client-side configuration helper for registering web service endpoints.
12
4
 
13
5
  ## Overview
14
6
 
15
- The `@owlmeans/client-config` package is part of the OwlMeans Common libraries ecosystem and provides client-side configuration extensions. It follows the OwlMeans package structure with types, constants, and helpers specifically designed for client-side applications.
16
-
17
- **Important Note**: This package is not intended to be used separately from the OwlMeans Common ecosystem. It extends the functionality of `@owlmeans/config` and should be used in conjunction with it. The configuration objects created by this package build upon the configuration structure provided by `@owlmeans/config`.
18
-
19
- ### Key Features
20
-
21
- - **Web Service Management**: Configure and manage web services with aliases
22
- - **Primary Host/Port Settings**: Set primary host and port configurations
23
- - **Service Aliases**: Create short aliases for services
24
- - **Client-Side Extensions**: Extend base configuration with client-specific properties
25
- - **Seamless Integration**: Works seamlessly with the base `@owlmeans/config` package
26
-
27
- ## Core Concepts
28
-
29
- ### Client Configuration
30
-
31
- Client configurations extend the base `CommonConfig` from `@owlmeans/config` with client-specific properties that are relevant for client-side applications.
32
-
33
- ### Web Service Management
34
-
35
- The package provides functionality to manage web services, supporting both single service configurations and multiple aliased services for complex client applications.
7
+ - `addWebService(cfg, alias, url)` add a named web service URL to a client config
8
+ - Used when configuring which URLs the client uses to reach backend services
9
+ - Re-exported from `@owlmeans/server-app` for convenience in full-stack config setup
36
10
 
37
- ### Service Aliases
38
-
39
- Service aliases allow you to create short, memorable names for services, making it easier to reference them in client-side code.
40
-
41
- ## Quick Start
42
-
43
- ### Basic Client Configuration
44
-
45
- ```typescript
46
- import { BasicClientConfig, addWebService } from '@owlmeans/client-config'
47
- import { makeConfig } from '@owlmeans/config'
48
- import { AppType } from '@owlmeans/config/exports'
11
+ ## Installation
49
12
 
50
- // Create a client configuration
51
- const config: BasicClientConfig = makeConfig(AppType.Frontend, 'my-client', {
52
- webService: 'https://api.example.com',
53
- primaryHost: 'app.example.com',
54
- primaryPort: 443,
55
- shortAlias: 'myapp'
56
- })
13
+ ```bash
14
+ bun add @owlmeans/client-config
57
15
  ```
58
16
 
59
- ### Adding Web Services
17
+ ## Usage
60
18
 
61
19
  ```typescript
62
20
  import { addWebService } from '@owlmeans/client-config'
21
+ // or via server-app:
22
+ import { addWebService } from '@owlmeans/server-app'
63
23
 
64
- // Add a single web service
65
- const configWithService = addWebService('https://api.example.com', config)
66
-
67
- // Add a web service with alias
68
- const configWithAlias = addWebService('https://api.example.com', 'api', config)
69
-
70
- // Add multiple services with aliases
71
- let multiServiceConfig = addWebService('https://api.example.com', 'api', config)
72
- multiServiceConfig = addWebService('https://auth.example.com', 'auth', multiServiceConfig)
73
- ```
74
-
75
- ## API Reference
76
-
77
- ### Types
78
-
79
- #### `BasicClientConfig`
80
-
81
- Extends `CommonConfig` from `@owlmeans/config` with client-specific properties.
82
-
83
- ```typescript
84
- interface BasicClientConfig extends CommonConfig {
85
- webService?: string | Record<string, string>
86
- primaryHost?: string
87
- primaryPort?: number
88
- shortAlias?: string
89
- }
90
- ```
91
-
92
- **Properties:**
93
-
94
- - `webService` - Web service configuration. Can be a single service URL (string) or a record of aliased services
95
- - `primaryHost` - Primary host for the client application
96
- - `primaryPort` - Primary port for the client application
97
- - `shortAlias` - Short alias for the service, useful for client-side identification
98
-
99
- **Example:**
100
- ```typescript
101
- const clientConfig: BasicClientConfig = {
102
- // ... inherited from CommonConfig
103
- webService: {
104
- default: 'https://api.example.com',
105
- auth: 'https://auth.example.com',
106
- storage: 'https://storage.example.com'
107
- },
108
- primaryHost: 'app.example.com',
109
- primaryPort: 443,
110
- shortAlias: 'myapp'
111
- }
112
- ```
113
-
114
- ### Helper Functions
115
-
116
- #### `addWebService<C extends BasicClientConfig>(service: string, alias?: string | Partial<C>, cfg?: Partial<C>): C`
117
-
118
- Adds a web service to the client configuration. This function handles both single services and multiple aliased services.
119
-
120
- **Parameters:**
121
- - `service` - The web service URL to add
122
- - `alias` - Service alias (string) or partial configuration object
123
- - `cfg` - Optional partial configuration to extend
124
-
125
- **Returns:** Configuration with added web service
126
-
127
- **Behavior:**
128
- - If `alias` is not provided or is an object, sets the service as the default or single web service
129
- - If `alias` is a string, adds the service with the specified alias
130
- - Handles conversion between string and record-based web service configurations
131
- - Maintains existing services when adding new ones
132
-
133
- **Examples:**
134
-
135
- **Adding a single web service:**
136
- ```typescript
137
- const config = addWebService('https://api.example.com')
138
- // Result: { webService: 'https://api.example.com' }
139
- ```
140
-
141
- **Adding a web service with alias:**
142
- ```typescript
143
- const config = addWebService('https://api.example.com', 'api')
144
- // Result: { webService: { default: 'https://api.example.com', api: 'https://api.example.com' } }
145
- ```
146
-
147
- **Adding multiple services:**
148
- ```typescript
149
- let config = addWebService('https://api.example.com', 'api')
150
- config = addWebService('https://auth.example.com', 'auth', config)
151
- // Result: {
152
- // webService: {
153
- // default: 'https://api.example.com',
154
- // api: 'https://api.example.com',
155
- // auth: 'https://auth.example.com'
156
- // }
157
- // }
24
+ const appConfig = config(AppType.Frontend, 'manager-web')
25
+ addWebService(appConfig, 'api', 'https://api.example.com')
158
26
  ```
159
27
 
160
- **Adding service with existing string configuration:**
161
- ```typescript
162
- const baseConfig = { webService: 'https://base.example.com' }
163
- const config = addWebService('https://api.example.com', 'api', baseConfig)
164
- // Result: {
165
- // webService: {
166
- // default: 'https://base.example.com',
167
- // api: 'https://api.example.com'
168
- // }
169
- // }
170
- ```
171
-
172
- **Using partial configuration object:**
173
- ```typescript
174
- const config = addWebService('https://api.example.com', {
175
- primaryHost: 'app.example.com',
176
- shortAlias: 'myapp'
177
- })
178
- // Result: {
179
- // webService: 'https://api.example.com',
180
- // primaryHost: 'app.example.com',
181
- // shortAlias: 'myapp'
182
- // }
183
- ```
28
+ ## API
184
29
 
185
- ## Constants
30
+ ### `addWebService(cfg, alias, url): void`
186
31
 
187
- ### `DEFAULT_KEY`
32
+ Registers a web service under `alias` in the config. The client resolves `alias` to `url` when making API calls.
188
33
 
189
- Default key used for web service configurations when no alias is specified.
34
+ ### `ClientConfig`
190
35
 
191
- ```typescript
192
- const DEFAULT_KEY = 'default'
193
- ```
194
-
195
- **Usage:**
196
- ```typescript
197
- import { DEFAULT_KEY } from '@owlmeans/client-config'
198
-
199
- // Access default web service
200
- const defaultService = config.webService?.[DEFAULT_KEY]
201
- ```
202
-
203
- ## Usage Examples
204
-
205
- ### Complete Client Application Configuration
206
-
207
- ```typescript
208
- import {
209
- BasicClientConfig,
210
- addWebService,
211
- DEFAULT_KEY
212
- } from '@owlmeans/client-config'
213
- import { makeConfig, service } from '@owlmeans/config'
214
- import { AppType } from '@owlmeans/config/exports'
215
-
216
- // Create base client configuration
217
- let config: BasicClientConfig = makeConfig(AppType.Frontend, 'web-app', {
218
- debug: { enabled: true },
219
- brand: { home: '/dashboard' },
220
- primaryHost: 'app.example.com',
221
- primaryPort: 443,
222
- shortAlias: 'webapp'
223
- })
224
-
225
- // Add web services
226
- config = addWebService('https://api.example.com', 'api', config)
227
- config = addWebService('https://auth.example.com', 'auth', config)
228
- config = addWebService('https://storage.example.com', 'storage', config)
229
-
230
- // Add backend services (using base config functionality)
231
- config = service({
232
- service: 'websocket',
233
- host: 'ws.example.com',
234
- port: 8080,
235
- base: '/ws'
236
- }, config)
237
-
238
- console.log('API Service:', config.webService?.api)
239
- console.log('Auth Service:', config.webService?.auth)
240
- console.log('Default Service:', config.webService?.[DEFAULT_KEY])
241
- ```
242
-
243
- ### Dynamic Service Configuration
244
-
245
- ```typescript
246
- import { addWebService, BasicClientConfig } from '@owlmeans/client-config'
247
-
248
- class ClientConfigManager {
249
- private config: BasicClientConfig
250
-
251
- constructor(baseConfig: BasicClientConfig) {
252
- this.config = baseConfig
253
- }
254
-
255
- addService(url: string, alias: string): void {
256
- this.config = addWebService(url, alias, this.config)
257
- }
258
-
259
- getService(alias: string = DEFAULT_KEY): string | undefined {
260
- if (typeof this.config.webService === 'string') {
261
- return alias === DEFAULT_KEY ? this.config.webService : undefined
262
- }
263
- return this.config.webService?.[alias]
264
- }
265
-
266
- getAllServices(): Record<string, string> {
267
- if (typeof this.config.webService === 'string') {
268
- return { [DEFAULT_KEY]: this.config.webService }
269
- }
270
- return this.config.webService || {}
271
- }
272
- }
273
-
274
- // Usage
275
- const manager = new ClientConfigManager(baseConfig)
276
- manager.addService('https://api.example.com', 'api')
277
- manager.addService('https://auth.example.com', 'auth')
278
-
279
- const apiUrl = manager.getService('api')
280
- const allServices = manager.getAllServices()
281
- ```
282
-
283
- ### Environment-Specific Configuration
284
-
285
- ```typescript
286
- import { addWebService, BasicClientConfig } from '@owlmeans/client-config'
287
-
288
- function createEnvironmentConfig(env: 'development' | 'staging' | 'production'): BasicClientConfig {
289
- const hosts = {
290
- development: 'localhost:3000',
291
- staging: 'staging.example.com',
292
- production: 'app.example.com'
293
- }
294
-
295
- const apiUrls = {
296
- development: 'http://localhost:8000',
297
- staging: 'https://staging-api.example.com',
298
- production: 'https://api.example.com'
299
- }
300
-
301
- let config: BasicClientConfig = {
302
- primaryHost: hosts[env],
303
- primaryPort: env === 'development' ? 3000 : 443,
304
- shortAlias: `app-${env}`
305
- }
306
-
307
- // Add environment-specific services
308
- config = addWebService(apiUrls[env], 'api', config)
309
-
310
- if (env !== 'development') {
311
- config = addWebService(`https://${env}-auth.example.com`, 'auth', config)
312
- }
313
-
314
- return config
315
- }
316
-
317
- // Usage
318
- const devConfig = createEnvironmentConfig('development')
319
- const prodConfig = createEnvironmentConfig('production')
320
- ```
321
-
322
- ## Integration with OwlMeans Ecosystem
323
-
324
- The `@owlmeans/client-config` package integrates seamlessly with other OwlMeans packages:
325
-
326
- - **@owlmeans/config**: Provides base configuration functionality that this package extends
327
- - **@owlmeans/context**: Supports context management for client configurations
328
- - **@owlmeans/client**: Works with other client-side packages in the ecosystem
329
- - **@owlmeans/route**: Complements routing capabilities with client configuration
330
- - **@owlmeans/web-client**: Provides web-specific implementations
331
-
332
- ## Best Practices
333
-
334
- 1. **Service Organization**: Use aliases for different types of services (api, auth, storage, etc.)
335
-
336
- 2. **Environment Configuration**: Create environment-specific configurations for different deployment stages
337
-
338
- 3. **Type Safety**: Always use the `BasicClientConfig` type for type-safe configuration management
339
-
340
- 4. **Service Discovery**: Use meaningful aliases that reflect the service purpose
341
-
342
- 5. **Configuration Validation**: Validate service URLs and configuration before use
343
-
344
- 6. **Fallback Handling**: Always handle cases where services might not be configured
345
-
346
- ## Migration from Base Config
347
-
348
- If you're migrating from using only `@owlmeans/config`, here's how to adopt client-config:
349
-
350
- ```typescript
351
- // Before (base config only)
352
- import { makeConfig } from '@owlmeans/config'
353
-
354
- const config = makeConfig(AppType.Frontend, 'my-app', {
355
- // ... base configuration
356
- })
357
-
358
- // After (with client-config)
359
- import { makeConfig } from '@owlmeans/config'
360
- import { addWebService, BasicClientConfig } from '@owlmeans/client-config'
361
-
362
- let config: BasicClientConfig = makeConfig(AppType.Frontend, 'my-app', {
363
- // ... base configuration
364
- primaryHost: 'app.example.com',
365
- shortAlias: 'myapp'
366
- })
367
-
368
- config = addWebService('https://api.example.com', 'api', config)
369
- ```
36
+ Extends `BasicConfig` with `web?: Record<string, string>` (service alias → URL map).
370
37
 
371
- ## License
38
+ ## Related Packages
372
39
 
373
- This package is part of the OwlMeans Common libraries and follows the project's licensing terms.
40
+ - [`@owlmeans/config`](../config) base config utilities
41
+ - [`@owlmeans/client-context`](../client-context) — context that uses the web service config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@owlmeans/client-config",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -21,11 +21,12 @@
21
21
  }
22
22
  },
23
23
  "dependencies": {
24
- "@owlmeans/config": "^0.1.2"
24
+ "@owlmeans/config": "^0.1.4"
25
25
  },
26
26
  "devDependencies": {
27
+ "@owlmeans/dep-config": "workspace:*",
27
28
  "nodemon": "^3.1.11",
28
- "typescript": "^5.8.3"
29
+ "typescript": "^6.0.2"
29
30
  },
30
31
  "publishConfig": {
31
32
  "access": "public"
package/tsconfig.json CHANGED
@@ -1,14 +1,10 @@
1
1
  {
2
2
  "extends": [
3
- "../tsconfig.default.json",
3
+ "@owlmeans/dep-config/tsconfig.base.json"
4
4
  ],
5
5
  "compilerOptions": {
6
- "rootDir": "./src/", /* Specify the root folder within your source files. */
7
- "outDir": "./build/", /* Specify an output folder for all emitted files. */
6
+ "rootDir": "./src/",
7
+ "outDir": "./build/"
8
8
  },
9
- "exclude": [
10
- "./dist/**/*",
11
- "./build/**/*",
12
- "./*.ts"
13
- ]
14
- }
9
+ "exclude": ["./dist/**/*", "./build/**/*", "./*.ts"]
10
+ }