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