create-cloudflare 0.0.0-e33bb44a → 0.0.0-e4011b14

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.
Files changed (70) hide show
  1. package/README.md +1 -52
  2. package/dist/angular/templates/{src/main.server.ts → server.ts} +14 -15
  3. package/dist/angular/templates/tools/alter-polyfills.mjs +27 -0
  4. package/dist/angular/templates/tools/copy-files.mjs +9 -0
  5. package/dist/cli.js +26243 -21576
  6. package/package.json +89 -73
  7. package/templates/chatgptPlugin/ts/README.md +2 -2
  8. package/templates/chatgptPlugin/ts/__dot__gitignore +171 -0
  9. package/templates/chatgptPlugin/ts/package.json +2 -2
  10. package/templates/chatgptPlugin/ts/src/search.ts +3 -7
  11. package/templates/common/js/__dot__gitignore +172 -0
  12. package/templates/common/js/package.json +1 -1
  13. package/templates/common/js/wrangler.toml +2 -2
  14. package/templates/common/ts/__dot__gitignore +172 -0
  15. package/templates/common/ts/package.json +1 -1
  16. package/templates/common/ts/wrangler.toml +2 -2
  17. package/templates/hello-world/js/__dot__gitignore +172 -0
  18. package/templates/hello-world/js/package.json +1 -2
  19. package/templates/hello-world/js/wrangler.toml +2 -2
  20. package/templates/hello-world/ts/__dot__gitignore +172 -0
  21. package/templates/hello-world/ts/package.json +1 -1
  22. package/templates/hello-world/ts/wrangler.toml +2 -2
  23. package/templates/hello-world-durable-object/js/.editorconfig +13 -0
  24. package/templates/hello-world-durable-object/js/.prettierrc +6 -0
  25. package/templates/hello-world-durable-object/js/__dot__gitignore +172 -0
  26. package/templates/hello-world-durable-object/js/package.json +13 -0
  27. package/templates/hello-world-durable-object/js/src/index.js +65 -0
  28. package/templates/hello-world-durable-object/js/wrangler.toml +51 -0
  29. package/templates/hello-world-durable-object/ts/.editorconfig +13 -0
  30. package/templates/hello-world-durable-object/ts/.prettierrc +6 -0
  31. package/templates/hello-world-durable-object/ts/__dot__gitignore +172 -0
  32. package/templates/hello-world-durable-object/ts/package.json +15 -0
  33. package/templates/hello-world-durable-object/ts/src/index.ts +78 -0
  34. package/templates/hello-world-durable-object/ts/tsconfig.json +101 -0
  35. package/templates/hello-world-durable-object/ts/wrangler.toml +51 -0
  36. package/templates/openapi/ts/README.md +25 -0
  37. package/templates/openapi/ts/__dot__gitignore +171 -0
  38. package/templates/openapi/ts/package.json +18 -0
  39. package/templates/openapi/ts/src/endpoints/taskCreate.ts +48 -0
  40. package/templates/openapi/ts/src/endpoints/taskDelete.ts +55 -0
  41. package/templates/openapi/ts/src/endpoints/taskFetch.ts +74 -0
  42. package/templates/openapi/ts/src/endpoints/taskList.ts +66 -0
  43. package/templates/openapi/ts/src/index.ts +29 -0
  44. package/templates/openapi/ts/src/types.ts +9 -0
  45. package/templates/openapi/ts/tsconfig.json +32 -0
  46. package/templates/openapi/ts/wrangler.toml +3 -0
  47. package/templates/queues/js/__dot__gitignore +172 -0
  48. package/templates/queues/js/package.json +1 -2
  49. package/templates/queues/js/wrangler.toml +3 -3
  50. package/templates/queues/ts/__dot__gitignore +172 -0
  51. package/templates/queues/ts/package.json +1 -1
  52. package/templates/queues/ts/wrangler.toml +3 -3
  53. package/templates/scheduled/js/__dot__gitignore +172 -0
  54. package/templates/scheduled/js/package.json +1 -2
  55. package/templates/scheduled/js/wrangler.toml +1 -1
  56. package/templates/scheduled/ts/__dot__gitignore +172 -0
  57. package/templates/scheduled/ts/package.json +1 -1
  58. package/templates/scheduled/ts/wrangler.toml +2 -2
  59. package/dist/angular/templates/tools/bundle.mjs +0 -77
  60. package/dist/angular/templates/tools/copy-client-files.mjs +0 -4
  61. package/dist/angular/templates/tools/copy-worker-files.mjs +0 -10
  62. package/dist/angular/templates/tsconfig.server.json +0 -5
  63. /package/templates/common/js/src/{worker.js → index.js} +0 -0
  64. /package/templates/common/ts/src/{worker.ts → index.ts} +0 -0
  65. /package/templates/hello-world/js/src/{worker.js → index.js} +0 -0
  66. /package/templates/hello-world/ts/src/{worker.ts → index.ts} +0 -0
  67. /package/templates/queues/js/src/{worker.js → index.js} +0 -0
  68. /package/templates/queues/ts/src/{worker.ts → index.ts} +0 -0
  69. /package/templates/scheduled/js/src/{worker.js → index.js} +0 -0
  70. /package/templates/scheduled/ts/src/{worker.ts → index.ts} +0 -0
@@ -0,0 +1,101 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
+
5
+ /* Projects */
6
+ // "incremental": true, /* Enable incremental compilation */
7
+ // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
8
+ // "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
9
+ // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
10
+ // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
11
+ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
+
13
+ /* Language and Environment */
14
+ "target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
15
+ "lib": ["es2021"] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
16
+ "jsx": "react" /* Specify what JSX code is generated. */,
17
+ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
18
+ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
19
+ // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
20
+ // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
21
+ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
22
+ // "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
23
+ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
24
+ // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
+
26
+ /* Modules */
27
+ "module": "es2022" /* Specify what module code is generated. */,
28
+ // "rootDir": "./", /* Specify the root folder within your source files. */
29
+ "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
30
+ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
31
+ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
32
+ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
33
+ // "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
34
+ "types": ["@cloudflare/workers-types"] /* Specify type package names to be included without being referenced in a source file. */,
35
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
36
+ "resolveJsonModule": true /* Enable importing .json files */,
37
+ // "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
38
+
39
+ /* JavaScript Support */
40
+ "allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
41
+ "checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
42
+ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
43
+
44
+ /* Emit */
45
+ // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
46
+ // "declarationMap": true, /* Create sourcemaps for d.ts files. */
47
+ // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
48
+ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
49
+ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
50
+ // "outDir": "./", /* Specify an output folder for all emitted files. */
51
+ // "removeComments": true, /* Disable emitting comments. */
52
+ "noEmit": true /* Disable emitting files from a compilation. */,
53
+ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
54
+ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
55
+ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
56
+ // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
57
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
58
+ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
59
+ // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
60
+ // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
61
+ // "newLine": "crlf", /* Set the newline character for emitting files. */
62
+ // "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
63
+ // "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
64
+ // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
65
+ // "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
66
+ // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
67
+ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
68
+
69
+ /* Interop Constraints */
70
+ "isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
71
+ "allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
72
+ // "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
73
+ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
74
+ "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
75
+
76
+ /* Type Checking */
77
+ "strict": true /* Enable all strict type-checking options. */,
78
+ // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
79
+ // "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
80
+ // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
81
+ // "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
82
+ // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
83
+ // "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
84
+ // "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
85
+ // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
86
+ // "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
87
+ // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
88
+ // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
89
+ // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
90
+ // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
91
+ // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
92
+ // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
93
+ // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
94
+ // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
95
+ // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
96
+
97
+ /* Completeness */
98
+ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
99
+ "skipLibCheck": true /* Skip type checking all .d.ts files. */
100
+ }
101
+ }
@@ -0,0 +1,51 @@
1
+ name = "<TBD>"
2
+ main = "src/index.ts"
3
+ compatibility_date = "<TBD>"
4
+
5
+ # Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
6
+ # Note: Use secrets to store sensitive data.
7
+ # Docs: https://developers.cloudflare.com/workers/platform/environment-variables
8
+ # [vars]
9
+ # MY_VARIABLE = "production_value"
10
+
11
+ # Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
12
+ # Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
13
+ # [[kv_namespaces]]
14
+ # binding = "MY_KV_NAMESPACE"
15
+ # id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
16
+
17
+ # Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
18
+ # Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
19
+ # [[r2_buckets]]
20
+ # binding = "MY_BUCKET"
21
+ # bucket_name = "my-bucket"
22
+
23
+ # Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
24
+ # Docs: https://developers.cloudflare.com/queues/get-started
25
+ # [[queues.producers]]
26
+ # binding = "MY_QUEUE"
27
+ # queue = "my-queue"
28
+
29
+ # Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
30
+ # Docs: https://developers.cloudflare.com/queues/get-started
31
+ # [[queues.consumers]]
32
+ # queue = "my-queue"
33
+
34
+ # Bind another Worker service. Use this binding to call another Worker without network overhead.
35
+ # Docs: https://developers.cloudflare.com/workers/platform/services
36
+ # [[services]]
37
+ # binding = "MY_SERVICE"
38
+ # service = "my-service"
39
+
40
+ # Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
41
+ # Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
42
+ # Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
43
+ [[durable_objects.bindings]]
44
+ name = "MY_DURABLE_OBJECT"
45
+ class_name = "MyDurableObject"
46
+
47
+ # Durable Object migrations.
48
+ # Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations
49
+ [[migrations]]
50
+ tag = "v1"
51
+ new_classes = ["MyDurableObject"]
@@ -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,171 @@
1
+ # Logs
2
+
3
+ logs
4
+ _.log
5
+ npm-debug.log_
6
+ yarn-debug.log*
7
+ yarn-error.log*
8
+ lerna-debug.log*
9
+ .pnpm-debug.log*
10
+
11
+ # Diagnostic reports (https://nodejs.org/api/report.html)
12
+
13
+ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
14
+
15
+ # Runtime data
16
+
17
+ pids
18
+ _.pid
19
+ _.seed
20
+ \*.pid.lock
21
+
22
+ # Directory for instrumented libs generated by jscoverage/JSCover
23
+
24
+ lib-cov
25
+
26
+ # Coverage directory used by tools like istanbul
27
+
28
+ coverage
29
+ \*.lcov
30
+
31
+ # nyc test coverage
32
+
33
+ .nyc_output
34
+
35
+ # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
36
+
37
+ .grunt
38
+
39
+ # Bower dependency directory (https://bower.io/)
40
+
41
+ bower_components
42
+
43
+ # node-waf configuration
44
+
45
+ .lock-wscript
46
+
47
+ # Compiled binary addons (https://nodejs.org/api/addons.html)
48
+
49
+ build/Release
50
+
51
+ # Dependency directories
52
+
53
+ node_modules/
54
+ jspm_packages/
55
+
56
+ # Snowpack dependency directory (https://snowpack.dev/)
57
+
58
+ web_modules/
59
+
60
+ # TypeScript cache
61
+
62
+ \*.tsbuildinfo
63
+
64
+ # Optional npm cache directory
65
+
66
+ .npm
67
+
68
+ # Optional eslint cache
69
+
70
+ .eslintcache
71
+
72
+ # Optional stylelint cache
73
+
74
+ .stylelintcache
75
+
76
+ # Microbundle cache
77
+
78
+ .rpt2_cache/
79
+ .rts2_cache_cjs/
80
+ .rts2_cache_es/
81
+ .rts2_cache_umd/
82
+
83
+ # Optional REPL history
84
+
85
+ .node_repl_history
86
+
87
+ # Output of 'npm pack'
88
+
89
+ \*.tgz
90
+
91
+ # Yarn Integrity file
92
+
93
+ .yarn-integrity
94
+
95
+ # dotenv environment variable files
96
+
97
+ .env
98
+ .env.development.local
99
+ .env.test.local
100
+ .env.production.local
101
+ .env.local
102
+
103
+ # parcel-bundler cache (https://parceljs.org/)
104
+
105
+ .cache
106
+ .parcel-cache
107
+
108
+ # Next.js build output
109
+
110
+ .next
111
+ out
112
+
113
+ # Nuxt.js build / generate output
114
+
115
+ .nuxt
116
+ dist
117
+
118
+ # Gatsby files
119
+
120
+ .cache/
121
+
122
+ # Comment in the public line in if your project uses Gatsby and not Next.js
123
+
124
+ # https://nextjs.org/blog/next-9-1#public-directory-support
125
+
126
+ # public
127
+
128
+ # vuepress build output
129
+
130
+ .vuepress/dist
131
+
132
+ # vuepress v2.x temp and cache directory
133
+
134
+ .temp
135
+ .cache
136
+
137
+ # Docusaurus cache and generated files
138
+
139
+ .docusaurus
140
+
141
+ # Serverless directories
142
+
143
+ .serverless/
144
+
145
+ # FuseBox cache
146
+
147
+ .fusebox/
148
+
149
+ # DynamoDB Local files
150
+
151
+ .dynamodb/
152
+
153
+ # TernJS port file
154
+
155
+ .tern-port
156
+
157
+ # Stores VSCode versions used for testing VSCode extensions
158
+
159
+ .vscode-test
160
+
161
+ # yarn v2
162
+
163
+ .yarn/cache
164
+ .yarn/unplugged
165
+ .yarn/build-state.yml
166
+ .yarn/install-state.gz
167
+ .pnp.\*
168
+
169
+ # wrangler project
170
+
171
+ .dev.vars
@@ -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
+ "dev": "wrangler dev",
8
+ "start": "wrangler dev"
9
+ },
10
+ "dependencies": {
11
+ "@cloudflare/itty-router-openapi": "^1.0.1"
12
+ },
13
+ "devDependencies": {
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,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
+ };