rhythia-api 103.0.0 → 105.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ import { NextResponse } from "next/server";
2
+ import z from "zod";
3
+ import { Database } from "../types/database";
4
+ import { protectedApi, validUser } from "../utils/requestUtils";
5
+ import { supabase } from "../utils/supabase";
6
+ export const Schema = {
7
+ input: z.strictObject({
8
+ session: z.string(),
9
+ data: z.object({
10
+ about_me: z.string().optional(),
11
+ }),
12
+ }),
13
+ output: z.object({
14
+ error: z.string().optional(),
15
+ }),
16
+ };
17
+
18
+ export async function POST(request: Request): Promise<NextResponse> {
19
+ return protectedApi({
20
+ request,
21
+ schema: Schema,
22
+ authorization: validUser,
23
+ activity: handler,
24
+ });
25
+ }
26
+
27
+ export async function handler(
28
+ data: (typeof Schema)["input"]["_type"]
29
+ ): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
30
+ if (!data.data.about_me) {
31
+ return NextResponse.json(
32
+ {
33
+ error: "Missing body.",
34
+ },
35
+ { status: 404 }
36
+ );
37
+ }
38
+
39
+ if (data.data.about_me.length > 500) {
40
+ return NextResponse.json(
41
+ {
42
+ error: "Too long.",
43
+ },
44
+ { status: 404 }
45
+ );
46
+ }
47
+
48
+ const user = (await supabase.auth.getUser(data.session)).data.user!;
49
+ let userData: Database["public"]["Tables"]["profiles"]["Update"];
50
+
51
+ // Find user's entry
52
+ {
53
+ let { data: queryUserData, error } = await supabase
54
+ .from("profiles")
55
+ .select("*")
56
+ .eq("uid", user.id);
57
+
58
+ if (!queryUserData?.length) {
59
+ return NextResponse.json(
60
+ {
61
+ error: "User cannot be retrieved from session",
62
+ },
63
+ { status: 404 }
64
+ );
65
+ }
66
+ userData = queryUserData[0];
67
+ }
68
+
69
+ const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
70
+ id: userData.id,
71
+ about_me: data.data.about_me,
72
+ };
73
+
74
+ const upsertResult = await supabase
75
+ .from("profiles")
76
+ .upsert(upsertPayload)
77
+ .select();
78
+
79
+ if (upsertResult.error) {
80
+ return NextResponse.json(
81
+ {
82
+ error: "Can't update..",
83
+ },
84
+ { status: 404 }
85
+ );
86
+ }
87
+
88
+ return NextResponse.json({});
89
+ }
@@ -8,7 +8,6 @@ export const Schema = {
8
8
  session: z.string(),
9
9
  data: z.object({
10
10
  avatar_url: z.string().optional(),
11
- about_me: z.string().optional(),
12
11
  username: z.string().optional(),
13
12
  }),
14
13
  }),
@@ -3,23 +3,32 @@ import z from "zod";
3
3
  import { protectedApi, validUser } from "../utils/requestUtils";
4
4
  import { supabase } from "../utils/supabase";
5
5
 
6
- import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
6
+ import {
7
+ PutBucketCorsCommand,
8
+ PutObjectCommand,
9
+ S3Client,
10
+ } from "@aws-sdk/client-s3";
7
11
  import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
8
12
 
9
13
  const s3Client = new S3Client({
10
- region: "us-east-1",
14
+ region: "auto",
15
+ endpoint: "https://s3.eu-central-003.backblazeb2.com",
11
16
  credentials: {
12
- secretAccessKey: "0036dd2cb06d730015861d27ce0796cccb5031123e",
13
- accessKeyId: "c245e893e806",
17
+ secretAccessKey: "K0039mm4iKsteQOXpZSzf0+VDzuH89U",
18
+ accessKeyId: "003c245e893e8060000000001",
14
19
  },
15
20
  });
16
21
 
17
22
  export const Schema = {
18
23
  input: z.strictObject({
19
24
  session: z.string(),
25
+ contentLength: z.number(),
26
+ contentType: z.string(),
20
27
  }),
21
28
  output: z.strictObject({
22
- url: z.string(),
29
+ error: z.string().optional(),
30
+ url: z.string().optional(),
31
+ objectKey: z.string().optional(),
23
32
  }),
24
33
  };
25
34
 
@@ -34,15 +43,25 @@ export async function POST(request: Request): Promise<NextResponse> {
34
43
 
35
44
  export async function handler({
36
45
  session,
46
+ contentLength,
47
+ contentType,
37
48
  }: (typeof Schema)["input"]["_type"]): Promise<
38
49
  NextResponse<(typeof Schema)["output"]["_type"]>
39
50
  > {
40
51
  const user = (await supabase.auth.getUser(session)).data.user!;
41
52
 
53
+ if (contentLength > 5000000) {
54
+ return NextResponse.json({
55
+ error: "Max content length exceeded.",
56
+ });
57
+ }
58
+
59
+ const key = `user-avatar-${Date.now()}-${user.id}`;
42
60
  const command = new PutObjectCommand({
43
- Bucket: "rhythia-avatars",
44
- Key: `user-avatar-${Date.now()}-${user.id}`,
45
- ContentLength: 5000000,
61
+ Bucket: "rhthia-avatars",
62
+ Key: key,
63
+ ContentLength: contentLength,
64
+ ContentType: contentType,
46
65
  });
47
66
 
48
67
  const presigned = await getSignedUrl(s3Client, command, {
@@ -50,6 +69,6 @@ export async function handler({
50
69
  });
51
70
  return NextResponse.json({
52
71
  url: presigned,
53
- objectKey: `user-avatar-${Date.now()}-${user.id}`,
72
+ objectKey: key,
54
73
  });
55
74
  }
package/api/getProfile.ts CHANGED
@@ -84,7 +84,8 @@ export async function handler(
84
84
  .upsert({
85
85
  uid: user.id,
86
86
  about_me: "",
87
- avatar_url: user.user_metadata.avatar_url,
87
+ avatar_url:
88
+ "https://rhthia-avatars.s3.eu-central-003.backblazeb2.com/user-avatar-1725309193296-72002e6b-321c-4f60-a692-568e0e75147d",
88
89
  badges: ["Early Bird"],
89
90
  username: `${user.user_metadata.full_name.slice(0, 20)}${Math.round(
90
91
  Math.random() * 900000 + 100000
package/index.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import { handleApi } from "./handleApi"
2
2
 
3
+ // ./api/editAboutMe.ts API
4
+ import { Schema as EditAboutMe } from "./api/editAboutMe"
5
+ export { Schema as SchemaEditAboutMe } from "./api/editAboutMe"
6
+ export const editAboutMe = handleApi({url:"/api/editAboutMe",...EditAboutMe})
7
+
3
8
  // ./api/editProfile.ts API
4
9
  import { Schema as EditProfile } from "./api/editProfile"
5
10
  export { Schema as SchemaEditProfile } from "./api/editProfile"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rhythia-api",
3
- "version": "103.0.0",
3
+ "version": "105.0.0",
4
4
  "main": "index.ts",
5
5
  "scripts": {
6
6
  "update": "bun ./scripts/update.ts",