blob: 4a68c873321c97cb29d853cbbc525d8b2b7dca02 [file] [log] [blame]
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +01001import {correctArrayKeys, inverseCorrectArrayKeys} from '../common/protojs';
Adrià Vilanova Martínez102d54b2022-12-18 11:12:11 +01002
Adrià Vilanova Martínez9c418ab2024-12-05 15:34:40 +01003import xhrInterceptors from './interceptors/interceptors';
Adrià Vilanova Martínez102d54b2022-12-18 11:12:11 +01004
Adrià Vilanova Martínez9c418ab2024-12-05 15:34:40 +01005/**
6 * @deprecated Use `InterceptorHandler`.
7 */
Adrià Vilanova Martínez102d54b2022-12-18 11:12:11 +01008export function matchInterceptors(interceptFilter, url) {
9 return xhrInterceptors.interceptors.filter(interceptor => {
10 var regex = new RegExp(interceptor.urlRegex);
11 return interceptor.intercepts == interceptFilter && regex.test(url);
12 });
13}
14
15export function getResponseText(xhr, transformArrayPb = true) {
16 let response;
17 if (xhr.responseType === 'arraybuffer') {
18 var arrBuffer = xhr.response;
19 if (!arrBuffer) {
20 console.error('No array buffer.');
21 return undefined;
22 }
23 let byteArray = new Uint8Array(arrBuffer);
24 let dec = new TextDecoder('utf-8');
25 response = dec.decode(byteArray);
26 } else if (xhr.responseType === 'text' || xhr.responseType === '') {
27 response = xhr.responseText;
28 } else if (xhr.responseType === 'json') {
29 response = JSON.stringify(xhr.response);
30 } else {
31 console.error(
32 'Unexpected responseType ' + xhr.responseType + '. Request url: ',
33 xhr.$TWPTRequestURL);
34 return undefined;
35 }
36
37 if (xhr.$isArrayProto && transformArrayPb)
38 response = correctArrayKeys(response);
39
40 return response;
41}
42
43export function getResponseJSON(xhr) {
44 let response;
45 if (xhr.responseType === 'arraybuffer') {
46 var arrBuffer = xhr.response;
47 if (!arrBuffer) {
48 console.error('No array buffer.');
49 return undefined;
50 }
51 let byteArray = new Uint8Array(arrBuffer);
52 let dec = new TextDecoder('utf-8');
53 let rawResponse = dec.decode(byteArray);
54 response = JSON.parse(rawResponse);
55 } else if (xhr.responseType === 'text' || xhr.responseType === '') {
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010056 response = JSON.parse(xhr.response);
Adrià Vilanova Martínez102d54b2022-12-18 11:12:11 +010057 } else if (xhr.responseType === 'json') {
58 response = xhr.response;
59 } else {
60 console.error(
61 'Unexpected responseType ' + xhr.responseType + '. Request url: ',
62 xhr.$TWPTRequestURL);
63 return undefined;
64 }
65
66 if (xhr.$isArrayProto) response = correctArrayKeys(response);
67 return response;
68}
69
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010070export function convertJSONToResponse(xhr, json) {
71 if (xhr.$isArrayProto) json = inverseCorrectArrayKeys(json);
72
73 switch (xhr.responseType) {
74 case 'json':
75 return json;
76
77 case 'text':
78 case '':
79 return JSON.stringify(json);
80
81 case 'arraybuffer':
Adrià Vilanova Martínez8a17fa82023-02-04 19:19:27 +010082 var encoder = new TextEncoder();
Adrià Vilanova Martínezac2a5612022-12-27 21:51:40 +010083 return encoder.encode(JSON.stringify(json)).buffer;
84
85 default:
86 console.error(
87 'Unexpected responseType ' + xhr.responseType + '. Request url: ',
88 xhr.$TWPTRequestURL || xhr.responseURL);
89 return undefined;
90 }
91}
92
Adrià Vilanova Martínez8a17fa82023-02-04 19:19:27 +010093export function convertJSONToResponseText(xhr, json) {
Adrià Vilanova Martínez9c418ab2024-12-05 15:34:40 +010094 return convertJSONToResponse(
95 {
96 $isArrayProto: xhr.$isArrayProto,
97 responseType: 'text',
98 },
99 json);
Adrià Vilanova Martínez8a17fa82023-02-04 19:19:27 +0100100}
101
Adrià Vilanova Martínez9c418ab2024-12-05 15:34:40 +0100102/**
103 * @deprecated Use `InterceptorHandler`.
104 */
Adrià Vilanova Martínez102d54b2022-12-18 11:12:11 +0100105export function triggerEvent(eventName, body, id) {
106 var evt = new CustomEvent('TWPT_' + eventName, {
107 detail: {
108 body,
109 id,
110 }
111 });
112 window.dispatchEvent(evt);
113}