Skip to content

Commit

Permalink
feat(api-client): Create controller for Variable module (#395)
Browse files Browse the repository at this point in the history
Co-authored-by: vr-varad <varadgupta21#gmail.com>
  • Loading branch information
vr-varad authored and rajdip-b committed Sep 5, 2024
1 parent 697d38b commit 3e114d9
Show file tree
Hide file tree
Showing 3 changed files with 457 additions and 0 deletions.
105 changes: 105 additions & 0 deletions packages/api-client/src/controllers/variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { APIClient } from '../core/client'
import { parseResponse } from '../core/response-parser'
import { ClientResponse } from '../types/index.types'
import {
CreateVariableRequest,
CreateVariableResponse,
DeleteVariableRequest,
DeleteVariableResponse,
GetAllVariablesOfEnvironmentRequest,
GetAllVariablesOfEnvironmentResponse,
GetAllVariablesOfProjectRequest,
GetAllVariablesOfProjectResponse,
RollBackVariableRequest,
RollBackVariableResponse,
UpdateVariableRequest,
UpdateVariableResponse
} from '../types/variable.types'

export default class VariableController {
private apiClient: APIClient

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

async createVariable(
request: CreateVariableRequest,
headers: Record<string, string>
): Promise<ClientResponse<CreateVariableResponse>> {
const response = await this.apiClient.post(
`/api/variable/${request.projectId}`,
request,
headers
)
return await parseResponse<CreateVariableResponse>(response)
}

async updateVariable(
request: UpdateVariableRequest,
headers: Record<string, string>
): Promise<ClientResponse<UpdateVariableResponse>> {
const response = await this.apiClient.put(
`/api/variable/${request.variableId}`,
request,
headers
)

return await parseResponse<UpdateVariableResponse>(response)
}

async rollbackVariable(
request: RollBackVariableRequest,
headers: Record<string, string>
): Promise<ClientResponse<RollBackVariableResponse>> {
const response = await this.apiClient.put(
`/api/variable/${request.variableId}/rollback/${request.version}?environmentId=${request.environmentId}`,
request,
headers
)

return await parseResponse<RollBackVariableResponse>(response)
}

async deleteVariable(
request: DeleteVariableRequest,
headers: Record<string, string>
): Promise<ClientResponse<DeleteVariableResponse>> {
const response = await this.apiClient.delete(
`/api/variable/${request.variableId}`,
headers
)

return await parseResponse<DeleteVariableResponse>(response)
}

async getAllVariablesOfProject(
request: GetAllVariablesOfProjectRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetAllVariablesOfProjectResponse>> {
let url = `/api/variable/${request.projectId}`
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<GetAllVariablesOfProjectResponse>(response)
}

async getAllVariablesOfEnvironment(
request: GetAllVariablesOfEnvironmentRequest,
headers: Record<string, string>
): Promise<ClientResponse<GetAllVariablesOfEnvironmentResponse>> {
let url = `/api/variable/${request.projectId}/${request.environmentId}`
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<GetAllVariablesOfEnvironmentResponse>(response)
}
}
122 changes: 122 additions & 0 deletions packages/api-client/src/types/variable.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { Page } from '../../../../apps/cli/src/types/index.types'

export interface CreateVariableRequest {
projectId: string
name: string
note?: string
entries?: [
{
value: string
environmentId: string
}
]
}

export interface CreateVariableResponse {
id: string
name: string
createdAt: string
updatedAt: string
note: string | null
lastUpdatedById: string
projectId: string
project: {
workspaceId: string
}
versions: [
{
value: string
environmentId: string
}
]
}
export interface UpdateVariableRequest {
variableId: string
name?: string
entries?: [
{
value: string
environmentId: string
}
]
}
export interface UpdateVariableResponse {
variable: {
id: string
name: string
note: string
}
updatedVersions: [
{
value: string
environmentId: string
}
]
}

export interface RollBackVariableRequest {
variableId: string
version: number
environmentId: string
}

export interface RollBackVariableResponse {
count: string
}

export interface DeleteVariableRequest {
variableId: string
}

export interface DeleteVariableResponse {}

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

export interface GetAllVariablesOfProjectResponse
extends Page<{
variable: {
id: string
name: string
createdAt: string
updatedAt: string
note: string | null
lastUpdatedById: string
projectId: string
lastUpdatedBy: {
id: string
name: string
}
}
values: {
environment: {
id: string
name: string
}
value: string
version: number
}
}> {}

export interface GetAllVariablesOfEnvironmentRequest {
projectId: string
environmentId: string
page?: number
limit?: number
sort?: string
order?: string
search?: string
}

export interface GetAllVariablesOfEnvironmentResponse
extends Page<{
name: string
value: string
isPlaintext: boolean
}> {}
Loading

0 comments on commit 3e114d9

Please sign in to comment.