Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 1 | import {correctArrayKeys, inverseCorrectArrayKeys} from '../common/protojs'; |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 2 | |
Adrià Vilanova Martínez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 3 | import xhrInterceptors from './interceptors/interceptors'; |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 5 | /** |
| 6 | * @deprecated Use `InterceptorHandler`. |
| 7 | */ |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 8 | export 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 | |
| 15 | export 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 | |
| 43 | export 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ínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 56 | response = JSON.parse(xhr.response); |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 57 | } 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ínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 70 | export 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ínez | 8a17fa8 | 2023-02-04 19:19:27 +0100 | [diff] [blame] | 82 | var encoder = new TextEncoder(); |
Adrià Vilanova Martínez | ac2a561 | 2022-12-27 21:51:40 +0100 | [diff] [blame] | 83 | 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ínez | 8a17fa8 | 2023-02-04 19:19:27 +0100 | [diff] [blame] | 93 | export function convertJSONToResponseText(xhr, json) { |
Adrià Vilanova Martínez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 94 | return convertJSONToResponse( |
| 95 | { |
| 96 | $isArrayProto: xhr.$isArrayProto, |
| 97 | responseType: 'text', |
| 98 | }, |
| 99 | json); |
Adrià Vilanova Martínez | 8a17fa8 | 2023-02-04 19:19:27 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Adrià Vilanova Martínez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 102 | /** |
| 103 | * @deprecated Use `InterceptorHandler`. |
| 104 | */ |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 105 | export 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 | } |