@owlmeans/server-wl 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 +800 -0
- package/build/actions/index.d.ts +2 -0
- package/build/actions/index.d.ts.map +1 -0
- package/build/actions/index.js +2 -0
- package/build/actions/index.js.map +1 -0
- package/build/actions/provide.d.ts +3 -0
- package/build/actions/provide.d.ts.map +1 -0
- package/build/actions/provide.js +16 -0
- package/build/actions/provide.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +2 -0
- package/build/index.js.map +1 -0
- package/build/modules.d.ts +3 -0
- package/build/modules.d.ts.map +1 -0
- package/build/modules.js +6 -0
- package/build/modules.js.map +1 -0
- package/build/types.d.ts +18 -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 +38 -0
- package/src/actions/index.ts +2 -0
- package/src/actions/provide.ts +28 -0
- package/src/index.ts +3 -0
- package/src/modules.ts +9 -0
- package/src/types.ts +21 -0
- package/tsconfig.json +15 -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,800 @@
|
|
|
1
|
+
# @owlmeans/server-wl
|
|
2
|
+
|
|
3
|
+
Server-side whitelabeling functionality for OwlMeans Common Libraries. This package provides backend whitelabeling capabilities enabling server applications to deliver customized branding, theming, and content personalization based on entity-specific configurations.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The `@owlmeans/server-wl` package serves as the server-side implementation of the OwlMeans Whitelabeling Subsystem, designed for fullstack applications with focus on security and dynamic brand customization. It provides:
|
|
8
|
+
|
|
9
|
+
- **Server-side Whitelabeling**: Backend whitelabeling provider services and API endpoints
|
|
10
|
+
- **Entity-based Branding**: Dynamic brand customization based on entity identification
|
|
11
|
+
- **Multi-provider Architecture**: Support for multiple whitelabeling data providers (company info, styles, media, DNS)
|
|
12
|
+
- **API Integration**: RESTful endpoints for whitelabeling data retrieval
|
|
13
|
+
- **Identifier Resolution**: DNS and domain-based entity identification services
|
|
14
|
+
- **Security Integration**: Secure whitelabeling data access with OwlMeans authentication
|
|
15
|
+
|
|
16
|
+
This package follows the OwlMeans "quadra" pattern as the **server** implementation, complementing:
|
|
17
|
+
- **@owlmeans/wled**: Common whitelabeling declarations and base functionality *(base package)*
|
|
18
|
+
- **@owlmeans/client-wl**: Client-side whitelabeling implementation
|
|
19
|
+
- **@owlmeans/web-wl**: Web-specific whitelabeling implementation
|
|
20
|
+
- **@owlmeans/server-wl**: Server-side whitelabeling implementation *(this package)*
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @owlmeans/server-wl
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Dependencies
|
|
29
|
+
|
|
30
|
+
This package requires and integrates with:
|
|
31
|
+
- `@owlmeans/wled`: Core whitelabeling types and modules
|
|
32
|
+
- `@owlmeans/server-context`: Server context management
|
|
33
|
+
- `@owlmeans/server-module`: Server module system for whitelabeling APIs
|
|
34
|
+
- `@owlmeans/server-api`: Server API utilities for request handling
|
|
35
|
+
- `@owlmeans/context`: Context management and service registration
|
|
36
|
+
|
|
37
|
+
## Key Concepts
|
|
38
|
+
|
|
39
|
+
### Whitelabeling Providers
|
|
40
|
+
The package implements a provider pattern where different aspects of whitelabeling can be handled by separate services:
|
|
41
|
+
|
|
42
|
+
- **Company Information Provider**: Provides company details, branding information
|
|
43
|
+
- **Styles Provider**: Delivers custom colors, fonts, and visual styling
|
|
44
|
+
- **Media Provider**: Serves custom logos, images, and brand assets
|
|
45
|
+
- **DNS Provider**: Handles domain-based entity identification
|
|
46
|
+
|
|
47
|
+
### Entity Identification
|
|
48
|
+
Server-side entity identification enables:
|
|
49
|
+
- **Domain Resolution**: Map domains/hostnames to specific entities
|
|
50
|
+
- **Subdomain Routing**: Route based on subdomain patterns
|
|
51
|
+
- **Custom Identifiers**: Support for custom entity identification schemes
|
|
52
|
+
- **Fallback Handling**: Graceful handling of unrecognized entities
|
|
53
|
+
|
|
54
|
+
### API Integration
|
|
55
|
+
Provides RESTful endpoints for:
|
|
56
|
+
- **Whitelabeling Data Retrieval**: Get entity-specific branding data
|
|
57
|
+
- **Real-time Updates**: Dynamic branding updates without deployment
|
|
58
|
+
- **Caching Support**: Efficient caching of whitelabeling data
|
|
59
|
+
- **Validation**: Server-side validation of whitelabeling configurations
|
|
60
|
+
|
|
61
|
+
## API Reference
|
|
62
|
+
|
|
63
|
+
### Core Interfaces
|
|
64
|
+
|
|
65
|
+
#### `WlProvider`
|
|
66
|
+
|
|
67
|
+
Service interface for whitelabeling data providers.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
interface WlProvider extends InitializedService {
|
|
71
|
+
provide: (entityId: string) => Promise<ProvidedWL>
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Methods:**
|
|
76
|
+
|
|
77
|
+
**`provide(entityId: string): Promise<ProvidedWL>`**
|
|
78
|
+
- **Purpose**: Provide whitelabeling data for a specific entity
|
|
79
|
+
- **Parameters**: `entityId` - Unique entity identifier
|
|
80
|
+
- **Returns**: Promise resolving to whitelabeling data object
|
|
81
|
+
- **Behavior**: Retrieves and returns entity-specific whitelabeling configuration
|
|
82
|
+
|
|
83
|
+
#### `WlEntityIdentifier`
|
|
84
|
+
|
|
85
|
+
Service interface for entity identification.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
interface WlEntityIdentifier extends InitializedService {
|
|
89
|
+
identifyEntity: (identifier: string) => Promise<string | null>
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Methods:**
|
|
94
|
+
|
|
95
|
+
**`identifyEntity(identifier: string): Promise<string | null>`**
|
|
96
|
+
- **Purpose**: Identify entity from domain, hostname, or custom identifier
|
|
97
|
+
- **Parameters**: `identifier` - Domain, hostname, or custom identifier string
|
|
98
|
+
- **Returns**: Promise resolving to entity ID or null if not found
|
|
99
|
+
- **Behavior**: Maps identifier to entity ID using configured resolution strategy
|
|
100
|
+
|
|
101
|
+
#### `WlProviderAppend`
|
|
102
|
+
|
|
103
|
+
Configuration interface for whitelabeling provider setup.
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
interface WlProviderAppend {
|
|
107
|
+
wlProviders: string[] // Array of whitelabeling provider service names
|
|
108
|
+
wlIdentifierService?: string // Optional entity identifier service name
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### `Config`
|
|
113
|
+
|
|
114
|
+
Server configuration interface extending base server config with whitelabeling settings.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
interface Config extends ServerConfig, WlProviderAppend {
|
|
118
|
+
// Inherits all server configuration options
|
|
119
|
+
// Plus whitelabeling-specific configuration
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
#### `Context<C extends Config = Config>`
|
|
124
|
+
|
|
125
|
+
Server context interface with whitelabeling support.
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
interface Context<C extends Config = Config> extends ServerContext<C> {
|
|
129
|
+
// Inherits all server context functionality
|
|
130
|
+
// With typed configuration for whitelabeling
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Module Integration
|
|
135
|
+
|
|
136
|
+
The package automatically elevates base whitelabeling modules with server-specific handlers:
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
import { elevate } from '@owlmeans/server-module'
|
|
140
|
+
import { WL_PROVIDE, modules as wlModules } from '@owlmeans/wled'
|
|
141
|
+
import * as actions from './actions/index.js'
|
|
142
|
+
|
|
143
|
+
// Elevate base modules with server handlers
|
|
144
|
+
elevate(wlModules, WL_PROVIDE, actions.provide)
|
|
145
|
+
|
|
146
|
+
export const modules = wlModules as ServerModule<unknown>[]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Server Actions
|
|
150
|
+
|
|
151
|
+
#### `provide` Action Handler
|
|
152
|
+
|
|
153
|
+
Server-side handler for whitelabeling data provision requests.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const provide: RefedModuleHandler = handleParams<ProvideParams>(
|
|
157
|
+
async (params, ctx) => {
|
|
158
|
+
const context = assertContext(ctx, 'provide') as Context
|
|
159
|
+
|
|
160
|
+
// Optional DNS-based entity identification
|
|
161
|
+
const dns = context.cfg.wlIdentifierService == null ? undefined
|
|
162
|
+
: context.service<WlEntityIdentifier>(context.cfg.wlIdentifierService)
|
|
163
|
+
|
|
164
|
+
// Resolve entity ID from identifier
|
|
165
|
+
const entityId = dns != null
|
|
166
|
+
? await dns.identifyEntity(params.entity) ?? params.entity
|
|
167
|
+
: params.entity
|
|
168
|
+
|
|
169
|
+
// Collect data from all registered providers
|
|
170
|
+
const wl = Object.fromEntries(await Promise.all(
|
|
171
|
+
context.cfg.wlProviders.map(async provider => {
|
|
172
|
+
const srv = context.service<WlProvider>(provider)
|
|
173
|
+
return [provider, await srv.provide(entityId)]
|
|
174
|
+
})
|
|
175
|
+
))
|
|
176
|
+
|
|
177
|
+
return wl
|
|
178
|
+
}
|
|
179
|
+
)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Usage Examples
|
|
183
|
+
|
|
184
|
+
### Basic Server Whitelabeling Setup
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { makeServerContext } from '@owlmeans/server-context'
|
|
188
|
+
import { modules } from '@owlmeans/server-wl'
|
|
189
|
+
import type { Config, Context, WlProvider } from '@owlmeans/server-wl'
|
|
190
|
+
|
|
191
|
+
// Configure server with whitelabeling support
|
|
192
|
+
const config: Config = {
|
|
193
|
+
service: 'whitelabel-server',
|
|
194
|
+
type: AppType.Backend,
|
|
195
|
+
layer: Layer.Service,
|
|
196
|
+
wlProviders: ['company-provider', 'styles-provider', 'media-provider'],
|
|
197
|
+
wlIdentifierService: 'dns-identifier'
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const context: Context = makeServerContext(config)
|
|
201
|
+
|
|
202
|
+
// Register whitelabeling modules
|
|
203
|
+
context.registerModules(modules)
|
|
204
|
+
|
|
205
|
+
// Initialize context
|
|
206
|
+
await context.configure().init()
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Company Information Provider
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { createService } from '@owlmeans/context'
|
|
213
|
+
import type { WlProvider } from '@owlmeans/server-wl'
|
|
214
|
+
import type { CompanyInfo, ProvidedWL } from '@owlmeans/wled'
|
|
215
|
+
|
|
216
|
+
// Create company information provider
|
|
217
|
+
const companyProvider = createService<WlProvider>('company-provider', {
|
|
218
|
+
async provide(entityId: string): Promise<ProvidedWL<CompanyInfo>> {
|
|
219
|
+
try {
|
|
220
|
+
// Load company information from database
|
|
221
|
+
const company = await loadCompanyFromDatabase(entityId)
|
|
222
|
+
|
|
223
|
+
if (!company) {
|
|
224
|
+
return {
|
|
225
|
+
type: 'company',
|
|
226
|
+
exists: false
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
type: 'company',
|
|
232
|
+
exists: true,
|
|
233
|
+
entityId: company.id,
|
|
234
|
+
fullName: company.fullName,
|
|
235
|
+
shortName: company.shortName,
|
|
236
|
+
slug: company.slug,
|
|
237
|
+
description: company.description,
|
|
238
|
+
resource: 'company-info'
|
|
239
|
+
}
|
|
240
|
+
} catch (error) {
|
|
241
|
+
console.error(`Failed to load company info for entity ${entityId}:`, error)
|
|
242
|
+
return {
|
|
243
|
+
type: 'company',
|
|
244
|
+
exists: null // null indicates error
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}, (service) => async () => {
|
|
249
|
+
// Initialize database connection
|
|
250
|
+
await initializeDatabase()
|
|
251
|
+
service.initialized = true
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
context.registerService(companyProvider)
|
|
255
|
+
|
|
256
|
+
// Database loading function
|
|
257
|
+
const loadCompanyFromDatabase = async (entityId: string): Promise<CompanyInfo | null> => {
|
|
258
|
+
const query = 'SELECT * FROM companies WHERE entity_id = ?'
|
|
259
|
+
const result = await database.query(query, [entityId])
|
|
260
|
+
return result.rows[0] || null
|
|
261
|
+
}
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Custom Styles Provider
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
import type { WlProvider } from '@owlmeans/server-wl'
|
|
268
|
+
import type { CustomStyles, ProvidedWL } from '@owlmeans/wled'
|
|
269
|
+
|
|
270
|
+
const stylesProvider = createService<WlProvider>('styles-provider', {
|
|
271
|
+
async provide(entityId: string): Promise<ProvidedWL<CustomStyles>> {
|
|
272
|
+
try {
|
|
273
|
+
const styles = await loadStylesFromDatabase(entityId)
|
|
274
|
+
|
|
275
|
+
if (!styles) {
|
|
276
|
+
// Return default styles for unknown entities
|
|
277
|
+
return {
|
|
278
|
+
type: 'styles',
|
|
279
|
+
exists: false,
|
|
280
|
+
entityId,
|
|
281
|
+
font: {
|
|
282
|
+
fontFamily: 'Roboto',
|
|
283
|
+
basicSize: 14
|
|
284
|
+
},
|
|
285
|
+
colors: {
|
|
286
|
+
primaryColor: '#1976d2',
|
|
287
|
+
secondaryColor: '#dc004e',
|
|
288
|
+
primaryBackground: '#ffffff',
|
|
289
|
+
secondaryBackground: '#f5f5f5'
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
type: 'styles',
|
|
296
|
+
exists: true,
|
|
297
|
+
...styles
|
|
298
|
+
}
|
|
299
|
+
} catch (error) {
|
|
300
|
+
console.error(`Failed to load styles for entity ${entityId}:`, error)
|
|
301
|
+
return {
|
|
302
|
+
type: 'styles',
|
|
303
|
+
exists: null
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
context.registerService(stylesProvider)
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Media Assets Provider
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import type { WlProvider } from '@owlmeans/server-wl'
|
|
316
|
+
import type { CustomMedia, ProvidedWL } from '@owlmeans/wled'
|
|
317
|
+
|
|
318
|
+
const mediaProvider = createService<WlProvider>('media-provider', {
|
|
319
|
+
async provide(entityId: string): Promise<ProvidedWL<CustomMedia>> {
|
|
320
|
+
try {
|
|
321
|
+
const media = await loadMediaFromStorage(entityId)
|
|
322
|
+
|
|
323
|
+
return {
|
|
324
|
+
type: 'media',
|
|
325
|
+
exists: media !== null,
|
|
326
|
+
entityId,
|
|
327
|
+
brand: {
|
|
328
|
+
squareLogo: media?.squareLogo ? `/assets/${entityId}/square-logo.png` : undefined,
|
|
329
|
+
wideLogo: media?.wideLogo ? `/assets/${entityId}/wide-logo.png` : undefined
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
} catch (error) {
|
|
333
|
+
console.error(`Failed to load media for entity ${entityId}:`, error)
|
|
334
|
+
return {
|
|
335
|
+
type: 'media',
|
|
336
|
+
exists: null,
|
|
337
|
+
entityId,
|
|
338
|
+
brand: {}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
context.registerService(mediaProvider)
|
|
345
|
+
|
|
346
|
+
const loadMediaFromStorage = async (entityId: string) => {
|
|
347
|
+
// Check if media files exist in storage
|
|
348
|
+
const squareLogoExists = await fileExists(`/storage/assets/${entityId}/square-logo.png`)
|
|
349
|
+
const wideLogoExists = await fileExists(`/storage/assets/${entityId}/wide-logo.png`)
|
|
350
|
+
|
|
351
|
+
if (!squareLogoExists && !wideLogoExists) {
|
|
352
|
+
return null
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return {
|
|
356
|
+
squareLogo: squareLogoExists,
|
|
357
|
+
wideLogo: wideLogoExists
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
### DNS-based Entity Identifier
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
import type { WlEntityIdentifier } from '@owlmeans/server-wl'
|
|
366
|
+
|
|
367
|
+
const dnsIdentifier = createService<WlEntityIdentifier>('dns-identifier', {
|
|
368
|
+
async identifyEntity(identifier: string): Promise<string | null> {
|
|
369
|
+
try {
|
|
370
|
+
// Handle different identifier patterns
|
|
371
|
+
|
|
372
|
+
// Subdomain pattern: entity.example.com
|
|
373
|
+
const subdomainMatch = identifier.match(/^([a-zA-Z0-9-]+)\.example\.com$/)
|
|
374
|
+
if (subdomainMatch) {
|
|
375
|
+
const entitySlug = subdomainMatch[1]
|
|
376
|
+
return await resolveEntityBySlug(entitySlug)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Custom domain pattern
|
|
380
|
+
const customDomain = await resolveCustomDomain(identifier)
|
|
381
|
+
if (customDomain) {
|
|
382
|
+
return customDomain.entityId
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Direct entity ID
|
|
386
|
+
if (await isValidEntityId(identifier)) {
|
|
387
|
+
return identifier
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return null
|
|
391
|
+
} catch (error) {
|
|
392
|
+
console.error(`Failed to identify entity for ${identifier}:`, error)
|
|
393
|
+
return null
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
context.registerService(dnsIdentifier)
|
|
399
|
+
|
|
400
|
+
const resolveEntityBySlug = async (slug: string): Promise<string | null> => {
|
|
401
|
+
const query = 'SELECT entity_id FROM entities WHERE slug = ?'
|
|
402
|
+
const result = await database.query(query, [slug])
|
|
403
|
+
return result.rows[0]?.entity_id || null
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
const resolveCustomDomain = async (domain: string) => {
|
|
407
|
+
const query = 'SELECT entity_id FROM custom_domains WHERE domain = ?'
|
|
408
|
+
const result = await database.query(query, [domain])
|
|
409
|
+
return result.rows[0] || null
|
|
410
|
+
}
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
### Express.js Integration
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
import express from 'express'
|
|
417
|
+
import { handleApiRequest } from '@owlmeans/server-api'
|
|
418
|
+
|
|
419
|
+
const app = express()
|
|
420
|
+
|
|
421
|
+
// Whitelabeling endpoint
|
|
422
|
+
app.get('/api/whitelabel/:entity', async (req, res) => {
|
|
423
|
+
try {
|
|
424
|
+
const result = await handleApiRequest(context, 'wl-provide', {
|
|
425
|
+
entity: req.params.entity
|
|
426
|
+
})
|
|
427
|
+
|
|
428
|
+
res.json(result)
|
|
429
|
+
} catch (error) {
|
|
430
|
+
console.error('Whitelabeling API error:', error)
|
|
431
|
+
res.status(500).json({ error: 'Failed to load whitelabeling data' })
|
|
432
|
+
}
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
// Middleware for entity identification from hostname
|
|
436
|
+
app.use(async (req, res, next) => {
|
|
437
|
+
const hostname = req.hostname
|
|
438
|
+
const dnsService = context.service<WlEntityIdentifier>('dns-identifier')
|
|
439
|
+
|
|
440
|
+
const entityId = await dnsService.identifyEntity(hostname)
|
|
441
|
+
if (entityId) {
|
|
442
|
+
req.entityId = entityId
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
next()
|
|
446
|
+
})
|
|
447
|
+
|
|
448
|
+
// Dynamic branding endpoint
|
|
449
|
+
app.get('/api/branding', async (req, res) => {
|
|
450
|
+
const entityId = req.entityId || req.query.entity as string
|
|
451
|
+
|
|
452
|
+
if (!entityId) {
|
|
453
|
+
return res.status(400).json({ error: 'Entity not specified' })
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
try {
|
|
457
|
+
const brandingData = await getAllWhitelabelingData(entityId)
|
|
458
|
+
res.json(brandingData)
|
|
459
|
+
} catch (error) {
|
|
460
|
+
res.status(500).json({ error: 'Failed to load branding data' })
|
|
461
|
+
}
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
const getAllWhitelabelingData = async (entityId: string) => {
|
|
465
|
+
const providers = ['company-provider', 'styles-provider', 'media-provider']
|
|
466
|
+
const data = {}
|
|
467
|
+
|
|
468
|
+
for (const providerName of providers) {
|
|
469
|
+
const provider = context.service<WlProvider>(providerName)
|
|
470
|
+
data[providerName] = await provider.provide(entityId)
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return data
|
|
474
|
+
}
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
### Caching Implementation
|
|
478
|
+
|
|
479
|
+
```typescript
|
|
480
|
+
import NodeCache from 'node-cache'
|
|
481
|
+
|
|
482
|
+
// Cache whitelabeling data for performance
|
|
483
|
+
const wlCache = new NodeCache({ stdTTL: 300 }) // 5 minutes TTL
|
|
484
|
+
|
|
485
|
+
const cachedProvider = createService<WlProvider>('cached-company-provider', {
|
|
486
|
+
async provide(entityId: string): Promise<ProvidedWL<CompanyInfo>> {
|
|
487
|
+
const cacheKey = `company:${entityId}`
|
|
488
|
+
|
|
489
|
+
// Check cache first
|
|
490
|
+
const cached = wlCache.get<ProvidedWL<CompanyInfo>>(cacheKey)
|
|
491
|
+
if (cached) {
|
|
492
|
+
return cached
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Load from database
|
|
496
|
+
const data = await originalProvider.provide(entityId)
|
|
497
|
+
|
|
498
|
+
// Cache only successful results
|
|
499
|
+
if (data.exists === true) {
|
|
500
|
+
wlCache.set(cacheKey, data)
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return data
|
|
504
|
+
}
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
// Cache invalidation on updates
|
|
508
|
+
const invalidateWhitelabelingCache = (entityId: string) => {
|
|
509
|
+
const keys = wlCache.keys().filter(key => key.includes(entityId))
|
|
510
|
+
keys.forEach(key => wlCache.del(key))
|
|
511
|
+
}
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
### Multi-tenant Configuration
|
|
515
|
+
|
|
516
|
+
```typescript
|
|
517
|
+
// Multi-tenant server configuration
|
|
518
|
+
const multiTenantConfig: Config = {
|
|
519
|
+
service: 'multi-tenant-server',
|
|
520
|
+
type: AppType.Backend,
|
|
521
|
+
layer: Layer.System,
|
|
522
|
+
wlProviders: [
|
|
523
|
+
'company-provider',
|
|
524
|
+
'styles-provider',
|
|
525
|
+
'media-provider',
|
|
526
|
+
'urls-provider'
|
|
527
|
+
],
|
|
528
|
+
wlIdentifierService: 'multi-tenant-dns'
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Advanced DNS identifier for multi-tenancy
|
|
532
|
+
const multiTenantDns = createService<WlEntityIdentifier>('multi-tenant-dns', {
|
|
533
|
+
async identifyEntity(identifier: string): Promise<string | null> {
|
|
534
|
+
// Priority order for entity identification:
|
|
535
|
+
|
|
536
|
+
// 1. Custom domain (highest priority)
|
|
537
|
+
const customEntity = await resolveCustomDomain(identifier)
|
|
538
|
+
if (customEntity) return customEntity.entityId
|
|
539
|
+
|
|
540
|
+
// 2. Subdomain pattern
|
|
541
|
+
const subdomainEntity = await resolveSubdomain(identifier)
|
|
542
|
+
if (subdomainEntity) return subdomainEntity
|
|
543
|
+
|
|
544
|
+
// 3. Path-based routing (from referrer or context)
|
|
545
|
+
const pathEntity = await resolveFromPath(identifier)
|
|
546
|
+
if (pathEntity) return pathEntity
|
|
547
|
+
|
|
548
|
+
// 4. Default entity for main domain
|
|
549
|
+
if (identifier === 'example.com') {
|
|
550
|
+
return 'default-entity'
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
return null
|
|
554
|
+
}
|
|
555
|
+
})
|
|
556
|
+
```
|
|
557
|
+
|
|
558
|
+
## Error Handling
|
|
559
|
+
|
|
560
|
+
### Provider Error Handling
|
|
561
|
+
|
|
562
|
+
```typescript
|
|
563
|
+
import { ResilientError } from '@owlmeans/error'
|
|
564
|
+
|
|
565
|
+
// Custom whitelabeling errors
|
|
566
|
+
export class WhitelabelingProviderError extends ResilientError {
|
|
567
|
+
constructor(provider: string, entityId: string, originalError: Error) {
|
|
568
|
+
super('WL_PROVIDER_ERROR', `Whitelabeling provider ${provider} failed for entity ${entityId}`, {
|
|
569
|
+
provider, entityId, originalError: originalError.message
|
|
570
|
+
})
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
export class EntityNotFoundError extends ResilientError {
|
|
575
|
+
constructor(identifier: string) {
|
|
576
|
+
super('WL_ENTITY_NOT_FOUND', `Entity not found for identifier: ${identifier}`, {
|
|
577
|
+
identifier
|
|
578
|
+
})
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// Error-resilient provider wrapper
|
|
583
|
+
const resilientProvider = (baseProvider: WlProvider, providerName: string): WlProvider => ({
|
|
584
|
+
...baseProvider,
|
|
585
|
+
async provide(entityId: string): Promise<ProvidedWL> {
|
|
586
|
+
try {
|
|
587
|
+
return await baseProvider.provide(entityId)
|
|
588
|
+
} catch (error) {
|
|
589
|
+
console.error(`Provider ${providerName} failed for entity ${entityId}:`, error)
|
|
590
|
+
|
|
591
|
+
// Return error state instead of throwing
|
|
592
|
+
return {
|
|
593
|
+
type: providerName,
|
|
594
|
+
exists: null, // null indicates error
|
|
595
|
+
error: error.message
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
})
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
### Graceful Degradation
|
|
603
|
+
|
|
604
|
+
```typescript
|
|
605
|
+
// Graceful degradation for missing providers
|
|
606
|
+
const handleWhitelabelingRequest = async (entityId: string) => {
|
|
607
|
+
const results = {}
|
|
608
|
+
const errors = []
|
|
609
|
+
|
|
610
|
+
for (const providerName of config.wlProviders) {
|
|
611
|
+
try {
|
|
612
|
+
const provider = context.service<WlProvider>(providerName)
|
|
613
|
+
results[providerName] = await provider.provide(entityId)
|
|
614
|
+
} catch (error) {
|
|
615
|
+
errors.push({ provider: providerName, error: error.message })
|
|
616
|
+
|
|
617
|
+
// Provide default/fallback data
|
|
618
|
+
results[providerName] = getDefaultWhitelabelingData(providerName, entityId)
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
return {
|
|
623
|
+
data: results,
|
|
624
|
+
errors: errors.length > 0 ? errors : undefined,
|
|
625
|
+
success: errors.length === 0
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
```
|
|
629
|
+
|
|
630
|
+
## Security Considerations
|
|
631
|
+
|
|
632
|
+
### Access Control
|
|
633
|
+
|
|
634
|
+
```typescript
|
|
635
|
+
// Secure whitelabeling data access
|
|
636
|
+
const secureProvider = (baseProvider: WlProvider): WlProvider => ({
|
|
637
|
+
...baseProvider,
|
|
638
|
+
async provide(entityId: string): Promise<ProvidedWL> {
|
|
639
|
+
// Validate entity ID format
|
|
640
|
+
if (!isValidEntityId(entityId)) {
|
|
641
|
+
throw new Error('Invalid entity ID format')
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Check access permissions
|
|
645
|
+
if (!await hasWhitelabelingAccess(entityId)) {
|
|
646
|
+
throw new Error('Insufficient permissions for whitelabeling data')
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return baseProvider.provide(entityId)
|
|
650
|
+
}
|
|
651
|
+
})
|
|
652
|
+
|
|
653
|
+
const isValidEntityId = (entityId: string): boolean => {
|
|
654
|
+
return /^[a-zA-Z0-9-_]{1,50}$/.test(entityId)
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const hasWhitelabelingAccess = async (entityId: string): Promise<boolean> => {
|
|
658
|
+
// Implement access control logic
|
|
659
|
+
return true // Placeholder
|
|
660
|
+
}
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
### Data Validation
|
|
664
|
+
|
|
665
|
+
```typescript
|
|
666
|
+
import { validate } from 'ajv'
|
|
667
|
+
import { CompanyInfoSchema, CustomStylesSchema } from '@owlmeans/wled'
|
|
668
|
+
|
|
669
|
+
// Validate whitelabeling data before serving
|
|
670
|
+
const validateWhitelabelingData = (data: any, type: string): boolean => {
|
|
671
|
+
switch (type) {
|
|
672
|
+
case 'company':
|
|
673
|
+
return validate(CompanyInfoSchema, data)
|
|
674
|
+
case 'styles':
|
|
675
|
+
return validate(CustomStylesSchema, data)
|
|
676
|
+
default:
|
|
677
|
+
return true
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
```
|
|
681
|
+
|
|
682
|
+
## Performance Optimization
|
|
683
|
+
|
|
684
|
+
### Connection Pooling
|
|
685
|
+
|
|
686
|
+
```typescript
|
|
687
|
+
// Database connection pooling for providers
|
|
688
|
+
import { Pool } from 'pg'
|
|
689
|
+
|
|
690
|
+
const dbPool = new Pool({
|
|
691
|
+
connectionString: process.env.DATABASE_URL,
|
|
692
|
+
max: 20,
|
|
693
|
+
idleTimeoutMillis: 30000,
|
|
694
|
+
connectionTimeoutMillis: 2000
|
|
695
|
+
})
|
|
696
|
+
|
|
697
|
+
const optimizedProvider = createService<WlProvider>('optimized-provider', {
|
|
698
|
+
async provide(entityId: string): Promise<ProvidedWL> {
|
|
699
|
+
const client = await dbPool.connect()
|
|
700
|
+
try {
|
|
701
|
+
const result = await client.query('SELECT * FROM whitelabeling WHERE entity_id = $1', [entityId])
|
|
702
|
+
return formatWhitelabelingData(result.rows[0])
|
|
703
|
+
} finally {
|
|
704
|
+
client.release()
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
})
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
### Batch Loading
|
|
711
|
+
|
|
712
|
+
```typescript
|
|
713
|
+
// Batch loading for multiple entities
|
|
714
|
+
const batchProvider = createService<WlProvider>('batch-provider', {
|
|
715
|
+
private cache: Map<string, Promise<ProvidedWL>> = new Map()
|
|
716
|
+
|
|
717
|
+
async provide(entityId: string): Promise<ProvidedWL> {
|
|
718
|
+
// Return existing promise if already loading
|
|
719
|
+
if (this.cache.has(entityId)) {
|
|
720
|
+
return this.cache.get(entityId)!
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// Create new loading promise
|
|
724
|
+
const promise = this.loadWhitelabelingData(entityId)
|
|
725
|
+
this.cache.set(entityId, promise)
|
|
726
|
+
|
|
727
|
+
// Clean up cache after resolution
|
|
728
|
+
promise.finally(() => {
|
|
729
|
+
setTimeout(() => this.cache.delete(entityId), 60000) // 1 minute cleanup
|
|
730
|
+
})
|
|
731
|
+
|
|
732
|
+
return promise
|
|
733
|
+
},
|
|
734
|
+
|
|
735
|
+
private async loadWhitelabelingData(entityId: string): Promise<ProvidedWL> {
|
|
736
|
+
// Implementation
|
|
737
|
+
}
|
|
738
|
+
})
|
|
739
|
+
```
|
|
740
|
+
|
|
741
|
+
## Integration with OwlMeans Ecosystem
|
|
742
|
+
|
|
743
|
+
### Context Integration
|
|
744
|
+
```typescript
|
|
745
|
+
import { makeServerContext } from '@owlmeans/server-context'
|
|
746
|
+
|
|
747
|
+
const context = makeServerContext(config)
|
|
748
|
+
const wlProvider = context.service<WlProvider>('company-provider')
|
|
749
|
+
```
|
|
750
|
+
|
|
751
|
+
### Module System Integration
|
|
752
|
+
```typescript
|
|
753
|
+
import { modules } from '@owlmeans/server-wl'
|
|
754
|
+
|
|
755
|
+
// Register whitelabeling modules
|
|
756
|
+
context.registerModules(modules)
|
|
757
|
+
```
|
|
758
|
+
|
|
759
|
+
### API Integration
|
|
760
|
+
```typescript
|
|
761
|
+
import { handleApiRequest } from '@owlmeans/server-api'
|
|
762
|
+
|
|
763
|
+
// Handle whitelabeling API requests
|
|
764
|
+
const result = await handleApiRequest(context, 'wl-provide', { entity: entityId })
|
|
765
|
+
```
|
|
766
|
+
|
|
767
|
+
## Best Practices
|
|
768
|
+
|
|
769
|
+
1. **Provider Design**: Keep providers focused and single-responsibility
|
|
770
|
+
2. **Caching Strategy**: Implement appropriate caching with TTL for performance
|
|
771
|
+
3. **Error Handling**: Provide graceful degradation for missing data
|
|
772
|
+
4. **Security**: Validate entity IDs and implement access controls
|
|
773
|
+
5. **Performance**: Use connection pooling and batch loading where appropriate
|
|
774
|
+
6. **Monitoring**: Log provider performance and error rates
|
|
775
|
+
7. **Fallbacks**: Always provide sensible defaults for missing configurations
|
|
776
|
+
|
|
777
|
+
## Related Packages
|
|
778
|
+
|
|
779
|
+
- **@owlmeans/wled**: Core whitelabeling types and modules
|
|
780
|
+
- **@owlmeans/client-wl**: Client-side whitelabeling implementation
|
|
781
|
+
- **@owlmeans/web-wl**: Web-specific whitelabeling implementation
|
|
782
|
+
- **@owlmeans/server-context**: Server context management
|
|
783
|
+
- **@owlmeans/server-module**: Server module system
|
|
784
|
+
- **@owlmeans/server-api**: Server API utilities
|
|
785
|
+
|
|
786
|
+
## TypeScript Support
|
|
787
|
+
|
|
788
|
+
This package is written in TypeScript and provides full type safety:
|
|
789
|
+
|
|
790
|
+
```typescript
|
|
791
|
+
import type {
|
|
792
|
+
WlProvider,
|
|
793
|
+
WlEntityIdentifier,
|
|
794
|
+
Config,
|
|
795
|
+
Context
|
|
796
|
+
} from '@owlmeans/server-wl'
|
|
797
|
+
|
|
798
|
+
const provider: WlProvider = createService('my-provider', { /* ... */ })
|
|
799
|
+
const context: Context = makeServerContext(config)
|
|
800
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/actions/index.ts"],"names":[],"mappings":"AACA,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/actions/index.ts"],"names":[],"mappings":"AACA,cAAc,cAAc,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provide.d.ts","sourceRoot":"","sources":["../../src/actions/provide.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAMjE,eAAO,MAAM,OAAO,EAAE,kBAqBrB,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { handleParams } from '@owlmeans/server-api';
|
|
2
|
+
import { assertContext } from '@owlmeans/context';
|
|
3
|
+
export const provide = handleParams(async (params, ctx) => {
|
|
4
|
+
const context = assertContext(ctx, 'provide');
|
|
5
|
+
const dns = context.cfg.wlIdentifierService == null ? undefined
|
|
6
|
+
: context.service(context.cfg.wlIdentifierService);
|
|
7
|
+
const entityId = dns != null
|
|
8
|
+
? await dns.identifyEntity(params.entity) ?? params.entity
|
|
9
|
+
: params.entity;
|
|
10
|
+
const wl = Object.fromEntries(await Promise.all(context.cfg.wlProviders.map(async (provider) => {
|
|
11
|
+
const srv = context.service(provider);
|
|
12
|
+
return [provider, await srv.provide(entityId)];
|
|
13
|
+
})));
|
|
14
|
+
return wl;
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=provide.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provide.js","sourceRoot":"","sources":["../../src/actions/provide.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAGjD,MAAM,CAAC,MAAM,OAAO,GAAuB,YAAY,CACrD,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE;IACpB,MAAM,OAAO,GAAG,aAAa,CAAC,GAAG,EAAE,SAAS,CAAY,CAAA;IAExD,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,IAAI,CAAC,CAAC,CAAC,SAAS;QAC7D,CAAC,CAAC,OAAO,CAAC,OAAO,CAAqB,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;IAExE,MAAM,QAAQ,GAAG,GAAG,IAAI,IAAI;QAC1B,CAAC,CAAC,MAAM,GAAG,CAAC,cAAc,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,MAAM;QAC1D,CAAC,CAAC,MAAM,CAAC,MAAM,CAAA;IAEjB,MAAM,EAAE,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,OAAO,CAAC,GAAG,CAC7C,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,KAAK,EAAC,QAAQ,EAAC,EAAE;QAC3C,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAa,QAAQ,CAAC,CAAA;QAEjD,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAA;IAChD,CAAC,CAAC,CACH,CAAC,CAAA;IAEF,OAAO,EAAE,CAAA;AACX,CAAC,CACF,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"}
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modules.d.ts","sourceRoot":"","sources":["../src/modules.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAA;AAM3D,eAAO,MAAM,OAAO,EAAgB,YAAY,CAAC,OAAO,CAAC,EAAE,CAAA"}
|
package/build/modules.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { elevate } from '@owlmeans/server-module';
|
|
2
|
+
import { WL_PROVIDE, modules as wlModules } from '@owlmeans/wled';
|
|
3
|
+
import * as actions from './actions/index.js';
|
|
4
|
+
elevate(wlModules, WL_PROVIDE, actions.provide);
|
|
5
|
+
export const modules = wlModules;
|
|
6
|
+
//# sourceMappingURL=modules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modules.js","sourceRoot":"","sources":["../src/modules.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAA;AAEjD,OAAO,EAAE,UAAU,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAA;AACjE,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAA;AAE7C,OAAO,CAAC,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;AAE/C,MAAM,CAAC,MAAM,OAAO,GAAG,SAAoC,CAAA"}
|
package/build/types.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { InitializedService } from '@owlmeans/context';
|
|
2
|
+
import type { ServerConfig, ServerContext } from '@owlmeans/server-context';
|
|
3
|
+
import type { ProvidedWL } from '@owlmeans/wled';
|
|
4
|
+
export interface WlProvider extends InitializedService {
|
|
5
|
+
provide: (entityId: string) => Promise<ProvidedWL>;
|
|
6
|
+
}
|
|
7
|
+
export interface WlEntityIdentifier extends InitializedService {
|
|
8
|
+
identifyEntity: (identifier: string) => Promise<string | null>;
|
|
9
|
+
}
|
|
10
|
+
export interface WlProviderAppend {
|
|
11
|
+
wlProviders: string[];
|
|
12
|
+
wlIdentifierService?: string;
|
|
13
|
+
}
|
|
14
|
+
export interface Config extends ServerConfig, WlProviderAppend {
|
|
15
|
+
}
|
|
16
|
+
export interface Context<C extends Config = Config> extends ServerContext<C> {
|
|
17
|
+
}
|
|
18
|
+
//# 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,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAA;AAC3E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAEhD,MAAM,WAAW,UAAW,SAAQ,kBAAkB;IACpD,OAAO,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,UAAU,CAAC,CAAA;CACnD;AAED,MAAM,WAAW,kBAAmB,SAAQ,kBAAkB;IAC5D,cAAc,EAAE,CAAC,UAAU,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAA;CAC/D;AAED,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC7B;AAED,MAAM,WAAW,MAAO,SAAQ,YAAY,EAAE,gBAAgB;CAAI;AAElE,MAAM,WAAW,OAAO,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,CAAE,SAAQ,aAAa,CAAC,CAAC,CAAC;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,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@owlmeans/server-wl",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"build": "tsc -b",
|
|
7
|
+
"dev": "sleep 330 && 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
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@owlmeans/context": "^0.1.0",
|
|
24
|
+
"@owlmeans/server-api": "^0.1.0",
|
|
25
|
+
"@owlmeans/server-context": "^0.1.0",
|
|
26
|
+
"@owlmeans/server-module": "^0.1.0",
|
|
27
|
+
"@owlmeans/wled": "^0.1.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^22.7.8",
|
|
31
|
+
"nodemon": "^3.1.7",
|
|
32
|
+
"typescript": "^5.6.3"
|
|
33
|
+
},
|
|
34
|
+
"private": false,
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { RefedModuleHandler } from '@owlmeans/server-module'
|
|
2
|
+
import { handleParams } from '@owlmeans/server-api'
|
|
3
|
+
import type { Context, WlEntityIdentifier, WlProvider } from '../types.js'
|
|
4
|
+
import { assertContext } from '@owlmeans/context'
|
|
5
|
+
import type { ProvideParams } from '@owlmeans/wled'
|
|
6
|
+
|
|
7
|
+
export const provide: RefedModuleHandler = handleParams<ProvideParams>(
|
|
8
|
+
async (params, ctx) => {
|
|
9
|
+
const context = assertContext(ctx, 'provide') as Context
|
|
10
|
+
|
|
11
|
+
const dns = context.cfg.wlIdentifierService == null ? undefined
|
|
12
|
+
: context.service<WlEntityIdentifier>(context.cfg.wlIdentifierService)
|
|
13
|
+
|
|
14
|
+
const entityId = dns != null
|
|
15
|
+
? await dns.identifyEntity(params.entity) ?? params.entity
|
|
16
|
+
: params.entity
|
|
17
|
+
|
|
18
|
+
const wl = Object.fromEntries(await Promise.all(
|
|
19
|
+
context.cfg.wlProviders.map(async provider => {
|
|
20
|
+
const srv = context.service<WlProvider>(provider)
|
|
21
|
+
|
|
22
|
+
return [provider, await srv.provide(entityId)]
|
|
23
|
+
})
|
|
24
|
+
))
|
|
25
|
+
|
|
26
|
+
return wl
|
|
27
|
+
}
|
|
28
|
+
)
|
package/src/index.ts
ADDED
package/src/modules.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
|
|
2
|
+
import { elevate } from '@owlmeans/server-module'
|
|
3
|
+
import type { ServerModule } from '@owlmeans/server-module'
|
|
4
|
+
import { WL_PROVIDE, modules as wlModules } from '@owlmeans/wled'
|
|
5
|
+
import * as actions from './actions/index.js'
|
|
6
|
+
|
|
7
|
+
elevate(wlModules, WL_PROVIDE, actions.provide)
|
|
8
|
+
|
|
9
|
+
export const modules = wlModules as ServerModule<unknown>[]
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { InitializedService } from '@owlmeans/context'
|
|
2
|
+
import type { ServerConfig, ServerContext } from '@owlmeans/server-context'
|
|
3
|
+
import type { ProvidedWL } from '@owlmeans/wled'
|
|
4
|
+
|
|
5
|
+
export interface WlProvider extends InitializedService {
|
|
6
|
+
provide: (entityId: string) => Promise<ProvidedWL>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface WlEntityIdentifier extends InitializedService {
|
|
10
|
+
identifyEntity: (identifier: string) => Promise<string | null>
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface WlProviderAppend {
|
|
14
|
+
wlProviders: string[]
|
|
15
|
+
wlIdentifierService?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Config extends ServerConfig, WlProviderAppend { }
|
|
19
|
+
|
|
20
|
+
export interface Context<C extends Config = Config> extends ServerContext<C> {
|
|
21
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"../tsconfig.default.json",
|
|
4
|
+
],
|
|
5
|
+
"compilerOptions": {
|
|
6
|
+
"rootDir": "./src/", /* Specify the root folder within your source files. */
|
|
7
|
+
"outDir": "./build/", /* Specify an output folder for all emitted files. */
|
|
8
|
+
"moduleResolution": "Bundler",
|
|
9
|
+
},
|
|
10
|
+
"exclude": [
|
|
11
|
+
"./dist/**/*",
|
|
12
|
+
"./build/**/*",
|
|
13
|
+
"./*.ts"
|
|
14
|
+
]
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"root":["./src/index.ts","./src/modules.ts","./src/types.ts","./src/actions/index.ts","./src/actions/provide.ts"],"version":"5.6.3"}
|