-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
createHighlights.ts
50 lines (41 loc) · 1.21 KB
/
createHighlights.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import changeCapitalization from "lib/utils/change-capitalization";
import { supabase } from "lib/utils/supabase";
interface CreateHighlightsProps {
url: string;
title?: string;
highlight: string;
shipped_at?: Date;
}
interface ServerError {
statusCode: number;
message: string[];
error: string;
}
const baseUrl = process.env.NEXT_PUBLIC_API_URL;
const createHighlights = async (data: CreateHighlightsProps) => {
const sessionResponse = await supabase.auth.getSession();
const sessionToken = sessionResponse?.data.session?.access_token;
try {
const response = await fetch(`${baseUrl}/user/highlights`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${sessionToken}`
},
method: "POST",
body: JSON.stringify({ ...data })
});
if (!response.ok) {
throw response;
}
return response.json();
} catch (error) {
return (error as Response).json().then((err: Error | ServerError) => {
if (err instanceof Error) {
return changeCapitalization(err.message, true);
}
return changeCapitalization(err.message[0], true);
});
}
};
export { createHighlights };