@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.
- package/README.md +119 -111
- package/dist/bento-node-sdk.cjs.development.js +185 -26
- package/dist/bento-node-sdk.cjs.development.js.map +1 -1
- package/dist/bento-node-sdk.cjs.production.min.js +1 -1
- package/dist/bento-node-sdk.cjs.production.min.js.map +1 -1
- package/dist/bento-node-sdk.esm.js +186 -26
- package/dist/bento-node-sdk.esm.js.map +1 -1
- package/dist/sdk/batch/enums.d.ts +1 -0
- package/dist/sdk/client/errors.d.ts +0 -2
- package/dist/sdk/commands/enums.d.ts +1 -0
- package/dist/sdk/commands/index.d.ts +8 -1
- package/dist/versions/v1/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/sdk/batch/enums.ts +1 -0
- package/src/sdk/batch/events.d.ts +16 -1
- package/src/sdk/client/errors.ts +0 -1
- package/src/sdk/client/index.ts +22 -7
- package/src/sdk/commands/enums.ts +1 -0
- package/src/sdk/commands/index.ts +33 -4
- package/src/sdk/commands/types.d.ts +5 -0
- package/src/versions/v1/index.ts +2 -2
- package/src/versions/v1/types.d.ts +1 -1
package/src/sdk/client/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fetch from 'isomorphic-fetch';
|
|
2
|
-
import {
|
|
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
|
-
|
|
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
|
}
|
|
@@ -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
|
}
|
package/src/versions/v1/index.ts
CHANGED
|
@@ -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;
|