A Cloudflare Worker which works in conjunction with https://github.com/indexxing/bsky-alt-text
1import { Bool, OpenAPIRoute, Str } from "chanfana";
2import { z } from "zod";
3import { type AppContext, Task } from "../types";
4
5export class TaskFetch extends OpenAPIRoute {
6 schema = {
7 tags: ["Tasks"],
8 summary: "Get a single Task by slug",
9 request: {
10 params: z.object({
11 taskSlug: Str({ description: "Task slug" }),
12 }),
13 },
14 responses: {
15 "200": {
16 description: "Returns a single task if found",
17 content: {
18 "application/json": {
19 schema: z.object({
20 series: z.object({
21 success: Bool(),
22 result: z.object({
23 task: Task,
24 }),
25 }),
26 }),
27 },
28 },
29 },
30 "404": {
31 description: "Task not found",
32 content: {
33 "application/json": {
34 schema: z.object({
35 series: z.object({
36 success: Bool(),
37 error: Str(),
38 }),
39 }),
40 },
41 },
42 },
43 },
44 };
45
46 async handle(c: AppContext) {
47 // Get validated data
48 const data = await this.getValidatedData<typeof this.schema>();
49
50 // Retrieve the validated slug
51 const { taskSlug } = data.params;
52
53 // Implement your own object fetch here
54
55 const exists = true;
56
57 // @ts-ignore: check if the object exists
58 if (exists === false) {
59 return Response.json(
60 {
61 success: false,
62 error: "Object not found",
63 },
64 {
65 status: 404,
66 },
67 );
68 }
69
70 return {
71 success: true,
72 task: {
73 name: "my task",
74 slug: taskSlug,
75 description: "this needs to be done",
76 completed: false,
77 due_date: new Date().toISOString().slice(0, 10),
78 },
79 };
80 }
81}