From 2337c7bd07a9eabfc377450d4866f0010fb42867 Mon Sep 17 00:00:00 2001 From: shrouti1507 Date: Tue, 3 Dec 2024 15:46:02 +0530 Subject: [PATCH] fix: converting to typescript --- .../{commonStrategy.js => commonStrategy.ts} | 14 +++- .../destinations/iterable/networkHandler.js | 32 --------- .../destinations/iterable/networkHandler.ts | 38 +++++++++++ ...esponseStrategy.js => responseStrategy.ts} | 17 +++-- .../iterable/trackIdentifyStrategy.js | 42 ------------ .../iterable/trackIdentifyStrategy.ts | 65 +++++++++++++++++++ src/v1/destinations/iterable/type.ts | 26 ++++++++ 7 files changed, 153 insertions(+), 81 deletions(-) rename src/v1/destinations/iterable/{commonStrategy.js => commonStrategy.ts} (58%) delete mode 100644 src/v1/destinations/iterable/networkHandler.js create mode 100644 src/v1/destinations/iterable/networkHandler.ts rename src/v1/destinations/iterable/{responseStrategy.js => responseStrategy.ts} (77%) delete mode 100644 src/v1/destinations/iterable/trackIdentifyStrategy.js create mode 100644 src/v1/destinations/iterable/trackIdentifyStrategy.ts create mode 100644 src/v1/destinations/iterable/type.ts diff --git a/src/v1/destinations/iterable/commonStrategy.js b/src/v1/destinations/iterable/commonStrategy.ts similarity index 58% rename from src/v1/destinations/iterable/commonStrategy.js rename to src/v1/destinations/iterable/commonStrategy.ts index b98c0bac5e..5d6aa2a892 100644 --- a/src/v1/destinations/iterable/commonStrategy.js +++ b/src/v1/destinations/iterable/commonStrategy.ts @@ -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; @@ -20,4 +30,4 @@ class CommonStrategy extends ResponseStrategy { } } -module.exports = { CommonStrategy }; +export { CommonStrategy }; diff --git a/src/v1/destinations/iterable/networkHandler.js b/src/v1/destinations/iterable/networkHandler.js deleted file mode 100644 index 7b10b148bd..0000000000 --- a/src/v1/destinations/iterable/networkHandler.js +++ /dev/null @@ -1,32 +0,0 @@ -const { prepareProxyRequest, proxyRequest } = require('../../../adapters/network'); -const { processAxiosResponse } = require('../../../adapters/utils/networkUtils'); -const { BULK_ENDPOINTS } = require('../../../v0/destinations/iterable/config'); -const { CommonStrategy } = require('./commonStrategy'); -const { TrackIdentifyStrategy } = require('./trackIdentifyStrategy'); - -const strategyRegistry = { - [TrackIdentifyStrategy.name]: new TrackIdentifyStrategy(), - [CommonStrategy.name]: new CommonStrategy(), -}; - -const getResponseStrategy = (endpoint) => { - if (BULK_ENDPOINTS.some((path) => endpoint.includes(path))) { - return strategyRegistry[TrackIdentifyStrategy.name]; - } - return strategyRegistry[CommonStrategy.name]; -}; - -const responseHandler = (responseParams) => { - const { destinationRequest } = responseParams; - const strategy = getResponseStrategy(destinationRequest.endpoint); - return strategy.handleResponse(responseParams); -}; - -function networkHandler() { - this.prepareProxy = prepareProxyRequest; - this.proxy = proxyRequest; - this.processAxiosResponse = processAxiosResponse; - this.responseHandler = responseHandler; -} - -module.exports = { networkHandler }; diff --git a/src/v1/destinations/iterable/networkHandler.ts b/src/v1/destinations/iterable/networkHandler.ts new file mode 100644 index 0000000000..b409facc4f --- /dev/null +++ b/src/v1/destinations/iterable/networkHandler.ts @@ -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 }; diff --git a/src/v1/destinations/iterable/responseStrategy.js b/src/v1/destinations/iterable/responseStrategy.ts similarity index 77% rename from src/v1/destinations/iterable/responseStrategy.js rename to src/v1/destinations/iterable/responseStrategy.ts index 7277898a1e..cbbe305be5 100644 --- a/src/v1/destinations/iterable/responseStrategy.js +++ b/src/v1/destinations/iterable/responseStrategy.ts @@ -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'; @@ -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 }; diff --git a/src/v1/destinations/iterable/trackIdentifyStrategy.js b/src/v1/destinations/iterable/trackIdentifyStrategy.js deleted file mode 100644 index e040f4a40d..0000000000 --- a/src/v1/destinations/iterable/trackIdentifyStrategy.js +++ /dev/null @@ -1,42 +0,0 @@ -const { ResponseStrategy } = require('./responseStrategy'); -const { - checkIfEventIsAbortableAndExtractErrorMessage, -} = require('../../../v0/destinations/iterable/util'); - -class TrackIdentifyStrategy extends ResponseStrategy { - handleSuccess(responseParams) { - const { destinationResponse, rudderJobMetadata, destinationRequest } = responseParams; - const { status } = destinationResponse; - const responseWithIndividualEvents = []; - - const { events, users } = destinationRequest.body.JSON; - const finalData = events || users; - - 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, - }; - } -} - -module.exports = { TrackIdentifyStrategy }; diff --git a/src/v1/destinations/iterable/trackIdentifyStrategy.ts b/src/v1/destinations/iterable/trackIdentifyStrategy.ts new file mode 100644 index 0000000000..5ca5d4ae84 --- /dev/null +++ b/src/v1/destinations/iterable/trackIdentifyStrategy.ts @@ -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 }; diff --git a/src/v1/destinations/iterable/type.ts b/src/v1/destinations/iterable/type.ts new file mode 100644 index 0000000000..98d5d23190 --- /dev/null +++ b/src/v1/destinations/iterable/type.ts @@ -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; // 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 +}