-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
161 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { DurableObject } from "cloudflare:workers"; | ||
import { InboxMessage } from "../types"; | ||
|
||
export class InboxDurableObject implements DurableObject { | ||
state: DurableObjectState; | ||
|
||
constructor(state: DurableObjectState, env: any) { | ||
this.state = state; | ||
} | ||
|
||
async fetch(request: Request): Promise<Response> { | ||
const url = new URL(request.url); | ||
const { method } = request; | ||
|
||
if (method === "POST" && url.pathname === "/add") { | ||
const { timestamp, ticketId } = await request.json<InboxMessage>(); | ||
const inbox = | ||
(await this.state.storage.get<InboxMessage[]>("inbox")) || []; | ||
inbox.push({ timestamp, ticketId }); | ||
await this.state.storage.put("inbox", inbox); | ||
return new Response("Message added to inbox", { status: 200 }); | ||
} | ||
|
||
if (method === "GET" && url.pathname === "/retrieve") { | ||
console.log("get data"); | ||
const inbox = | ||
(await this.state.storage.get<InboxMessage[]>("inbox")) || []; | ||
await this.state.storage.delete("inbox"); | ||
return new Response(JSON.stringify(inbox), { status: 200 }); | ||
} | ||
|
||
return new Response("Not Found", { status: 404 }); | ||
} | ||
} |
Oops, something went wrong.