Skip to content

Commit

Permalink
refactor: Migrate IdentityAPIClient to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
alexs-mparticle committed Nov 13, 2024
1 parent bf3639f commit ac20e96
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 336 deletions.
90 changes: 0 additions & 90 deletions src/aliasRequestApiClient.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/identityApiClient.interfaces.ts

This file was deleted.

65 changes: 53 additions & 12 deletions src/identityApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import Constants, { HTTP_ACCEPTED, HTTP_OK } from './constants';
import { sendAliasRequest } from './aliasRequestApiClient';
import { FetchUploader, XHRUploader } from './uploaders';
import { CACHE_HEADER } from './identity-utils';
import { parseNumber } from './utils';
import { IIdentityApiClient } from './identityApiClient.interfaces';
import { IAliasCallback, IAliasRequest, IdentityAPIMethod, IIdentity, IIdentityAPIRequestData } from './identity.interfaces';
import { IdentityApiData, MPID, UserIdentities } from '@mparticle/web-sdk';
import { IdentityCallback, IdentityResultBody, IIdentityResponse } from './identity-user-interfaces';

const {
HTTPCodes,
Expand All @@ -13,17 +14,35 @@ const {

const { Modify } = IdentityMethods;

export interface IIdentityApiClient {
sendAliasRequest: (
aliasRequest: IAliasRequest,
aliasCallback: IAliasCallback
) => Promise<void>;
sendIdentityRequest: (
identityApiRequest: IIdentityAPIRequestData,
method: IdentityAPIMethod,
callback: IdentityCallback,
originalIdentityApiData: IdentityApiData,
parseIdentityResponse: IIdentity['parseIdentityResponse'],
mpid: MPID,
knownIdentities: UserIdentities
) => Promise<void>;
getUploadUrl: (method: IdentityAPIMethod, mpid: MPID) => string;
getIdentityResponseFromFetch: (
response: Response,
responseBody: IdentityResultBody
) => IIdentityResponse;
getIdentityResponseFromXHR: (response: XMLHttpRequest) => IIdentityResponse;
}


// TODO: Move this to interfaces file
interface IAliasResponseBody {
export interface IAliasResponseBody {
message?: string
}


export default function IdentityAPIClient(this: IIdentityApiClient, mpInstance) {
this.sendAliasRequest = async function(aliasRequest, aliasCallback) {
// await sendAliasRequest(mpInstance, aliasRequest, callback);

const { verbose, error } = mpInstance.Logger;
const { invokeAliasCallback } = mpInstance._Helpers;
const { aliasUrl } = mpInstance._Store.SDKConfig;
Expand Down Expand Up @@ -101,13 +120,14 @@ export default function IdentityAPIClient(this: IIdentityApiClient, mpInstance)
};

this.sendIdentityRequest = async function(
identityApiRequest,
method,
// QUESTION: Did we screw up? Is it Identity Reponse or Request?
identityApiRequest: IIdentityAPIRequestData,
method: IdentityAPIMethod,
callback,
originalIdentityApiData,
parseIdentityResponse,
mpid,
knownIdentities
mpid: MPID,
knownIdentities: UserIdentities
) {
const { verbose, error } = mpInstance.Logger;
const { invokeCallback } = mpInstance._Helpers;
Expand Down Expand Up @@ -135,6 +155,9 @@ export default function IdentityAPIClient(this: IIdentityApiClient, mpInstance)
? new FetchUploader(uploadUrl)
: new XHRUploader(uploadUrl);

// TODO: Write test to verify fetch payload

// https://go.mparticle.com/work/SQDSDKS-6568
const fetchPayload = {
method: 'post',
headers: {
Expand All @@ -145,21 +168,35 @@ export default function IdentityAPIClient(this: IIdentityApiClient, mpInstance)
body: JSON.stringify(identityApiRequest),
};

console.log('fetchPayload', fetchPayload)

try {
console.log('uploader', uploader);

mpInstance._Store.identityCallInFlight = true;
const response = await uploader.upload(fetchPayload);

let identityResponse;
console.log('response', response);

let identityResponse: IIdentityResponse;

if (response.json) {

console.log('fetch response')

// https://go.mparticle.com/work/SQDSDKS-6568
// FetchUploader returns the response as a JSON object that we have to await
const responseBody = await response.json();

console.log('responseBody', responseBody);

identityResponse = this.getIdentityResponseFromFetch(
response,
responseBody
);
} else {
console.log('xhr response', response)

identityResponse = this.getIdentityResponseFromXHR(response as unknown as XMLHttpRequest);
}

Expand All @@ -168,6 +205,8 @@ export default function IdentityAPIClient(this: IIdentityApiClient, mpInstance)
JSON.stringify(identityResponse.responseText)
);

console.log('parseIdentityResponse', parseIdentityResponse)

parseIdentityResponse(
identityResponse,
previousMPID,
Expand All @@ -178,6 +217,8 @@ export default function IdentityAPIClient(this: IIdentityApiClient, mpInstance)
false
);
} catch (err) {
console.log('did i error?');
console.log('callback', callback);
mpInstance._Store.identityCallInFlight = false;
invokeCallback(callback, HTTPCodes.noHttpCoverage, err);
error('Error sending identity request to servers' + ' - ' + err);
Expand Down
26 changes: 0 additions & 26 deletions test/jest/identityApiClient.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion test/src/_test.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import './tests-feature-flags';
import './tests-user';
import './tests-legacy-alias-requests';
import './tests-identityApiClient';
import './tests-aliasRequestApiClient';
import './tests-integration-capture';
import './tests-batchUploader_4';

Loading

0 comments on commit ac20e96

Please sign in to comment.