import { Response , Status } from '@amplitude/types' ; import { IncomingMessage } from 'http' ; import { mapHttpCodeToStatus } from './status' ; param response returns export const collectInvalidEventIndices = ( response ) : number [ ] => { const invalidEventIndices = new Set < number > ( ) ; if ( response . status === Status . Invalid && response . body !== undefined ) { const { eventsWithInvalidFields , eventsWithMissingFields } = response . body ; Object . keys ( eventsWithInvalidFields ) . forEach ( ( field ) => { const eventIndices = eventsWithInvalidFields [ field ] ?? [ ] ; eventIndices . forEach ( ( index ) => { invalidEventIndices . add ( index ) ; } ) ; } ) ; Object . keys ( eventsWithMissingFields ) . forEach ( ( field ) => { const eventIndices = eventsWithMissingFields [ field ] ?? [ ] ; eventIndices . forEach ( ( index ) => { invalidEventIndices . add ( index ) ; } ) ; } ) ; } return Array . from ( invalidEventIndices ) . sort ( ( numberOne , numberTwo ) => numberOne - numberTwo ) ; } ; param httpRes returns export const mapHttpMessageToResponse = ( httpRes ) : Response => { const statusCode = httpRes . statusCode === undefined ? 0 : httpRes . statusCode ; const status = mapHttpCodeToStatus ( statusCode ) ; return { status , statusCode , } ; } ; param responseJSON returns export const mapJSONToResponse = ( responseJSON ) : Response | null => { if ( typeof responseJSON !== 'object' ) { return null ; } const status = mapHttpCodeToStatus ( responseJSON . code ) ; const statusCode = responseJSON . code ; switch ( status ) { case Status . Success : return { status , statusCode , body : { eventsIngested : responseJSON . events_ingested , payloadSizeBytes : responseJSON . payload_size_bytes , serverUploadTime : responseJSON . server_upload_time , } , } ; case Status . Invalid : return { status , statusCode , body : { error : responseJSON . error ?? '' , missingField : responseJSON . missing_field ?? null , eventsWithInvalidFields : responseJSON . events_with_invalid_fields ?? { } , eventsWithMissingFields : responseJSON . events_with_missing_fields ?? { } , } , } ; case Status . PayloadTooLarge : return { status , statusCode , body : { error : responseJSON . error ?? '' , } , } ; case Status . RateLimit : return { status , statusCode , body : { error : responseJSON . error ?? '' , epsThreshold : responseJSON . eps_threshold , throttledDevices : responseJSON . throttled_devices ?? { } , throttledUsers : responseJSON . throttled_users ?? { } , exceededDailyQuotaDevices : responseJSON . exceeded_daily_quota_devices ?? { } , exceededDailyQuotaUsers : responseJSON . exceeded_daily_quota_users ?? { } , throttledEvents : responseJSON . throttled_events ?? [ ] , } , } ; case Status . Timeout : default : return { status , statusCode , } ; } } ;