summaryrefslogtreecommitdiff
path: root/src/pages/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/pages/api')
-rw-r--r--src/pages/api/counter.ts52
-rw-r--r--src/pages/api/hello.ts54
2 files changed, 7 insertions, 99 deletions
diff --git a/src/pages/api/counter.ts b/src/pages/api/counter.ts
index 1d3d0b7..4f42236 100644
--- a/src/pages/api/counter.ts
+++ b/src/pages/api/counter.ts
@@ -6,54 +6,16 @@ interface Env {
export const GET: APIRoute = async ({ locals }) => {
const env = locals.runtime.env as Env;
- if (!env?.COUNTER_DO) {
- return new Response(JSON.stringify({ error: 'Durable Object not available' }), {
- status: 500,
- headers: { 'Content-Type': 'application/json' }
- });
- }
-
- const id = env.COUNTER_DO.idFromName('global-counter');
+ const id = env.COUNTER_DO.idFromName('counter');
const stub = env.COUNTER_DO.get(id);
-
- const response = await stub.fetch(new Request('https://counter.do/get'));
- const data = await response.json();
-
- return new Response(JSON.stringify(data), {
- headers: { 'Content-Type': 'application/json' }
- });
+ const response = await stub.fetch('https://counter.do/get');
+ return response;
};
-export const POST: APIRoute = async ({ request, locals }) => {
+export const POST: APIRoute = async ({ locals }) => {
const env = locals.runtime.env as Env;
- if (!env?.COUNTER_DO) {
- return new Response(JSON.stringify({ error: 'Durable Object not available' }), {
- status: 500,
- headers: { 'Content-Type': 'application/json' }
- });
- }
-
- const url = new URL(request.url);
- const action = url.searchParams.get('action') || 'increment';
-
- const id = env.COUNTER_DO.idFromName('global-counter');
+ const id = env.COUNTER_DO.idFromName('counter');
const stub = env.COUNTER_DO.get(id);
-
- let doRequest: Request;
- switch (action) {
- case 'reset':
- doRequest = new Request('https://counter.do/reset');
- break;
- case 'increment':
- default:
- doRequest = new Request('https://counter.do/increment');
- break;
- }
-
- const response = await stub.fetch(doRequest);
- const data = await response.json();
-
- return new Response(JSON.stringify(data), {
- headers: { 'Content-Type': 'application/json' }
- });
+ const response = await stub.fetch('https://counter.do/increment');
+ return response;
}; \ No newline at end of file
diff --git a/src/pages/api/hello.ts b/src/pages/api/hello.ts
deleted file mode 100644
index eba198d..0000000
--- a/src/pages/api/hello.ts
+++ /dev/null
@@ -1,54 +0,0 @@
-import type { APIRoute } from 'astro';
-
-export const GET: APIRoute = async ({ request }) => {
- const url = new URL(request.url);
- const name = url.searchParams.get('name') || 'World';
-
- return new Response(
- JSON.stringify({
- message: `Hello, ${name}!`,
- timestamp: new Date().toISOString(),
- method: 'GET'
- }),
- {
- status: 200,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- );
-};
-
-export const POST: APIRoute = async ({ request }) => {
- try {
- const body = await request.json();
-
- return new Response(
- JSON.stringify({
- message: 'Data received successfully',
- received: body,
- timestamp: new Date().toISOString(),
- method: 'POST'
- }),
- {
- status: 200,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- );
- } catch (error) {
- return new Response(
- JSON.stringify({
- error: 'Invalid JSON',
- timestamp: new Date().toISOString()
- }),
- {
- status: 400,
- headers: {
- 'Content-Type': 'application/json',
- },
- }
- );
- }
-}; \ No newline at end of file