Skip to content

Commit 0a20b0c

Browse files
committed
feat: add api handlers for projects
1 parent b3ad36c commit 0a20b0c

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/api-client.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getConfig } from './utils.js'
22
import { got } from 'got'
3-
import { APIHealthResponse } from './types.js'
3+
import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails } from './types.js'
44

55
export const apiClient = () => {
66
const config = getConfig()
@@ -13,7 +13,42 @@ export const getAPIDetails = async (): Promise<APIHealthResponse> => {
1313
const client = apiClient()
1414
const response = await client.get('__health', { responseType: 'json' })
1515
if (response.statusCode !== 200) {
16-
throw new Error('Failed to get the data from the API')
16+
throw new Error(`Failed to get the data from the API: ${response.statusCode} ${response.body}`)
1717
}
1818
return response.body as APIHealthResponse
1919
}
20+
21+
export const createProject = async (name: string): Promise<APIProjectDetails> => {
22+
const client = apiClient()
23+
const response = await client.post('project', {
24+
json: { name },
25+
responseType: 'json',
26+
throwHttpErrors: false
27+
})
28+
if (response.statusCode === 409) {
29+
throw new Error(`Project (${name}) already exists`)
30+
}
31+
if (response.statusCode !== 201) {
32+
throw new Error(`Failed to create the project: ${response.statusCode} ${response.body}`)
33+
}
34+
return response.body as APIProjectDetails
35+
}
36+
37+
export const addGithubOrgToProject = async (projectId: number, githubOrgUrl: string): Promise<APIGithubOrgDetails> => {
38+
const client = apiClient()
39+
const response = await client.post(`project/${projectId}/gh-org`, {
40+
json: { githubOrgUrl },
41+
responseType: 'json',
42+
throwHttpErrors: false
43+
})
44+
if (response.statusCode === 409) {
45+
throw new Error(`GitHub organization (${githubOrgUrl}) already exists in the project`)
46+
}
47+
if (response.statusCode === 404) {
48+
throw new Error(`Project (${projectId}) not found`)
49+
}
50+
if (response.statusCode !== 201) {
51+
throw new Error(`Failed to add the GitHub organization (${githubOrgUrl}) to the project: ${response.statusCode} ${response.body}`)
52+
}
53+
return response.body as APIGithubOrgDetails
54+
}

0 commit comments

Comments
 (0)