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
71 changes: 71 additions & 0 deletions packages/api-client/src/controllers/variable/variable.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,71 @@
import client from '@package/client'
import {
CreateVariableRequest,
CreateVariableResponse,
DeleteVariableRequest,
DeleteVariableResponse,
GetAllVariablesOfEnvironmentRequest,
GetAllVariablesOfEnvironmentResponse,
GetAllVariablesOfProjectRequest,
GetAllVariablesOfProjectResponse,
RollBackVariableRequest,
RollBackVariableResponse,
UpdateVariableRequest,
UpdateVariableResponse
} from '@package/types/variable.types'

export default class VariableController {
private static apiClient = client

static async createVariable(
request: CreateVariableRequest,
headers: Record<string, string>
): Promise<CreateVariableResponse> {
return this.apiClient.post(
`/api/variable/${request.projectId}`,
request,
headers
)
}
static async updateVariable(
request: UpdateVariableRequest,
headers: Record<string, string>
): Promise<UpdateVariableResponse> {
return this.apiClient.put(
`/api/variable/${request.variableId}`,
request,
headers
)
}
static async rollbackVariable(
request: RollBackVariableRequest,
headers: Record<string, string>
): Promise<RollBackVariableResponse> {
return this.apiClient.put(
`/api/variable/${request.variableId}/rollback/${request.version}?environmentId=${request.environmentId}`,
request,
headers
)
}
static async deleteVariable(
request: DeleteVariableRequest,
headers: Record<string, string>
): Promise<DeleteVariableResponse> {
return this.apiClient.delete(`/api/variable/${request.variableId}`, headers)
}
static async getAllVariablesOfProject(
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
request: GetAllVariablesOfProjectRequest,
headers: Record<string, string>
): Promise<GetAllVariablesOfProjectResponse> {
return this.apiClient.get(`/api/variable/${request.projectId}`, headers)
}
static async getAllVariablesOfEnvironment(
request: GetAllVariablesOfEnvironmentRequest,
headers: Record<string, string>
): Promise<GetAllVariablesOfEnvironmentResponse> {
return this.apiClient.get(
`/api/variable/${request.projectId}/${request.environmentId}`,
headers
)
}
}
85 changes: 85 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,85 @@
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?: Entries[]
}
export interface UpdateVariableResponse {
variable: Variable
updatedVersions: Entries[]
}

export interface Variable {
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
id: string
name: string
note: 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 {
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
[]
}

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 {}
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
157 changes: 157 additions & 0 deletions packages/api-client/tests/variable.spec.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,157 @@
import client from '@package/client'
import VariableController from '@package/controllers/variable/variable'

describe('Get Variable Tests', () => {
const email = 'johndoe@example.com'
let workspaceId: string | null
let projectId: string | null
let environment: any
let variableId: string | null

beforeAll(async () => {
const workspaceResponse = (await client.post(
'/api/workspace',
{
name: 'My Workspace'
},
{
'x-e2e-user-email': email
}
)) as any

workspaceId = workspaceResponse.id

const projectResponse = (await client.post(
`/api/project/${workspaceId}`,
{
name: 'Project',
storePrivateKey: true
},
{
'x-e2e-user-email': email
}
)) as any

projectId = projectResponse.id

const createEnvironmentResponse = await client.post(
`/api/environment/${projectId}`,
{
name: 'Dev'
},
{
'x-e2e-user-email': email
}
)

environment = createEnvironmentResponse
})
afterAll(async () => {
await client.delete(`/api/workspace/${workspaceId}`, {
'x-e2e-user-email': email
})
})

// Create a variable
it('should create a variable', async () => {
const variable = await VariableController.createVariable(
{
projectId,
name: 'Variable 1',
entries: [{ value: 'Variable 1 value', environmentId: environment.id }]
},
{
'x-e2e-user-email': email
}
)
expect(variable.name).toBe('Variable 1')
expect(variable.projectId).toBe(projectId)
expect(variable.project.workspaceId).toBe(workspaceId)
expect(variable.versions.length).toBe(1)
expect(variable.versions[0].value).toBe('Variable 1 value')
expect(variable.versions[0].environmentId).toBe(environment.id)
variableId = variable.id
})

// Update Name of the Variable
it('should update the name a variable', async () => {
const updatedVariable = await VariableController.updateVariable(
{
name: 'UpdatedVariable 1',
variableId
},
{
'x-e2e-user-email': email
}
)
expect(updatedVariable.variable.name).toBe('UpdatedVariable 1')
expect(updatedVariable.variable.id).toBe(variableId)
})

// Create a new version of Variable
it('should add version to a variable', async () => {
const updateVariable = await VariableController.updateVariable(
{
entries: [
{
value: '1234',
environmentId: environment.id
}
],
variableId
},
{ 'x-e2e-user-email': email }
)
expect(updateVariable.updatedVersions.length).toBe(1)
expect(updateVariable.updatedVersions[0].value).toBe('1234')
expect(updateVariable.updatedVersions[0].environmentId).toBe(environment.id)
})

// Roll back a variable
it('should rollback a variable', async () => {
const rolledBackVariable: any = await VariableController.rollbackVariable(
{
variableId,
version: 1,
environmentId: environment.id
},
{ 'x-e2e-user-email': email }
)
expect(rolledBackVariable.count).toBe(1)
})

// Get all the variables of project
it('should get all variables of project', async () => {
const variables: any = await VariableController.getAllVariablesOfProject(
{ projectId },
{ 'x-e2e-user-email': email }
)
expect(variables.length).toBe(1)
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
})

// Get all variables for an environment
it('should get all variables for an environment', async () => {
const variables: any =
await VariableController.getAllVariablesOfEnvironment(
{
environmentId: environment.id,
projectId
},
{ 'x-e2e-user-email': email }
)
expect(variables.length).toBe(1)
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
})
vr-varad marked this conversation as resolved.
Show resolved Hide resolved

// Delete a variable
it('should delete variable', async () => {
await VariableController.deleteVariable(
{ variableId },
{ 'x-e2e-user-email': email }
)
const variables: any = await VariableController.getAllVariablesOfProject(
{ projectId },
{ 'x-e2e-user-email': email }
)
expect(variables.length).toBe(0)
})
})
Loading