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 Secret module #396

Merged
merged 6 commits into from
Aug 3, 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
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
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
limit?: number
sort?: string
order?: string
search?: string
}
export interface GetAllSecretsOfEnvironmentResponse
extends Page<{
name: string
value: string
isPlaintext: boolean
}> {}
Loading
Loading