| 18 | }; |
| 19 | |
| 20 | const handler = async (request: Request, env: Env, ctx: ExecutionContext) => { |
| 21 | const handler = createRouteHandler({ |
| 22 | router: uploadRouter, |
| 23 | config: { |
| 24 | /** |
| 25 | * Since workers doesn't have envs on `process`. We need to pass |
| 26 | * secret and isDev flag manually. |
| 27 | */ |
| 28 | token: env.UPLOADTHING_TOKEN, |
| 29 | isDev: env.ENVIRONMENT === "development", |
| 30 | logLevel: "Debug", |
| 31 | /* |
| 32 | * Cloudflare Workers doesn't support the cache option |
| 33 | * so we need to remove it from the request init. |
| 34 | */ |
| 35 | fetch: (url, init) => { |
| 36 | if (init && "cache" in init) delete init.cache; |
| 37 | return fetch(url, init); |
| 38 | }, |
| 39 | handleDaemonPromise: (promise) => ctx.waitUntil(promise), |
| 40 | ingestUrl: "http://localhost:3001", |
| 41 | }, |
| 42 | }); |
| 43 | |
| 44 | // World's simplest router. Handle GET/POST requests to /api/uploadthing |
| 45 | switch (new URL(request.url).pathname) { |
| 46 | case "/api": { |
| 47 | return cors(new Response("Hello from Cloudflare Workers!")); |
| 48 | } |
| 49 | case "/api/uploadthing": { |
| 50 | if (request.method !== "GET" && request.method !== "POST") { |
| 51 | return cors(new Response("Method not allowed", { status: 405 })); |
| 52 | } |
| 53 | |
| 54 | const response = await handler(request); |
| 55 | return cors(response); |
| 56 | } |
| 57 | default: { |
| 58 | return cors(new Response("Not found", { status: 404 })); |
| 59 | } |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | export default { |
| 64 | async fetch(request, env, ctx) { |