Skip to content

Commit

Permalink
feat(api-client): Create controller for Secret module (#396)
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 ed18eb0 commit 7e929c0
Show file tree
Hide file tree
Showing 3 changed files with 408 additions and 0 deletions.
102 changes: 102 additions & 0 deletions packages/api-client/src/controllers/secret/secret.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { APIClient } from '../../core/client'
import { ClientResponse } from 'src/types/index.types'
import { parseResponse } from '../../core/response-parser'
import {
CreateSecretRequest,
CreateSecretResponse,
DeleteSecretRequest,
DeleteSecretResponse,
GetAllSecretsOfEnvironmentRequest,
GetAllSecretsOfEnvironmentResponse,
GetAllSecretsOfProjectRequest,
GetAllSecretsOfProjectResponse,
RollBackSecretRequest,
RollBackSecretResponse,
UpdateSecretRequest,
UpdateSecretResponse
} from '../../types/secret.types'

export default class SecretController {
private apiClient: APIClient

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

async createSecret(
request: CreateSecretRequest,
headers?: Record<string, string>
): Promise<ClientResponse<CreateSecretResponse>> {
const response = await this.apiClient.post(
`/api/secret/${request.projectId}`,
request,
headers
)
return await parseResponse<CreateSecretResponse>(response)
}
async updateSecret(
request: UpdateSecretRequest,
headers?: Record<string, string>
): Promise<ClientResponse<UpdateSecretResponse>> {
const response = await this.apiClient.put(
`/api/secret/${request.secretId}`,
request,
headers
)

return await parseResponse<UpdateSecretResponse>(response)
}

async rollbackSecret(
request: RollBackSecretRequest,
headers?: Record<string, string>
): Promise<ClientResponse<RollBackSecretResponse>> {
const response = await this.apiClient.put(
`/api/secret/${request.secretId}/rollback/${request.version}?environmentId=${request.environmentId}`,
request,
headers
)
return await parseResponse<RollBackSecretResponse>(response)
}

async deleteSecret(
request: DeleteSecretRequest,
headers?: Record<string, string>
): Promise<ClientResponse<DeleteSecretResponse>> {
const response = await this.apiClient.delete(
`/api/secret/${request.secretId}`,
headers
)
return await parseResponse<DeleteSecretResponse>(response)
}

async getAllSecretsOfProject(
request: GetAllSecretsOfProjectRequest,
headers?: Record<string, string>
): Promise<ClientResponse<GetAllSecretsOfProjectResponse>> {
let url = `/api/secret/${request.projectId}?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)

return await parseResponse<GetAllSecretsOfProjectResponse>(response)
}

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

export interface CreateSecretRequest {
projectId: string
name: string
note?: string
rotateAfter?: '24' | '168' | '720' | '8760' | 'never'
entries?: [
{
value: string
environmentId: string
}
]
}

export interface CreateSecretResponse {
id: string
name: string
createdAt: string
updatedAt: string
rotateAt: string | null
note: string | null
lastUpdatedById: string
projectId: string
project: {
workspaceId: string
}
versions: [
{
value: string
environmentId: string
}
]
}

export interface UpdateSecretRequest {
secretId: string
name?: string
note?: string
rotateAfter?: '24' | '168' | '720' | '8760' | 'never'
entries?: [
{
value: string
environmentId: string
}
]
}

export interface UpdateSecretResponse {
secret: {
id: string
name: string
note: string
}
updatedVersions: [
{
id?: string
environmentId: string
value: string
}
]
}

export interface DeleteSecretRequest {
secretId: string
}

export interface DeleteSecretResponse {}

export interface RollBackSecretRequest {
environmentId: string
version: number
secretId: string
}
export interface RollBackSecretResponse {
count: string
}

export interface GetAllSecretsOfProjectRequest {
projectId: string
page?: number
limit?: number
sort?: string
order?: string
search?: string
}
export interface GetAllSecretsOfProjectResponse
extends Page<{
secret: {
id: string
name: string
createdAt: string
updatedAt: string
rotateAt: 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 GetAllSecretsOfEnvironmentRequest {
projectId: string
environmentId: string
page?: number
limit?: number
sort?: string
order?: string
search?: string
}
export interface GetAllSecretsOfEnvironmentResponse
extends Page<{
name: string
value: string
isPlaintext: boolean
}> {}
Loading

0 comments on commit 7e929c0

Please sign in to comment.