create-cloudflare 0.0.0-e2a6fa40 → 0.0.0-e3990bbf
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.
- package/README.md +1 -1
- package/dist/cli.js +9 -2
- package/package.json +2 -2
- package/templates/chatgptPlugin/ts/README.md +2 -2
- package/templates/chatgptPlugin/ts/package.json +1 -1
- package/templates/chatgptPlugin/ts/src/search.ts +3 -7
- package/templates/openapi/ts/README.md +25 -0
- package/templates/openapi/ts/package.json +18 -0
- package/templates/openapi/ts/src/endpoints/taskCreate.ts +48 -0
- package/templates/openapi/ts/src/endpoints/taskDelete.ts +55 -0
- package/templates/openapi/ts/src/endpoints/taskFetch.ts +74 -0
- package/templates/openapi/ts/src/endpoints/taskList.ts +66 -0
- package/templates/openapi/ts/src/index.ts +29 -0
- package/templates/openapi/ts/src/types.ts +9 -0
- package/templates/openapi/ts/tsconfig.json +32 -0
- package/templates/openapi/ts/wrangler.toml +3 -0
package/README.md
CHANGED
|
@@ -46,7 +46,7 @@ To create a new Typescript "Hello World" worker, run:
|
|
|
46
46
|
npm create cloudflare@latest hello-world -- --type hello-world --ts
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
Current template options are: `hello-world`, `common`, or `
|
|
49
|
+
Current template options are: `hello-world`, `common`, `chatgptPlugin`, or `openapi`.
|
|
50
50
|
|
|
51
51
|
#### Additional arguments
|
|
52
52
|
|
package/dist/cli.js
CHANGED
|
@@ -57864,7 +57864,7 @@ var Yargs = YargsFactory(esm_default);
|
|
|
57864
57864
|
var yargs_default = Yargs;
|
|
57865
57865
|
|
|
57866
57866
|
// package.json
|
|
57867
|
-
var version = "0.0.0-
|
|
57867
|
+
var version = "0.0.0-e3990bbf";
|
|
57868
57868
|
var devDependencies = {
|
|
57869
57869
|
"@babel/parser": "^7.21.3",
|
|
57870
57870
|
"@babel/types": "^7.21.4",
|
|
@@ -61023,7 +61023,7 @@ function secondsSince(start) {
|
|
|
61023
61023
|
}
|
|
61024
61024
|
|
|
61025
61025
|
// ../wrangler/package.json
|
|
61026
|
-
var version2 = "3.
|
|
61026
|
+
var version2 = "3.8.0";
|
|
61027
61027
|
|
|
61028
61028
|
// src/common.ts
|
|
61029
61029
|
var { name, npm: npm11 } = detectPackageManager();
|
|
@@ -61627,6 +61627,13 @@ var templateMap = {
|
|
|
61627
61627
|
ts: true
|
|
61628
61628
|
})
|
|
61629
61629
|
},
|
|
61630
|
+
openapi: {
|
|
61631
|
+
label: `OpenAPI 3.1`,
|
|
61632
|
+
handler: (args) => runWorkersGenerator({
|
|
61633
|
+
...args,
|
|
61634
|
+
ts: true
|
|
61635
|
+
})
|
|
61636
|
+
},
|
|
61630
61637
|
"pre-existing": {
|
|
61631
61638
|
label: "Pre-existing Worker (from Dashboard)",
|
|
61632
61639
|
handler: runWorkersGenerator,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-cloudflare",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-e3990bbf",
|
|
4
4
|
"description": "A CLI for creating and deploying new applications to Cloudflare.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"@clack/core": "^0.3.2",
|
|
63
63
|
"@types/semver": "^7.5.1",
|
|
64
64
|
"@cloudflare/workers-tsconfig": "0.0.0",
|
|
65
|
-
"wrangler": "3.
|
|
65
|
+
"wrangler": "3.8.0"
|
|
66
66
|
},
|
|
67
67
|
"engines": {
|
|
68
68
|
"node": ">=16.13.0"
|
|
@@ -17,8 +17,8 @@ The sample plugin allows ChatGPT users to search for repositories using GitHub's
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
19
|
1. You can configure the `.well-known/ai-plugin.json` route in `index.ts`.
|
|
20
|
-
2.
|
|
21
|
-
3.
|
|
20
|
+
2. You can set up any new routes and the associated OpenAPI schema by defining new routes. See `search.ts` for an example.
|
|
21
|
+
3. For more information read the [itty-router-openapi official documentation](https://cloudflare.github.io/itty-router-openapi/).
|
|
22
22
|
|
|
23
23
|
## Deploying to OpenAI's API
|
|
24
24
|
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ApiException,
|
|
3
|
-
OpenAPIRoute,
|
|
4
|
-
Query,
|
|
5
|
-
ValidationError,
|
|
6
|
-
} from "@cloudflare/itty-router-openapi";
|
|
1
|
+
import { OpenAPIRoute, Query } from "@cloudflare/itty-router-openapi";
|
|
7
2
|
|
|
8
3
|
export class GetSearch extends OpenAPIRoute {
|
|
9
4
|
static schema = {
|
|
@@ -17,6 +12,7 @@ export class GetSearch extends OpenAPIRoute {
|
|
|
17
12
|
},
|
|
18
13
|
responses: {
|
|
19
14
|
"200": {
|
|
15
|
+
description: "Successfully response",
|
|
20
16
|
schema: {
|
|
21
17
|
repos: [
|
|
22
18
|
{
|
|
@@ -33,7 +29,7 @@ export class GetSearch extends OpenAPIRoute {
|
|
|
33
29
|
};
|
|
34
30
|
|
|
35
31
|
async handle(request: Request, env, ctx, data: Record<string, any>) {
|
|
36
|
-
const url = `https://api.github.com/search/repositories?q=${data.q}`;
|
|
32
|
+
const url = `https://api.github.com/search/repositories?q=${data.query.q}`;
|
|
37
33
|
|
|
38
34
|
const resp = await fetch(url, {
|
|
39
35
|
headers: {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Cloudflare Workers OpenAPI 3.1
|
|
2
|
+
|
|
3
|
+
This is a Cloudflare Worker with OpenAPI 3.1 using [itty-router-openapi](https://github.com/cloudflare/itty-router-openapi).
|
|
4
|
+
|
|
5
|
+
This is an example project made to be used as a quick start into building OpenAPI compliant Workers that generates the
|
|
6
|
+
`openapi.json` schema automatically from code and validates the incoming request to the defined parameters or request body.
|
|
7
|
+
|
|
8
|
+
## Get started
|
|
9
|
+
|
|
10
|
+
1. Sign up for [Cloudflare Workers](https://workers.dev). The free tier is more than enough for most use cases.
|
|
11
|
+
2. Clone this project and install dependencies with `npm install`
|
|
12
|
+
3. Run `wrangler login` to login to your Cloudflare account in wrangler
|
|
13
|
+
4. Run `wrangler deploy` to publish the API to Cloudflare Workers
|
|
14
|
+
|
|
15
|
+
## Project structure
|
|
16
|
+
|
|
17
|
+
1. Your main router is defined in `src/index.ts`.
|
|
18
|
+
2. Each endpoint has its own file in `src/endpoints/`.
|
|
19
|
+
3. For more information read the [itty-router-openapi official documentation](https://cloudflare.github.io/itty-router-openapi/).
|
|
20
|
+
|
|
21
|
+
## Development
|
|
22
|
+
|
|
23
|
+
1. Run `wrangler dev` to start a local instance of the API.
|
|
24
|
+
2. Open `http://localhost:9000/` in your browser to see the Swagger interface where you can try the endpoints.
|
|
25
|
+
3. Changes made in the `src/` folder will automatically trigger the server to reload, you only need to refresh the Swagger interface.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cloudflare-workers-openapi",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"private": true,
|
|
5
|
+
"scripts": {
|
|
6
|
+
"deploy": "wrangler deploy",
|
|
7
|
+
"start": "wrangler dev"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@cloudflare/itty-router-openapi": "^1.0.1"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@cloudflare/workers-types": "^4.20230821.0",
|
|
14
|
+
"@types/node": "^20.5.7",
|
|
15
|
+
"@types/service-worker-mock": "^2.0.1",
|
|
16
|
+
"wrangler": "^3.0.0"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -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,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
|
+
}
|