Skip to content

Commit

Permalink
data array
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 4, 2024
1 parent 8b17cdc commit 36caa95
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/destinations/web/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const destinationWebAPI: Destination = {

if (!url) return;

const value = isDefined(options.data) ? options.data : event;
const data = isDefined(options.data) ? options.data : event;
const value = Array.isArray(data) ? data[0] : data;
const body = transform
? transform(value, config, mapping) // Transform event data
: JSON.stringify(value);
Expand Down
2 changes: 1 addition & 1 deletion packages/destinations/web/api/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface Custom {
export interface CustomEvent {}

export type Transform = (
event: WalkerOS.Event | WalkerOS.Property,
event?: WalkerOS.Event | WalkerOS.Property,
config?: Config,
mapping?: DestinationWeb.EventMapping<CustomEvent>,
) => SendDataValue;
10 changes: 7 additions & 3 deletions packages/sources/node/src/lib/destination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WalkerOS } from '@elbwalker/types';
import type { Destination, WalkerOS } from '@elbwalker/types';
import type { SourceNode, DestinationNode } from '../types';
import {
debounce,
Expand Down Expand Up @@ -75,7 +75,7 @@ export async function destinationPush(
destination.config.mapping,
);

let data: WalkerOS.Property | undefined;
let data: Destination.Data;

if (eventMapping) {
// Check if event should be processed or ignored
Expand All @@ -85,7 +85,11 @@ export async function destinationPush(
if (eventMapping.name) event.event = eventMapping.name;

// Transform event to a custom data
if (eventMapping.data) data = getMappingValue(event, eventMapping.data);
if (eventMapping.data) {
data = Array.isArray(eventMapping.data)
? eventMapping.data.map((item) => getMappingValue(event, item))
: getMappingValue(event, eventMapping.data);
}
}

const options = { data, instance };
Expand Down
2 changes: 1 addition & 1 deletion packages/sources/node/src/types/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface EventMapping<CustomEvent = unknown>
extends WalkerOSMapping.EventConfig<CustomEvent> {}

export interface Options {
data?: WalkerOS.Property;
data?: WalkerOSDestination.Data;
instance?: SourceNode.Instance;
}

Expand Down
10 changes: 7 additions & 3 deletions packages/sources/walkerjs/src/lib/destination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { WalkerOS } from '@elbwalker/types';
import type { Destination, WalkerOS } from '@elbwalker/types';
import type { SourceWalkerjs, DestinationWeb } from '../types';
import {
debounce,
Expand Down Expand Up @@ -100,7 +100,7 @@ export function destinationPush(
destination.config.mapping,
);

let data: WalkerOS.Property | undefined;
let data: Destination.Data;

if (eventMapping) {
// Check if event should be processed or ignored
Expand All @@ -110,7 +110,11 @@ export function destinationPush(
if (eventMapping.name) event.event = eventMapping.name;

// Transform event to a custom data
if (eventMapping.data) data = getMappingValue(event, eventMapping.data);
if (eventMapping.data) {
data = Array.isArray(eventMapping.data)
? eventMapping.data.map((item) => getMappingValue(event, item))
: getMappingValue(event, eventMapping.data);
}
}

const options = { data, instance };
Expand Down
2 changes: 1 addition & 1 deletion packages/sources/walkerjs/src/types/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export interface EventMapping<CustomEvent = unknown>
extends WalkerOSMapping.EventConfig<CustomEvent> {}

export interface Options {
data?: WalkerOS.Property;
data?: WalkerOSDestination.Data;
instance?: SourceWalkerjs.Instance;
}
7 changes: 6 additions & 1 deletion packages/types/src/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,15 @@ export type PushBatchFn<Custom, CustomEvent> = (
export interface Batch<CustomEvent> {
key: string;
events: WalkerOS.Events;
data: WalkerOS.Property[];
data: Array<Data>;
mapping?: Mapping.EventConfig<CustomEvent>;
}

export interface Options {
instance?: WalkerOS.Instance;
}

export type Data =
| WalkerOS.Property
| undefined
| Array<WalkerOS.Property | undefined>;
3 changes: 2 additions & 1 deletion packages/types/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface EventConfig<CustomEvent = unknown> {
condition?: Condition; // Added condition
consent?: WalkerOS.Consent; // Required consent states process the event
custom?: CustomEvent; // Arbitrary but protected configurations for custom event config
data?: Value; // Mapping of event data
data?: Value | Values; // Mapping of event data
ignore?: boolean; // Choose to no process an event when set to true
name?: string; // Use a custom event name
}
Expand All @@ -27,6 +27,7 @@ export interface EventMapping {
}

export type Value = ValueType | Array<ValueType>;
export type Values = Array<Value>;
export type ValueType = string | ValueConfig;

export interface ValueConfig {
Expand Down

0 comments on commit 36caa95

Please sign in to comment.