Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 1 | const CC_API_BASE_URL = 'https://support.google.com/s/community/api/'; |
| 2 | |
Adrià Vilanova Martínez | feb2819 | 2021-08-07 23:31:17 +0200 | [diff] [blame] | 3 | const apiErrors = { |
| 4 | 0: 'OK', |
| 5 | 1: 'CANCELLED', |
| 6 | 2: 'UNKNOWN', |
| 7 | 3: 'INVALID_ARGUMENT', |
| 8 | 4: 'DEADLINE_EXCEEDED', |
| 9 | 5: 'NOT_FOUND', |
| 10 | 6: 'ALREADY_EXISTS', |
| 11 | 7: 'PERMISSION_DENIED', |
| 12 | 8: 'RESOURCE_EXHAUSTED', |
| 13 | 9: 'FAILED_PRECONDITION', |
| 14 | 10: 'ABORTED', |
| 15 | 11: 'OUT_OF_RANGE', |
avm99963 | 47c407b | 2021-08-18 09:54:35 +0200 | [diff] [blame] | 16 | 12: 'UNIMPLEMENTED', |
Adrià Vilanova Martínez | feb2819 | 2021-08-07 23:31:17 +0200 | [diff] [blame] | 17 | 13: 'INTERNAL', |
| 18 | 14: 'UNAVAILABLE', |
| 19 | 15: 'DATA_LOSS', |
| 20 | 16: 'UNAUTHENTICATED', |
| 21 | }; |
| 22 | |
Adrià Vilanova Martínez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 23 | export const XClientHeader = 'X-Client'; |
| 24 | export const XClientValue = 'twpt'; |
| 25 | |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 26 | // Function to wrap calls to the Community Console API with intelligent error |
| 27 | // handling. |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 28 | export function CCApi( |
| 29 | method, data, authenticated, authuser = 0, |
| 30 | returnUnauthorizedStatus = false) { |
Adrià Vilanova Martínez | feb2819 | 2021-08-07 23:31:17 +0200 | [diff] [blame] | 31 | let authuserPart = |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 32 | authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser); |
| 33 | |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 34 | let context; |
Adrià Vilanova Martínez | 96382a9 | 2022-04-03 20:48:36 +0200 | [diff] [blame] | 35 | // #!if browser_target == 'gecko' |
| 36 | // See |
| 37 | // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#xhr_and_fetch |
| 38 | // and https://developer.mozilla.org/en-US/docs/Web/API/Window/content. |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 39 | context = window.content || window; |
Adrià Vilanova Martínez | 96382a9 | 2022-04-03 20:48:36 +0200 | [diff] [blame] | 40 | // #!else |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 41 | context = window; |
Adrià Vilanova Martínez | 96382a9 | 2022-04-03 20:48:36 +0200 | [diff] [blame] | 42 | // #!endif |
| 43 | |
| 44 | return context |
| 45 | .fetch(CC_API_BASE_URL + method + authuserPart, { |
| 46 | 'headers': { |
| 47 | 'content-type': 'text/plain; charset=utf-8', |
Adrià Vilanova Martínez | 9c418ab | 2024-12-05 15:34:40 +0100 | [diff] [blame] | 48 | // Used to exclude our requests from being handled by FetchProxy. |
| 49 | // FetchProxy will remove this header. |
| 50 | [XClientHeader]: XClientValue, |
Adrià Vilanova Martínez | 96382a9 | 2022-04-03 20:48:36 +0200 | [diff] [blame] | 51 | }, |
| 52 | 'body': JSON.stringify(data), |
| 53 | 'method': 'POST', |
| 54 | 'mode': 'cors', |
| 55 | 'credentials': (authenticated ? 'include' : 'omit'), |
| 56 | }) |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 57 | .then(res => { |
| 58 | if (res.status == 200 || res.status == 400) { |
| 59 | return res.json().then(data => ({ |
| 60 | status: res.status, |
| 61 | body: data, |
| 62 | })); |
| 63 | } else { |
| 64 | throw new Error( |
| 65 | 'Status code ' + res.status + ' was not expected when calling ' + |
| 66 | method + '.'); |
| 67 | } |
| 68 | }) |
| 69 | .then(res => { |
| 70 | if (res.status == 400) { |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 71 | // If the canonicalCode is PERMISSION_DENIED: |
| 72 | if (returnUnauthorizedStatus && res.body?.[2] == 7) |
| 73 | return { |
| 74 | unauthorized: true, |
| 75 | }; |
| 76 | |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 77 | throw new Error( |
| 78 | res.body[4] || |
| 79 | ('Response status 400 for method ' + method + '. ' + |
Adrià Vilanova Martínez | feb2819 | 2021-08-07 23:31:17 +0200 | [diff] [blame] | 80 | 'Error code: ' + |
| 81 | (apiErrors[res.body?.[2]] ?? res.body?.[2] ?? 'unknown'))); |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 84 | if (returnUnauthorizedStatus) |
| 85 | return { |
| 86 | unauthorized: false, |
| 87 | body: res.body, |
| 88 | }; |
| 89 | |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 90 | return res.body; |
| 91 | }); |
| 92 | } |