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 Variable module #395

Merged
merged 12 commits into from
Aug 16, 2024
Merged
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
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
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
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
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
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
limit?: number
sort?: string
order?: string
search?: string
}

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