Skip to content

Commit fa6e5b0

Browse files
committed
Fix default js worker
1 parent 6fef0f2 commit fa6e5b0

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

examples/default-worker-js.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default {
2+
// Handle HTTP requests
3+
// https://openworkers.com/docs/workers/event-fetch
4+
async fetch(request, env, ctx) {
5+
try {
6+
return await handleRequest(request, env);
7+
} catch (err) {
8+
return new Response(err.stack, { status: 500 });
9+
}
10+
},
11+
12+
// Handle scheduled events (cron)
13+
// https://openworkers.com/docs/workers/scheduled-tasks
14+
async scheduled(event, env, ctx) {
15+
console.log(`Scheduled event at ${new Date(event.scheduledTime).toISOString()}`);
16+
}
17+
};
18+
19+
async function handleRequest(request, env) {
20+
const { pathname } = new URL(request.url);
21+
22+
if (pathname === "/favicon.ico") {
23+
return new Response("Not found", { status: 404 });
24+
}
25+
26+
if (pathname.startsWith("/api")) {
27+
return Response.json({ pathname });
28+
}
29+
30+
return new Response("<h3>Hello world!</h3>", {
31+
headers: { "Content-Type": "text/html" },
32+
});
33+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openworkers-api",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"license": "MIT",
55
"module": "src/index.ts",
66
"type": "module",

src/routes/workers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { cronsService } from '../services/crons';
44
import { checkWorkerNameExists } from '../services/db/workers';
55
import { WorkerCreateInputSchema, WorkerUpdateInputSchema, WorkerSchema } from '../types';
66
import { jsonResponse, jsonArrayResponse } from '../utils/validate';
7-
import defaultWorkerScript from '../../examples/default-worker.txt';
7+
import defaultWorkerJs from '../../examples/default-worker-js.txt';
8+
import defaultWorkerTs from '../../examples/default-worker-ts.txt';
89

910
const workers = new Hono();
1011

@@ -60,10 +61,11 @@ workers.post('/', async (c) => {
6061

6162
try {
6263
const payload = WorkerCreateInputSchema.parse(body);
64+
const defaultScript = payload.language === 'typescript' ? defaultWorkerTs : defaultWorkerJs;
6365

6466
const worker = await workersService.create(userId, {
6567
name: payload.name,
66-
script: payload.script || defaultWorkerScript,
68+
script: payload.script || defaultScript,
6769
language: payload.language,
6870
environmentId: undefined // TODO: Handle environment mapping if needed
6971
});

0 commit comments

Comments
 (0)