@bentonow/bento-node-sdk 0.1.9 → 0.1.12

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.
@@ -1,5 +1,5 @@
1
1
  import fetch from 'isomorphic-fetch';
2
- import { InternalServerError, NotAuthorizedError } from './errors';
2
+ import { NotAuthorizedError } from './errors';
3
3
  import { AnalyticsOptions, AuthenticationOptions } from '../interfaces';
4
4
 
5
5
  export class BentoClient {
@@ -29,12 +29,12 @@ export class BentoClient {
29
29
  method: 'GET',
30
30
  headers: this._headers,
31
31
  })
32
- .then(result => {
32
+ .then(async result => {
33
33
  if (this._isSuccessfulStatus(result.status)) {
34
34
  return result.json();
35
35
  }
36
36
 
37
- throw this._getErrorForResponse(result);
37
+ throw await this._getErrorForResponse(result);
38
38
  })
39
39
  .then(data => resolve(data))
40
40
  .catch(error => reject(error));
@@ -61,12 +61,12 @@ export class BentoClient {
61
61
  },
62
62
  body,
63
63
  })
64
- .then(result => {
64
+ .then(async result => {
65
65
  if (this._isSuccessfulStatus(result.status)) {
66
66
  return result.json();
67
67
  }
68
68
 
69
- throw this._getErrorForResponse(result);
69
+ throw await this._getErrorForResponse(result);
70
70
  })
71
71
  .then(data => resolve(data))
72
72
  .catch(error => reject(error));
@@ -143,8 +143,23 @@ export class BentoClient {
143
143
  * @param response Response
144
144
  * @returns Error
145
145
  */
146
- private _getErrorForResponse(response: Response): Error {
146
+ private async _getErrorForResponse(response: Response): Promise<Error> {
147
147
  if (response.status === 401) return new NotAuthorizedError();
148
- return new InternalServerError();
148
+ const contentType = response.headers.get('Content-Type');
149
+ let responseMessage = '';
150
+
151
+ switch (contentType?.toLocaleLowerCase()) {
152
+ case 'text/plain':
153
+ responseMessage = await response.text();
154
+ break;
155
+ case 'application/json':
156
+ responseMessage = JSON.stringify(await response.json());
157
+ break;
158
+ default:
159
+ responseMessage = 'Unknown response from the Bento API.';
160
+ break;
161
+ }
162
+
163
+ return new Error(`[${response.status}] - ${responseMessage}`);
149
164
  }
150
165
  }
@@ -4,6 +4,7 @@
4
4
  export enum CommandTypes {
5
5
  ADD_FIELD = 'add_field',
6
6
  ADD_TAG = 'add_tag',
7
+ CHANGE_EMAIL = 'change_email',
7
8
  REMOVE_FIELD = 'remove_field',
8
9
  REMOVE_TAG = 'remove_tag',
9
10
  SUBSCRIBE = 'subscribe',
@@ -1,15 +1,16 @@
1
+ import { BentoClient } from '../client';
2
+ import { DataResponse } from '../client/types';
3
+ import { Subscriber } from '../subscribers/types';
4
+ import { CommandTypes } from './enums';
1
5
  import {
2
6
  AddFieldParameters,
3
7
  AddTagParameters,
8
+ ChangeEmailParameters,
4
9
  RemoveFieldParameters,
5
10
  RemoveTagParameters,
6
11
  SubscribeParameters,
7
12
  UnsubscribeParameters,
8
13
  } from './types';
9
- import { BentoClient } from '../client';
10
- import { DataResponse } from '../client/types';
11
- import { Subscriber } from '../subscribers/types';
12
- import { CommandTypes } from './enums';
13
14
 
14
15
  export class BentoCommands<S> {
15
16
  private readonly _url = '/fetch/commands';
@@ -203,4 +204,32 @@ export class BentoCommands<S> {
203
204
  throw error;
204
205
  }
205
206
  }
207
+
208
+ /**
209
+ * Updates the email of a user in Bento.
210
+ *
211
+ * @param parameters \{ oldEmail: string, newEmail: string \}
212
+ * @returns Promise\<Subscriber | null\>
213
+ */
214
+ public async changeEmail(
215
+ parameters: ChangeEmailParameters
216
+ ): Promise<Subscriber<S> | null> {
217
+ try {
218
+ const result = await this._client.post<DataResponse<Subscriber<S>>>(
219
+ this._url,
220
+ {
221
+ command: {
222
+ command: CommandTypes.CHANGE_EMAIL,
223
+ email: parameters.oldEmail,
224
+ query: parameters.newEmail,
225
+ },
226
+ }
227
+ );
228
+
229
+ if (Object.keys(result).length === 0 || !result.data) return null;
230
+ return result.data;
231
+ } catch (error) {
232
+ throw error;
233
+ }
234
+ }
206
235
  }
@@ -31,3 +31,8 @@ export type SubscribeParameters = {
31
31
  export type UnsubscribeParameters = {
32
32
  email: string;
33
33
  };
34
+
35
+ export type ChangeEmailParameters = {
36
+ oldEmail: string;
37
+ newEmail: string;
38
+ };
@@ -1,4 +1,3 @@
1
- import { AnalyticsOptions } from '../../sdk/interfaces';
2
1
  import {
3
2
  BentoBatch,
4
3
  BentoClient,
@@ -9,6 +8,8 @@ import {
9
8
  BentoSubscribers,
10
9
  BentoTags,
11
10
  } from '../../sdk';
11
+ import { BentoEvents } from '../../sdk/batch/enums';
12
+ import { AnalyticsOptions } from '../../sdk/interfaces';
12
13
  import {
13
14
  AddSubscriberParameters,
14
15
  RemoveSubscriberParameters,
@@ -17,7 +18,6 @@ import {
17
18
  TrackPurchaseParameters,
18
19
  UpdateFieldsParameters,
19
20
  } from './types';
20
- import { BentoEvents } from '../../sdk/batch/enums';
21
21
 
22
22
  export class BentoAPIV1<S = { [key: string]: any }, E = '$custom'> {
23
23
  private readonly _client: BentoClient;
@@ -32,4 +32,4 @@ export type UpdateFieldsParameters<S> = {
32
32
  fields: Partial<S>;
33
33
  };
34
34
 
35
- export type TrackParameters<S, E> = BaseEvent<S, E>;
35
+ export type TrackParameters<S, E> = BaseEvent<S, E> & { fields: Partial<S> };