Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-client): Create controller for Project module #370

Merged
merged 20 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/api-client/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const config: Config = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'^@package/(.*)$': '<rootDir>/src/$1'
'^@api-client/(.*)$': '<rootDir>/src/$1'
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/api-client/src/controllers/environment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ClientResponse } from '../types/index.types'
import { APIClient } from '../core/client'
import { parseResponse } from '../core/response-parser'
import { APIClient } from '@api-client/core/client'
import { parseResponse } from '@api-client/core/response-parser'
import {
CreateEnvironmentRequest,
CreateEnvironmentResponse,
Expand All @@ -12,7 +11,8 @@ import {
GetEnvironmentByIdResponse,
UpdateEnvironmentRequest,
UpdateEnvironmentResponse
} from '../types/environment.types'
} from '@api-client/types/environment.types'
import { ClientResponse } from '@api-client/types/index.types'

export default class EnvironmentController {
private apiClient: APIClient
Expand Down
9 changes: 6 additions & 3 deletions packages/api-client/src/controllers/event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { GetEventsRequest, GetEventsResponse } from '../types/event.types'
import {
GetEventsRequest,
GetEventsResponse
} from '@api-client/types/event.types'
import { APIClient } from '../core/client'
import { ClientResponse } from '../types/index.types'
import { parseResponse } from '../core/response-parser'
import { ClientResponse } from '@api-client/types/index.types'
import { parseResponse } from '@api-client/core/response-parser'

export default class EventController {
private apiClient: APIClient
Expand Down
8 changes: 4 additions & 4 deletions packages/api-client/src/controllers/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {
GetIntegrationResponse,
UpdateIntegrationRequest,
UpdateIntegrationResponse
} from '../types/integration.types'
import { APIClient } from '../core/client'
import { ClientResponse } from '../types/index.types'
import { parseResponse } from '../core/response-parser'
} from '@api-client/types/integration.types'
import { APIClient } from '@api-client/core/client'
import { ClientResponse } from '@api-client/types/index.types'
import { parseResponse } from '@api-client/core/response-parser'

export default class IntegrationController {
private apiClient: APIClient
Expand Down
149 changes: 149 additions & 0 deletions packages/api-client/src/controllers/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { ClientResponse } from '@api-client/types/index.types'
import { APIClient } from '@api-client/core/client'
import {
CreateProjectRequest,
CreateProjectResponse,
DeleteProjectRequest,
DeleteProjectResponse,
ForkProjectRequest,
ForkProjectResponse,
GetAllProjectsRequest,
GetAllProjectsResponse,
GetForkRequest,
GetForkResponse,
GetProjectRequest,
GetProjectResponse,
SyncProjectRequest,
SyncProjectResponse,
UnlinkProjectRequest,
UnlinkProjectResponse,
UpdateProjectRequest,
UpdateProjectResponse
} from '@api-client/types/project.types'
import { parseResponse } from '@api-client/core/response-parser'

export default class ProjectController {
private apiClient: APIClient

constructor(private readonly backendUrl: string) {
this.apiClient = new APIClient(this.backendUrl)
}

async createProject(
request: CreateProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<CreateProjectResponse>> {
const response = await this.apiClient.post(
`/api/project/${request.workspaceId}`,
request,
headers
)

return await parseResponse<CreateProjectResponse>(response)
}

async updateProject(
request: UpdateProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<UpdateProjectResponse>> {
const response = await this.apiClient.put(
`/api/project/${request.projectId}`,
request,
headers
)

return await parseResponse<UpdateProjectResponse>(response)
}

async deleteProject(
request: DeleteProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<DeleteProjectResponse>> {
const response = await this.apiClient.delete(
`/api/project/${request.projectId}`,
headers
)

return await parseResponse<DeleteProjectResponse>(response)
}

async getProject(
request: GetProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetProjectResponse>> {
const response = await this.apiClient.get(
`/api/project/${request.projectId}`,
headers
)

return await parseResponse<GetProjectResponse>(response)
}

async forkProject(
request: ForkProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<ForkProjectResponse>> {
const response = await this.apiClient.post(
`/api/project/${request.projectId}/fork`,
request,
headers
)

return await parseResponse<ForkProjectResponse>(response)
}

async syncFork(
request: SyncProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<SyncProjectResponse>> {
const response = await this.apiClient.put(
`/project/${request.projectId}/fork`,
request,
headers
)

return await parseResponse<SyncProjectResponse>(response)
}

async unlinkFork(
request: UnlinkProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<UnlinkProjectResponse>> {
const response = await this.apiClient.delete(
`/api/project/${request.projectId}/fork`,
headers
)

return await parseResponse<UnlinkProjectResponse>(response)
}

async getForks(
request: GetForkRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetForkResponse>> {
let url = `/api/project/${request.projectId}/forks`
request.page && (url += `page=${request.page}&`)
request.limit && (url += `limit=${request.limit}&`)
request.sort && (url += `sort=${request.sort}&`)
request.order && (url += `order=${request.order}&`)
request.search && (url += `search=${request.search}&`)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetForkResponse>(response)
}

async getAllProjects(
request: GetAllProjectsRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetAllProjectsResponse>> {
let url = `/api/project/all/${request.workspaceId}`
request.page && (url += `page=${request.page}&`)
request.limit && (url += `limit=${request.limit}&`)
request.sort && (url += `sort=${request.sort}&`)
request.order && (url += `order=${request.order}&`)
request.search && (url += `search=${request.search}&`)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetAllProjectsResponse>(response)
}
}
8 changes: 4 additions & 4 deletions packages/api-client/src/controllers/secret.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { APIClient } from '../core/client'
import { ClientResponse } from '../types/index.types'
import { parseResponse } from '../core/response-parser'
import { APIClient } from '@api-client/core/client'
import { ClientResponse } from '@api-client/types/index.types'
import { parseResponse } from '@api-client/core/response-parser'
import {
CreateSecretRequest,
CreateSecretResponse,
Expand All @@ -14,7 +14,7 @@ import {
RollBackSecretResponse,
UpdateSecretRequest,
UpdateSecretResponse
} from '../types/secret.types'
} from '@api-client/types/secret.types'

export default class SecretController {
private apiClient: APIClient
Expand Down
8 changes: 4 additions & 4 deletions packages/api-client/src/controllers/variable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { APIClient } from '../core/client'
import { parseResponse } from '../core/response-parser'
import { ClientResponse } from '../types/index.types'
import { APIClient } from '@api-client/core/client'
import { parseResponse } from '@api-client/core/response-parser'
import { ClientResponse } from '@api-client/types/index.types'
import {
CreateVariableRequest,
CreateVariableResponse,
Expand All @@ -14,7 +14,7 @@ import {
RollBackVariableResponse,
UpdateVariableRequest,
UpdateVariableResponse
} from '../types/variable.types'
} from '@api-client/types/variable.types'

export default class VariableController {
private apiClient: APIClient
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/core/response-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientResponse, ResponseError } from '../types/index.types'
import { ClientResponse, ResponseError } from '@api-client/types/index.types'

export async function parseResponse<T>(
response: Response
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/types/environment.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Page } from '../../../../apps/cli/src/types/index.types'
import { Page } from './index.types'

export interface CreateEnvironmentRequest {
name: string
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/types/event.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Page } from '../../../../apps/cli/src/types/index.types'
import { Page } from './index.types'

export enum EventSource {
SECRET,
Expand Down
3 changes: 2 additions & 1 deletion packages/api-client/src/types/integration.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Page } from '../../../../apps/cli/src/types/index.types'
import { Page } from './index.types'

export enum IntegrationType {
DISCORD,
SLACK,
Expand Down
Loading
Loading