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: any
): Promise<CreateVariableResponse> {
return this.apiClient.post(
`/api/variable/${request.projectId}`,
request,
headers
)
}
static async updateVariable(
request: UpdateVariableRequest,
headers: any
): Promise<UpdateVariableResponse> {
return this.apiClient.put(
`/api/variable/${request.variableId}`,
request,
headers
)
}
static async rollbackVariable(
request: RollBackVariableRequest,
headers: any
): Promise<RollBackVariableResponse> {
return this.apiClient.put(
`/api/variable/${request.variableId}/rollback/${request.version}?environmentId=${request.environmentId}`,
request,
headers
)
}
static async deleteVariable(
request: DeleteVariableRequest,
headers: any
): 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: any
): Promise<getAllVariablesOfProjectResponse> {
return this.apiClient.get(`/api/variable/${request.projectId}`, headers)
}
static async getAllVariablesOfEnvironment(
request: getAllVariablesOfEnvironmentRequest,
headers: any
): Promise<getAllVariablesOfEnvironmentResponse> {
return this.apiClient.get(
`/api/variable/${request.projectId}/${request.environmentId}`,
headers
)
}
}
58 changes: 58 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,58 @@
export interface CreateVariableRequest {
projectId: string
name: string
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
entries: Entries[]
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
}
export interface Entries {
value: string
environmentId: string
}
export interface CreateVariableResponse {
id: string
name: string
createdAt: string
updatedAt: string
note: string | null
lastUpdatedById: string
projectId: string
project: Project
versions: Entries[]
}

export interface Project {
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
workspaceId: string
}
export interface UpdateVariableRequest {
variableId?: string
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
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 {}
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
export interface DeleteVariableRequest {
variableId: string
}
export interface DeleteVariableResponse {}
export interface getAllVariablesOfProjectRequest {
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
projectId: string
}
export interface getAllVariablesOfProjectResponse {}
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
export interface getAllVariablesOfEnvironmentRequest {
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
projectId: string
environmentId: string
}
export interface getAllVariablesOfEnvironmentResponse {}
vr-varad marked this conversation as resolved.
Show resolved Hide resolved
137 changes: 137 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,137 @@
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
})
})
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
})

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)
})

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)
})

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)
})

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
})

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
})
Loading