forked from VertexChainLabs/VertexChain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgists.ts
More file actions
64 lines (56 loc) · 1.48 KB
/
Copy pathgists.ts
File metadata and controls
64 lines (56 loc) · 1.48 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// =============================================================================
// Gists API — typed client for the VertexChain backend POST /gists endpoint.
// =============================================================================
export interface GistPayload {
content: string;
lat: number;
lon: number;
author?: string;
}
export interface GistResponse {
id: string;
content: string;
lat: number;
lon: number;
author: string | null;
created_at: string;
stellar_gist_id?: string | null;
}
export class GistApiError extends Error {
constructor(
message: string,
public readonly status?: number,
) {
super(message);
this.name = "GistApiError";
}
}
const BASE_URL =
process.env.NEXT_PUBLIC_API_URL || "http://localhost:3000";
/**
* POST a new gist to the backend.
*
* Returns the server-confirmed gist on success.
* Throws a {@link GistApiError} on network failure or non-2xx response.
*/
export async function postGist(payload: GistPayload): Promise<GistResponse> {
const url = `${BASE_URL}/gists`;
const res = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!res.ok) {
let body: string;
try {
body = await res.text();
} catch {
body = "Unable to read response body";
}
throw new GistApiError(
`POST /gists failed (${res.status}): ${body}`,
res.status,
);
}
return res.json() as Promise<GistResponse>;
}