create-cloudflare 2.2.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,48 @@
1
+ import {
2
+ OpenAPIRoute,
3
+ OpenAPIRouteSchema,
4
+ } from "@cloudflare/itty-router-openapi";
5
+ import { Task } from "../types";
6
+
7
+ export class TaskCreate extends OpenAPIRoute {
8
+ static schema: OpenAPIRouteSchema = {
9
+ tags: ["Tasks"],
10
+ summary: "Create a new Task",
11
+ requestBody: Task,
12
+ responses: {
13
+ "200": {
14
+ description: "Returns the created task",
15
+ schema: {
16
+ success: Boolean,
17
+ result: {
18
+ task: Task,
19
+ },
20
+ },
21
+ },
22
+ },
23
+ };
24
+
25
+ async handle(
26
+ request: Request,
27
+ env: any,
28
+ context: any,
29
+ data: Record<string, any>
30
+ ) {
31
+ // Retrieve the validated request body
32
+ const taskToCreate = data.body;
33
+
34
+ // Implement your own object insertion here
35
+
36
+ // return the new task
37
+ return {
38
+ success: true,
39
+ task: {
40
+ name: taskToCreate.name,
41
+ slug: taskToCreate.slug,
42
+ description: taskToCreate.description,
43
+ completed: taskToCreate.completed,
44
+ due_date: taskToCreate.due_date,
45
+ },
46
+ };
47
+ }
48
+ }
@@ -0,0 +1,55 @@
1
+ import {
2
+ OpenAPIRoute,
3
+ OpenAPIRouteSchema,
4
+ Path,
5
+ } from "@cloudflare/itty-router-openapi";
6
+ import { Task } from "../types";
7
+
8
+ export class TaskDelete extends OpenAPIRoute {
9
+ static schema: OpenAPIRouteSchema = {
10
+ tags: ["Tasks"],
11
+ summary: "Delete a Task",
12
+ parameters: {
13
+ taskSlug: Path(String, {
14
+ description: "Task slug",
15
+ }),
16
+ },
17
+ responses: {
18
+ "200": {
19
+ description: "Returns if the task was deleted successfully",
20
+ schema: {
21
+ success: Boolean,
22
+ result: {
23
+ task: Task,
24
+ },
25
+ },
26
+ },
27
+ },
28
+ };
29
+
30
+ async handle(
31
+ request: Request,
32
+ env: any,
33
+ context: any,
34
+ data: Record<string, any>
35
+ ) {
36
+ // Retrieve the validated slug
37
+ const { taskSlug } = data.params;
38
+
39
+ // Implement your own object deletion here
40
+
41
+ // Return the deleted task for confirmation
42
+ return {
43
+ result: {
44
+ task: {
45
+ name: "Build something awesome with Cloudflare Workers",
46
+ slug: taskSlug,
47
+ description: "Lorem Ipsum",
48
+ completed: true,
49
+ due_date: "2022-12-24",
50
+ },
51
+ },
52
+ success: true,
53
+ };
54
+ }
55
+ }
@@ -0,0 +1,74 @@
1
+ import {
2
+ OpenAPIRoute,
3
+ OpenAPIRouteSchema,
4
+ Path,
5
+ } from "@cloudflare/itty-router-openapi";
6
+ import { Task } from "../types";
7
+
8
+ export class TaskFetch extends OpenAPIRoute {
9
+ static schema: OpenAPIRouteSchema = {
10
+ tags: ["Tasks"],
11
+ summary: "Get a single Task by slug",
12
+ parameters: {
13
+ taskSlug: Path(String, {
14
+ description: "Task slug",
15
+ }),
16
+ },
17
+ responses: {
18
+ "200": {
19
+ description: "Returns a single task if found",
20
+ schema: {
21
+ success: Boolean,
22
+ result: {
23
+ task: Task,
24
+ },
25
+ },
26
+ },
27
+ "404": {
28
+ description: "Task not found",
29
+ schema: {
30
+ success: Boolean,
31
+ error: String,
32
+ },
33
+ },
34
+ },
35
+ };
36
+
37
+ async handle(
38
+ request: Request,
39
+ env: any,
40
+ context: any,
41
+ data: Record<string, any>
42
+ ) {
43
+ // Retrieve the validated slug
44
+ const { taskSlug } = data.params;
45
+
46
+ // Implement your own object fetch here
47
+
48
+ const exists = true;
49
+
50
+ // @ts-ignore: check if the object exists
51
+ if (exists === false) {
52
+ return Response.json(
53
+ {
54
+ success: false,
55
+ error: "Object not found",
56
+ },
57
+ {
58
+ status: 404,
59
+ }
60
+ );
61
+ }
62
+
63
+ return {
64
+ success: true,
65
+ task: {
66
+ name: "my task",
67
+ slug: taskSlug,
68
+ description: "this needs to be done",
69
+ completed: false,
70
+ due_date: new Date().toISOString().slice(0, 10),
71
+ },
72
+ };
73
+ }
74
+ }
@@ -0,0 +1,66 @@
1
+ import { Task } from "../types";
2
+ import {
3
+ OpenAPIRoute,
4
+ OpenAPIRouteSchema,
5
+ Query,
6
+ } from "@cloudflare/itty-router-openapi";
7
+
8
+ export class TaskList extends OpenAPIRoute {
9
+ static schema: OpenAPIRouteSchema = {
10
+ tags: ["Tasks"],
11
+ summary: "List Tasks",
12
+ parameters: {
13
+ page: Query(Number, {
14
+ description: "Page number",
15
+ default: 0,
16
+ }),
17
+ isCompleted: Query(Boolean, {
18
+ description: "Filter by completed flag",
19
+ required: false,
20
+ }),
21
+ },
22
+ responses: {
23
+ "200": {
24
+ description: "Returns a list of tasks",
25
+ schema: {
26
+ success: Boolean,
27
+ result: {
28
+ tasks: [Task],
29
+ },
30
+ },
31
+ },
32
+ },
33
+ };
34
+
35
+ async handle(
36
+ request: Request,
37
+ env: any,
38
+ context: any,
39
+ data: Record<string, any>
40
+ ) {
41
+ // Retrieve the validated parameters
42
+ const { page, isCompleted } = data.query;
43
+
44
+ // Implement your own object list here
45
+
46
+ return {
47
+ success: true,
48
+ tasks: [
49
+ {
50
+ name: "Clean my room",
51
+ slug: "clean-room",
52
+ description: null,
53
+ completed: false,
54
+ due_date: "2025-01-05",
55
+ },
56
+ {
57
+ name: "Build something awesome with Cloudflare Workers",
58
+ slug: "cloudflare-workers",
59
+ description: "Lorem Ipsum",
60
+ completed: true,
61
+ due_date: "2022-12-24",
62
+ },
63
+ ],
64
+ };
65
+ }
66
+ }
@@ -0,0 +1,29 @@
1
+ import { OpenAPIRouter } from "@cloudflare/itty-router-openapi";
2
+ import { TaskList } from "./endpoints/taskList";
3
+ import { TaskCreate } from "./endpoints/taskCreate";
4
+ import { TaskFetch } from "./endpoints/taskFetch";
5
+ import { TaskDelete } from "./endpoints/taskDelete";
6
+
7
+ export const router = OpenAPIRouter({
8
+ docs_url: "/",
9
+ });
10
+
11
+ router.get("/api/tasks/", TaskList);
12
+ router.post("/api/tasks/", TaskCreate);
13
+ router.get("/api/tasks/:taskSlug/", TaskFetch);
14
+ router.delete("/api/tasks/:taskSlug/", TaskDelete);
15
+
16
+ // 404 for everything else
17
+ router.all("*", () =>
18
+ Response.json(
19
+ {
20
+ success: false,
21
+ error: "Route not found",
22
+ },
23
+ { status: 404 }
24
+ )
25
+ );
26
+
27
+ export default {
28
+ fetch: router.handle,
29
+ };
@@ -0,0 +1,9 @@
1
+ import { DateTime, Str } from "@cloudflare/itty-router-openapi";
2
+
3
+ export const Task = {
4
+ name: new Str({ example: "lorem" }),
5
+ slug: String,
6
+ description: new Str({ required: false }),
7
+ completed: Boolean,
8
+ due_date: new DateTime(),
9
+ };
@@ -0,0 +1,32 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowJs": true,
4
+ "allowSyntheticDefaultImports": true,
5
+ "baseUrl": "src",
6
+ "declaration": true,
7
+ "sourceMap": true,
8
+ "esModuleInterop": true,
9
+ "inlineSourceMap": false,
10
+ "lib": ["esnext"],
11
+ "listEmittedFiles": false,
12
+ "listFiles": false,
13
+ "moduleResolution": "node",
14
+ "noFallthroughCasesInSwitch": true,
15
+ "pretty": true,
16
+ "resolveJsonModule": true,
17
+ "rootDir": ".",
18
+ "skipLibCheck": true,
19
+ "strict": false,
20
+ "traceResolution": false,
21
+ "outDir": "",
22
+ "target": "esnext",
23
+ "module": "esnext",
24
+ "types": [
25
+ "@types/node",
26
+ "@types/service-worker-mock",
27
+ "@cloudflare/workers-types"
28
+ ]
29
+ },
30
+ "exclude": ["node_modules", "dist", "tests"],
31
+ "include": ["src", "scripts"]
32
+ }
@@ -0,0 +1,3 @@
1
+ name = "<TBD>"
2
+ main = "src/index.ts"
3
+ compatibility_date = "<TBD>"