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 | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 23 | // Function to wrap calls to the Community Console API with intelligent error |
| 24 | // handling. |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 25 | export function CCApi( |
| 26 | method, data, authenticated, authuser = 0, |
| 27 | returnUnauthorizedStatus = false) { |
Adrià Vilanova Martínez | feb2819 | 2021-08-07 23:31:17 +0200 | [diff] [blame] | 28 | let authuserPart = |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 29 | authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser); |
| 30 | |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 31 | let context; |
Adrià Vilanova Martínez | 96382a9 | 2022-04-03 20:48:36 +0200 | [diff] [blame] | 32 | // #!if browser_target == 'gecko' |
| 33 | // See |
| 34 | // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#xhr_and_fetch |
| 35 | // 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] | 36 | context = window.content || window; |
Adrià Vilanova Martínez | 96382a9 | 2022-04-03 20:48:36 +0200 | [diff] [blame] | 37 | // #!else |
Adrià Vilanova Martínez | bbc8d7c | 2024-04-21 16:28:05 +0200 | [diff] [blame] | 38 | context = window; |
Adrià Vilanova Martínez | 96382a9 | 2022-04-03 20:48:36 +0200 | [diff] [blame] | 39 | // #!endif |
| 40 | |
| 41 | return context |
| 42 | .fetch(CC_API_BASE_URL + method + authuserPart, { |
| 43 | 'headers': { |
| 44 | 'content-type': 'text/plain; charset=utf-8', |
| 45 | }, |
| 46 | 'body': JSON.stringify(data), |
| 47 | 'method': 'POST', |
| 48 | 'mode': 'cors', |
| 49 | 'credentials': (authenticated ? 'include' : 'omit'), |
| 50 | }) |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 51 | .then(res => { |
| 52 | if (res.status == 200 || res.status == 400) { |
| 53 | return res.json().then(data => ({ |
| 54 | status: res.status, |
| 55 | body: data, |
| 56 | })); |
| 57 | } else { |
| 58 | throw new Error( |
| 59 | 'Status code ' + res.status + ' was not expected when calling ' + |
| 60 | method + '.'); |
| 61 | } |
| 62 | }) |
| 63 | .then(res => { |
| 64 | if (res.status == 400) { |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 65 | // If the canonicalCode is PERMISSION_DENIED: |
| 66 | if (returnUnauthorizedStatus && res.body?.[2] == 7) |
| 67 | return { |
| 68 | unauthorized: true, |
| 69 | }; |
| 70 | |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 71 | throw new Error( |
| 72 | res.body[4] || |
| 73 | ('Response status 400 for method ' + method + '. ' + |
Adrià Vilanova Martínez | feb2819 | 2021-08-07 23:31:17 +0200 | [diff] [blame] | 74 | 'Error code: ' + |
| 75 | (apiErrors[res.body?.[2]] ?? res.body?.[2] ?? 'unknown'))); |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 78 | if (returnUnauthorizedStatus) |
| 79 | return { |
| 80 | unauthorized: false, |
| 81 | body: res.body, |
| 82 | }; |
| 83 | |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 84 | return res.body; |
| 85 | }); |
| 86 | } |