-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
140 lines (123 loc) · 4.8 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const subdomain = 'yourdomain' //your Zendesk subdomain
const auth = '123457890qwerty=' //Base 64 encoded admin@domain.com/token:{Zendesk API token}
const source_id = '01GT1VEVYWJ10HMZKMR5YRXQHB';
const headers_body = {
'content-type': 'application/json;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET,HEAD,POST,OPTIONS',
'Access-Control-Max-Age': '86400',
}
export default {
async fetch(request, env) {
try {
if (request.method == 'OPTIONS') {
return handleOptionsRequest(request)
} else {
const { pathname } = new URL(request.url);
var path_part = pathname.split('/')
//We return all records in the federated search index
if (pathname.startsWith("/get")) {
var results = await getRecords('',[]);
return new Response(JSON.stringify(results), {headers: headers_body});
}
//We add a new record to the federated search index
if (pathname.startsWith("/add")) {
var enquiry = JSON.stringify(await request.json());
var results = await addRecord(enquiry);
return new Response(JSON.stringify(results), {headers: headers_body});
}
//We delete a record from the federated search index
if (pathname.startsWith("/delete")) {
var result = await deleteRecord(path_part[2]);
return new Response('deleted ' + result, {headers: headers_body});
}
return new Response('Path not found', {status: 404,headers: headers_body});
}
} catch(e) {
return new Response(err.stack, {status: 500,headers: headers_body});
}
}
}
async function deleteRecord(id){
const url = 'https://'+subdomain+'.zendesk.com/api/v2/guide/external_content/records/'+id;
var init = {
method: 'DELETE',
headers: {
'content-type': 'application/json;charset=UTF-8',
'Authorization': 'Basic ' + auth,
},
};
const result = await fetch(url, init);
return result;
}
async function addRecord(enquiry){
const url = 'https://'+subdomain+'.zendesk.com/api/v2/guide/external_content/records';
enquire.source_id = source_id;
var init = {
body: enquiry,
method: 'POST',
headers: {
'content-type': 'application/json;charset=UTF-8',
'Authorization': 'Basic ' + auth,
},
};
const response = await fetch(url, init);
const results = await response.json();
return id;
}
async function getRecords(url,federated_items){
url = url != ''? url : 'https://'+subdomain+'.zendesk.com/api/v2/guide/external_content/records?page[size]=10';
var init = {
method: 'GET',
headers: {
'content-type': 'application/json;charset=UTF-8',
'Authorization': 'Basic ' + auth,
},
};
const response = await fetch(url, init);
const results = await response.json();
merge(federated_items,results.records);
//Check if the results are paginated and if so, call the function again
if (results.meta.has_more == true){
return await getRecords('https://'+subdomain+'.zendesk.com/api/v2/guide/external_content/records?page[size]=10&page[after]='+results.meta.after_cursor,federated_items);
} else {
//filter federated_items to remove all records where source.id is not 01GT1VEVYWJ10HMZKMR5YRXQHB
federated_items = federated_items.filter(function(item){
return item.source.id == source_id;
});
return federated_items;
}
}
//This fixes CORS error issues.
function handleOptionsRequest(request) {
let headers = request.headers;
if (
headers.get("Origin") !== null &&
headers.get("Access-Control-Request-Method") !== null &&
headers.get("Access-Control-Request-Headers") !== null
){
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, HEAD, POST, OPTIONS",
"Access-Control-Max-Age": "86400",
"Access-Control-Allow-Headers": request.headers.get("Access-Control-Request-Headers"),
}
})
}
else {
return new Response(null, {
headers: {
Allow: "GET, HEAD, POST, OPTIONS",
},
})
}
}
//Helper function to merge the paginated results into one array
const merge = (first, second) => {
for(let i=0; i<second.length; i++) {
first.push(second[i]);
}
return first;
}