@zeltjs/adapter-cloudflare-workers 0.9.1-canary.5 → 0.10.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.
- package/README.md +42 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ pnpm add @zeltjs/adapter-cloudflare-workers
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
import { createApp, Controller, Get } from '@zeltjs/core';
|
|
18
|
+
import { createApp, http, Controller, Get } from '@zeltjs/core';
|
|
19
19
|
import { onCloudflareWorkers } from '@zeltjs/adapter-cloudflare-workers';
|
|
20
20
|
|
|
21
21
|
@Controller('/hello')
|
|
@@ -26,7 +26,7 @@ class HelloController {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
const app = createApp({ controllers: [HelloController] });
|
|
29
|
+
const app = createApp([http({ controllers: [HelloController] })]);
|
|
30
30
|
|
|
31
31
|
const workers = await onCloudflareWorkers(app);
|
|
32
32
|
|
|
@@ -49,7 +49,7 @@ onCloudflareWorkers(app, {
|
|
|
49
49
|
`onCloudflareWorkers()` automatically registers `CloudflareWorkersEnvAdaptor`, which reads from the `cloudflare:workers` env. Use `inject(Env)` to access environment variables:
|
|
50
50
|
|
|
51
51
|
```typescript
|
|
52
|
-
import { createApp, Controller, Get, Env, inject } from '@zeltjs/core';
|
|
52
|
+
import { createApp, http, Controller, Get, Env, inject } from '@zeltjs/core';
|
|
53
53
|
import { onCloudflareWorkers } from '@zeltjs/adapter-cloudflare-workers';
|
|
54
54
|
|
|
55
55
|
@Controller('/config')
|
|
@@ -62,11 +62,47 @@ class ConfigController {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
const app = createApp({
|
|
66
|
-
http: { controllers: [ConfigController] },
|
|
67
|
-
});
|
|
65
|
+
const app = createApp([http({ controllers: [ConfigController] })]);
|
|
68
66
|
|
|
69
67
|
const workers = await onCloudflareWorkers(app);
|
|
70
68
|
|
|
71
69
|
export default { fetch: workers.fetch };
|
|
72
70
|
```
|
|
71
|
+
|
|
72
|
+
## Cloudflare Bindings
|
|
73
|
+
|
|
74
|
+
Wrangler bindings (D1, KV, R2, Durable Objects, etc.) are passed to `fetch(request, env, ctx)` as `env`, separate from the string-only environment variables exposed through `Env`. Use `CloudflareBindings` to access them with types generated by `wrangler types`:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { createApp, http, Controller, Get, inject } from '@zeltjs/core';
|
|
78
|
+
import { CloudflareBindings, onCloudflareWorkers } from '@zeltjs/adapter-cloudflare-workers';
|
|
79
|
+
|
|
80
|
+
// `wrangler types` augments the global `Env` with the bindings declared
|
|
81
|
+
// in wrangler.toml, e.g.:
|
|
82
|
+
// declare global {
|
|
83
|
+
// interface Env {
|
|
84
|
+
// DB: D1Database;
|
|
85
|
+
// }
|
|
86
|
+
// }
|
|
87
|
+
|
|
88
|
+
@Controller('/data')
|
|
89
|
+
class DataController {
|
|
90
|
+
constructor(private bindings = inject(CloudflareBindings)) {}
|
|
91
|
+
|
|
92
|
+
@Get('/')
|
|
93
|
+
async list() {
|
|
94
|
+
const db = this.bindings.get('DB');
|
|
95
|
+
return { rows: await db.prepare('SELECT * FROM items').all() };
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const app = createApp([http({ controllers: [DataController] })]);
|
|
100
|
+
|
|
101
|
+
const workers = await onCloudflareWorkers(app);
|
|
102
|
+
|
|
103
|
+
export default { fetch: workers.fetch };
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`bindings.get('DB')` is typed from the augmented global `Env`, so no manual typing is needed.
|
|
107
|
+
|
|
108
|
+
**Important:** `CloudflareBindings.get()` reads from request-scoped storage populated by `onCloudflareWorkers()`, so it only works during request handling — calling it outside a request (e.g. at module load time) throws.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeltjs/adapter-cloudflare-workers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@zeltjs/core": "0.
|
|
30
|
+
"@zeltjs/core": "0.10.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@types/node": "22.19.17",
|