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', |
| 16 | 12: 'OUT_OF_RANGE', |
| 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 | |
| 31 | return fetch(CC_API_BASE_URL + method + authuserPart, { |
| 32 | 'headers': { |
| 33 | 'content-type': 'text/plain; charset=utf-8', |
| 34 | }, |
| 35 | 'body': JSON.stringify(data), |
| 36 | 'method': 'POST', |
| 37 | 'mode': 'cors', |
| 38 | 'credentials': (authenticated ? 'include' : 'omit'), |
| 39 | }) |
| 40 | .then(res => { |
| 41 | if (res.status == 200 || res.status == 400) { |
| 42 | return res.json().then(data => ({ |
| 43 | status: res.status, |
| 44 | body: data, |
| 45 | })); |
| 46 | } else { |
| 47 | throw new Error( |
| 48 | 'Status code ' + res.status + ' was not expected when calling ' + |
| 49 | method + '.'); |
| 50 | } |
| 51 | }) |
| 52 | .then(res => { |
| 53 | if (res.status == 400) { |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 54 | // If the canonicalCode is PERMISSION_DENIED: |
| 55 | if (returnUnauthorizedStatus && res.body?.[2] == 7) |
| 56 | return { |
| 57 | unauthorized: true, |
| 58 | }; |
| 59 | |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 60 | throw new Error( |
| 61 | res.body[4] || |
| 62 | ('Response status 400 for method ' + method + '. ' + |
Adrià Vilanova Martínez | feb2819 | 2021-08-07 23:31:17 +0200 | [diff] [blame] | 63 | 'Error code: ' + |
| 64 | (apiErrors[res.body?.[2]] ?? res.body?.[2] ?? 'unknown'))); |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 65 | } |
| 66 | |
Adrià Vilanova Martínez | c41edf4 | 2021-07-18 02:06:55 +0200 | [diff] [blame] | 67 | if (returnUnauthorizedStatus) |
| 68 | return { |
| 69 | unauthorized: false, |
| 70 | body: res.body, |
| 71 | }; |
| 72 | |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 73 | return res.body; |
| 74 | }); |
| 75 | } |