rhythia-api 66.0.0 → 67.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.
- package/api/editProfile.ts +35 -10
- package/api/getProfile.ts +27 -12
- package/api/webhook_createUser.ts +1 -1
- package/package.json +6 -3
- package/scripts/test.ts +1 -1
- package/utils/requestUtils.ts +55 -0
- package/utils/supabase.ts +1 -1
- /package/types/{supabase.ts → database.ts} +0 -0
package/api/editProfile.ts
CHANGED
|
@@ -1,22 +1,47 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
1
2
|
import z from "zod";
|
|
3
|
+
import { Database } from "../types/database";
|
|
4
|
+
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
5
|
+
import { supabase } from "../utils/supabase";
|
|
2
6
|
|
|
3
7
|
export const Schema = {
|
|
4
8
|
input: z.object({
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
session: z.string(),
|
|
10
|
+
data: z.object({
|
|
11
|
+
avatar_url: z.string().optional(),
|
|
12
|
+
about_me: z.string().optional(),
|
|
13
|
+
username: z.string().optional(),
|
|
14
|
+
}),
|
|
7
15
|
}),
|
|
8
16
|
output: z.object({
|
|
9
|
-
|
|
17
|
+
error: z.string().optional(),
|
|
10
18
|
}),
|
|
11
19
|
};
|
|
12
20
|
|
|
13
|
-
export async function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
21
|
+
export async function POST(res: Response): Promise<NextResponse> {
|
|
22
|
+
return protectedApi({
|
|
23
|
+
response: res,
|
|
24
|
+
schema: Schema,
|
|
25
|
+
authorization: validUser,
|
|
26
|
+
activity: handler,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function handler(
|
|
31
|
+
data: (typeof Schema)["input"]["_type"]
|
|
32
|
+
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
33
|
+
const user = (await supabase.auth.getUser(data.session)).data.user!;
|
|
18
34
|
|
|
19
|
-
|
|
20
|
-
uid:
|
|
35
|
+
const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
|
|
36
|
+
uid: user.id,
|
|
37
|
+
flag: "",
|
|
38
|
+
...data.data,
|
|
21
39
|
};
|
|
40
|
+
|
|
41
|
+
const upsertResult = await supabase
|
|
42
|
+
.from("profiles")
|
|
43
|
+
.upsert(upsertPayload)
|
|
44
|
+
.select();
|
|
45
|
+
|
|
46
|
+
return NextResponse.json({});
|
|
22
47
|
}
|
package/api/getProfile.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import { NOT_FOUND } from "http-status";
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
1
3
|
import z from "zod";
|
|
4
|
+
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
2
5
|
import { supabase } from "../utils/supabase";
|
|
3
6
|
|
|
4
7
|
export const Schema = {
|
|
5
8
|
input: z.object({
|
|
6
|
-
|
|
9
|
+
session: z.string(),
|
|
10
|
+
id: z.number(),
|
|
7
11
|
}),
|
|
8
12
|
output: z.object({
|
|
9
13
|
error: z.string().optional(),
|
|
@@ -23,27 +27,38 @@ export const Schema = {
|
|
|
23
27
|
}),
|
|
24
28
|
};
|
|
25
29
|
|
|
26
|
-
export async function POST(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
export async function POST(res: Response): Promise<NextResponse> {
|
|
31
|
+
return protectedApi({
|
|
32
|
+
response: res,
|
|
33
|
+
schema: Schema,
|
|
34
|
+
authorization: validUser,
|
|
35
|
+
activity: handler,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
31
38
|
|
|
39
|
+
async function handler(
|
|
40
|
+
data: (typeof Schema)["input"]["_type"]
|
|
41
|
+
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
32
42
|
let { data: profiles, error } = await supabase
|
|
33
43
|
.from("profiles")
|
|
34
44
|
.select("*")
|
|
35
|
-
.eq("
|
|
45
|
+
.eq("id", data.id);
|
|
46
|
+
|
|
47
|
+
console.log(profiles, error);
|
|
36
48
|
|
|
37
49
|
if (!profiles?.length) {
|
|
38
|
-
return
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
return NextResponse.json(
|
|
51
|
+
{
|
|
52
|
+
error: "User not found",
|
|
53
|
+
},
|
|
54
|
+
{ status: NOT_FOUND }
|
|
55
|
+
);
|
|
41
56
|
}
|
|
42
57
|
|
|
43
58
|
const user = profiles[0];
|
|
44
|
-
return {
|
|
59
|
+
return NextResponse.json({
|
|
45
60
|
user: {
|
|
46
61
|
...user,
|
|
47
62
|
},
|
|
48
|
-
};
|
|
63
|
+
});
|
|
49
64
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rhythia-api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "67.0.0",
|
|
4
4
|
"main": "index.ts",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"update": "bun ./scripts/update.ts",
|
|
7
7
|
"ci-deploy": "tsx ./scripts/ci-deploy.ts",
|
|
8
8
|
"test": "bun ./scripts/test.ts",
|
|
9
|
-
"sync": "npx supabase gen types typescript --project-id \"pfkajngbllcbdzoylrvp\" --schema public > types/
|
|
9
|
+
"sync": "npx supabase gen types typescript --project-id \"pfkajngbllcbdzoylrvp\" --schema public > types/database.ts",
|
|
10
10
|
"pipeline:build-api": "tsx ./scripts/build.ts",
|
|
11
11
|
"pipeline:deploy-testing": "tsx ./scripts/build.ts"
|
|
12
12
|
},
|
|
@@ -18,14 +18,17 @@
|
|
|
18
18
|
"@types/bun": "^1.1.6",
|
|
19
19
|
"@types/lodash": "^4.17.7",
|
|
20
20
|
"@types/node": "^22.2.0",
|
|
21
|
+
"@vercel/node": "^3.2.8",
|
|
21
22
|
"aws-lambda": "^1.0.7",
|
|
22
23
|
"bufferutil": "^4.0.8",
|
|
23
24
|
"bun": "^1.1.22",
|
|
24
25
|
"esbuild": "^0.23.0",
|
|
26
|
+
"http-status": "^1.7.4",
|
|
25
27
|
"isomorphic-git": "^1.27.1",
|
|
26
28
|
"lodash": "^4.17.21",
|
|
29
|
+
"next": "^14.2.5",
|
|
27
30
|
"simple-git": "^3.25.0",
|
|
28
|
-
"supabase": "^1.
|
|
31
|
+
"supabase": "^1.190.0",
|
|
29
32
|
"tsx": "^4.17.0",
|
|
30
33
|
"utf-8-validate": "^6.0.4",
|
|
31
34
|
"zod": "^3.23.8"
|
package/scripts/test.ts
CHANGED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import status, { INTERNAL_SERVER_ERROR, UNAUTHORIZED } from "http-status";
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
import { supabase } from "./supabase";
|
|
4
|
+
|
|
5
|
+
export async function protectedApi({
|
|
6
|
+
response,
|
|
7
|
+
schema,
|
|
8
|
+
authorization,
|
|
9
|
+
activity,
|
|
10
|
+
}: {
|
|
11
|
+
response: Response;
|
|
12
|
+
schema: { input: Zod.ZodObject<any>; output: Zod.ZodObject<any> };
|
|
13
|
+
authorization: Function;
|
|
14
|
+
activity: Function;
|
|
15
|
+
}) {
|
|
16
|
+
let data;
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
const toParse = await response.json();
|
|
20
|
+
data = schema.input.parse(toParse);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return NextResponse.json(
|
|
23
|
+
{ error: error.toString() },
|
|
24
|
+
{ status: status.BAD_REQUEST }
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const authorizationReponse = await authorization(data);
|
|
29
|
+
if (authorizationReponse) {
|
|
30
|
+
return authorizationReponse;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return await activity(data);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function validUser(data) {
|
|
37
|
+
if (!data.session) {
|
|
38
|
+
return NextResponse.json(
|
|
39
|
+
{
|
|
40
|
+
error: "Session is missing",
|
|
41
|
+
},
|
|
42
|
+
{ status: INTERNAL_SERVER_ERROR }
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const user = await supabase.auth.getUser(data.session);
|
|
47
|
+
if (!user.error || !user.data.user) {
|
|
48
|
+
return NextResponse.json(
|
|
49
|
+
{
|
|
50
|
+
error: "Invalid user session",
|
|
51
|
+
},
|
|
52
|
+
{ status: UNAUTHORIZED }
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
}
|
package/utils/supabase.ts
CHANGED
|
File without changes
|