@internetarchive/ia-book-actions 0.0.16-4 → 0.0.16-5

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": "@internetarchive/ia-book-actions",
3
- "version": "0.0.16-4",
3
+ "version": "0.0.16-5",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -4,7 +4,7 @@
4
4
  * Helper to call loan service
5
5
  * @param {Object} options
6
6
  */
7
- export default async function ActionsHandlerService(options) {
7
+ export default function ActionsHandlerService(options) {
8
8
  const option = {
9
9
  action: null,
10
10
  identifier: '',
@@ -16,13 +16,7 @@ export default async function ActionsHandlerService(options) {
16
16
  let baseHost = '/services/loans/loan';
17
17
  const location = window?.location;
18
18
 
19
- console.log('failed-to-fecth-loan-data fix is here');
20
-
21
- // return error reponse when not production and has ?error=true param...
22
- const tokenError = 'loan token not found. please try again later.';
23
- const borrowError =
24
- 'This book is not available to borrow at this time. Please try again later.';
25
- const erroneousActions = ['browse_book', 'borrow_book', 'create_token'];
19
+ // return intentional error on dev environment
26
20
  const shouldReturnError =
27
21
  location?.href?.indexOf('?error=true') !== -1 &&
28
22
  location?.hostname !== 'archive.org';
@@ -32,39 +26,43 @@ export default async function ActionsHandlerService(options) {
32
26
  let formData = new FormData();
33
27
  formData.append('action', option.action);
34
28
  formData.append('identifier', option.identifier);
35
- try {
36
- await fetch(baseHost, {
37
- method: 'POST',
38
- body: formData,
39
- })
40
- .then(response => {
41
- // intentional error on localhost
42
- if (shouldReturnError && erroneousActions.includes(option?.action)) {
43
- return {
44
- success: false,
45
- error: option?.action === 'create_token' ? tokenError : borrowError,
46
- };
47
- }
48
29
 
49
- // return success response for /demo/ server...
50
- if (baseHost == '/demo/1' || baseHost == '/demo/') {
51
- return {
52
- success: true,
53
- message: 'operation executed successfully!',
54
- };
55
- }
30
+ fetch(baseHost, {
31
+ method: 'POST',
32
+ body: formData,
33
+ })
34
+ .then(response => {
35
+ const tokenError = 'loan token not found. please try again later.';
36
+ const borrowError =
37
+ 'This book is not available to borrow at this time. Please try again later.';
38
+
39
+ // return error reponse when not production and has ?error=true param...
40
+ const erroneousActions = ['browse_book', 'borrow_book', 'create_token'];
41
+ if (shouldReturnError && erroneousActions.includes(option?.action)) {
42
+ return {
43
+ success: false,
44
+ error: option?.action === 'create_token' ? tokenError : borrowError,
45
+ };
46
+ }
56
47
 
57
- // The response is a Response instance.
58
- // You parse the data into a useable format using `.json()`
59
- return response.json();
60
- })
61
- .then(data => {
62
- // `data` is the parsed version of the JSON returned from the above endpoint.
63
- if (!data?.error) {
64
- option?.success(data);
65
- } else {
66
- option?.error(data);
67
- }
68
- });
69
- } catch (err) {}
48
+ // return success response for /demo/ server...
49
+ if (baseHost == '/demo/1' || baseHost == '/demo/') {
50
+ return {
51
+ success: true,
52
+ message: 'operation executed successfully!',
53
+ };
54
+ }
55
+
56
+ // The response is a Response instance.
57
+ // You parse the data into a useable format using `.json()`
58
+ return response.json();
59
+ })
60
+ .then(data => {
61
+ // `data` is the parsed version of the JSON returned from the above endpoint.
62
+ if (!data?.error) {
63
+ option?.success(data);
64
+ } else {
65
+ option?.error(data);
66
+ }
67
+ });
70
68
  }
@@ -194,6 +194,10 @@ export default class ActionsHandler extends LitElement {
194
194
  * @fires ActionsHandler#lendingActionError
195
195
  */
196
196
  dispatchActionError(action, data = {}) {
197
+ console.log('LendingServiceError');
198
+ // send LendingServiceError to GA
199
+ this.sendEvent('LendingServiceError', action || 'unknown_action');
200
+
197
201
  this.dispatchEvent(
198
202
  new CustomEvent('lendingActionError', {
199
203
  detail: { action, data },