Skip to content

Commit

Permalink
fix: added types
Browse files Browse the repository at this point in the history
  • Loading branch information
manish339k committed Feb 1, 2025
1 parent 3244922 commit 9745a44
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 50 deletions.
8 changes: 4 additions & 4 deletions src/v0/destinations/customerio_audience/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConfigurationError, isDefinedAndNotNullAndNotEmpty } from '@rudderstack/integrations-lib';
import { SegmentAction } from './config';
import { EventStructure, RespList } from './type';
import { CustomerIORouterRequestType, RespList } from './type';

const { InstrumentationError } = require('@rudderstack/integrations-lib');
const { batchResponseBuilder, getEventAction } = require('./utils');
Expand All @@ -11,7 +11,7 @@ interface ProcessedEvent extends RespList {
eventAction: keyof typeof SegmentAction;
}

const createEventChunk = (event: EventStructure): ProcessedEvent => {
const createEventChunk = (event: CustomerIORouterRequestType): ProcessedEvent => {
const eventAction = getEventAction(event);
const { identifiers } = event?.message || {};
const id: string | number = Object.values(identifiers)[0];
Expand All @@ -23,7 +23,7 @@ const createEventChunk = (event: EventStructure): ProcessedEvent => {
};
};

const validateEvent = (event: EventStructure): boolean => {
const validateEvent = (event: CustomerIORouterRequestType): boolean => {
const eventType = getEventType(event?.message);
if (eventType !== EventType.RECORD) {
throw new InstrumentationError(`message type ${eventType} is not supported`);
Expand Down Expand Up @@ -52,7 +52,7 @@ const validateEvent = (event: EventStructure): boolean => {
return true;
};

const processRouterDest = async (inputs: any[], reqMetadata: any) => {
const processRouterDest = async (inputs: CustomerIORouterRequestType[], reqMetadata: any) => {
if (!inputs?.length) return [];

const { destination, connection } = inputs[0];
Expand Down
94 changes: 63 additions & 31 deletions src/v0/destinations/customerio_audience/type.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
import { Connection, Destination, Metadata } from '../../../types';
import { FixMe } from '../../../util/types';

export type RespList = {
payload: {
ids: (string | number)[];
};
metadata: Record<string, unknown>;
};

export type EventStructure = {
message: {
action: string;
identifiers: Record<string, string | number>;
};
metadata: Record<string, unknown>;
destination: DestinationStructure;
connection: ConnectionStructure;
};

export type ConnectionStructure = {
config: {
destination: {
audienceId: string | number;
identifierMappings: {
from: string;
to: string;
}[];
};
};
};

export type DestinationStructure = {
Config: {
apiKey: string;
appApiKey: string;
siteId: string;
};
metadata: CustomerIOMetadataType;
};

export type SegmentationPayloadType = {
Expand All @@ -47,3 +20,62 @@ export type SegmentationHeadersType = {
'Content-Type': string;
Authorization: string;
};

type CIOConnectionConfigType = {
destination: {
audienceId: string | number;
identifierMappings: {
from: string;
to: string;
}[];
[key: string]: any;
};
[key: string]: any;
};

type CIODestinationConfigType = {
apiKey: string;
appApiKey: string;
siteId: string;
[key: string]: any;
};

type GenericRouterRequestData<
CIOMessage = object,
CIODestination = Destination,
CIOConnection = Connection,
> = {
message: CIOMessage;
metadata: Metadata;
destination: CIODestination;
connection: CIOConnection;
[key: string]: any;
};

type CIODestination<CIODestinationConfig = FixMe> = {
Config: CIODestinationConfig;
[key: string]: any;
};

type CIOConnection<CIOConnectionConfig = Record<string, unknown>> = {
config: CIOConnectionConfig;
[key: string]: any;
};

export type CustomerIOMessageType = {
action: string;
identifiers: Record<string, string | number>;
[key: string]: any;
};

export type CustomerIOMetadataType = Metadata;

export type CustomerIODestinationType = CIODestination<CIODestinationConfigType>;

export type CustomerIOConnectionType = CIOConnection<CIOConnectionConfigType>;

export type CustomerIORouterRequestType = GenericRouterRequestData<
CustomerIOMessageType,
CustomerIODestinationType,
CustomerIOConnectionType
>;
31 changes: 16 additions & 15 deletions src/v0/destinations/customerio_audience/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,45 @@ import { base64Convertor } from '@rudderstack/integrations-lib';
import { BatchUtils } from '@rudderstack/workflow-engine';
import { BASE_ENDPOINT, MAX_ITEMS } from './config';
import {
ConnectionStructure,
DestinationStructure,
EventStructure,
CustomerIOConnectionType,
CustomerIODestinationType,
CustomerIOMetadataType,
CustomerIORouterRequestType,
RespList,
SegmentationHeadersType,
SegmentationParamType,
SegmentationPayloadType,
} from './type';

const getIdType = (connection: ConnectionStructure): string =>
const getIdType = (connection: CustomerIOConnectionType): string =>
connection.config.destination.identifierMappings[0]?.to || 'id';

const getSegmentId = (connection: ConnectionStructure): string | number =>
const getSegmentId = (connection: CustomerIOConnectionType): string | number =>
connection.config.destination.audienceId;

const getHeaders = (destination: DestinationStructure): SegmentationHeadersType => ({
const getHeaders = (destination: CustomerIODestinationType): SegmentationHeadersType => ({
'Content-Type': 'application/json',
Authorization: `Basic ${base64Convertor(`${destination.Config.siteId}:${destination.Config.apiKey}`)}`,
});

const getParams = (connection: ConnectionStructure): SegmentationParamType => ({
const getParams = (connection: CustomerIOConnectionType): SegmentationParamType => ({
id_type: getIdType(connection),
});

const getMergedPayload = (batch: RespList[]): SegmentationPayloadType => ({
ids: batch.flatMap((input) => input.payload.ids),
});

const getMergedMetadata = (batch: RespList[]): Record<string, unknown>[] =>
const getMergedMetadata = (batch: RespList[]): CustomerIOMetadataType[] =>
batch.map((input) => input.metadata);

const buildBatchedResponse = (
payload: SegmentationPayloadType,
endpoint: string,
headers: SegmentationHeadersType,
params: SegmentationParamType,
metadata: Record<string, unknown>[],
destination: DestinationStructure,
metadata: CustomerIOMetadataType[],
destination: CustomerIODestinationType,
) => ({
batchedRequest: {
body: {
Expand All @@ -65,8 +66,8 @@ const buildBatchedResponse = (
const processBatch = (
respList: RespList[],
endpoint: string,
destination: DestinationStructure,
connection: ConnectionStructure,
destination: CustomerIODestinationType,
connection: CustomerIOConnectionType,
): any[] => {
if (!respList?.length) {
return [];

Check warning on line 73 in src/v0/destinations/customerio_audience/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/customerio_audience/utils.ts#L73

Added line #L73 was not covered by tests
Expand All @@ -93,8 +94,8 @@ const processBatch = (
const batchResponseBuilder = (
insertOrUpdateRespList: RespList[],
deleteRespList: RespList[],
destination: DestinationStructure,
connection: ConnectionStructure,
destination: CustomerIODestinationType,
connection: CustomerIOConnectionType,
): any[] => {
const segmentId = getSegmentId(connection);

Expand All @@ -115,7 +116,7 @@ const batchResponseBuilder = (
return [...insertResponses, ...deleteResponses];
};

const getEventAction = (event: EventStructure): string =>
const getEventAction = (event: CustomerIORouterRequestType): string =>
event?.message?.action?.toLowerCase() || '';

export { batchResponseBuilder, getEventAction };

0 comments on commit 9745a44

Please sign in to comment.