rhythia-api 83.0.0 → 85.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/.prettierrc.json +6 -0
- package/api/editProfile.ts +74 -74
- package/api/getLeaderboard.ts +68 -68
- package/api/getProfile.ts +115 -115
- package/api/searchUsers.ts +44 -46
- package/api/submitScore.ts +106 -92
- package/handleApi.ts +19 -19
- package/index.html +2 -2
- package/index.ts +5 -5
- package/package.json +1 -1
- package/scripts/ci-deploy.ts +76 -76
- package/scripts/update.ts +49 -49
- package/types/database.ts +9 -3
- package/utils/requestUtils.ts +55 -51
- package/utils/supabase.ts +13 -13
- package/vercel.json +12 -12
package/.prettierrc.json
ADDED
package/api/editProfile.ts
CHANGED
|
@@ -1,74 +1,74 @@
|
|
|
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
|
-
|
|
7
|
-
export const Schema = {
|
|
8
|
-
input: z.strictObject({
|
|
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
|
-
}),
|
|
15
|
-
}),
|
|
16
|
-
output: z.object({
|
|
17
|
-
error: z.string().optional(),
|
|
18
|
-
}),
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export async function POST(
|
|
22
|
-
return protectedApi({
|
|
23
|
-
|
|
24
|
-
schema: Schema,
|
|
25
|
-
authorization: validUser,
|
|
26
|
-
activity: handler,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export 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!;
|
|
34
|
-
let userData: Database["public"]["Tables"]["profiles"]["Update"];
|
|
35
|
-
|
|
36
|
-
// Find user's entry
|
|
37
|
-
{
|
|
38
|
-
let { data: queryUserData, error } = await supabase
|
|
39
|
-
.from("profiles")
|
|
40
|
-
.select("*")
|
|
41
|
-
.eq("uid", user.id);
|
|
42
|
-
|
|
43
|
-
if (!queryUserData?.length) {
|
|
44
|
-
return NextResponse.json(
|
|
45
|
-
{
|
|
46
|
-
error: "User cannot be retrieved from session",
|
|
47
|
-
},
|
|
48
|
-
{ status: 404 }
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
userData = queryUserData[0];
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
|
|
55
|
-
id: userData.id,
|
|
56
|
-
...data.data,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const upsertResult = await supabase
|
|
60
|
-
.from("profiles")
|
|
61
|
-
.upsert(upsertPayload)
|
|
62
|
-
.select();
|
|
63
|
-
|
|
64
|
-
if (upsertResult.status == 409) {
|
|
65
|
-
return NextResponse.json(
|
|
66
|
-
{
|
|
67
|
-
error: "Can't update, username might be used by someone else!",
|
|
68
|
-
},
|
|
69
|
-
{ status: 404 }
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return NextResponse.json({});
|
|
74
|
-
}
|
|
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
|
+
|
|
7
|
+
export const Schema = {
|
|
8
|
+
input: z.strictObject({
|
|
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
|
+
}),
|
|
15
|
+
}),
|
|
16
|
+
output: z.object({
|
|
17
|
+
error: z.string().optional(),
|
|
18
|
+
}),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export async function POST(request: Request): Promise<NextResponse> {
|
|
22
|
+
return protectedApi({
|
|
23
|
+
request,
|
|
24
|
+
schema: Schema,
|
|
25
|
+
authorization: validUser,
|
|
26
|
+
activity: handler,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export 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!;
|
|
34
|
+
let userData: Database["public"]["Tables"]["profiles"]["Update"];
|
|
35
|
+
|
|
36
|
+
// Find user's entry
|
|
37
|
+
{
|
|
38
|
+
let { data: queryUserData, error } = await supabase
|
|
39
|
+
.from("profiles")
|
|
40
|
+
.select("*")
|
|
41
|
+
.eq("uid", user.id);
|
|
42
|
+
|
|
43
|
+
if (!queryUserData?.length) {
|
|
44
|
+
return NextResponse.json(
|
|
45
|
+
{
|
|
46
|
+
error: "User cannot be retrieved from session",
|
|
47
|
+
},
|
|
48
|
+
{ status: 404 }
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
userData = queryUserData[0];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const upsertPayload: Database["public"]["Tables"]["profiles"]["Update"] = {
|
|
55
|
+
id: userData.id,
|
|
56
|
+
...data.data,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const upsertResult = await supabase
|
|
60
|
+
.from("profiles")
|
|
61
|
+
.upsert(upsertPayload)
|
|
62
|
+
.select();
|
|
63
|
+
|
|
64
|
+
if (upsertResult.status == 409) {
|
|
65
|
+
return NextResponse.json(
|
|
66
|
+
{
|
|
67
|
+
error: "Can't update, username might be used by someone else!",
|
|
68
|
+
},
|
|
69
|
+
{ status: 404 }
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return NextResponse.json({});
|
|
74
|
+
}
|
package/api/getLeaderboard.ts
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { NextResponse } from "next/server";
|
|
2
|
-
import z from "zod";
|
|
3
|
-
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
4
|
-
import { supabase } from "../utils/supabase";
|
|
5
|
-
|
|
6
|
-
export const Schema = {
|
|
7
|
-
input: z.strictObject({
|
|
8
|
-
session: z.string(),
|
|
9
|
-
page: z.number().optional(),
|
|
10
|
-
}),
|
|
11
|
-
output: z.object({
|
|
12
|
-
error: z.string().optional(),
|
|
13
|
-
total: z.number().optional(),
|
|
14
|
-
leaderboard: z
|
|
15
|
-
.array(
|
|
16
|
-
z.object({
|
|
17
|
-
flag: z.string().nullable(),
|
|
18
|
-
id: z.number(),
|
|
19
|
-
username: z.string().nullable(),
|
|
20
|
-
play_count: z.number().nullable(),
|
|
21
|
-
skill_points: z.number().nullable(),
|
|
22
|
-
total_score: z.number().nullable(),
|
|
23
|
-
})
|
|
24
|
-
)
|
|
25
|
-
.optional(),
|
|
26
|
-
}),
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export async function POST(res: Response): Promise<NextResponse> {
|
|
30
|
-
return protectedApi({
|
|
31
|
-
response: res,
|
|
32
|
-
schema: Schema,
|
|
33
|
-
authorization: validUser,
|
|
34
|
-
activity: handler,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export async function handler(
|
|
39
|
-
data: (typeof Schema)["input"]["_type"]
|
|
40
|
-
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
41
|
-
const range = [0, 100];
|
|
42
|
-
if (data.page) {
|
|
43
|
-
range[0] = 100 * data.page;
|
|
44
|
-
range[1] = range[0] + 100;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const countQuery = await supabase
|
|
48
|
-
.from("profiles")
|
|
49
|
-
.select("*", { count: "exact", head: true });
|
|
50
|
-
|
|
51
|
-
let { data: queryData, error } = await supabase
|
|
52
|
-
.from("profiles")
|
|
53
|
-
.select("*")
|
|
54
|
-
.order("skill_points", { ascending: false })
|
|
55
|
-
.range(range[0], range[1]);
|
|
56
|
-
|
|
57
|
-
return NextResponse.json({
|
|
58
|
-
total: countQuery.count || 0,
|
|
59
|
-
leaderboard: queryData?.map((user) => ({
|
|
60
|
-
flag: user.flag,
|
|
61
|
-
id: user.id,
|
|
62
|
-
play_count: user.play_count,
|
|
63
|
-
skill_points: user.skill_points,
|
|
64
|
-
total_score: user.total_score,
|
|
65
|
-
username: user.username,
|
|
66
|
-
})),
|
|
67
|
-
});
|
|
68
|
-
}
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
4
|
+
import { supabase } from "../utils/supabase";
|
|
5
|
+
|
|
6
|
+
export const Schema = {
|
|
7
|
+
input: z.strictObject({
|
|
8
|
+
session: z.string(),
|
|
9
|
+
page: z.number().optional(),
|
|
10
|
+
}),
|
|
11
|
+
output: z.object({
|
|
12
|
+
error: z.string().optional(),
|
|
13
|
+
total: z.number().optional(),
|
|
14
|
+
leaderboard: z
|
|
15
|
+
.array(
|
|
16
|
+
z.object({
|
|
17
|
+
flag: z.string().nullable(),
|
|
18
|
+
id: z.number(),
|
|
19
|
+
username: z.string().nullable(),
|
|
20
|
+
play_count: z.number().nullable(),
|
|
21
|
+
skill_points: z.number().nullable(),
|
|
22
|
+
total_score: z.number().nullable(),
|
|
23
|
+
})
|
|
24
|
+
)
|
|
25
|
+
.optional(),
|
|
26
|
+
}),
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export async function POST(res: Response): Promise<NextResponse> {
|
|
30
|
+
return protectedApi({
|
|
31
|
+
response: res,
|
|
32
|
+
schema: Schema,
|
|
33
|
+
authorization: validUser,
|
|
34
|
+
activity: handler,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function handler(
|
|
39
|
+
data: (typeof Schema)["input"]["_type"]
|
|
40
|
+
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
41
|
+
const range = [0, 100];
|
|
42
|
+
if (data.page) {
|
|
43
|
+
range[0] = 100 * data.page;
|
|
44
|
+
range[1] = range[0] + 100;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const countQuery = await supabase
|
|
48
|
+
.from("profiles")
|
|
49
|
+
.select("*", { count: "exact", head: true });
|
|
50
|
+
|
|
51
|
+
let { data: queryData, error } = await supabase
|
|
52
|
+
.from("profiles")
|
|
53
|
+
.select("*")
|
|
54
|
+
.order("skill_points", { ascending: false })
|
|
55
|
+
.range(range[0], range[1]);
|
|
56
|
+
|
|
57
|
+
return NextResponse.json({
|
|
58
|
+
total: countQuery.count || 0,
|
|
59
|
+
leaderboard: queryData?.map((user) => ({
|
|
60
|
+
flag: user.flag,
|
|
61
|
+
id: user.id,
|
|
62
|
+
play_count: user.play_count,
|
|
63
|
+
skill_points: user.skill_points,
|
|
64
|
+
total_score: user.total_score,
|
|
65
|
+
username: user.username,
|
|
66
|
+
})),
|
|
67
|
+
});
|
|
68
|
+
}
|
package/api/getProfile.ts
CHANGED
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
import { geolocation } from "@vercel/edge";
|
|
2
|
-
import { NextResponse } from "next/server";
|
|
3
|
-
import z from "zod";
|
|
4
|
-
import { Database } from "../types/database";
|
|
5
|
-
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
6
|
-
import { supabase } from "../utils/supabase";
|
|
7
|
-
|
|
8
|
-
export const Schema = {
|
|
9
|
-
input: z.strictObject({
|
|
10
|
-
session: z.string(),
|
|
11
|
-
id: z.number().optional(),
|
|
12
|
-
}),
|
|
13
|
-
output: z.object({
|
|
14
|
-
error: z.string().optional(),
|
|
15
|
-
user: z
|
|
16
|
-
.object({
|
|
17
|
-
about_me: z.string().nullable(),
|
|
18
|
-
avatar_url: z.string().nullable(),
|
|
19
|
-
badges: z.any().nullable(),
|
|
20
|
-
created_at: z.number().nullable(),
|
|
21
|
-
flag: z.string().nullable(),
|
|
22
|
-
id: z.number(),
|
|
23
|
-
uid: z.string().nullable(),
|
|
24
|
-
username: z.string().nullable(),
|
|
25
|
-
verified: z.boolean().nullable(),
|
|
26
|
-
play_count: z.number().nullable(),
|
|
27
|
-
skill_points: z.number().nullable(),
|
|
28
|
-
squares_hit: z.number().nullable(),
|
|
29
|
-
total_score: z.number().nullable(),
|
|
30
|
-
position: z.number().nullable(),
|
|
31
|
-
})
|
|
32
|
-
.optional(),
|
|
33
|
-
}),
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export async function POST(
|
|
37
|
-
return protectedApi({
|
|
38
|
-
|
|
39
|
-
schema: Schema,
|
|
40
|
-
authorization: validUser,
|
|
41
|
-
activity: handler,
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function handler(
|
|
46
|
-
data: (typeof Schema)["input"]["_type"],
|
|
47
|
-
req: Request
|
|
48
|
-
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
49
|
-
let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
|
|
50
|
-
|
|
51
|
-
// Fetch by id
|
|
52
|
-
if (data.id !== undefined) {
|
|
53
|
-
let { data: queryData, error } = await supabase
|
|
54
|
-
.from("profiles")
|
|
55
|
-
.select("*")
|
|
56
|
-
.eq("id", data.id);
|
|
57
|
-
|
|
58
|
-
console.log(profiles, error);
|
|
59
|
-
|
|
60
|
-
if (!queryData?.length) {
|
|
61
|
-
return NextResponse.json(
|
|
62
|
-
{
|
|
63
|
-
error: "User not found",
|
|
64
|
-
},
|
|
65
|
-
{ status: 404 }
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
profiles = queryData;
|
|
70
|
-
} else {
|
|
71
|
-
// Fetch by session id
|
|
72
|
-
const user = (await supabase.auth.getUser(data.session)).data.user!;
|
|
73
|
-
|
|
74
|
-
let { data: queryData, error } = await supabase
|
|
75
|
-
.from("profiles")
|
|
76
|
-
.select("*")
|
|
77
|
-
.eq("uid", user.id);
|
|
78
|
-
|
|
79
|
-
console.log(profiles, error);
|
|
80
|
-
if (!queryData?.length) {
|
|
81
|
-
const geo = geolocation(req);
|
|
82
|
-
const data = await supabase
|
|
83
|
-
.from("profiles")
|
|
84
|
-
.upsert({
|
|
85
|
-
uid: user.id,
|
|
86
|
-
about_me: "",
|
|
87
|
-
avatar_url: user.user_metadata.avatar_url,
|
|
88
|
-
badges: ["Early Bird"],
|
|
89
|
-
username: user.user_metadata.full_name,
|
|
90
|
-
flag: (geo.country || "US").toUpperCase(),
|
|
91
|
-
created_at: Date.now(),
|
|
92
|
-
})
|
|
93
|
-
.select();
|
|
94
|
-
|
|
95
|
-
profiles = data.data!;
|
|
96
|
-
} else {
|
|
97
|
-
profiles = queryData;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const user = profiles[0];
|
|
102
|
-
|
|
103
|
-
// Query to count how many players have more skill points than the specific player
|
|
104
|
-
const { count: playersWithMorePoints, error: rankError } = await supabase
|
|
105
|
-
.from("profiles")
|
|
106
|
-
.select("*", { count: "exact", head: true })
|
|
107
|
-
.gt("skill_points", user.skill_points);
|
|
108
|
-
|
|
109
|
-
return NextResponse.json({
|
|
110
|
-
user: {
|
|
111
|
-
...user,
|
|
112
|
-
position: (playersWithMorePoints || 0) + 1,
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
}
|
|
1
|
+
import { geolocation } from "@vercel/edge";
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
import z from "zod";
|
|
4
|
+
import { Database } from "../types/database";
|
|
5
|
+
import { protectedApi, validUser } from "../utils/requestUtils";
|
|
6
|
+
import { supabase } from "../utils/supabase";
|
|
7
|
+
|
|
8
|
+
export const Schema = {
|
|
9
|
+
input: z.strictObject({
|
|
10
|
+
session: z.string(),
|
|
11
|
+
id: z.number().optional(),
|
|
12
|
+
}),
|
|
13
|
+
output: z.object({
|
|
14
|
+
error: z.string().optional(),
|
|
15
|
+
user: z
|
|
16
|
+
.object({
|
|
17
|
+
about_me: z.string().nullable(),
|
|
18
|
+
avatar_url: z.string().nullable(),
|
|
19
|
+
badges: z.any().nullable(),
|
|
20
|
+
created_at: z.number().nullable(),
|
|
21
|
+
flag: z.string().nullable(),
|
|
22
|
+
id: z.number(),
|
|
23
|
+
uid: z.string().nullable(),
|
|
24
|
+
username: z.string().nullable(),
|
|
25
|
+
verified: z.boolean().nullable(),
|
|
26
|
+
play_count: z.number().nullable(),
|
|
27
|
+
skill_points: z.number().nullable(),
|
|
28
|
+
squares_hit: z.number().nullable(),
|
|
29
|
+
total_score: z.number().nullable(),
|
|
30
|
+
position: z.number().nullable(),
|
|
31
|
+
})
|
|
32
|
+
.optional(),
|
|
33
|
+
}),
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export async function POST(request: Request): Promise<NextResponse> {
|
|
37
|
+
return protectedApi({
|
|
38
|
+
request,
|
|
39
|
+
schema: Schema,
|
|
40
|
+
authorization: validUser,
|
|
41
|
+
activity: handler,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function handler(
|
|
46
|
+
data: (typeof Schema)["input"]["_type"],
|
|
47
|
+
req: Request
|
|
48
|
+
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
49
|
+
let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
|
|
50
|
+
|
|
51
|
+
// Fetch by id
|
|
52
|
+
if (data.id !== undefined) {
|
|
53
|
+
let { data: queryData, error } = await supabase
|
|
54
|
+
.from("profiles")
|
|
55
|
+
.select("*")
|
|
56
|
+
.eq("id", data.id);
|
|
57
|
+
|
|
58
|
+
console.log(profiles, error);
|
|
59
|
+
|
|
60
|
+
if (!queryData?.length) {
|
|
61
|
+
return NextResponse.json(
|
|
62
|
+
{
|
|
63
|
+
error: "User not found",
|
|
64
|
+
},
|
|
65
|
+
{ status: 404 }
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
profiles = queryData;
|
|
70
|
+
} else {
|
|
71
|
+
// Fetch by session id
|
|
72
|
+
const user = (await supabase.auth.getUser(data.session)).data.user!;
|
|
73
|
+
|
|
74
|
+
let { data: queryData, error } = await supabase
|
|
75
|
+
.from("profiles")
|
|
76
|
+
.select("*")
|
|
77
|
+
.eq("uid", user.id);
|
|
78
|
+
|
|
79
|
+
console.log(profiles, error);
|
|
80
|
+
if (!queryData?.length) {
|
|
81
|
+
const geo = geolocation(req);
|
|
82
|
+
const data = await supabase
|
|
83
|
+
.from("profiles")
|
|
84
|
+
.upsert({
|
|
85
|
+
uid: user.id,
|
|
86
|
+
about_me: "",
|
|
87
|
+
avatar_url: user.user_metadata.avatar_url,
|
|
88
|
+
badges: ["Early Bird"],
|
|
89
|
+
username: user.user_metadata.full_name,
|
|
90
|
+
flag: (geo.country || "US").toUpperCase(),
|
|
91
|
+
created_at: Date.now(),
|
|
92
|
+
})
|
|
93
|
+
.select();
|
|
94
|
+
|
|
95
|
+
profiles = data.data!;
|
|
96
|
+
} else {
|
|
97
|
+
profiles = queryData;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const user = profiles[0];
|
|
102
|
+
|
|
103
|
+
// Query to count how many players have more skill points than the specific player
|
|
104
|
+
const { count: playersWithMorePoints, error: rankError } = await supabase
|
|
105
|
+
.from("profiles")
|
|
106
|
+
.select("*", { count: "exact", head: true })
|
|
107
|
+
.gt("skill_points", user.skill_points);
|
|
108
|
+
|
|
109
|
+
return NextResponse.json({
|
|
110
|
+
user: {
|
|
111
|
+
...user,
|
|
112
|
+
position: (playersWithMorePoints || 0) + 1,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
package/api/searchUsers.ts
CHANGED
|
@@ -1,46 +1,44 @@
|
|
|
1
|
-
import { NextResponse } from "next/server";
|
|
2
|
-
import z from "zod";
|
|
3
|
-
import { protectedApi } from "../utils/requestUtils";
|
|
4
|
-
import { supabase } from "../utils/supabase";
|
|
5
|
-
|
|
6
|
-
export const Schema = {
|
|
7
|
-
input: z.strictObject({
|
|
8
|
-
text: z.string(),
|
|
9
|
-
}),
|
|
10
|
-
output: z.object({
|
|
11
|
-
error: z.string().optional(),
|
|
12
|
-
results: z
|
|
13
|
-
.array(
|
|
14
|
-
z.object({
|
|
15
|
-
id: z.number(),
|
|
16
|
-
username: z.string().nullable(),
|
|
17
|
-
})
|
|
18
|
-
)
|
|
19
|
-
.optional(),
|
|
20
|
-
}),
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export async function POST(
|
|
24
|
-
return protectedApi({
|
|
25
|
-
|
|
26
|
-
schema: Schema,
|
|
27
|
-
authorization: () => {},
|
|
28
|
-
activity: handler,
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export async function handler(
|
|
33
|
-
data:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
.
|
|
37
|
-
.
|
|
38
|
-
|
|
39
|
-
.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
}
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import z from "zod";
|
|
3
|
+
import { protectedApi } from "../utils/requestUtils";
|
|
4
|
+
import { supabase } from "../utils/supabase";
|
|
5
|
+
|
|
6
|
+
export const Schema = {
|
|
7
|
+
input: z.strictObject({
|
|
8
|
+
text: z.string(),
|
|
9
|
+
}),
|
|
10
|
+
output: z.object({
|
|
11
|
+
error: z.string().optional(),
|
|
12
|
+
results: z
|
|
13
|
+
.array(
|
|
14
|
+
z.object({
|
|
15
|
+
id: z.number(),
|
|
16
|
+
username: z.string().nullable(),
|
|
17
|
+
})
|
|
18
|
+
)
|
|
19
|
+
.optional(),
|
|
20
|
+
}),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export async function POST(request: Request) {
|
|
24
|
+
return protectedApi({
|
|
25
|
+
request,
|
|
26
|
+
schema: Schema,
|
|
27
|
+
authorization: () => {},
|
|
28
|
+
activity: handler,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function handler(data: (typeof Schema)["input"]["_type"]) {
|
|
33
|
+
const { data: searchData, error } = await supabase
|
|
34
|
+
.from("profiles")
|
|
35
|
+
.select()
|
|
36
|
+
.ilike("username", `%${data.text}%`)
|
|
37
|
+
.limit(10);
|
|
38
|
+
return NextResponse.json({
|
|
39
|
+
results: (searchData || []).map((data) => ({
|
|
40
|
+
id: data.id,
|
|
41
|
+
username: data.username,
|
|
42
|
+
})),
|
|
43
|
+
});
|
|
44
|
+
}
|