Skip to content

Commit 64f7f7c

Browse files
Added activity durable object
1 parent 0b5aaa5 commit 64f7f7c

35 files changed

Lines changed: 91 additions & 38 deletions

src/domains/router.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default function createRouter() {
7373

7474
router.post("/api/user/avatar", withAuth, withContent, withSchema(uploadUserImageRequestSchema), handleUploadUserAvatarRequest);
7575

76-
router.get("/api/ping", withContent, async (request: Request, env: Env) => {
76+
router.get("/api/ping", withContent, async (request: RequestWithKey, env: Env) => {
7777
return Response.json({
7878
ping: "pong",
7979
success: true
@@ -83,11 +83,11 @@ export default function createRouter() {
8383
router.post("/staging/register", withStaging, withContent, handleStagingDeleteUserRequest);
8484
router.post("/staging/verification", withStaging, withContent, handleStagingVerificationRequest);
8585

86-
router.get("/staging/github", withStaging, async (request: Request, env: Env) => {
86+
router.get("/staging/github", withStaging, async (request: RequestWithKey, env: Env) => {
8787
return Response.json({ sha: env.GITHUB_SHA });
8888
});
8989

90-
router.get("/api/auth/random", withStaging, async (request: Request, env: Env) => {
90+
router.get("/api/auth/random", withStaging, async (request: RequestWithKey, env: Env) => {
9191
return Response.json({
9292
success: true,
9393
key: await env.DATABASE.prepare("SELECT * FROM tokens WHERE user IS NOT NULL").first<string>("key")

src/global.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ declare type Env = {
33

44
DATABASE: D1Database;
55
BUCKET: R2Bucket;
6+
ACTIVITY_DURABLE_OBJECT: DurableObjectNamespace;
67

78
ENVIRONMENT: "production" | "staging" | "dev";
89
CLOUDFLARE_ACCOUNT_ID: string;
@@ -16,8 +17,8 @@ declare type Env = {
1617
GITHUB_SHA: string | undefined;
1718
};
1819

19-
declare type Request = {
20-
[key: string]: any | undefined;
20+
declare type RequestWithKey = Request & {
21+
[key: string]: any;
2122

2223
key: {
2324
id: string;

src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,27 @@ export default {
5858
}
5959
}
6060
};
61+
62+
export class ActivityDurableObject {
63+
state: DurableObjectState;
64+
65+
constructor(state: DurableObjectState, env: Env) {
66+
this.state = state;
67+
};
68+
69+
async fetch(request: Request) {
70+
const body = await request.json() as {
71+
id?: string;
72+
};
73+
74+
if(!body.id)
75+
return Response.json({ success: false });
76+
77+
console.log("recieved id: " + body.id);
78+
79+
return Response.json({
80+
hello: "world",
81+
activity: body.id
82+
});
83+
};
84+
};

src/middlewares/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getTokenByKey } from "../controllers/tokens/getTokenByKey";
22

3-
export async function withAuth(request: Request, env: Env, context: any) {
3+
export async function withAuth(request: RequestWithKey, env: Env, context: any) {
44
const authorization = request.headers.get("Authorization").split(' ');
55

66
if(authorization[0] !== "Bearer" || authorization.length !== 2)

src/middlewares/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export function withSchema(schema: any) {
22
const entries = Object.keys(schema);
33

4-
return (request: Request) => {
4+
return (request: RequestWithKey) => {
55
for(let key of entries) {
66
if(request[key] == undefined)
77
return Response.json({ message: `Bad schema validation, missing ${key} body.` }, { status: 400 });

src/middlewares/staging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export async function withStaging(request: Request, env: Env, context: any) {
1+
export async function withStaging(request: RequestWithKey, env: Env, context: any) {
22
if(env.ENVIRONMENT === "production")
33
return new Response(null, { status: 404, statusText: "File Not Found" });
44
};

src/routes/activities/[activityId]/summary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const activitySummaryRequestSchema = {
1717
}
1818
};
1919

20-
export async function handleActivitySummaryRequest(request: Request, env: Env) {
20+
export async function handleActivitySummaryRequest(request: RequestWithKey, env: Env) {
2121
const { activityId } = request.params;
2222

2323
const activity = await getActivityById(env.DATABASE, activityId);

src/routes/activities/comments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { getBikeSummaryById } from "../../controllers/bikes/summary/getBikeSumma
88
import { getUserById } from "../../controllers/users/getUserById";
99
import { User } from "../../models/user";
1010

11-
export async function handleActivityCommentsRequest(request: Request, env: Env) {
11+
export async function handleActivityCommentsRequest(request: RequestWithKey, env: Env) {
1212
const { id } = request.params;
1313

1414
const comments = await getActivityComments(env.DATABASE, id);

src/routes/activities/comments/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const activityCreateCommentRequestSchema = {
3030
}
3131
};
3232

33-
export async function handleActivityCreateCommentRequest(request: Request, env: Env) {
33+
export async function handleActivityCreateCommentRequest(request: RequestWithKey, env: Env) {
3434
const { id } = request.params;
3535
const { parent, message } = request.content;
3636

src/routes/activities/comments/delete.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const activityDeleteCommentRequestSchema = {
1818
}
1919
};
2020

21-
export async function handleActivityDeleteCommentRequest(request: Request, env: Env) {
21+
export async function handleActivityDeleteCommentRequest(request: RequestWithKey, env: Env) {
2222
const { activityId, commentId } = request.params;
2323

2424
const activity = await getActivityById(env.DATABASE, activityId);

0 commit comments

Comments
 (0)