Skip to content

Commit

Permalink
feat(api-client): Added workspace role controller (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Sep 14, 2024
1 parent 2f4edec commit b03ce8e
Show file tree
Hide file tree
Showing 18 changed files with 435 additions and 111 deletions.
12 changes: 5 additions & 7 deletions packages/api-client/src/controllers/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIClient } from '@api-client/core/client'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'
import { parseResponse } from '@api-client/core/response-parser'
import {
CreateEnvironmentRequest,
Expand Down Expand Up @@ -63,13 +64,10 @@ export default class EnvironmentController {
request: GetAllEnvironmentsOfProjectRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetAllEnvironmentsOfProjectResponse>> {
let url = `/api/environment/all/${request.projectSlug}?`
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 url = parsePaginationUrl(
`/api/environment/all/${request.projectSlug}`,
request
)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetAllEnvironmentsOfProjectResponse>(response)
Expand Down
12 changes: 5 additions & 7 deletions packages/api-client/src/controllers/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { APIClient } from '@api-client/core/client'
import { ClientResponse } from '@api-client/types/index.types'
import { parseResponse } from '@api-client/core/response-parser'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'

export default class IntegrationController {
private apiClient: APIClient
Expand Down Expand Up @@ -60,13 +61,10 @@ export default class IntegrationController {
request: GetAllIntegrationRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetAllIntegrationResponse>> {
let url = `/api/integration/all/${request.workspaceSlug}`
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 url = parsePaginationUrl(
`/api/integration/all/${request.workspaceSlug}`,
request
)
const response = await this.apiClient.get(url, headers)
return await parseResponse<GetAllIntegrationResponse>(response)
}
Expand Down
21 changes: 9 additions & 12 deletions packages/api-client/src/controllers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
UpdateProjectResponse
} from '@api-client/types/project.types'
import { parseResponse } from '@api-client/core/response-parser'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'

export default class ProjectController {
private apiClient: APIClient
Expand Down Expand Up @@ -121,12 +122,10 @@ export default class ProjectController {
request: GetForkRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetForkResponse>> {
let url = `/api/project/${request.projectSlug}/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 url = parsePaginationUrl(
`/api/project/${request.projectSlug}/forks`,
request
)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetForkResponse>(response)
Expand All @@ -136,12 +135,10 @@ export default class ProjectController {
request: GetAllProjectsRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetAllProjectsResponse>> {
let url = `/api/project/all/${request.workspaceSlug}`
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 url = parsePaginationUrl(
`/api/project/all/${request.workspaceSlug}`,
request
)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetAllProjectsResponse>(response)
Expand Down
16 changes: 9 additions & 7 deletions packages/api-client/src/controllers/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
UpdateSecretRequest,
UpdateSecretResponse
} from '@api-client/types/secret.types'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'

export default class SecretController {
private apiClient: APIClient
Expand Down Expand Up @@ -78,13 +79,14 @@ export default class SecretController {
request: GetAllSecretsOfProjectRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetAllSecretsOfProjectResponse>> {
let url = `/api/secret/${request.projectSlug}?decryptValue=true`
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)
const url = parsePaginationUrl(
`/api/secret/${request.projectSlug}`,
request
)
const response = await this.apiClient.get(
`${url}&decryptValue=true`,
headers
)

return await parseResponse<GetAllSecretsOfProjectResponse>(response)
}
Expand Down
11 changes: 5 additions & 6 deletions packages/api-client/src/controllers/variable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIClient } from '@api-client/core/client'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'
import { parseResponse } from '@api-client/core/response-parser'
import { ClientResponse } from '@api-client/types/index.types'
import {
Expand Down Expand Up @@ -77,12 +78,10 @@ export default class VariableController {
request: GetAllVariablesOfProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetAllVariablesOfProjectResponse>> {
let url = `/api/variable/${request.projectSlug}`
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 url = parsePaginationUrl(
`/api/variable/${request.projectSlug}`,
request
)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetAllVariablesOfProjectResponse>(response)
Expand Down
101 changes: 101 additions & 0 deletions packages/api-client/src/controllers/workspace-role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { APIClient } from '@api-client/core/client'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'
import { parseResponse } from '@api-client/core/response-parser'
import { ClientResponse } from '@api-client/types/index.types'
import {
CreateWorkspaceRoleRequest,
CreateWorkspaceRoleResponse,
UpdateWorkspaceRoleRequest,
UpdateWorkspaceRoleResponse,
DeleteWorkspaceRoleRequest,
DeleteWorkspaceRoleResponse,
CheckWorkspaceRoleExistsResponse,
GetWorkspaceRoleRequest,
GetWorkspaceRoleResponse,
GetWorkspaceRolesOfWorkspaceRequest,
GetWorkspaceRolesOfWorkspaceResponse,
CheckWorkspaceRoleExistsRequest
} from '@api-client/types/workspace-role.types'

export default class WorkspaceRoleController {
private apiClient: APIClient

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

async createWorkspaceRole(
request: CreateWorkspaceRoleRequest,
headers?: Record<string, string>
): Promise<ClientResponse<CreateWorkspaceRoleResponse>> {
const response = await this.apiClient.post(
`/api/workspace-role/${request.workspaceSlug}`,
request,
headers
)

return await parseResponse<CreateWorkspaceRoleResponse>(response)
}

async updateWorkspaceRole(
request: UpdateWorkspaceRoleRequest,
headers?: Record<string, string>
): Promise<ClientResponse<UpdateWorkspaceRoleResponse>> {
const response = await this.apiClient.put(
`/api/workspace-role/${request.workspaceRoleSlug}`,
request,
headers
)

return await parseResponse<UpdateWorkspaceRoleResponse>(response)
}

async deleteWorkspaceRole(
request: DeleteWorkspaceRoleRequest,
headers?: Record<string, string>
): Promise<ClientResponse<DeleteWorkspaceRoleResponse>> {
const response = await this.apiClient.delete(
`/api/workspace-role/${request.workspaceRoleSlug}`,
headers
)

return await parseResponse<DeleteWorkspaceRoleResponse>(response)
}

async checkWorkspaceRoleExists(
request: CheckWorkspaceRoleExistsRequest,
headers?: Record<string, string>
): Promise<ClientResponse<CheckWorkspaceRoleExistsResponse>> {
const response = await this.apiClient.get(
`/api/workspace-role/${request.workspaceSlug}/exists/${request.workspaceRoleName}`,
headers
)

return await parseResponse<CheckWorkspaceRoleExistsResponse>(response)
}

async getWorkspaceRole(
request: GetWorkspaceRoleRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetWorkspaceRoleResponse>> {
const response = await this.apiClient.get(
`/api/workspace-role/${request.workspaceRoleSlug}`,
headers
)

return await parseResponse<GetWorkspaceRoleResponse>(response)
}

async getWorkspaceRolesOfWorkspace(
request: GetWorkspaceRolesOfWorkspaceRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetWorkspaceRolesOfWorkspaceResponse>> {
const url = parsePaginationUrl(
`/api/workspace-role/${request.workspaceSlug}/all`,
request
)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetWorkspaceRolesOfWorkspaceResponse>(response)
}
}
9 changes: 2 additions & 7 deletions packages/api-client/src/controllers/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { APIClient } from '@api-client/core/client'
import { parsePaginationUrl } from '@api-client/core/pagination-parser'
import { parseResponse } from '@api-client/core/response-parser'
import { ClientResponse } from '@api-client/types/index.types'
import {
Expand Down Expand Up @@ -79,13 +80,7 @@ export default class WorkspaceController {
request: GetAllWorkspacesOfUserRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetAllWorkspacesOfUserResponse>> {
let url = `/api/workspace?`
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 url = parsePaginationUrl('/api/workspace', request)
const response = await this.apiClient.get(url, headers)

return await parseResponse<GetAllWorkspacesOfUserResponse>(response)
Expand Down
23 changes: 23 additions & 0 deletions packages/api-client/src/core/pagination-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { PageRequest } from '@api-client/types/index.types'

/**
* Constructs a URL by appending the given page request
* parameters to the given base URL.
*
* @param baseUrl The base URL to append to.
* @param request The page request to parse.
* @returns The constructed URL.
*/
export function parsePaginationUrl(
baseUrl: string,
request: PageRequest
): string {
let url = `${baseUrl}?`
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}&`)

return url
}
11 changes: 3 additions & 8 deletions 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 './index.types'
import { PageRequest, PageResponse } from './index.types'

export interface CreateEnvironmentRequest {
name: string
Expand Down Expand Up @@ -49,17 +49,12 @@ export interface GetEnvironmentResponse {
projectId: string
}

export interface GetAllEnvironmentsOfProjectRequest {
export interface GetAllEnvironmentsOfProjectRequest extends PageRequest {
projectSlug: string
page?: number
limit?: number
sort?: string
order?: string
search?: string
}

export interface GetAllEnvironmentsOfProjectResponse
extends Page<{
extends PageResponse<{
id: string
slug: string
name: string
Expand Down
4 changes: 2 additions & 2 deletions 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 './index.types'
import { PageResponse } from './index.types'

export enum EventSource {
SECRET,
Expand Down Expand Up @@ -57,7 +57,7 @@ export interface GetEventsRequest {
}

export interface GetEventsResponse
extends Page<{
extends PageResponse<{
id: string
source: EventSource
triggerer: EventTriggerer
Expand Down
10 changes: 9 additions & 1 deletion packages/api-client/src/types/index.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface Page<T> {
export interface PageResponse<T> {
items: T[]
metadata: {
page: number
Expand All @@ -15,6 +15,14 @@ export interface Page<T> {
}
}

export interface PageRequest {
page?: number
limit?: number
sort?: string
order?: string
search?: string
}

export interface ResponseError {
message: string
error: string
Expand Down
11 changes: 3 additions & 8 deletions packages/api-client/src/types/integration.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Page } from './index.types'
import { PageRequest, PageResponse } from './index.types'

export enum IntegrationType {
DISCORD,
Expand Down Expand Up @@ -109,17 +109,12 @@ export interface GetIntegrationResponse {
environmentId: string
}

export interface GetAllIntegrationRequest {
page?: number
limit?: number
sort?: string
order?: string
search?: string
export interface GetAllIntegrationRequest extends PageRequest {
workspaceSlug: string
}

export interface GetAllIntegrationResponse
extends Page<{
extends PageResponse<{
id: string
name: string
slug: string
Expand Down
Loading

0 comments on commit b03ce8e

Please sign in to comment.