forked from cloudflare/worker-template-fetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (106 loc) · 3.1 KB
/
index.js
File metadata and controls
117 lines (106 loc) · 3.1 KB
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
/**
* Example someHost is set up to respond with JSON and HTML according to the path
* */
const someHost = 'https://workers-tooling.cf/demos'
const someJSONURL = someHost + '/requests/json'
const someHTMLURL = someHost + '/static/html'
const someJSONToSend = {
results: ['default data to send'],
errors: null,
msg: 'I sent this to the fetch',
}
const someDefaultJSONToRespond = {
results: ['default result'],
errors: null,
msg: 'success in sending a POST',
}
/**
* gatherResponse awaits and returns a response body as a string.
* Use await gatherResponse(..) in an async function to get the response body
* @param {Response} response to
*/
async function gatherResponse(response) {
const { headers } = response
const contentType = headers.get('content-type')
if (contentType.includes('application/json')) {
const body = await response.json()
return JSON.stringify(body)
} else if (contentType.includes('application/text')) {
const body = await response.text()
return body
} else if (contentType.includes('text/html')) {
const body = await response.text()
return body
} else {
const body = await response.text()
return body
}
}
/**
* fetchPostJson sends a POST request with data in JSON and
* and reads in the response body. Use await fetchPostJson(..)
* in an async function to get the response body
* @param {string} url the URL to send the request to
* @param {BodyInit} body the JSON data to send in the request
*/
async function fetchPostJson(url, body = {}) {
const init = {
body: JSON.stringify(body),
method: 'POST',
headers: {
'content-type': 'application/json;charset=UTF-8',
},
}
const response = await fetch(url, init)
const results = await gatherResponse(response)
const retBody = Object.assign(someDefaultJSONToRespond, { results })
return JSON.stringify(retBody)
}
/**
* fetchGetHtml sends a GET request expecting html
* Use await fetchGetHtml(..) in an async function to get the HTML
* @param {string} url the URL to send the request to
*/
async function fetchGetHtml(url) {
const init = {
method: 'Get',
headers: {
'content-type': 'text/html;charset=UTF-8',
},
}
const response = await fetch(url)
const respBody = await gatherResponse(response)
return respBody
}
/**
* Example of how fetch methods above can be used in an application
* */
addEventListener('fetch', async event => {
const { url, method } = event.request
// Set respBody and init according to the route
// and method of the incoming request
if (url.endsWith('/html')) {
init = {
headers: {
'content-type': 'text/html;charset=UTF-8',
},
}
respBody = fetchGetHtml(someHTMLURL)
}
if (url.endsWith('/json')) {
init = {
headers: {
'content-type': 'application/json;charset=UTF-8',
},
}
respBody = fetchPostJson(someJSONURL, someJSONToSend)
}
// Turn the the respBody string into a Response
// return this response to the requester
event.respondWith(
(async function() {
const body = await respBody
return new Response(body, init)
})()
)
})