Skip to content

Commit

Permalink
fix: converting to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Dec 3, 2024
1 parent 909cad0 commit 2337c7b
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 81 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { CommonResponse } from './type';

const { ResponseStrategy } = require('./responseStrategy');

class CommonStrategy extends ResponseStrategy {
handleSuccess(responseParams) {
handleSuccess(responseParams: {
destinationResponse: { status: number; response: CommonResponse };
rudderJobMetadata: any[];
}): {
status: number;
message: string;
destinationResponse: { status: number; response: CommonResponse };
response: { statusCode: number; metadata: any; error: string }[];
} {
const { destinationResponse, rudderJobMetadata } = responseParams;
const { status } = destinationResponse;

Expand All @@ -20,4 +30,4 @@ class CommonStrategy extends ResponseStrategy {
}
}

module.exports = { CommonStrategy };
export { CommonStrategy };
32 changes: 0 additions & 32 deletions src/v1/destinations/iterable/networkHandler.js

This file was deleted.

38 changes: 38 additions & 0 deletions src/v1/destinations/iterable/networkHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { prepareProxyRequest, proxyRequest } from '../../../adapters/network';
import { processAxiosResponse } from '../../../adapters/utils/networkUtils';
import { BULK_ENDPOINTS } from '../../../v0/destinations/iterable/config';
import { CommonStrategy } from './commonStrategy';
import { TrackIdentifyStrategy } from './trackIdentifyStrategy';

interface ResponseParams {
destinationRequest: {
endpoint: string;
};
}

const strategyRegistry: { [key: string]: any } = {
[TrackIdentifyStrategy.name]: new TrackIdentifyStrategy(),
[CommonStrategy.name]: new CommonStrategy(),
};

const getResponseStrategy = (endpoint: string) => {
if (BULK_ENDPOINTS.some((path) => endpoint.includes(path))) {
return strategyRegistry[TrackIdentifyStrategy.name];
}
return strategyRegistry[CommonStrategy.name];
};

const responseHandler = (responseParams: ResponseParams) => {
const { destinationRequest } = responseParams;
const strategy = getResponseStrategy(destinationRequest.endpoint);
return strategy.handleResponse(responseParams);
};

function networkHandler(this: any) {
this.prepareProxy = prepareProxyRequest;
this.proxy = proxyRequest;
this.processAxiosResponse = processAxiosResponse;
this.responseHandler = responseHandler;
}

export { networkHandler };
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { CommonResponse } from './type';

const { isHttpStatusSuccess } = require('../../../v0/util/index');
const { TransformerProxyError } = require('../../../v0/util/errorTypes');
const { getDynamicErrorType } = require('../../../adapters/utils/networkUtils');
const tags = require('../../../v0/util/tags');

class ResponseStrategy {
handleResponse(responseParams) {
handleResponse(responseParams: {
destinationResponse: { status: number; response: CommonResponse };
}): void {
const { destinationResponse } = responseParams;
const { status } = destinationResponse;

if (!isHttpStatusSuccess(status)) {
return this.handleError(responseParams);
return this.handleError({
destinationResponse,
rudderJobMetadata: [],
});
}

return this.handleSuccess(responseParams);
}

handleError(responseParams) {
handleError(responseParams): void {
const { destinationResponse, rudderJobMetadata } = responseParams;
const { response, status } = destinationResponse;
const errorMessage = JSON.stringify(response.params) || 'unknown error format';
Expand All @@ -36,9 +43,9 @@ class ResponseStrategy {
);
}

handleSuccess(responseParams) {
handleSuccess(responseParams: any): void {
throw new TransformerProxyError(`success response handling is not added:${responseParams}`);
}
}

module.exports = { ResponseStrategy };
export { ResponseStrategy };
42 changes: 0 additions & 42 deletions src/v1/destinations/iterable/trackIdentifyStrategy.js

This file was deleted.

65 changes: 65 additions & 0 deletions src/v1/destinations/iterable/trackIdentifyStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { ResponseStrategy } from './responseStrategy';
import { checkIfEventIsAbortableAndExtractErrorMessage } from '../../../v0/destinations/iterable/util';
import { CommonResponse } from './type';

interface ResponseParams {
destinationResponse: { status: number; response: CommonResponse };
rudderJobMetadata: any[];
destinationRequest: {
body: {
JSON: {
events?: any[];
users?: any[];
};
};
};
}

class TrackIdentifyStrategy extends ResponseStrategy {
handleSuccess(responseParams: ResponseParams): {
status: number;
message: string;
destinationResponse: { status: number; response: CommonResponse };
response: Array<{ statusCode: number; metadata: any; error: string }>;
} {
const { destinationResponse, rudderJobMetadata, destinationRequest } = responseParams;
const { status } = destinationResponse;
const responseWithIndividualEvents: Array<{
statusCode: number;
metadata: any;
error: string;
}> = [];

const { events, users } = destinationRequest.body.JSON;
const finalData = events || users;

if (finalData) {
finalData.forEach((event, idx) => {
const proxyOutput = {
statusCode: 200,
metadata: rudderJobMetadata[idx],
error: 'success',
};

const { isAbortable, errorMsg } = checkIfEventIsAbortableAndExtractErrorMessage(
event,
destinationResponse,
);
if (isAbortable) {
proxyOutput.statusCode = 400;
proxyOutput.error = errorMsg;
}
responseWithIndividualEvents.push(proxyOutput);
});
}

return {
status,
message: '[ITERABLE Response Handler] - Request Processed Successfully',
destinationResponse,
response: responseWithIndividualEvents,
};
}
}

export { TrackIdentifyStrategy };
26 changes: 26 additions & 0 deletions src/v1/destinations/iterable/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
interface FailedUpdates {
invalidEmails?: string[];
invalidUserIds?: string[];
notFoundEmails?: string[];
notFoundUserIds?: string[];
invalidDataEmails?: string[];
invalidDataUserIds?: string[];
conflictEmails?: string[];
conflictUserIds?: string[];
forgottenEmails?: string[];
forgottenUserIds?: string[];
}

export interface CommonResponse {
msg?: string; // Optional since it's only in Response 1
code?: string; // Optional since it's only in Response 1
params?: Record<string, unknown>; // Optional, specific to Response 1
successCount?: number; // Shared by Response 2 and 3
failCount?: number; // Shared by Response 2 and 3
invalidEmails?: string[]; // Shared by Response 2 and 3
invalidUserIds?: string[]; // Shared by Response 2 and 3
filteredOutFields?: string[]; // Shared by Response 2 and 3
createdFields?: string[]; // Shared by Response 2 and 3
disallowedEventNames?: string[]; // Specific to Response 3
failedUpdates?: FailedUpdates; // Nested object for failed updates
}

0 comments on commit 2337c7b

Please sign in to comment.