@startinblox/components-ds4go 2.1.1 → 2.2.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.
@@ -16,6 +16,8 @@
16
16
  * ```
17
17
  */
18
18
 
19
+ import type { Resource, UnknownResource } from "@src/component";
20
+
19
21
  // import type { Resource } from '@startinblox/core';
20
22
 
21
23
  // ============================================================================
@@ -46,7 +48,11 @@ export interface ResourceMapperConfig {
46
48
  /**
47
49
  * Optional custom post-processing function
48
50
  */
49
- postProcess?: (resource: Resource, source: any, context: MappingContext) => Resource;
51
+ postProcess?: (
52
+ resource: Resource,
53
+ source: any,
54
+ context: MappingContext,
55
+ ) => Resource;
50
56
  }
51
57
 
52
58
  export interface FieldMapping {
@@ -90,7 +96,10 @@ export interface ContainerFieldMapping {
90
96
  /**
91
97
  * Field mappings for each item
92
98
  */
93
- itemFields: Record<string, (item: any, index: number, context: MappingContext) => any>;
99
+ itemFields: Record<
100
+ string,
101
+ (item: any, index: number, context: MappingContext) => any
102
+ >;
94
103
 
95
104
  /**
96
105
  * Optional filter function to include/exclude items
@@ -143,10 +152,8 @@ export interface MappingContext {
143
152
  * Strips URN prefixes from IDs
144
153
  */
145
154
  export function stripUrnPrefix(value: string | undefined): string {
146
- if (!value) return '';
147
- return value
148
- .replace(/^urn:uuid:/i, '')
149
- .replace(/^urn:tems:/i, '');
155
+ if (!value) return "";
156
+ return value.replace(/^urn:uuid:/i, "").replace(/^urn:tems:/i, "");
150
157
  }
151
158
 
152
159
  /**
@@ -155,20 +162,20 @@ export function stripUrnPrefix(value: string | undefined): string {
155
162
  export function processPolicyTransform(
156
163
  policy: any,
157
164
  source: any,
158
- _context: MappingContext
165
+ _context: MappingContext,
159
166
  ): any {
160
167
  if (!policy) return null;
161
168
 
162
169
  const processedPolicy = JSON.parse(JSON.stringify(policy)); // Deep clone
163
170
 
164
171
  // Strip URN prefix from policy @id
165
- if (processedPolicy['@id']) {
166
- processedPolicy['@id'] = stripUrnPrefix(processedPolicy['@id']);
172
+ if (processedPolicy["@id"]) {
173
+ processedPolicy["@id"] = stripUrnPrefix(processedPolicy["@id"]);
167
174
  }
168
175
 
169
176
  // Add target field (asset ID)
170
- const assetId = getNestedValue(source, ['@id']) || '';
171
- processedPolicy['target'] = stripUrnPrefix(assetId);
177
+ const assetId = getNestedValue(source, ["@id"]) || "";
178
+ processedPolicy.target = stripUrnPrefix(assetId);
172
179
 
173
180
  return processedPolicy;
174
181
  }
@@ -186,8 +193,8 @@ export function firstOrSelf(value: any): any {
186
193
  export function generateIdFromName(name: string, base: string): string {
187
194
  const slug = name
188
195
  .toLowerCase()
189
- .replace(/[^a-z0-9]+/g, '-')
190
- .replace(/^-+|-+$/g, '');
196
+ .replace(/[^a-z0-9]+/g, "-")
197
+ .replace(/^-+|-+$/g, "");
191
198
  return `${base}${slug}`;
192
199
  }
193
200
 
@@ -248,7 +255,7 @@ export class ResourceMapper {
248
255
  * Maps source data to TEMS-compatible resource
249
256
  */
250
257
  map(source: any, context: MappingContext = {}): Resource {
251
- const resource: Resource = {};
258
+ const resource: UnknownResource = {};
252
259
 
253
260
  // Map base fields
254
261
  for (const [destKey, mapping] of Object.entries(this.config.baseFields)) {
@@ -260,7 +267,9 @@ export class ResourceMapper {
260
267
 
261
268
  // Map container fields
262
269
  if (this.config.containerFields) {
263
- for (const [destKey, mapping] of Object.entries(this.config.containerFields)) {
270
+ for (const [destKey, mapping] of Object.entries(
271
+ this.config.containerFields,
272
+ )) {
264
273
  const container = this.mapContainerField(source, mapping, context);
265
274
  if (container) {
266
275
  resource[destKey] = container;
@@ -270,7 +279,9 @@ export class ResourceMapper {
270
279
 
271
280
  // Map nested objects
272
281
  if (this.config.nestedObjects) {
273
- for (const [destKey, mapping] of Object.entries(this.config.nestedObjects)) {
282
+ for (const [destKey, mapping] of Object.entries(
283
+ this.config.nestedObjects,
284
+ )) {
274
285
  const nestedObj = this.mapNestedObject(source, mapping, context);
275
286
  if (nestedObj) {
276
287
  resource[destKey] = nestedObj;
@@ -280,7 +291,9 @@ export class ResourceMapper {
280
291
 
281
292
  // Map contract fields
282
293
  if (this.config.contractFields) {
283
- for (const [destKey, mapping] of Object.entries(this.config.contractFields)) {
294
+ for (const [destKey, mapping] of Object.entries(
295
+ this.config.contractFields,
296
+ )) {
284
297
  const value = this.mapField(source, mapping, context);
285
298
  if (value !== undefined) {
286
299
  resource[destKey] = value;
@@ -290,16 +303,20 @@ export class ResourceMapper {
290
303
 
291
304
  // Apply post-processing
292
305
  if (this.config.postProcess) {
293
- return this.config.postProcess(resource, source, context);
306
+ return this.config.postProcess((resource as Resource), source, context);
294
307
  }
295
308
 
296
- return resource;
309
+ return (resource as Resource);
297
310
  }
298
311
 
299
312
  /**
300
313
  * Maps a single field
301
314
  */
302
- private mapField(source: any, mapping: FieldMapping, context: MappingContext): any {
315
+ private mapField(
316
+ source: any,
317
+ mapping: FieldMapping,
318
+ context: MappingContext,
319
+ ): any {
303
320
  // Get source value
304
321
  let value = getNestedValue(source, mapping.source);
305
322
 
@@ -309,9 +326,9 @@ export class ResourceMapper {
309
326
  }
310
327
 
311
328
  // Apply fallback
312
- if (value === undefined || value === null || value === '') {
329
+ if (value === undefined || value === null || value === "") {
313
330
  if (mapping.fallback) {
314
- if (typeof mapping.fallback === 'function') {
331
+ if (typeof mapping.fallback === "function") {
315
332
  value = mapping.fallback(source, context);
316
333
  } else {
317
334
  // Fallback is a path to another field
@@ -321,7 +338,7 @@ export class ResourceMapper {
321
338
  }
322
339
 
323
340
  // Apply default value
324
- if (value === undefined || value === null || value === '') {
341
+ if (value === undefined || value === null || value === "") {
325
342
  value = mapping.defaultValue;
326
343
  }
327
344
 
@@ -334,7 +351,7 @@ export class ResourceMapper {
334
351
  private mapContainerField(
335
352
  source: any,
336
353
  mapping: ContainerFieldMapping,
337
- context: MappingContext
354
+ context: MappingContext,
338
355
  ): any {
339
356
  // Get source array
340
357
  let sourceArray = getNestedValue(source, mapping.source);
@@ -355,15 +372,15 @@ export class ResourceMapper {
355
372
 
356
373
  // Filter items
357
374
  if (mapping.filter) {
358
- sourceArray = sourceArray.filter((item, index) =>
359
- mapping.filter!(item, index, context)
375
+ sourceArray = sourceArray.filter((item: any, index: number) =>
376
+ mapping.filter!(item, index, context),
360
377
  );
361
378
  }
362
379
 
363
380
  // Map items
364
- const items = sourceArray.map((item, index) => {
381
+ const items = sourceArray.map((item: any, index: number) => {
365
382
  const mappedItem: any = {
366
- '@type': mapping.itemType,
383
+ "@type": mapping.itemType,
367
384
  };
368
385
 
369
386
  for (const [fieldKey, fieldFn] of Object.entries(mapping.itemFields)) {
@@ -378,8 +395,8 @@ export class ResourceMapper {
378
395
 
379
396
  // Return LDP container
380
397
  return {
381
- '@type': mapping.containerType,
382
- 'ldp:contains': items,
398
+ "@type": mapping.containerType,
399
+ "ldp:contains": items,
383
400
  };
384
401
  }
385
402
 
@@ -389,7 +406,7 @@ export class ResourceMapper {
389
406
  private mapNestedObject(
390
407
  source: any,
391
408
  mapping: NestedObjectMapping,
392
- context: MappingContext
409
+ context: MappingContext,
393
410
  ): any {
394
411
  // Get source object
395
412
  const sourceObj = getNestedValue(source, mapping.source);
@@ -402,7 +419,7 @@ export class ResourceMapper {
402
419
 
403
420
  // Add type if specified
404
421
  if (mapping.type) {
405
- nestedObj['@type'] = mapping.type;
422
+ nestedObj["@type"] = mapping.type;
406
423
  }
407
424
 
408
425
  // Map fields
@@ -422,7 +439,9 @@ export class ResourceMapper {
422
439
  }
423
440
  }
424
441
 
425
- return Object.keys(nestedObj).length > (mapping.type ? 1 : 0) ? nestedObj : null;
442
+ return Object.keys(nestedObj).length > (mapping.type ? 1 : 0)
443
+ ? nestedObj
444
+ : null;
426
445
  }
427
446
 
428
447
  /**
@@ -437,11 +456,11 @@ export class ResourceMapper {
437
456
  for (const [key, value] of Object.entries(unwrapped)) {
438
457
  if (
439
458
  value &&
440
- typeof value === 'object' &&
441
- value['@type'] === 'ldp:Container' &&
442
- value['ldp:contains']
459
+ typeof value === "object" &&
460
+ value["@type"] === "ldp:Container" &&
461
+ value["ldp:contains"]
443
462
  ) {
444
- unwrapped[key] = value['ldp:contains'];
463
+ unwrapped[key] = value["ldp:contains"];
445
464
  }
446
465
  }
447
466
 
@@ -1,8 +1,12 @@
1
+ import {
2
+ StoreService,
3
+ StoreType,
4
+ } from "https://cdn.jsdelivr.net/npm/@startinblox/core@beta/+esm";
1
5
  import { OrbitComponent, setupComponentSubscriptions } from "@helpers";
2
6
  import { ResourceMapper } from "@helpers/components/ResourceMapper";
3
7
  import { dspMappingConfig } from "@helpers/mappings/dsp-mapping-config";
4
- import type { Resource } from "@src/component.d.ts";
5
- import { StoreService, StoreType } from "@startinblox/core";
8
+ import type { DSPComponentParameters, Resource } from "@src/component.d.ts";
9
+ import type { PropertyValues } from "lit";
6
10
  import { property, state } from "lit/decorators.js";
7
11
 
8
12
  export interface DSPProviderConfig {
@@ -22,37 +26,30 @@ export default class extends OrbitComponent {
22
26
  @property({ attribute: "participant-api-key", reflect: true })
23
27
  participantApiKey?: string;
24
28
 
25
- @property({ attribute: "providers", type: Array })
26
- private _providers: DSPProviderConfig[] = [];
27
-
28
- public get providers(): DSPProviderConfig[] {
29
- return this._providers;
30
- }
31
-
32
- public set providers(value: DSPProviderConfig[]) {
33
- if (typeof value === "string") {
34
- try {
35
- this._providers = JSON.parse(value);
36
- } catch (_e) {
37
- this._providers = [];
38
- }
39
- } else {
40
- this._providers = value || [];
41
- }
42
- }
43
-
44
29
  @property({ attribute: "api-gateway-config", reflect: true })
45
30
  apiGatewayConfig?: string;
46
31
 
47
32
  @state()
48
- storeService?: any;
33
+ storeService?: StoreService;
49
34
 
50
35
  @state()
51
- dspStoreService?: any;
36
+ dspStoreService?: StoreService;
52
37
 
53
38
  @state()
54
39
  _apiGatewayConfigParsed?: any;
55
40
 
41
+ @property({ type: Array })
42
+ providers: DSPProviderConfig[] = [];
43
+
44
+ willUpdate(_changedProperties: PropertyValues<this>) {
45
+ if (
46
+ (!this.providers || this.providers.length === 0) &&
47
+ this.component?.parameters?.providers
48
+ ) {
49
+ this.providers = this.component.parameters.providers || [];
50
+ }
51
+ }
52
+
56
53
  protected async _attach(
57
54
  defaultRoute: boolean,
58
55
  setupSubscriptions: boolean,
@@ -81,7 +78,7 @@ export default class extends OrbitComponent {
81
78
 
82
79
  // Get connector configuration from attributes or component params
83
80
  if (!this.participantConnectorUri) {
84
- const params = this.component?.parameters as any;
81
+ const params = this.component?.parameters as DSPComponentParameters;
85
82
  if (params?.["participant-connector-uri"]) {
86
83
  this.participantConnectorUri =
87
84
  params["participant-connector-uri"];
@@ -98,7 +95,7 @@ export default class extends OrbitComponent {
98
95
  }
99
96
 
100
97
  // Parse API Gateway configuration (OPTIONAL)
101
- const params = this.component?.parameters as any;
98
+ const params = this.component?.parameters as DSPComponentParameters;
102
99
 
103
100
  if (params?.["api-gateway-config"]) {
104
101
  this.apiGatewayConfig = params["api-gateway-config"];
@@ -1,7 +1,7 @@
1
1
  import { ComponentObjectHandler } from "@helpers/components/componentObjectHandler";
2
2
  import { ComponentObjectsHandler } from "@helpers/components/componentObjectsHandler";
3
- // import dspComponent from "@helpers/components/dspComponent";
4
3
  import OrbitComponent from "@helpers/components/orbitComponent";
4
+ import dspComponent from "@helpers/components/orbitDspComponent";
5
5
  import setupCacheInvalidation from "@helpers/components/setupCacheInvalidation";
6
6
  import setupCacheOnResourceReady from "@helpers/components/setupCacheOnResourceReady";
7
7
  import setupComponentSubscriptions from "@helpers/components/setupComponentSubscriptions";
@@ -20,7 +20,7 @@ import uniq from "@helpers/utils/uniq";
20
20
  import CLIENT_CONTEXT from "@src/context.json";
21
21
 
22
22
  export {
23
- // dspComponent,
23
+ dspComponent,
24
24
  CLIENT_CONTEXT,
25
25
  filterGenerator,
26
26
  filterObjectByDateAfter,
package/vite.config.ts CHANGED
@@ -38,11 +38,6 @@ export default defineConfig({
38
38
  "@stories": "/stories",
39
39
  "@styles": "/src/styles",
40
40
  "@src": "/src",
41
- "@startinblox/core":
42
- "https://cdn.jsdelivr.net/npm/@startinblox/core@beta/dist/index.js",
43
- // Node.js polyfills for sib-core
44
- stream: "stream-browserify",
45
- buffer: "buffer",
46
41
  },
47
42
  },
48
43
  });