blob: 11c0ad665879ce46baddcd3d258bfec522db2e21 [file] [log] [blame]
Adrià Vilanova Martíneza10fff22021-06-29 21:07:40 +02001const CC_API_BASE_URL = 'https://support.google.com/s/community/api/';
2
Adrià Vilanova Martínezfeb28192021-08-07 23:31:17 +02003const 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',
avm9996347c407b2021-08-18 09:54:35 +020016 12: 'UNIMPLEMENTED',
Adrià Vilanova Martínezfeb28192021-08-07 23:31:17 +020017 13: 'INTERNAL',
18 14: 'UNAVAILABLE',
19 15: 'DATA_LOSS',
20 16: 'UNAUTHENTICATED',
21};
22
Adrià Vilanova Martíneza10fff22021-06-29 21:07:40 +020023// Function to wrap calls to the Community Console API with intelligent error
24// handling.
Adrià Vilanova Martínezc41edf42021-07-18 02:06:55 +020025export function CCApi(
26 method, data, authenticated, authuser = 0,
27 returnUnauthorizedStatus = false) {
Adrià Vilanova Martínezfeb28192021-08-07 23:31:17 +020028 let authuserPart =
Adrià Vilanova Martíneza10fff22021-06-29 21:07:40 +020029 authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser);
30
Adrià Vilanova Martínez96382a92022-04-03 20:48:36 +020031 // #!if browser_target == 'gecko'
32 // See
33 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#xhr_and_fetch
34 // and https://developer.mozilla.org/en-US/docs/Web/API/Window/content.
35 const context = window.content || window;
36 // #!else
37 const context = window;
38 // #!endif
39
40 return context
41 .fetch(CC_API_BASE_URL + method + authuserPart, {
42 'headers': {
43 'content-type': 'text/plain; charset=utf-8',
44 },
45 'body': JSON.stringify(data),
46 'method': 'POST',
47 'mode': 'cors',
48 'credentials': (authenticated ? 'include' : 'omit'),
49 })
Adrià Vilanova Martíneza10fff22021-06-29 21:07:40 +020050 .then(res => {
51 if (res.status == 200 || res.status == 400) {
52 return res.json().then(data => ({
53 status: res.status,
54 body: data,
55 }));
56 } else {
57 throw new Error(
58 'Status code ' + res.status + ' was not expected when calling ' +
59 method + '.');
60 }
61 })
62 .then(res => {
63 if (res.status == 400) {
Adrià Vilanova Martínezc41edf42021-07-18 02:06:55 +020064 // If the canonicalCode is PERMISSION_DENIED:
65 if (returnUnauthorizedStatus && res.body?.[2] == 7)
66 return {
67 unauthorized: true,
68 };
69
Adrià Vilanova Martíneza10fff22021-06-29 21:07:40 +020070 throw new Error(
71 res.body[4] ||
72 ('Response status 400 for method ' + method + '. ' +
Adrià Vilanova Martínezfeb28192021-08-07 23:31:17 +020073 'Error code: ' +
74 (apiErrors[res.body?.[2]] ?? res.body?.[2] ?? 'unknown')));
Adrià Vilanova Martíneza10fff22021-06-29 21:07:40 +020075 }
76
Adrià Vilanova Martínezc41edf42021-07-18 02:06:55 +020077 if (returnUnauthorizedStatus)
78 return {
79 unauthorized: false,
80 body: res.body,
81 };
82
Adrià Vilanova Martíneza10fff22021-06-29 21:07:40 +020083 return res.body;
84 });
85}