-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathclient.ts
More file actions
105 lines (88 loc) · 3.38 KB
/
client.ts
File metadata and controls
105 lines (88 loc) · 3.38 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { env } from './env.js';
import { listReposResponseSchema, searchResponseSchema, fileSourceResponseSchema, listCommitsResponseSchema } from './schemas.js';
import { FileSourceRequest, ListReposQueryParams, SearchRequest, ListCommitsQueryParamsSchema } from './types.js';
import { isServiceError, ServiceErrorException } from './utils.js';
import { z } from 'zod';
const parseResponse = async <T extends z.ZodTypeAny>(
response: Response,
schema: T
): Promise<z.infer<T>> => {
const text = await response.text();
let json: unknown;
try {
json = JSON.parse(text);
} catch {
throw new Error(`Invalid JSON response: ${text}`);
}
// Check if the response is already a service error from the API
if (isServiceError(json)) {
throw new ServiceErrorException(json);
}
const parsed = schema.safeParse(json);
if (!parsed.success) {
throw new Error(`Failed to parse response: ${parsed.error.message}`);
}
return parsed.data;
};
export const search = async (request: SearchRequest) => {
const response = await fetch(`${env.SOURCEBOT_HOST}/api/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
body: JSON.stringify(request)
});
return parseResponse(response, searchResponseSchema);
}
export const listRepos = async (queryParams: ListReposQueryParams = {}) => {
const url = new URL(`${env.SOURCEBOT_HOST}/api/repos`);
for (const [key, value] of Object.entries(queryParams)) {
if (value) {
url.searchParams.set(key, value.toString());
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
});
const repos = await parseResponse(response, listReposResponseSchema);
const totalCount = parseInt(response.headers.get('X-Total-Count') ?? '0', 10);
return { repos, totalCount };
}
export const getFileSource = async (request: FileSourceRequest) => {
const url = new URL(`${env.SOURCEBOT_HOST}/api/source`);
for (const [key, value] of Object.entries(request)) {
if (value) {
url.searchParams.set(key, value.toString());
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
});
return parseResponse(response, fileSourceResponseSchema);
}
export const listCommits = async (queryParams: ListCommitsQueryParamsSchema) => {
const url = new URL(`${env.SOURCEBOT_HOST}/api/commits`);
for (const [key, value] of Object.entries(queryParams)) {
if (value) {
url.searchParams.set(key, value.toString());
}
}
const response = await fetch(url, {
method: 'GET',
headers: {
'X-Org-Domain': '~',
...(env.SOURCEBOT_API_KEY ? { 'X-Sourcebot-Api-Key': env.SOURCEBOT_API_KEY } : {})
},
});
const commits = await parseResponse(response, listCommitsResponseSchema);
const totalCount = parseInt(response.headers.get('X-Total-Count') ?? '0', 10);
return { commits, totalCount };
}