Skip to content

Commit e14b5ab

Browse files
authored
chore: test hello world binding support in vite plugin (#9663)
* chore: test hello world binding support in vite plugin * test hello world binding support in vite plugin
1 parent e216a76 commit e14b5ab

File tree

11 files changed

+150
-0
lines changed

11 files changed

+150
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { expect, test } from "vitest";
2+
import { getTextResponse } from "../../__test-utils__";
3+
4+
test("kv_namspaces support", async () => {
5+
const response = await getTextResponse("/kv");
6+
expect(response).toBe("KV binding works");
7+
});
8+
9+
test("unsafe_hello_world support", async () => {
10+
const response = await getTextResponse("/hello-world");
11+
expect(response).toBe("Hello World binding works");
12+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@playground/bindings",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "vite build --app",
7+
"cf-typegen": "wrangler types --include-runtime=false",
8+
"check:types": "tsc --build",
9+
"dev": "vite dev",
10+
"preview": "vite preview"
11+
},
12+
"devDependencies": {
13+
"@cloudflare/vite-plugin": "workspace:*",
14+
"@cloudflare/workers-tsconfig": "workspace:*",
15+
"@cloudflare/workers-types": "^4.20250617.0",
16+
"typescript": "catalog:default",
17+
"vite": "catalog:vite-plugin",
18+
"wrangler": "workspace:*"
19+
}
20+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
export default {
2+
async fetch(request, env) {
3+
const url = new URL(request.url);
4+
5+
switch (url.pathname) {
6+
case "/kv": {
7+
const value = Math.floor(Date.now() * Math.random()).toString(36);
8+
9+
await env.KV.put("value", value);
10+
11+
if (value !== (await env.KV.get("value"))) {
12+
return new Response("KV binding failed to set value", {
13+
status: 500,
14+
});
15+
}
16+
17+
return new Response("KV binding works", {
18+
status: 200,
19+
});
20+
}
21+
case "/hello-world": {
22+
const value = Math.floor(Date.now() * Math.random()).toString(36);
23+
await env.HELLO_WORLD.set(value);
24+
25+
const result = await env.HELLO_WORLD.get(value);
26+
if (value !== result.value) {
27+
return new Response("Hello World binding failed to set value", {
28+
status: 500,
29+
});
30+
}
31+
32+
return new Response("Hello World binding works", {
33+
status: 200,
34+
});
35+
}
36+
}
37+
38+
return new Response("Please specify a binding you want to test", {
39+
status: 400,
40+
});
41+
},
42+
} satisfies ExportedHandler<Env>;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.node.json" },
5+
{ "path": "./tsconfig.worker.json" }
6+
]
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["@cloudflare/workers-tsconfig/base.json"],
3+
"include": ["vite.config.ts", "__tests__"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["@cloudflare/workers-tsconfig/worker.json"],
3+
"include": ["src", "./worker-configuration.d.ts"]
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"$schema": "http://turbo.build/schema.json",
3+
"extends": ["//"],
4+
"tasks": {
5+
"build": {
6+
"outputs": ["dist/**"]
7+
}
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { cloudflare } from "@cloudflare/vite-plugin";
2+
import { defineConfig } from "vite";
3+
4+
export default defineConfig({
5+
plugins: [cloudflare({ inspectorPort: false, persistState: false })],
6+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable */
2+
// Generated by Wrangler by running `wrangler types --include-runtime=false` (hash: 2aff1a83eda1c659fd494a8ca649c27a)
3+
declare namespace Cloudflare {
4+
interface Env {
5+
KV: KVNamespace;
6+
HELLO_WORLD: HelloWorldBinding;
7+
}
8+
}
9+
interface Env extends Cloudflare.Env {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "worker",
3+
"main": "./src/index.ts",
4+
"compatibility_date": "2024-12-30",
5+
"kv_namespaces": [
6+
{
7+
"binding": "KV",
8+
"id": "test-kv-id",
9+
},
10+
],
11+
"unsafe_hello_world": [
12+
{
13+
"binding": "HELLO_WORLD",
14+
},
15+
],
16+
}

0 commit comments

Comments
 (0)