Skip to content

Commit e28bd28

Browse files
committed
fix: test do
1 parent dba836d commit e28bd28

File tree

6 files changed

+67
-1
lines changed

6 files changed

+67
-1
lines changed

packages/vite-plugin-cloudflare-functions/src/vite/generate.ts

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ export async function generate(functionsRoot: string, dtsPath: string) {
2222
if (!file.startsWith('/')) {
2323
file = '/' + file;
2424
}
25+
if (file.endsWith('/index')) {
26+
file = file.replace(/index$/, '');
27+
}
2528
file = file.replace(/\[\[([\w]+)\]\]$/g, '**:$1');
2629
file = file.replace(/\[([\w]+)\]/g, ':$1');
2730
return file;

playground/app/cloudflare.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ declare module 'vite-plugin-cloudflare-functions/client' {
1111
'/api/**': {
1212
ALL: CloudflareResponseBody<typeof import('../functions/api/_middleware')['onRequest']>;
1313
};
14+
'/api/counter/': {
15+
GET: CloudflareResponseBody<typeof import('../functions/api/counter/index')['onRequestGet']>;
16+
};
1417
'/api/state/:key': {
1518
GET: CloudflareResponseBody<typeof import('../functions/api/state/[key]')['onRequestGet']>;
1619
POST: CloudflareResponseBody<typeof import('../functions/api/state/[key]')['onRequestPost']>;

playground/app/vite.config.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ export default defineConfig({
1616
CloudflarePagesFunctions({
1717
root: '../functions',
1818
outDir: '../../',
19-
wrangler: { log: false, kv: 'STORE', binding: { USER: 'yjl9903' } }
19+
wrangler: {
20+
log: true,
21+
compatibilityDate: '2023-10-26',
22+
kv: 'STORE',
23+
binding: { USER: 'yjl9903' },
24+
do: { COUNTER: { class: 'Counter', script: 'do' } }
25+
}
2026
})
2127
]
2228
});
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { makePagesFunction } from 'vite-plugin-cloudflare-functions/worker';
2+
3+
export const onRequestGet = makePagesFunction(async ({ request, env }) => {
4+
const id = env.COUNTER.idFromName('root');
5+
const counter = env.COUNTER.get(id);
6+
7+
console.log(id, counter);
8+
9+
const url = new URL(request.url);
10+
const resp = await counter.fetch(url.origin);
11+
12+
return { value: await resp.text() };
13+
});

playground/functions/cloudflare.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ declare module 'vite-plugin-cloudflare-functions/worker' {
1010
STORE: KVNamespace;
1111

1212
USER: string;
13+
14+
COUNTER: DurableObjectNamespace;
1315
}
1416

1517
interface PagesFunctionData {}

playground/functions/do.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export class Counter implements DurableObject {
2+
private state: DurableObjectState;
3+
4+
constructor(state: DurableObjectState) {
5+
this.state = state;
6+
}
7+
8+
// Handle HTTP requests from clients.
9+
async fetch(request: Request) {
10+
// Apply requested action.
11+
const url = new URL(request.url);
12+
13+
// Durable Object storage is automatically cached in-memory, so reading the
14+
// same key every request is fast.
15+
// You could also store the value in a class member if you prefer.
16+
let value = +(await this.state.storage.get('value')) || 0;
17+
18+
switch (url.pathname) {
19+
case '/increment':
20+
++value;
21+
break;
22+
case '/decrement':
23+
--value;
24+
break;
25+
case '/':
26+
// Serves the current value.
27+
break;
28+
default:
29+
return new Response('Not found', { status: 404 });
30+
}
31+
32+
// You do not have to worry about a concurrent request having modified the value in storage.
33+
// "input gates" will automatically protect against unwanted concurrency.
34+
// Read-modify-write is safe.
35+
await this.state.storage.put('value', value);
36+
37+
return new Response('' + value);
38+
}
39+
}

0 commit comments

Comments
 (0)