@mparticle/web-sdk 2.28.2 → 2.28.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mparticle/web-sdk",
3
- "version": "2.28.2",
3
+ "version": "2.28.3",
4
4
  "description": "mParticle core SDK for web applications",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
@@ -0,0 +1,89 @@
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
+
7
+
8
+ const { HTTPCodes, Messages } = Constants;
9
+
10
+ interface IAliasResponseBody {
11
+ message?: string
12
+ }
13
+
14
+ export async function sendAliasRequest (mpInstance: MParticleWebSDK, aliasRequest: IAliasRequest, aliasCallback: IAliasCallback) {
15
+ const { verbose, error } = mpInstance.Logger;
16
+ const { invokeAliasCallback } = mpInstance._Helpers;
17
+ const { aliasUrl } = mpInstance._Store.SDKConfig;
18
+ const { devToken: apiKey } = mpInstance._Store;
19
+
20
+ verbose(Messages.InformationMessages.SendAliasHttp);
21
+
22
+ // https://go.mparticle.com/work/SQDSDKS-6750
23
+ const uploadUrl = `https://${aliasUrl}${apiKey}/Alias`;
24
+ const uploader = window.fetch
25
+ ? new FetchUploader(uploadUrl)
26
+ : new XHRUploader(uploadUrl);
27
+
28
+
29
+ // https://go.mparticle.com/work/SQDSDKS-6568
30
+ const uploadPayload = {
31
+ method: 'post',
32
+ headers: {
33
+ Accept: 'text/plain;charset=UTF-8',
34
+ 'Content-Type': 'application/json',
35
+ },
36
+ body: JSON.stringify(aliasRequest),
37
+ };
38
+
39
+ try {
40
+ const response = await uploader.upload(uploadPayload);
41
+
42
+ let message: string;
43
+ let aliasResponseBody: IAliasResponseBody;
44
+
45
+ // FetchUploader returns the response as a JSON object that we have to await
46
+ if (response.json) {
47
+ // 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.
48
+ try {
49
+ aliasResponseBody = await response.json();
50
+ } catch (e) {
51
+ verbose('The request has no response body');
52
+ }
53
+ } else {
54
+ // https://go.mparticle.com/work/SQDSDKS-6568
55
+ // XHRUploader returns the response as a string that we need to parse
56
+ const xhrResponse = response as unknown as XMLHttpRequest;
57
+ // debugger;
58
+ aliasResponseBody = xhrResponse.responseText
59
+ ? JSON.parse(xhrResponse.responseText)
60
+ : '';
61
+ }
62
+
63
+ let errorMessage: string;
64
+
65
+ switch (response.status) {
66
+ case HTTP_OK:
67
+ case HTTP_ACCEPTED:
68
+ // https://go.mparticle.com/work/SQDSDKS-6670
69
+ message =
70
+ 'Successfully sent forwarding stats to mParticle Servers';
71
+ break;
72
+ default:
73
+ // 400 has an error message, but 403 doesn't
74
+ if (aliasResponseBody?.message) {
75
+ errorMessage = aliasResponseBody.message;
76
+ }
77
+ message =
78
+ 'Issue with sending Alias Request to mParticle Servers, received HTTP Code of ' +
79
+ response.status;
80
+ }
81
+
82
+ verbose(message);
83
+ invokeAliasCallback(aliasCallback, response.status, errorMessage);
84
+ } catch (e) {
85
+ const err = e as Error;
86
+ error('Error sending alias request to mParticle servers. ' + err);
87
+ invokeAliasCallback(aliasCallback, HTTPCodes.noHttpCoverage, (err.message));
88
+ }
89
+ };
package/src/constants.ts CHANGED
@@ -189,4 +189,10 @@ export default Constants;
189
189
 
190
190
  // https://go.mparticle.com/work/SQDSDKS-6080
191
191
  export const ONE_DAY_IN_SECONDS = 60 * 60 * 24;
192
- export const MILLIS_IN_ONE_SEC = 1000;
192
+ export const MILLIS_IN_ONE_SEC = 1000;
193
+
194
+ // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
195
+ export const HTTP_OK = 200 as const;
196
+ export const HTTP_ACCEPTED = 202 as const;
197
+ export const HTTP_BAD_REQUEST = 400 as const;
198
+ export const HTTP_FORBIDDEN = 403 as const;
@@ -115,6 +115,15 @@ export interface IAliasRequest {
115
115
  scope?: string;
116
116
  }
117
117
 
118
+ export interface IAliasCallback {
119
+ (result: IAliasResult): void;
120
+ }
121
+
122
+ export interface IAliasResult {
123
+ httpCode: number;
124
+ message: string;
125
+ }
126
+
118
127
  export interface SDKIdentityApi {
119
128
  HTTPCodes: typeof HTTPCodes;
120
129
  identify?(
package/src/identity.js CHANGED
@@ -1,4 +1,4 @@
1
- import Constants from './constants';
1
+ import Constants, { HTTP_OK } from './constants';
2
2
  import Types from './types';
3
3
  import {
4
4
  cacheOrClearIdCache,
@@ -13,7 +13,6 @@ const { CacheIdentity } = FeatureFlags;
13
13
  const { Identify, Modify, Login, Logout } = IdentityMethods;
14
14
  import {
15
15
  generateDeprecationMessage,
16
- HTTP_SUCCESS,
17
16
  isEmpty,
18
17
  isFunction,
19
18
  isObject,
@@ -1513,7 +1512,7 @@ export default function Identity(mpInstance) {
1513
1512
  );
1514
1513
  }
1515
1514
 
1516
- if (identityResponse.status === HTTP_SUCCESS) {
1515
+ if (identityResponse.status === HTTP_OK) {
1517
1516
  if (getFeatureFlag(CacheIdentity)) {
1518
1517
  const identityResponseForCache = xhrIdentityResponseAdapter(
1519
1518
  identityResponse
@@ -1,5 +1,6 @@
1
1
  import Constants from './constants';
2
2
  import { xhrIdentityResponseAdapter } from './identity-utils';
3
+ import { sendAliasRequest } from './aliasRequestApiClient';
3
4
 
4
5
  var HTTPCodes = Constants.HTTPCodes,
5
6
  Messages = Constants.Messages;
@@ -7,59 +8,8 @@ var HTTPCodes = Constants.HTTPCodes,
7
8
  const { Modify } = Constants.IdentityMethods;
8
9
 
9
10
  export default function IdentityAPIClient(mpInstance) {
10
- this.sendAliasRequest = function(aliasRequest, callback) {
11
- var xhr,
12
- xhrCallback = function() {
13
- if (xhr.readyState === 4) {
14
- // https://go.mparticle.com/work/SQDSDKS-6368
15
- mpInstance.Logger.verbose(
16
- 'Received ' + xhr.statusText + ' from server'
17
- );
18
- //only parse error messages from failing requests
19
- if (xhr.status !== 200 && xhr.status !== 202) {
20
- if (xhr.responseText) {
21
- var response = JSON.parse(xhr.responseText);
22
- if (response.hasOwnProperty('message')) {
23
- var errorMessage = response.message;
24
- mpInstance._Helpers.invokeAliasCallback(
25
- callback,
26
- xhr.status,
27
- errorMessage
28
- );
29
- return;
30
- }
31
- }
32
- }
33
- mpInstance._Helpers.invokeAliasCallback(
34
- callback,
35
- xhr.status
36
- );
37
- }
38
- };
39
- mpInstance.Logger.verbose(Messages.InformationMessages.SendAliasHttp);
40
-
41
- xhr = mpInstance._Helpers.createXHR(xhrCallback);
42
- if (xhr) {
43
- try {
44
- xhr.open(
45
- 'post',
46
- mpInstance._Helpers.createServiceUrl(
47
- mpInstance._Store.SDKConfig.aliasUrl,
48
- mpInstance._Store.devToken
49
- ) + '/Alias'
50
- );
51
- xhr.send(JSON.stringify(aliasRequest));
52
- } catch (e) {
53
- mpInstance._Helpers.invokeAliasCallback(
54
- callback,
55
- HTTPCodes.noHttpCoverage,
56
- e
57
- );
58
- mpInstance.Logger.error(
59
- 'Error sending alias request to mParticle servers. ' + e
60
- );
61
- }
62
- }
11
+ this.sendAliasRequest = async function(aliasRequest, callback) {
12
+ await sendAliasRequest(mpInstance, aliasRequest, callback);
63
13
  };
64
14
 
65
15
  this.sendIdentityRequest = function(
@@ -16,7 +16,11 @@ import { IPersistence } from './persistence.interfaces';
16
16
  import { IMPSideloadedKit } from './sideloadedKit';
17
17
  import { ISessionManager } from './sessionManager';
18
18
  import { Kit, MPForwarder } from './forwarders.interfaces';
19
- import { IIdentity, SDKIdentityApi } from './identity.interfaces';
19
+ import {
20
+ IIdentity,
21
+ SDKIdentityApi,
22
+ IAliasCallback,
23
+ } from './identity.interfaces';
20
24
  import {
21
25
  ISDKUserAttributeChangeData,
22
26
  ISDKUserIdentityChanges,
@@ -265,6 +269,11 @@ export interface SDKHelpersApi {
265
269
  generateHash?(value: string): string;
266
270
  // https://go.mparticle.com/work/SQDSDKS-6317
267
271
  getFeatureFlag?(feature: string): boolean | string; // TODO: Feature Constants should be converted to enum
272
+ invokeAliasCallback(
273
+ aliasCallback: IAliasCallback,
274
+ number: number,
275
+ errorMessage: string
276
+ ): void;
268
277
  isDelayedByIntegration?(
269
278
  delayedIntegrations: Dictionary<boolean>,
270
279
  timeoutStart: number,
package/src/uploaders.ts CHANGED
@@ -47,6 +47,7 @@ export class XHRUploader extends AsyncUploader {
47
47
  // 3 LOADING Downloading; responseText holds partial data.
48
48
  // 4 DONE The operation is complete.
49
49
 
50
+ // https://go.mparticle.com/work/SQDSDKS-6736
50
51
  private async makeRequest(
51
52
  url: string,
52
53
  data: string,
@@ -58,13 +59,17 @@ export class XHRUploader extends AsyncUploader {
58
59
  if (xhr.readyState !== 4) return;
59
60
 
60
61
  // Process the response
61
- if (xhr.status >= 200 && xhr.status < 300) {
62
- resolve(xhr as unknown as Response);
63
- } else {
64
- reject(xhr);
65
- }
62
+ // We resolve all xhr responses whose ready state is 4 regardless of HTTP codes that may be errors (400+)
63
+ // because these are valid HTTP responses.
64
+ resolve((xhr as unknown) as Response);
66
65
  };
67
66
 
67
+ // Reject a promise only when there is an xhr error
68
+ xhr.onerror = () => {
69
+ reject((xhr as unknown) as Response);
70
+ };
71
+
72
+
68
73
  xhr.open(method, url);
69
74
  xhr.send(data);
70
75
  });
package/src/utils.ts CHANGED
@@ -9,8 +9,6 @@ export type Dictionary<V = any> = Record<string, V>;
9
9
 
10
10
  export type Environment = 'development' | 'production';
11
11
 
12
- export const HTTP_SUCCESS = 200 as const;
13
-
14
12
  const createCookieString = (value: string): string =>
15
13
  replaceCommasWithPipes(replaceQuotesWithApostrophes(value));
16
14