Skip to content

Commit

Permalink
chore: simplify types and generalise existing types (#4039)
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip authored Feb 3, 2025
2 parents a59fab2 + 4c452ae commit ab88c7b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 51 deletions.
16 changes: 10 additions & 6 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ type DestinationDefinition = {
Config: FixMe;
};

type Destination = {
type Destination<DestinationConfig = FixMe> = {
ID: string;
Name: string;
DestinationDefinition: DestinationDefinition;
Config: FixMe;
Config: DestinationConfig;
Enabled: boolean;
WorkspaceID: string;
Transformations: UserTransformationInput[];
Expand Down Expand Up @@ -164,12 +164,16 @@ type ProcessorTransformationRequest = {
credentials?: Credential[];
};

type RouterTransformationRequestData = {
type RouterTransformationRequestData<
Message = object,
DestinationType = Destination,
ConnectionType = Connection,
> = {
request?: object;
message: object;
message: Message;
metadata: Metadata;
destination: Destination;
connection?: Connection;
destination: DestinationType;
connection?: ConnectionType;
};

type RouterTransformationRequest = {
Expand Down
61 changes: 20 additions & 41 deletions src/v0/destinations/customerio_audience/type.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Connection, Destination, Metadata } from '../../../types';
import { FixMe } from '../../../util/types';
import { Connection, Destination, Metadata, RouterTransformationRequestData } from '../../../types';

// Basic response type for audience list operations
export type RespList = {
payload: {
ids: (string | number)[];
};
metadata: CustomerIOMetadataType;
metadata: Metadata;
};

// Types for API request components
export type SegmentationPayloadType = {
ids: (string | number)[];
};
Expand All @@ -21,60 +22,38 @@ export type SegmentationHeadersType = {
Authorization: string;
};

type CIOConnectionConfigType = {
// CustomerIO specific configuration types
type CustomerIODestinationConfig = {
apiKey: string;
appApiKey: string;
siteId: string;
[key: string]: any;
};

type CustomerIOConnectionConfig = {
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;
};

// Message type specific to CustomerIO
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>;
// Final exported types using generics from base types
export type CustomerIODestinationType = Destination<CustomerIODestinationConfig>;
export type CustomerIOConnectionType = Connection & {
config: CustomerIOConnectionConfig;
};

export type CustomerIORouterRequestType = GenericRouterRequestData<
export type CustomerIORouterRequestType = RouterTransformationRequestData<
CustomerIOMessageType,
CustomerIODestinationType,
CustomerIOConnectionType
Expand Down
7 changes: 3 additions & 4 deletions src/v0/destinations/customerio_audience/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { BASE_ENDPOINT, DEFAULT_ID_TYPE, MAX_ITEMS } from './config';
import {
CustomerIOConnectionType,
CustomerIODestinationType,
CustomerIOMetadataType,
CustomerIORouterRequestType,
RespList,
SegmentationHeadersType,
SegmentationParamType,
SegmentationPayloadType,
} from './type';
import { Metadata } from '../../../types';

const getIdType = (connection: CustomerIOConnectionType): string =>
connection.config.destination.identifierMappings[0]?.to || DEFAULT_ID_TYPE;
Expand All @@ -31,15 +31,14 @@ const getMergedPayload = (batch: RespList[]): SegmentationPayloadType => ({
ids: batch.flatMap((input) => input.payload.ids),
});

const getMergedMetadata = (batch: RespList[]): CustomerIOMetadataType[] =>
batch.map((input) => input.metadata);
const getMergedMetadata = (batch: RespList[]): Metadata[] => batch.map((input) => input.metadata);

const buildBatchedResponse = (
payload: SegmentationPayloadType,
endpoint: string,
headers: SegmentationHeadersType,
params: SegmentationParamType,
metadata: CustomerIOMetadataType[],
metadata: Metadata[],
destination: CustomerIODestinationType,
) => ({
batchedRequest: {
Expand Down

0 comments on commit ab88c7b

Please sign in to comment.