@mparticle/web-sdk 2.32.1 → 2.32.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mparticle.common.js +31 -33
- package/dist/mparticle.esm.js +31 -33
- package/dist/mparticle.js +199 -226
- package/package.json +1 -1
- package/src/audienceManager.ts +2 -2
- package/src/batchUploader.ts +2 -2
- package/src/configAPIClient.ts +2 -2
- package/src/identityApiClient.ts +284 -0
- package/src/sdkRuntimeModels.ts +2 -3
- package/src/uploaders.ts +4 -4
- package/src/aliasRequestApiClient.ts +0 -90
- package/src/identityApiClient.interfaces.ts +0 -44
- package/src/identityApiClient.js +0 -131
package/package.json
CHANGED
package/src/audienceManager.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
FetchUploader,
|
|
4
4
|
XHRUploader,
|
|
5
5
|
AsyncUploader,
|
|
6
|
-
|
|
6
|
+
IFetchPayload
|
|
7
7
|
} from './uploaders';
|
|
8
8
|
import Audience from './audience';
|
|
9
9
|
|
|
@@ -38,7 +38,7 @@ export default class AudienceManager {
|
|
|
38
38
|
public async sendGetUserAudienceRequest(mpid: string, callback: (userAudiences: IAudienceMemberships) => void) {
|
|
39
39
|
this.logger.verbose('Fetching user audiences from server');
|
|
40
40
|
|
|
41
|
-
const fetchPayload:
|
|
41
|
+
const fetchPayload: IFetchPayload = {
|
|
42
42
|
method: 'GET',
|
|
43
43
|
headers: {
|
|
44
44
|
Accept: '*/*',
|
package/src/batchUploader.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
AsyncUploader,
|
|
10
10
|
FetchUploader,
|
|
11
11
|
XHRUploader,
|
|
12
|
-
|
|
12
|
+
IFetchPayload,
|
|
13
13
|
} from './uploaders';
|
|
14
14
|
import { IMParticleUser } from './identity-user-interfaces';
|
|
15
15
|
|
|
@@ -366,7 +366,7 @@ export class BatchUploader {
|
|
|
366
366
|
logger.verbose(`Batch count: ${uploads.length}`);
|
|
367
367
|
|
|
368
368
|
for (let i = 0; i < uploads.length; i++) {
|
|
369
|
-
const fetchPayload:
|
|
369
|
+
const fetchPayload: IFetchPayload = {
|
|
370
370
|
method: 'POST',
|
|
371
371
|
headers: {
|
|
372
372
|
Accept: BatchUploader.CONTENT_TYPE,
|
package/src/configAPIClient.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
import { Dictionary } from './utils';
|
|
10
10
|
import {
|
|
11
11
|
AsyncUploader,
|
|
12
|
-
|
|
12
|
+
IFetchPayload,
|
|
13
13
|
FetchUploader,
|
|
14
14
|
XHRUploader,
|
|
15
15
|
} from './uploaders';
|
|
@@ -125,7 +125,7 @@ export default function ConfigAPIClient(
|
|
|
125
125
|
|
|
126
126
|
this.getSDKConfiguration = async (): Promise<IConfigResponse> => {
|
|
127
127
|
let configResponse: IConfigResponse;
|
|
128
|
-
const fetchPayload:
|
|
128
|
+
const fetchPayload: IFetchPayload = {
|
|
129
129
|
method: 'get',
|
|
130
130
|
headers: {
|
|
131
131
|
Accept: 'text/plain;charset=UTF-8',
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import Constants, { HTTP_ACCEPTED, HTTP_OK } from './constants';
|
|
2
|
+
import {
|
|
3
|
+
AsyncUploader,
|
|
4
|
+
FetchUploader,
|
|
5
|
+
XHRUploader,
|
|
6
|
+
IFetchPayload,
|
|
7
|
+
} from './uploaders';
|
|
8
|
+
import { CACHE_HEADER } from './identity-utils';
|
|
9
|
+
import { parseNumber } from './utils';
|
|
10
|
+
import {
|
|
11
|
+
IAliasCallback,
|
|
12
|
+
IAliasRequest,
|
|
13
|
+
IdentityAPIMethod,
|
|
14
|
+
IIdentity,
|
|
15
|
+
IIdentityAPIRequestData,
|
|
16
|
+
} from './identity.interfaces';
|
|
17
|
+
import {
|
|
18
|
+
Callback,
|
|
19
|
+
IdentityApiData,
|
|
20
|
+
MPID,
|
|
21
|
+
UserIdentities,
|
|
22
|
+
} from '@mparticle/web-sdk';
|
|
23
|
+
import {
|
|
24
|
+
IdentityCallback,
|
|
25
|
+
IdentityResultBody,
|
|
26
|
+
IIdentityResponse,
|
|
27
|
+
} from './identity-user-interfaces';
|
|
28
|
+
import { MParticleWebSDK } from './sdkRuntimeModels';
|
|
29
|
+
|
|
30
|
+
const { HTTPCodes, Messages, IdentityMethods } = Constants;
|
|
31
|
+
|
|
32
|
+
const { Modify } = IdentityMethods;
|
|
33
|
+
|
|
34
|
+
export interface IIdentityApiClient {
|
|
35
|
+
sendAliasRequest: (
|
|
36
|
+
aliasRequest: IAliasRequest,
|
|
37
|
+
aliasCallback: IAliasCallback
|
|
38
|
+
) => Promise<void>;
|
|
39
|
+
sendIdentityRequest: (
|
|
40
|
+
identityApiRequest: IIdentityAPIRequestData,
|
|
41
|
+
method: IdentityAPIMethod,
|
|
42
|
+
callback: IdentityCallback,
|
|
43
|
+
originalIdentityApiData: IdentityApiData,
|
|
44
|
+
parseIdentityResponse: IIdentity['parseIdentityResponse'],
|
|
45
|
+
mpid: MPID,
|
|
46
|
+
knownIdentities: UserIdentities
|
|
47
|
+
) => Promise<void>;
|
|
48
|
+
getUploadUrl: (method: IdentityAPIMethod, mpid: MPID) => string;
|
|
49
|
+
getIdentityResponseFromFetch: (
|
|
50
|
+
response: Response,
|
|
51
|
+
responseBody: IdentityResultBody
|
|
52
|
+
) => IIdentityResponse;
|
|
53
|
+
getIdentityResponseFromXHR: (response: XMLHttpRequest) => IIdentityResponse;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface IAliasResponseBody {
|
|
57
|
+
message?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface IdentityApiRequestPayload extends IFetchPayload {
|
|
61
|
+
headers: {
|
|
62
|
+
Accept: string;
|
|
63
|
+
'Content-Type': string;
|
|
64
|
+
'x-mp-key': string;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default function IdentityAPIClient(
|
|
69
|
+
this: IIdentityApiClient,
|
|
70
|
+
mpInstance: MParticleWebSDK
|
|
71
|
+
) {
|
|
72
|
+
this.sendAliasRequest = async function(
|
|
73
|
+
aliasRequest: IAliasRequest,
|
|
74
|
+
aliasCallback: IAliasCallback
|
|
75
|
+
) {
|
|
76
|
+
const { verbose, error } = mpInstance.Logger;
|
|
77
|
+
const { invokeAliasCallback } = mpInstance._Helpers;
|
|
78
|
+
const { aliasUrl } = mpInstance._Store.SDKConfig;
|
|
79
|
+
const { devToken: apiKey } = mpInstance._Store;
|
|
80
|
+
|
|
81
|
+
verbose(Messages.InformationMessages.SendAliasHttp);
|
|
82
|
+
|
|
83
|
+
// https://go.mparticle.com/work/SQDSDKS-6750
|
|
84
|
+
const uploadUrl = `https://${aliasUrl}${apiKey}/Alias`;
|
|
85
|
+
const uploader: AsyncUploader = window.fetch
|
|
86
|
+
? new FetchUploader(uploadUrl)
|
|
87
|
+
: new XHRUploader(uploadUrl);
|
|
88
|
+
|
|
89
|
+
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
90
|
+
const uploadPayload: IFetchPayload = {
|
|
91
|
+
method: 'post',
|
|
92
|
+
headers: {
|
|
93
|
+
Accept: 'text/plain;charset=UTF-8',
|
|
94
|
+
'Content-Type': 'application/json',
|
|
95
|
+
},
|
|
96
|
+
body: JSON.stringify(aliasRequest),
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
const response: Response = await uploader.upload(uploadPayload);
|
|
101
|
+
|
|
102
|
+
let message: string;
|
|
103
|
+
let aliasResponseBody: IAliasResponseBody;
|
|
104
|
+
|
|
105
|
+
// FetchUploader returns the response as a JSON object that we have to await
|
|
106
|
+
if (response.json) {
|
|
107
|
+
// HTTP responses of 202, 200, and 403 do not have a response. response.json will always exist on a fetch, but can only be await-ed when the response is not empty, otherwise it will throw an error.
|
|
108
|
+
try {
|
|
109
|
+
aliasResponseBody = await response.json();
|
|
110
|
+
} catch (e) {
|
|
111
|
+
verbose('The request has no response body');
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
115
|
+
// XHRUploader returns the response as a string that we need to parse
|
|
116
|
+
const xhrResponse = (response as unknown) as XMLHttpRequest;
|
|
117
|
+
|
|
118
|
+
aliasResponseBody = xhrResponse.responseText
|
|
119
|
+
? JSON.parse(xhrResponse.responseText)
|
|
120
|
+
: '';
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let errorMessage: string;
|
|
124
|
+
|
|
125
|
+
switch (response.status) {
|
|
126
|
+
case HTTP_OK:
|
|
127
|
+
case HTTP_ACCEPTED:
|
|
128
|
+
// https://go.mparticle.com/work/SQDSDKS-6670
|
|
129
|
+
message =
|
|
130
|
+
'Successfully sent forwarding stats to mParticle Servers';
|
|
131
|
+
break;
|
|
132
|
+
default:
|
|
133
|
+
// 400 has an error message, but 403 doesn't
|
|
134
|
+
if (aliasResponseBody?.message) {
|
|
135
|
+
errorMessage = aliasResponseBody.message;
|
|
136
|
+
}
|
|
137
|
+
message =
|
|
138
|
+
'Issue with sending Alias Request to mParticle Servers, received HTTP Code of ' +
|
|
139
|
+
response.status;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
verbose(message);
|
|
143
|
+
invokeAliasCallback(aliasCallback, response.status, errorMessage);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
const err = e as Error;
|
|
146
|
+
error('Error sending alias request to mParticle servers. ' + err);
|
|
147
|
+
invokeAliasCallback(
|
|
148
|
+
aliasCallback,
|
|
149
|
+
HTTPCodes.noHttpCoverage,
|
|
150
|
+
err.message
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
this.sendIdentityRequest = async function(
|
|
156
|
+
identityApiRequest: IIdentityAPIRequestData,
|
|
157
|
+
method: IdentityAPIMethod,
|
|
158
|
+
callback: IdentityCallback,
|
|
159
|
+
originalIdentityApiData: IdentityApiData,
|
|
160
|
+
parseIdentityResponse: IIdentity['parseIdentityResponse'],
|
|
161
|
+
mpid: MPID,
|
|
162
|
+
knownIdentities: UserIdentities
|
|
163
|
+
) {
|
|
164
|
+
const { verbose, error } = mpInstance.Logger;
|
|
165
|
+
const { invokeCallback } = mpInstance._Helpers;
|
|
166
|
+
|
|
167
|
+
verbose(Messages.InformationMessages.SendIdentityBegin);
|
|
168
|
+
if (!identityApiRequest) {
|
|
169
|
+
error(Messages.ErrorMessages.APIRequestEmpty);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
verbose(Messages.InformationMessages.SendIdentityHttp);
|
|
173
|
+
|
|
174
|
+
if (mpInstance._Store.identityCallInFlight) {
|
|
175
|
+
invokeCallback(
|
|
176
|
+
callback,
|
|
177
|
+
HTTPCodes.activeIdentityRequest,
|
|
178
|
+
'There is currently an Identity request processing. Please wait for this to return before requesting again'
|
|
179
|
+
);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const previousMPID = mpid || null;
|
|
184
|
+
const uploadUrl = this.getUploadUrl(method, mpid);
|
|
185
|
+
|
|
186
|
+
const uploader: AsyncUploader = window.fetch
|
|
187
|
+
? new FetchUploader(uploadUrl)
|
|
188
|
+
: new XHRUploader(uploadUrl);
|
|
189
|
+
|
|
190
|
+
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
191
|
+
const fetchPayload: IdentityApiRequestPayload = {
|
|
192
|
+
method: 'post',
|
|
193
|
+
headers: {
|
|
194
|
+
Accept: 'text/plain;charset=UTF-8',
|
|
195
|
+
'Content-Type': 'application/json',
|
|
196
|
+
'x-mp-key': mpInstance._Store.devToken,
|
|
197
|
+
},
|
|
198
|
+
body: JSON.stringify(identityApiRequest),
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
mpInstance._Store.identityCallInFlight = true;
|
|
203
|
+
const response: Response = await uploader.upload(fetchPayload);
|
|
204
|
+
|
|
205
|
+
let identityResponse: IIdentityResponse;
|
|
206
|
+
|
|
207
|
+
if (response.json) {
|
|
208
|
+
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
209
|
+
// FetchUploader returns the response as a JSON object that we have to await
|
|
210
|
+
const responseBody: IdentityResultBody = await response.json();
|
|
211
|
+
|
|
212
|
+
identityResponse = this.getIdentityResponseFromFetch(
|
|
213
|
+
response,
|
|
214
|
+
responseBody
|
|
215
|
+
);
|
|
216
|
+
} else {
|
|
217
|
+
identityResponse = this.getIdentityResponseFromXHR(
|
|
218
|
+
(response as unknown) as XMLHttpRequest
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
verbose(
|
|
223
|
+
'Received Identity Response from server: ' +
|
|
224
|
+
JSON.stringify(identityResponse.responseText)
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
parseIdentityResponse(
|
|
228
|
+
identityResponse,
|
|
229
|
+
previousMPID,
|
|
230
|
+
callback,
|
|
231
|
+
originalIdentityApiData,
|
|
232
|
+
method,
|
|
233
|
+
knownIdentities,
|
|
234
|
+
false
|
|
235
|
+
);
|
|
236
|
+
} catch (err) {
|
|
237
|
+
const errorMessage = (err as Error).message || err.toString();
|
|
238
|
+
|
|
239
|
+
mpInstance._Store.identityCallInFlight = false;
|
|
240
|
+
invokeCallback(
|
|
241
|
+
callback,
|
|
242
|
+
HTTPCodes.noHttpCoverage,
|
|
243
|
+
errorMessage,
|
|
244
|
+
);
|
|
245
|
+
error('Error sending identity request to servers' + ' - ' + err);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
this.getUploadUrl = (method: IdentityAPIMethod, mpid: MPID) => {
|
|
250
|
+
const uploadServiceUrl: string = mpInstance._Helpers.createServiceUrl(
|
|
251
|
+
mpInstance._Store.SDKConfig.identityUrl
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
const uploadUrl: string =
|
|
255
|
+
method === Modify
|
|
256
|
+
? uploadServiceUrl + mpid + '/' + method
|
|
257
|
+
: uploadServiceUrl + method;
|
|
258
|
+
|
|
259
|
+
return uploadUrl;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
this.getIdentityResponseFromFetch = (
|
|
263
|
+
response: Response,
|
|
264
|
+
responseBody: IdentityResultBody
|
|
265
|
+
): IIdentityResponse => ({
|
|
266
|
+
status: response.status,
|
|
267
|
+
responseText: responseBody,
|
|
268
|
+
cacheMaxAge: parseInt(response.headers.get(CACHE_HEADER)) || 0,
|
|
269
|
+
expireTimestamp: 0,
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
this.getIdentityResponseFromXHR = (
|
|
273
|
+
response: XMLHttpRequest
|
|
274
|
+
): IIdentityResponse => ({
|
|
275
|
+
status: response.status,
|
|
276
|
+
responseText: response.responseText
|
|
277
|
+
? JSON.parse(response.responseText)
|
|
278
|
+
: {},
|
|
279
|
+
cacheMaxAge: parseNumber(
|
|
280
|
+
response.getResponseHeader(CACHE_HEADER) || ''
|
|
281
|
+
),
|
|
282
|
+
expireTimestamp: 0,
|
|
283
|
+
});
|
|
284
|
+
}
|
package/src/sdkRuntimeModels.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { DataPlanVersion } from '@mparticle/data-planning-models';
|
|
|
3
3
|
import {
|
|
4
4
|
MPConfiguration,
|
|
5
5
|
MPID,
|
|
6
|
-
Callback,
|
|
7
6
|
IdentityApiData,
|
|
8
7
|
} from '@mparticle/web-sdk';
|
|
9
8
|
import { IStore } from './store';
|
|
@@ -287,8 +286,8 @@ export interface SDKHelpersApi {
|
|
|
287
286
|
): boolean;
|
|
288
287
|
isObject?(item: any);
|
|
289
288
|
invokeCallback?(
|
|
290
|
-
callback:
|
|
291
|
-
code:
|
|
289
|
+
callback: IdentityCallback,
|
|
290
|
+
code: number,
|
|
292
291
|
body: string,
|
|
293
292
|
mParticleUser?: IMParticleUser,
|
|
294
293
|
previousMpid?: MPID
|
package/src/uploaders.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
type HTTPMethod = 'get' | 'post';
|
|
2
2
|
|
|
3
|
-
export interface
|
|
3
|
+
export interface IFetchPayload {
|
|
4
4
|
method: string;
|
|
5
5
|
headers: {
|
|
6
6
|
Accept: string;
|
|
@@ -12,7 +12,7 @@ export interface fetchPayload {
|
|
|
12
12
|
export abstract class AsyncUploader {
|
|
13
13
|
url: string;
|
|
14
14
|
public abstract upload(
|
|
15
|
-
fetchPayload:
|
|
15
|
+
fetchPayload: IFetchPayload,
|
|
16
16
|
url?: string
|
|
17
17
|
): Promise<Response>;
|
|
18
18
|
|
|
@@ -23,7 +23,7 @@ export abstract class AsyncUploader {
|
|
|
23
23
|
|
|
24
24
|
export class FetchUploader extends AsyncUploader {
|
|
25
25
|
public async upload(
|
|
26
|
-
fetchPayload:
|
|
26
|
+
fetchPayload: IFetchPayload,
|
|
27
27
|
_url?: string
|
|
28
28
|
): Promise<Response> {
|
|
29
29
|
const url = _url || this.url;
|
|
@@ -32,7 +32,7 @@ export class FetchUploader extends AsyncUploader {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
export class XHRUploader extends AsyncUploader {
|
|
35
|
-
public async upload(fetchPayload:
|
|
35
|
+
public async upload(fetchPayload: IFetchPayload): Promise<Response> {
|
|
36
36
|
const response: Response = await this.makeRequest(
|
|
37
37
|
this.url,
|
|
38
38
|
fetchPayload.body,
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { IAliasRequest, IAliasCallback } from "./identity.interfaces";
|
|
2
|
-
import { MParticleWebSDK } from "./sdkRuntimeModels";
|
|
3
|
-
import Constants from './constants';
|
|
4
|
-
import { FetchUploader, XHRUploader } from './uploaders';
|
|
5
|
-
import { HTTP_ACCEPTED, HTTP_OK } from "./constants";
|
|
6
|
-
import { IIdentityApiClientSendAliasRequest } from "./identityApiClient.interfaces";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const { HTTPCodes, Messages } = Constants;
|
|
10
|
-
|
|
11
|
-
interface IAliasResponseBody {
|
|
12
|
-
message?: string
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const sendAliasRequest: IIdentityApiClientSendAliasRequest = async function (mpInstance: MParticleWebSDK, aliasRequest: IAliasRequest, aliasCallback: IAliasCallback): Promise<void> {
|
|
16
|
-
const { verbose, error } = mpInstance.Logger;
|
|
17
|
-
const { invokeAliasCallback } = mpInstance._Helpers;
|
|
18
|
-
const { aliasUrl } = mpInstance._Store.SDKConfig;
|
|
19
|
-
const { devToken: apiKey } = mpInstance._Store;
|
|
20
|
-
|
|
21
|
-
verbose(Messages.InformationMessages.SendAliasHttp);
|
|
22
|
-
|
|
23
|
-
// https://go.mparticle.com/work/SQDSDKS-6750
|
|
24
|
-
const uploadUrl = `https://${aliasUrl}${apiKey}/Alias`;
|
|
25
|
-
const uploader = window.fetch
|
|
26
|
-
? new FetchUploader(uploadUrl)
|
|
27
|
-
: new XHRUploader(uploadUrl);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
31
|
-
const uploadPayload = {
|
|
32
|
-
method: 'post',
|
|
33
|
-
headers: {
|
|
34
|
-
Accept: 'text/plain;charset=UTF-8',
|
|
35
|
-
'Content-Type': 'application/json',
|
|
36
|
-
},
|
|
37
|
-
body: JSON.stringify(aliasRequest),
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
const response = await uploader.upload(uploadPayload);
|
|
42
|
-
|
|
43
|
-
let message: string;
|
|
44
|
-
let aliasResponseBody: IAliasResponseBody;
|
|
45
|
-
|
|
46
|
-
// FetchUploader returns the response as a JSON object that we have to await
|
|
47
|
-
if (response.json) {
|
|
48
|
-
// HTTP responses of 202, 200, and 403 do not have a response. response.json will always exist on a fetch, but can only be await-ed when the response is not empty, otherwise it will throw an error.
|
|
49
|
-
try {
|
|
50
|
-
aliasResponseBody = await response.json();
|
|
51
|
-
} catch (e) {
|
|
52
|
-
verbose('The request has no response body');
|
|
53
|
-
}
|
|
54
|
-
} else {
|
|
55
|
-
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
56
|
-
// XHRUploader returns the response as a string that we need to parse
|
|
57
|
-
const xhrResponse = response as unknown as XMLHttpRequest;
|
|
58
|
-
|
|
59
|
-
aliasResponseBody = xhrResponse.responseText
|
|
60
|
-
? JSON.parse(xhrResponse.responseText)
|
|
61
|
-
: '';
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
let errorMessage: string;
|
|
65
|
-
|
|
66
|
-
switch (response.status) {
|
|
67
|
-
case HTTP_OK:
|
|
68
|
-
case HTTP_ACCEPTED:
|
|
69
|
-
// https://go.mparticle.com/work/SQDSDKS-6670
|
|
70
|
-
message =
|
|
71
|
-
'Successfully sent forwarding stats to mParticle Servers';
|
|
72
|
-
break;
|
|
73
|
-
default:
|
|
74
|
-
// 400 has an error message, but 403 doesn't
|
|
75
|
-
if (aliasResponseBody?.message) {
|
|
76
|
-
errorMessage = aliasResponseBody.message;
|
|
77
|
-
}
|
|
78
|
-
message =
|
|
79
|
-
'Issue with sending Alias Request to mParticle Servers, received HTTP Code of ' +
|
|
80
|
-
response.status;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
verbose(message);
|
|
84
|
-
invokeAliasCallback(aliasCallback, response.status, errorMessage);
|
|
85
|
-
} catch (e) {
|
|
86
|
-
const err = e as Error;
|
|
87
|
-
error('Error sending alias request to mParticle servers. ' + err);
|
|
88
|
-
invokeAliasCallback(aliasCallback, HTTPCodes.noHttpCoverage, (err.message));
|
|
89
|
-
}
|
|
90
|
-
};
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { IdentityApiData, MPID, UserIdentities } from '@mparticle/web-sdk';
|
|
2
|
-
import {
|
|
3
|
-
IdentityCallback,
|
|
4
|
-
IIdentityResponse,
|
|
5
|
-
} from './identity-user-interfaces';
|
|
6
|
-
import {
|
|
7
|
-
IAliasRequest,
|
|
8
|
-
IAliasCallback,
|
|
9
|
-
IIdentityRequest,
|
|
10
|
-
IdentityAPIMethod,
|
|
11
|
-
IIdentity,
|
|
12
|
-
} from './identity.interfaces';
|
|
13
|
-
import { MParticleWebSDK } from './sdkRuntimeModels';
|
|
14
|
-
|
|
15
|
-
export interface IIdentityApiClient {
|
|
16
|
-
sendAliasRequest: (
|
|
17
|
-
aliasRequest: IAliasRequest,
|
|
18
|
-
aliasCallback: IAliasCallback
|
|
19
|
-
) => Promise<void>;
|
|
20
|
-
sendIdentityRequest: (
|
|
21
|
-
identityApiRequest: IIdentityRequest,
|
|
22
|
-
method: IdentityAPIMethod,
|
|
23
|
-
callback: IdentityCallback,
|
|
24
|
-
originalIdentityApiData: IdentityApiData,
|
|
25
|
-
parseIdentityResponse: IIdentity['parseIdentityResponse'],
|
|
26
|
-
mpid: MPID,
|
|
27
|
-
knownIdentities: UserIdentities
|
|
28
|
-
) => Promise<void>;
|
|
29
|
-
getUploadUrl: (method: IdentityAPIMethod, mpid: MPID) => string;
|
|
30
|
-
getIdentityResponseFromFetch: (
|
|
31
|
-
response: Response,
|
|
32
|
-
responseBody: string
|
|
33
|
-
) => IIdentityResponse;
|
|
34
|
-
getIdentityResponseFromXHR: (response: Response) => IIdentityResponse;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
38
|
-
// https://go.mparticle.com/work/SQDSDKS-6679
|
|
39
|
-
// Combine with `sendIdentityRequest` above once module is fully migrated
|
|
40
|
-
export type IIdentityApiClientSendAliasRequest = (
|
|
41
|
-
mpInstance: MParticleWebSDK,
|
|
42
|
-
aliasRequest: IAliasRequest,
|
|
43
|
-
aliasCallback: IAliasCallback
|
|
44
|
-
) => Promise<void>;
|
package/src/identityApiClient.js
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import Constants from './constants';
|
|
2
|
-
import { sendAliasRequest } from './aliasRequestApiClient';
|
|
3
|
-
import { FetchUploader, XHRUploader } from './uploaders';
|
|
4
|
-
import { CACHE_HEADER } from './identity-utils';
|
|
5
|
-
import { parseNumber } from './utils';
|
|
6
|
-
|
|
7
|
-
var HTTPCodes = Constants.HTTPCodes,
|
|
8
|
-
Messages = Constants.Messages;
|
|
9
|
-
|
|
10
|
-
const { Modify } = Constants.IdentityMethods;
|
|
11
|
-
|
|
12
|
-
export default function IdentityAPIClient(mpInstance) {
|
|
13
|
-
this.sendAliasRequest = async function(aliasRequest, callback) {
|
|
14
|
-
await sendAliasRequest(mpInstance, aliasRequest, callback);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
this.sendIdentityRequest = async function(
|
|
18
|
-
identityApiRequest,
|
|
19
|
-
method,
|
|
20
|
-
callback,
|
|
21
|
-
originalIdentityApiData,
|
|
22
|
-
parseIdentityResponse,
|
|
23
|
-
mpid,
|
|
24
|
-
knownIdentities
|
|
25
|
-
) {
|
|
26
|
-
const { verbose, error } = mpInstance.Logger;
|
|
27
|
-
const { invokeCallback } = mpInstance._Helpers;
|
|
28
|
-
|
|
29
|
-
verbose(Messages.InformationMessages.SendIdentityBegin);
|
|
30
|
-
if (!identityApiRequest) {
|
|
31
|
-
error(Messages.ErrorMessages.APIRequestEmpty);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
verbose(Messages.InformationMessages.SendIdentityHttp);
|
|
35
|
-
|
|
36
|
-
if (mpInstance._Store.identityCallInFlight) {
|
|
37
|
-
invokeCallback(
|
|
38
|
-
callback,
|
|
39
|
-
HTTPCodes.activeIdentityRequest,
|
|
40
|
-
'There is currently an Identity request processing. Please wait for this to return before requesting again'
|
|
41
|
-
);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const previousMPID = mpid || null;
|
|
46
|
-
const uploadUrl = this.getUploadUrl(method, mpid);
|
|
47
|
-
|
|
48
|
-
const uploader = window.fetch
|
|
49
|
-
? new FetchUploader(uploadUrl)
|
|
50
|
-
: new XHRUploader(uploadUrl);
|
|
51
|
-
|
|
52
|
-
const fetchPayload = {
|
|
53
|
-
method: 'post',
|
|
54
|
-
headers: {
|
|
55
|
-
Accept: 'text/plain;charset=UTF-8',
|
|
56
|
-
'Content-Type': 'application/json',
|
|
57
|
-
'x-mp-key': mpInstance._Store.devToken,
|
|
58
|
-
},
|
|
59
|
-
body: JSON.stringify(identityApiRequest),
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
try {
|
|
63
|
-
mpInstance._Store.identityCallInFlight = true;
|
|
64
|
-
const response = await uploader.upload(fetchPayload);
|
|
65
|
-
|
|
66
|
-
let identityResponse;
|
|
67
|
-
|
|
68
|
-
if (response.json) {
|
|
69
|
-
// https://go.mparticle.com/work/SQDSDKS-6568
|
|
70
|
-
// FetchUploader returns the response as a JSON object that we have to await
|
|
71
|
-
const responseBody = await response.json();
|
|
72
|
-
identityResponse = this.getIdentityResponseFromFetch(
|
|
73
|
-
response,
|
|
74
|
-
responseBody
|
|
75
|
-
);
|
|
76
|
-
} else {
|
|
77
|
-
identityResponse = this.getIdentityResponseFromXHR(response);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
verbose(
|
|
81
|
-
'Received Identity Response from server: ' +
|
|
82
|
-
JSON.stringify(identityResponse.responseText)
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
parseIdentityResponse(
|
|
86
|
-
identityResponse,
|
|
87
|
-
previousMPID,
|
|
88
|
-
callback,
|
|
89
|
-
originalIdentityApiData,
|
|
90
|
-
method,
|
|
91
|
-
knownIdentities,
|
|
92
|
-
false
|
|
93
|
-
);
|
|
94
|
-
} catch (err) {
|
|
95
|
-
mpInstance._Store.identityCallInFlight = false;
|
|
96
|
-
invokeCallback(callback, HTTPCodes.noHttpCoverage, err);
|
|
97
|
-
error('Error sending identity request to servers' + ' - ' + err);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
this.getUploadUrl = (method, mpid) => {
|
|
102
|
-
const uploadServiceUrl = mpInstance._Helpers.createServiceUrl(
|
|
103
|
-
mpInstance._Store.SDKConfig.identityUrl
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
const uploadUrl =
|
|
107
|
-
method === Modify
|
|
108
|
-
? uploadServiceUrl + mpid + '/' + method
|
|
109
|
-
: uploadServiceUrl + method;
|
|
110
|
-
|
|
111
|
-
return uploadUrl;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
this.getIdentityResponseFromFetch = (response, responseBody) => ({
|
|
115
|
-
status: response.status,
|
|
116
|
-
responseText: responseBody,
|
|
117
|
-
cacheMaxAge: parseInt(response.headers.get(CACHE_HEADER)) || 0,
|
|
118
|
-
expireTimestamp: 0,
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
this.getIdentityResponseFromXHR = response => ({
|
|
122
|
-
status: response.status,
|
|
123
|
-
responseText: response.responseText
|
|
124
|
-
? JSON.parse(response.responseText)
|
|
125
|
-
: {},
|
|
126
|
-
cacheMaxAge: parseNumber(
|
|
127
|
-
response.getResponseHeader(CACHE_HEADER) || ''
|
|
128
|
-
),
|
|
129
|
-
expireTimestamp: 0,
|
|
130
|
-
});
|
|
131
|
-
}
|