From 2c7887ac3c0a168302f69caceac8b368a9084177 Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 08:44:01 +0530 Subject: [PATCH 01/11] Initialized Solution with Structure --- .../api-client/src/controllers/variable/variable.ts | 12 ++++++++++++ packages/api-client/src/types/variable.types.d.ts | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 packages/api-client/src/controllers/variable/variable.ts create mode 100644 packages/api-client/src/types/variable.types.d.ts diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable/variable.ts new file mode 100644 index 00000000..40840660 --- /dev/null +++ b/packages/api-client/src/controllers/variable/variable.ts @@ -0,0 +1,12 @@ +import client from '@package/client' + +export default class VariableController { + private static apiClient = client + + static async createVariable() {} + static async updateVariable() {} + static async rollbackVariable() {} + static async deleteVariable() {} + static async getAllVariablesOfProject() {} + static async getAllVariablesOfEnvironment() {} +} diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts new file mode 100644 index 00000000..122524ca --- /dev/null +++ b/packages/api-client/src/types/variable.types.d.ts @@ -0,0 +1,12 @@ +export interface CreateVariableRequest {} +export interface CreateVariableResponse {} +export interface UpdateVariableRequest {} +export interface UpdateVariableResponse {} +export interface RollBackVariableRequest {} +export interface RollBackVariableResponse {} +export interface DeleteVariableRequest {} +export interface DeleteVariableResponse {} +export interface getAllVariablesOfProjectRequest {} +export interface getAllVariablesOfProjectResponse {} +export interface getAllVariablesOfEnvironmentRequest {} +export interface getAllVariablesOfEnvironmentResponse {} From 1c4e67c2778e649c0bf3ab2903b4ea7bc37915aa Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 09:11:45 +0530 Subject: [PATCH 02/11] Defined Controllers and Types for req and res --- .../src/controllers/variable/variable.ts | 71 +++++++++++++++++-- .../api-client/src/types/variable.types.d.ts | 62 +++++++++++++--- 2 files changed, 119 insertions(+), 14 deletions(-) diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable/variable.ts index 40840660..b4253160 100644 --- a/packages/api-client/src/controllers/variable/variable.ts +++ b/packages/api-client/src/controllers/variable/variable.ts @@ -1,12 +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() {} - static async updateVariable() {} - static async rollbackVariable() {} - static async deleteVariable() {} - static async getAllVariablesOfProject() {} - static async getAllVariablesOfEnvironment() {} + static async createVariable( + request: CreateVariableRequest, + headers: any + ): Promise { + return this.apiClient.post( + `/api/variable/${request.projectId}`, + request, + headers + ) + } + static async updateVariable( + request: UpdateVariableRequest, + headers: any + ): Promise { + return this.apiClient.put( + `/api/variable/${request.variableId}`, + request, + headers + ) + } + static async rollbackVariable( + request: RollBackVariableRequest, + headers: any + ): Promise { + return this.apiClient.put( + `/api/variable/${request.variableId}/rollback/${request.version}?environmentId=${request.environmentId}`, + request, + headers + ) + } + static async deleteVariable( + request: DeleteVariableRequest, + headers: any + ): Promise { + return this.apiClient.delete(`/api/variable/${request.variableId}`, headers) + } + static async getAllVariablesOfProject( + request: getAllVariablesOfProjectRequest, + headers: any + ): Promise { + return this.apiClient.get(`}/api/variable/${request.projectId}`, headers) + } + static async getAllVariablesOfEnvironment( + request: getAllVariablesOfEnvironmentRequest, + headers: any + ): Promise { + return this.apiClient.get( + `/api/variable/${request.projectId}/${request.environmentId}`, + headers + ) + } } diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 122524ca..76998618 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -1,12 +1,58 @@ -export interface CreateVariableRequest {} -export interface CreateVariableResponse {} -export interface UpdateVariableRequest {} -export interface UpdateVariableResponse {} -export interface RollBackVariableRequest {} +export interface CreateVariableRequest { + projectId: string + name: string + entries: Entries[] +} +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 { + workspaceId: string +} +export interface UpdateVariableRequest { + variableId: string + name?: string + entries?: Entries[] +} +export interface UpdateVariableResponse { + variables: Variable + updatedVersions: Entries[] +} + +export interface Variable { + id: string + name: string + note: string +} +export interface RollBackVariableRequest { + variableId: string + version: string + environmentId: string +} export interface RollBackVariableResponse {} -export interface DeleteVariableRequest {} +export interface DeleteVariableRequest { + variableId: string +} export interface DeleteVariableResponse {} -export interface getAllVariablesOfProjectRequest {} +export interface getAllVariablesOfProjectRequest { + projectId: string +} export interface getAllVariablesOfProjectResponse {} -export interface getAllVariablesOfEnvironmentRequest {} +export interface getAllVariablesOfEnvironmentRequest { + projectId: string + environmentId: string +} export interface getAllVariablesOfEnvironmentResponse {} From a8bcf927bba7084d39210e43289ec84d5c6b7a6d Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 10:46:34 +0530 Subject: [PATCH 03/11] Added Tests --- .../src/controllers/variable/variable.ts | 2 +- .../api-client/src/types/variable.types.d.ts | 6 +- packages/api-client/tests/variable.spec.ts | 137 ++++++++++++++++++ 3 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 packages/api-client/tests/variable.spec.ts diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable/variable.ts index b4253160..ff6b1ccf 100644 --- a/packages/api-client/src/controllers/variable/variable.ts +++ b/packages/api-client/src/controllers/variable/variable.ts @@ -57,7 +57,7 @@ export default class VariableController { request: getAllVariablesOfProjectRequest, headers: any ): Promise { - return this.apiClient.get(`}/api/variable/${request.projectId}`, headers) + return this.apiClient.get(`/api/variable/${request.projectId}`, headers) } static async getAllVariablesOfEnvironment( request: getAllVariablesOfEnvironmentRequest, diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 76998618..83ecc253 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -23,12 +23,12 @@ export interface Project { workspaceId: string } export interface UpdateVariableRequest { - variableId: string + variableId?: string name?: string entries?: Entries[] } export interface UpdateVariableResponse { - variables: Variable + variable: Variable updatedVersions: Entries[] } @@ -39,7 +39,7 @@ export interface Variable { } export interface RollBackVariableRequest { variableId: string - version: string + version: number environmentId: string } export interface RollBackVariableResponse {} diff --git a/packages/api-client/tests/variable.spec.ts b/packages/api-client/tests/variable.spec.ts new file mode 100644 index 00000000..6a25c654 --- /dev/null +++ b/packages/api-client/tests/variable.spec.ts @@ -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) + }) + + 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) + }) +}) From 6a00f411b0fac8c1e60986735eb8d1da8f13b2f6 Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 11:29:37 +0530 Subject: [PATCH 04/11] Some Minute Changes --- .../api-client/src/controllers/variable/variable.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable/variable.ts index ff6b1ccf..53aee71a 100644 --- a/packages/api-client/src/controllers/variable/variable.ts +++ b/packages/api-client/src/controllers/variable/variable.ts @@ -19,7 +19,7 @@ export default class VariableController { static async createVariable( request: CreateVariableRequest, - headers: any + headers: Record ): Promise { return this.apiClient.post( `/api/variable/${request.projectId}`, @@ -29,7 +29,7 @@ export default class VariableController { } static async updateVariable( request: UpdateVariableRequest, - headers: any + headers: Record ): Promise { return this.apiClient.put( `/api/variable/${request.variableId}`, @@ -39,7 +39,7 @@ export default class VariableController { } static async rollbackVariable( request: RollBackVariableRequest, - headers: any + headers: Record ): Promise { return this.apiClient.put( `/api/variable/${request.variableId}/rollback/${request.version}?environmentId=${request.environmentId}`, @@ -49,19 +49,19 @@ export default class VariableController { } static async deleteVariable( request: DeleteVariableRequest, - headers: any + headers: Record ): Promise { return this.apiClient.delete(`/api/variable/${request.variableId}`, headers) } static async getAllVariablesOfProject( request: getAllVariablesOfProjectRequest, - headers: any + headers: Record ): Promise { return this.apiClient.get(`/api/variable/${request.projectId}`, headers) } static async getAllVariablesOfEnvironment( request: getAllVariablesOfEnvironmentRequest, - headers: any + headers: Record ): Promise { return this.apiClient.get( `/api/variable/${request.projectId}/${request.environmentId}`, From 4d7a70c6e1cefd1a001d9b064e424b8ae6ee462d Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 11:49:44 +0530 Subject: [PATCH 05/11] Resolved Changes Requested --- .../src/controllers/variable/variable.ts | 16 ++--- .../api-client/src/types/variable.types.d.ts | 61 +++++++++++++------ packages/api-client/tests/variable.spec.ts | 20 ++++++ 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable/variable.ts index 53aee71a..a641f1a8 100644 --- a/packages/api-client/src/controllers/variable/variable.ts +++ b/packages/api-client/src/controllers/variable/variable.ts @@ -4,10 +4,10 @@ import { CreateVariableResponse, DeleteVariableRequest, DeleteVariableResponse, - getAllVariablesOfEnvironmentRequest, - getAllVariablesOfEnvironmentResponse, - getAllVariablesOfProjectRequest, - getAllVariablesOfProjectResponse, + GetAllVariablesOfEnvironmentRequest, + GetAllVariablesOfEnvironmentResponse, + GetAllVariablesOfProjectRequest, + GetAllVariablesOfProjectResponse, RollBackVariableRequest, RollBackVariableResponse, UpdateVariableRequest, @@ -54,15 +54,15 @@ export default class VariableController { return this.apiClient.delete(`/api/variable/${request.variableId}`, headers) } static async getAllVariablesOfProject( - request: getAllVariablesOfProjectRequest, + request: GetAllVariablesOfProjectRequest, headers: Record - ): Promise { + ): Promise { return this.apiClient.get(`/api/variable/${request.projectId}`, headers) } static async getAllVariablesOfEnvironment( - request: getAllVariablesOfEnvironmentRequest, + request: GetAllVariablesOfEnvironmentRequest, headers: Record - ): Promise { + ): Promise { return this.apiClient.get( `/api/variable/${request.projectId}/${request.environmentId}`, headers diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 83ecc253..9e9d7c0c 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -1,12 +1,15 @@ export interface CreateVariableRequest { projectId: string name: string - entries: Entries[] -} -export interface Entries { - value: string - environmentId: string + note?: string + entries?: [ + { + value: string + environmentId: string + } + ] } + export interface CreateVariableResponse { id: string name: string @@ -15,15 +18,18 @@ export interface CreateVariableResponse { note: string | null lastUpdatedById: string projectId: string - project: Project - versions: Entries[] -} - -export interface Project { - workspaceId: string + project: { + workspaceId: string + } + versions: [ + { + value: string + environmentId: string + } + ] } export interface UpdateVariableRequest { - variableId?: string + variableId: string name?: string entries?: Entries[] } @@ -42,17 +48,38 @@ export interface RollBackVariableRequest { version: number environmentId: string } -export interface RollBackVariableResponse {} + +export interface RollBackVariableResponse { + count: string +} + export interface DeleteVariableRequest { variableId: string } + export interface DeleteVariableResponse {} -export interface getAllVariablesOfProjectRequest { + +export interface GetAllVariablesOfProjectRequest { projectId: string + page?: number + limit?: number + sort?: string + order?: string + search?: string } -export interface getAllVariablesOfProjectResponse {} -export interface getAllVariablesOfEnvironmentRequest { + +export interface GetAllVariablesOfProjectResponse { + [] +} + +export interface GetAllVariablesOfEnvironmentRequest { projectId: string environmentId: string + page?: number + limit?: number + sort?: string + order?: string + search?: string } -export interface getAllVariablesOfEnvironmentResponse {} + +export interface GetAllVariablesOfEnvironmentResponse {} diff --git a/packages/api-client/tests/variable.spec.ts b/packages/api-client/tests/variable.spec.ts index 6a25c654..ad66dd36 100644 --- a/packages/api-client/tests/variable.spec.ts +++ b/packages/api-client/tests/variable.spec.ts @@ -51,6 +51,8 @@ describe('Get Variable Tests', () => { 'x-e2e-user-email': email }) }) + + // Create a variable it('should create a variable', async () => { const variable = await VariableController.createVariable( { @@ -71,6 +73,7 @@ describe('Get Variable Tests', () => { variableId = variable.id }) + // Update Name of the Variable it('should update the name a variable', async () => { const updatedVariable = await VariableController.updateVariable( { @@ -85,6 +88,7 @@ describe('Get Variable Tests', () => { 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( { @@ -103,6 +107,7 @@ describe('Get Variable Tests', () => { expect(updateVariable.updatedVersions[0].environmentId).toBe(environment.id) }) + // Roll back a variable it('should rollback a variable', async () => { const rolledBackVariable: any = await VariableController.rollbackVariable( { @@ -115,6 +120,7 @@ describe('Get Variable Tests', () => { 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 }, @@ -123,6 +129,7 @@ describe('Get Variable Tests', () => { expect(variables.length).toBe(1) }) + // Get all variables for an environment it('should get all variables for an environment', async () => { const variables: any = await VariableController.getAllVariablesOfEnvironment( @@ -134,4 +141,17 @@ describe('Get Variable Tests', () => { ) expect(variables.length).toBe(1) }) + + // 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) + }) }) From 30985568321effb9aa7ec49e0049420250ddf352 Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 11:55:46 +0530 Subject: [PATCH 06/11] Some Changes in types --- packages/api-client/src/types/variable.types.d.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 9e9d7c0c..348f4607 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -31,11 +31,21 @@ export interface CreateVariableResponse { export interface UpdateVariableRequest { variableId: string name?: string - entries?: Entries[] + entries?: [ + { + value: string + environmentId: string + } + ] } export interface UpdateVariableResponse { variable: Variable - updatedVersions: Entries[] + updatedVersions: [ + { + value: string + environmentId: string + } + ] } export interface Variable { From 7fcea123ba95b91c0278c1dfb0d78f3620232ca0 Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 12:05:16 +0530 Subject: [PATCH 07/11] Resolved Errors --- .../src/controllers/variable/variable.ts | 24 +++++++-- .../api-client/src/types/variable.types.d.ts | 51 ++++++++++++++++--- 2 files changed, 62 insertions(+), 13 deletions(-) diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable/variable.ts index a641f1a8..578de57a 100644 --- a/packages/api-client/src/controllers/variable/variable.ts +++ b/packages/api-client/src/controllers/variable/variable.ts @@ -27,6 +27,7 @@ export default class VariableController { headers ) } + static async updateVariable( request: UpdateVariableRequest, headers: Record @@ -37,6 +38,7 @@ export default class VariableController { headers ) } + static async rollbackVariable( request: RollBackVariableRequest, headers: Record @@ -47,25 +49,37 @@ export default class VariableController { headers ) } + static async deleteVariable( request: DeleteVariableRequest, headers: Record ): Promise { return this.apiClient.delete(`/api/variable/${request.variableId}`, headers) } + static async getAllVariablesOfProject( request: GetAllVariablesOfProjectRequest, headers: Record ): Promise { - return this.apiClient.get(`/api/variable/${request.projectId}`, headers) + let url = `/api/variable/${request.projectId}` + 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}&`) + return this.apiClient.get(url, headers) } + static async getAllVariablesOfEnvironment( request: GetAllVariablesOfEnvironmentRequest, headers: Record ): Promise { - return this.apiClient.get( - `/api/variable/${request.projectId}/${request.environmentId}`, - headers - ) + let url = `/api/variable/${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}&`) + return this.apiClient.get(url, headers) } } diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 348f4607..3a525478 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -39,7 +39,11 @@ export interface UpdateVariableRequest { ] } export interface UpdateVariableResponse { - variable: Variable + variable: { + id: string + name: string + note: string + } updatedVersions: [ { value: string @@ -48,11 +52,6 @@ export interface UpdateVariableResponse { ] } -export interface Variable { - id: string - name: string - note: string -} export interface RollBackVariableRequest { variableId: string version: number @@ -79,7 +78,24 @@ export interface GetAllVariablesOfProjectRequest { } export interface GetAllVariablesOfProjectResponse { - [] + items: { + 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 GetAllVariablesOfEnvironmentRequest { @@ -92,4 +108,23 @@ export interface GetAllVariablesOfEnvironmentRequest { search?: string } -export interface GetAllVariablesOfEnvironmentResponse {} +export interface GetAllVariablesOfEnvironmentResponse { + items: { + id: string + name: string + createdAt: string + updatedAt: string + note: string | null + lastUpdatedById: string + projectId: string + project: { + workspaceId: string + } + versions: [ + { + value: string + environmentId: string + } + ] + }[] +} From 435050f0dae39f6114df9c34e10931139a4bf499 Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sun, 28 Jul 2024 18:34:04 +0530 Subject: [PATCH 08/11] Resolved Issues --- .../src/controllers/variable/variable.ts | 4 +- .../api-client/src/types/variable.types.d.ts | 44 +++++-------- packages/api-client/tests/variable.spec.ts | 64 ++++++++++++++++++- 3 files changed, 80 insertions(+), 32 deletions(-) diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable/variable.ts index 578de57a..dac4df82 100644 --- a/packages/api-client/src/controllers/variable/variable.ts +++ b/packages/api-client/src/controllers/variable/variable.ts @@ -60,7 +60,7 @@ export default class VariableController { static async getAllVariablesOfProject( request: GetAllVariablesOfProjectRequest, headers: Record - ): Promise { + ): Promise { let url = `/api/variable/${request.projectId}` request.page && (url += `page=${request.page}&`) request.limit && (url += `limit=${request.limit}&`) @@ -73,7 +73,7 @@ export default class VariableController { static async getAllVariablesOfEnvironment( request: GetAllVariablesOfEnvironmentRequest, headers: Record - ): Promise { + ): Promise { let url = `/api/variable/${request.projectId}/${request.environmentId}` request.page && (url += `page=${request.page}&`) request.limit && (url += `limit=${request.limit}&`) diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 3a525478..65b0580e 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -78,7 +78,7 @@ export interface GetAllVariablesOfProjectRequest { } export interface GetAllVariablesOfProjectResponse { - items: { + variable: { id: string name: string createdAt: string @@ -86,16 +86,19 @@ export interface GetAllVariablesOfProjectResponse { note: string | null lastUpdatedById: string projectId: string - project: { - workspaceId: string + lastUpdatedBy: { + id: string + name: string + } + } + values: { + environment: { + id: string + name: string } - versions: [ - { - value: string - environmentId: string - } - ] - }[] + value: string + version: number + } } export interface GetAllVariablesOfEnvironmentRequest { @@ -109,22 +112,7 @@ export interface GetAllVariablesOfEnvironmentRequest { } export interface GetAllVariablesOfEnvironmentResponse { - items: { - id: string - name: string - createdAt: string - updatedAt: string - note: string | null - lastUpdatedById: string - projectId: string - project: { - workspaceId: string - } - versions: [ - { - value: string - environmentId: string - } - ] - }[] + name: string + value: string + isPlaintext: boolean } diff --git a/packages/api-client/tests/variable.spec.ts b/packages/api-client/tests/variable.spec.ts index ad66dd36..5e74d7f9 100644 --- a/packages/api-client/tests/variable.spec.ts +++ b/packages/api-client/tests/variable.spec.ts @@ -122,11 +122,56 @@ describe('Get Variable Tests', () => { // Get all the variables of project it('should get all variables of project', async () => { - const variables: any = await VariableController.getAllVariablesOfProject( + const response: any = await VariableController.getAllVariablesOfProject( { projectId }, { 'x-e2e-user-email': email } ) - expect(variables.length).toBe(1) + expect(response.length).toBe(1) + const variable1 = response[0] + const variable = variable1.variable + const values = variable1.values + expect(variable).toHaveProperty('id') + expect(typeof variable.id).toBe('string') + + expect(variable).toHaveProperty('name') + expect(typeof variable.name).toBe('string') + + expect(variable).toHaveProperty('createdAt') + expect(typeof variable.createdAt).toBe('string') + + expect(variable).toHaveProperty('updatedAt') + expect(typeof variable.updatedAt).toBe('string') + + expect(variable).toHaveProperty('note') + expect(typeof variable.note === 'string' || variable.note === null).toBe( + true + ) + + expect(variable).toHaveProperty('lastUpdatedById') + expect(typeof variable.lastUpdatedById).toBe('string') + + expect(variable).toHaveProperty('projectId') + expect(typeof variable.projectId).toBe('string') + + expect(variable).toHaveProperty('lastUpdatedBy') + expect(variable.lastUpdatedBy).toHaveProperty('id') + expect(typeof variable.lastUpdatedBy.id).toBe('string') + expect(variable.lastUpdatedBy).toHaveProperty('name') + expect(typeof variable.lastUpdatedBy.name).toBe('string') + + values.forEach((value) => { + expect(value).toHaveProperty('environment') + expect(value.environment).toHaveProperty('id') + expect(typeof value.environment.id).toBe('string') + expect(value.environment).toHaveProperty('name') + expect(typeof value.environment.name).toBe('string') + + expect(value).toHaveProperty('value') + expect(typeof value.value).toBe('string') + + expect(value).toHaveProperty('version') + expect(typeof value.version).toBe('number') + }) }) // Get all variables for an environment @@ -139,7 +184,22 @@ describe('Get Variable Tests', () => { }, { 'x-e2e-user-email': email } ) + expect(variables.length).toBe(1) + variables.forEach((variable) => { + expect(variable).toHaveProperty('name') + expect(typeof variable.name).toBe('string') + + expect(variable).toHaveProperty('value') + expect(typeof variable.value).toBe('string') + + expect(variable).toHaveProperty('isPlaintext') + expect(typeof variable.isPlaintext).toBe('boolean') + }) + const variable1 = variables[0] + expect(variable1.name).toBe('UpdatedVariable 1') + expect(variable1.value).toBe('Variable 1 value') + expect(variable1.isPlaintext).toBe(true) }) // Delete a variable From 40b4eee809268e0734467bcb4d867a29a92df70b Mon Sep 17 00:00:00 2001 From: vr-varad Date: Fri, 16 Aug 2024 09:32:06 +0530 Subject: [PATCH 09/11] Adding New Conventions --- .../controllers/{variable => }/variable.ts | 62 ++++++++++++------- .../api-client/src/types/variable.types.d.ts | 2 + 2 files changed, 43 insertions(+), 21 deletions(-) rename packages/api-client/src/controllers/{variable => }/variable.ts (53%) diff --git a/packages/api-client/src/controllers/variable/variable.ts b/packages/api-client/src/controllers/variable.ts similarity index 53% rename from packages/api-client/src/controllers/variable/variable.ts rename to packages/api-client/src/controllers/variable.ts index dac4df82..232b9d34 100644 --- a/packages/api-client/src/controllers/variable/variable.ts +++ b/packages/api-client/src/controllers/variable.ts @@ -1,4 +1,6 @@ -import client from '@package/client' +import { APIClient } from 'src/core/client' +import { parseResponse } from '../core/response-parser' +import { ClientResponse } from '../types/index.types' import { CreateVariableRequest, CreateVariableResponse, @@ -12,74 +14,92 @@ import { RollBackVariableResponse, UpdateVariableRequest, UpdateVariableResponse -} from '@package/types/variable.types' +} from '../types/variable.types' export default class VariableController { - private static apiClient = client + private apiClient: APIClient - static async createVariable( + constructor(private readonly backendUrl: string) { + this.apiClient = new APIClient(this.backendUrl) + } + + async createVariable( request: CreateVariableRequest, headers: Record - ): Promise { - return this.apiClient.post( + ): Promise> { + const response = await this.apiClient.post( `/api/variable/${request.projectId}`, request, headers ) + return await parseResponse(response) } - static async updateVariable( + async updateVariable( request: UpdateVariableRequest, headers: Record - ): Promise { - return this.apiClient.put( + ): Promise> { + const response = await this.apiClient.put( `/api/variable/${request.variableId}`, request, headers ) + + return await parseResponse(response) } - static async rollbackVariable( + async rollbackVariable( request: RollBackVariableRequest, headers: Record - ): Promise { - return this.apiClient.put( + ): Promise> { + const response = await this.apiClient.put( `/api/variable/${request.variableId}/rollback/${request.version}?environmentId=${request.environmentId}`, request, headers ) + + return await parseResponse(response) } - static async deleteVariable( + async deleteVariable( request: DeleteVariableRequest, headers: Record - ): Promise { - return this.apiClient.delete(`/api/variable/${request.variableId}`, headers) + ): Promise> { + const response = await this.apiClient.delete( + `/api/variable/${request.variableId}`, + headers + ) + + return await parseResponse(response) } - static async getAllVariablesOfProject( + async getAllVariablesOfProject( request: GetAllVariablesOfProjectRequest, headers: Record - ): Promise { + ): Promise> { let url = `/api/variable/${request.projectId}` 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}&`) - return this.apiClient.get(url, headers) + const response = await this.apiClient.get(url, headers) + + return await parseResponse(response) } - static async getAllVariablesOfEnvironment( + async getAllVariablesOfEnvironment( request: GetAllVariablesOfEnvironmentRequest, headers: Record - ): Promise { + ): Promise> { let url = `/api/variable/${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}&`) - return this.apiClient.get(url, headers) + const response = await this.apiClient.get(url, headers) + + return await parseResponse(response) } } diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 65b0580e..f9b73908 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -1,3 +1,5 @@ +import { Page } from '../../../../apps/cli/src/types/index.types' + export interface CreateVariableRequest { projectId: string name: string From 06e7b6bf1a3db6c452cf151961799b5d756acaea Mon Sep 17 00:00:00 2001 From: vr-varad Date: Fri, 16 Aug 2024 09:54:39 +0530 Subject: [PATCH 10/11] Adding New Conventions --- .../api-client/src/controllers/variable.ts | 2 +- .../api-client/src/types/variable.types.d.ts | 50 +++---- packages/api-client/tests/variable.spec.ts | 127 ++++++++++-------- 3 files changed, 97 insertions(+), 82 deletions(-) diff --git a/packages/api-client/src/controllers/variable.ts b/packages/api-client/src/controllers/variable.ts index 232b9d34..65a83404 100644 --- a/packages/api-client/src/controllers/variable.ts +++ b/packages/api-client/src/controllers/variable.ts @@ -1,4 +1,4 @@ -import { APIClient } from 'src/core/client' +import { APIClient } from '../core/client' import { parseResponse } from '../core/response-parser' import { ClientResponse } from '../types/index.types' import { diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index f9b73908..a6b97091 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -79,29 +79,30 @@ export interface GetAllVariablesOfProjectRequest { search?: string } -export interface GetAllVariablesOfProjectResponse { - variable: { - id: string - name: string - createdAt: string - updatedAt: string - note: string | null - lastUpdatedById: string - projectId: string - lastUpdatedBy: { +export interface GetAllVariablesOfProjectResponse + extends Page<{ + variable: { id: string name: string + createdAt: string + updatedAt: string + note: string | null + lastUpdatedById: string + projectId: string + lastUpdatedBy: { + id: string + name: string + } } - } - values: { - environment: { - id: string - name: string + values: { + environment: { + id: string + name: string + } + value: string + version: number } - value: string - version: number - } -} + }> {} export interface GetAllVariablesOfEnvironmentRequest { projectId: string @@ -113,8 +114,9 @@ export interface GetAllVariablesOfEnvironmentRequest { search?: string } -export interface GetAllVariablesOfEnvironmentResponse { - name: string - value: string - isPlaintext: boolean -} +export interface GetAllVariablesOfEnvironmentResponse + extends Page<{ + name: string + value: string + isPlaintext: boolean + }> {} diff --git a/packages/api-client/tests/variable.spec.ts b/packages/api-client/tests/variable.spec.ts index 5e74d7f9..f98efa01 100644 --- a/packages/api-client/tests/variable.spec.ts +++ b/packages/api-client/tests/variable.spec.ts @@ -1,7 +1,11 @@ -import client from '@package/client' -import VariableController from '@package/controllers/variable/variable' +import VariableController from '../src/controllers/variable' +import { APIClient } from '../src/core/client' describe('Get Variable Tests', () => { + const backendUrl = process.env.BACKEND_URL + + const client = new APIClient(backendUrl) + const variableController = new VariableController(backendUrl) const email = 'johndoe@example.com' let workspaceId: string | null let projectId: string | null @@ -9,40 +13,47 @@ describe('Get Variable Tests', () => { let variableId: string | null beforeAll(async () => { - const workspaceResponse = (await client.post( - '/api/workspace', - { - name: 'My Workspace' - }, - { - 'x-e2e-user-email': email - } - )) as any + const workspaceResponse = (await ( + await client.post( + '/api/workspace', + { + name: 'My Workspace' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any workspaceId = workspaceResponse.id - const projectResponse = (await client.post( - `/api/project/${workspaceId}`, - { - name: 'Project', - storePrivateKey: true - }, - { - 'x-e2e-user-email': email - } - )) as any + // Create a project + const projectResponse = (await ( + await client.post( + `/api/project/${workspaceId}`, + { + name: 'Project', + storePrivateKey: true + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any projectId = projectResponse.id - const createEnvironmentResponse = await client.post( - `/api/environment/${projectId}`, - { - name: 'Dev' - }, - { - 'x-e2e-user-email': email - } - ) + const createEnvironmentResponse = (await ( + await client.post( + `/api/environment/${projectId}`, + { + name: 'Dev' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any environment = createEnvironmentResponse }) @@ -54,7 +65,7 @@ describe('Get Variable Tests', () => { // Create a variable it('should create a variable', async () => { - const variable = await VariableController.createVariable( + const variable = await variableController.createVariable( { projectId, name: 'Variable 1', @@ -64,18 +75,18 @@ describe('Get Variable Tests', () => { '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 + expect(variable.data.name).toBe('Variable 1') + expect(variable.data.projectId).toBe(projectId) + expect(variable.data.project.workspaceId).toBe(workspaceId) + expect(variable.data.versions.length).toBe(1) + expect(variable.data.versions[0].value).toBe('Variable 1 value') + expect(variable.data.versions[0].environmentId).toBe(environment.id) + variableId = variable.data.id }) // Update Name of the Variable it('should update the name a variable', async () => { - const updatedVariable = await VariableController.updateVariable( + const updatedVariable = await variableController.updateVariable( { name: 'UpdatedVariable 1', variableId @@ -84,13 +95,13 @@ describe('Get Variable Tests', () => { 'x-e2e-user-email': email } ) - expect(updatedVariable.variable.name).toBe('UpdatedVariable 1') - expect(updatedVariable.variable.id).toBe(variableId) + expect(updatedVariable.data.variable.name).toBe('UpdatedVariable 1') + expect(updatedVariable.data.variable.id).toBe(variableId) }) // Create a new version of Variable it('should add version to a variable', async () => { - const updateVariable = await VariableController.updateVariable( + const updateVariable = await variableController.updateVariable( { entries: [ { @@ -102,14 +113,16 @@ describe('Get Variable Tests', () => { }, { '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) + expect(updateVariable.data.updatedVersions.length).toBe(1) + expect(updateVariable.data.updatedVersions[0].value).toBe('1234') + expect(updateVariable.data.updatedVersions[0].environmentId).toBe( + environment.id + ) }) // Roll back a variable it('should rollback a variable', async () => { - const rolledBackVariable: any = await VariableController.rollbackVariable( + const rolledBackVariable: any = await variableController.rollbackVariable( { variableId, version: 1, @@ -117,17 +130,17 @@ describe('Get Variable Tests', () => { }, { 'x-e2e-user-email': email } ) - expect(rolledBackVariable.count).toBe(1) + expect(rolledBackVariable.data.count).toBe(1) }) // Get all the variables of project it('should get all variables of project', async () => { - const response: any = await VariableController.getAllVariablesOfProject( + const response: any = await variableController.getAllVariablesOfProject( { projectId }, { 'x-e2e-user-email': email } ) - expect(response.length).toBe(1) - const variable1 = response[0] + expect(response.data.items.length).toBe(1) + const variable1 = response.data.items [0] const variable = variable1.variable const values = variable1.values expect(variable).toHaveProperty('id') @@ -177,7 +190,7 @@ describe('Get Variable Tests', () => { // Get all variables for an environment it('should get all variables for an environment', async () => { const variables: any = - await VariableController.getAllVariablesOfEnvironment( + await variableController.getAllVariablesOfEnvironment( { environmentId: environment.id, projectId @@ -185,8 +198,8 @@ describe('Get Variable Tests', () => { { 'x-e2e-user-email': email } ) - expect(variables.length).toBe(1) - variables.forEach((variable) => { + expect(variables.data.length).toBe(1) + variables.data.forEach((variable) => { expect(variable).toHaveProperty('name') expect(typeof variable.name).toBe('string') @@ -196,7 +209,7 @@ describe('Get Variable Tests', () => { expect(variable).toHaveProperty('isPlaintext') expect(typeof variable.isPlaintext).toBe('boolean') }) - const variable1 = variables[0] + const variable1 = variables.data[0] expect(variable1.name).toBe('UpdatedVariable 1') expect(variable1.value).toBe('Variable 1 value') expect(variable1.isPlaintext).toBe(true) @@ -204,14 +217,14 @@ describe('Get Variable Tests', () => { // Delete a variable it('should delete variable', async () => { - await VariableController.deleteVariable( + await variableController.deleteVariable( { variableId }, { 'x-e2e-user-email': email } ) - const variables: any = await VariableController.getAllVariablesOfProject( + const variables: any = await variableController.getAllVariablesOfProject( { projectId }, { 'x-e2e-user-email': email } ) - expect(variables.length).toBe(0) + expect(variables.data.items.length).toBe(0) }) }) From 933c496cf513bc1944f060437530df53319e1098 Mon Sep 17 00:00:00 2001 From: vr-varad Date: Fri, 16 Aug 2024 09:59:23 +0530 Subject: [PATCH 11/11] Resolving Issues --- packages/api-client/tests/variable.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/api-client/tests/variable.spec.ts b/packages/api-client/tests/variable.spec.ts index f98efa01..653c817d 100644 --- a/packages/api-client/tests/variable.spec.ts +++ b/packages/api-client/tests/variable.spec.ts @@ -140,7 +140,7 @@ describe('Get Variable Tests', () => { { 'x-e2e-user-email': email } ) expect(response.data.items.length).toBe(1) - const variable1 = response.data.items [0] + const variable1 = response.data.items[0] const variable = variable1.variable const values = variable1.values expect(variable).toHaveProperty('id')