forked from cf-pages/Telegraph-Image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.js
29 lines (29 loc) · 1.22 KB
/
upload.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
import { errorHandling, telemetryData } from "./utils/middleware";
export async function onRequestPost(context) { // Contents of context object
const {
request, // same as existing Worker API
env, // same as existing Worker API
params, // if filename includes [id] or [[path]]
waitUntil, // same as ctx.waitUntil in existing Worker API
next, // used for middleware or to fetch assets
data, // arbitrary space for passing data between middlewares
} = context;
const clonedRequest = request.clone();
await errorHandling(context);
telemetryData(context);
const url = new URL(clonedRequest.url);
url.searchParams.append('source', 'bugtracker');
const response = await fetch('https://telegra.ph/' + url.pathname + url.search, {
method: clonedRequest.method,
headers: clonedRequest.headers,
body: clonedRequest.body,
});
const originalBody = await response.json();
const wrappedBody = [originalBody];
const modifiedResponse = new Response(JSON.stringify(wrappedBody), {
status: response.status,
statusText: response.statusText,
headers: response.headers
});
return modifiedResponse;
}