-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-client): Create controller for Secret module (#396)
Co-authored-by: vr-varad <varadgupta21#gmail.com>
- Loading branch information
Showing
3 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}> {} |
Oops, something went wrong.