From 9e1bafb44f5a0ec0c801c6d7da0402d52432926e Mon Sep 17 00:00:00 2001 From: vr-varad Date: Sat, 3 Aug 2024 10:42:21 +0530 Subject: [PATCH] According to new convention --- .../api-client/src/controllers/event/event.ts | 16 +- .../api-client/src/types/event.types.d.ts | 11 +- packages/api-client/tests/event.spec.ts | 195 ++++++++++-------- 3 files changed, 126 insertions(+), 96 deletions(-) diff --git a/packages/api-client/src/controllers/event/event.ts b/packages/api-client/src/controllers/event/event.ts index d6f90da7..19005022 100644 --- a/packages/api-client/src/controllers/event/event.ts +++ b/packages/api-client/src/controllers/event/event.ts @@ -1,17 +1,23 @@ import { GetEventsRequest, GetEventsResponse } from '../../types/event.types' import { APIClient } from '../../core/client' +import { ClientResponse } from '../../types/index.types' +import { parseResponse } from '../../core/response-parser' -const baseUrl = '' export default class EventController { - private static apiClient = new APIClient(baseUrl) + private apiClient: APIClient - static async getEvents( + constructor(private readonly backendUrl: string) { + this.apiClient = new APIClient(this.backendUrl) + } + + async getEvents( request: GetEventsRequest, headers?: Record - ): Promise { - return this.apiClient.get( + ): Promise> { + const response = await this.apiClient.get( `/api/event/${request.workspaceId}?source=${request.source}`, headers ) + return await parseResponse(response) } } diff --git a/packages/api-client/src/types/event.types.d.ts b/packages/api-client/src/types/event.types.d.ts index 3ce09561..30eee490 100644 --- a/packages/api-client/src/types/event.types.d.ts +++ b/packages/api-client/src/types/event.types.d.ts @@ -1,3 +1,5 @@ +import { Page } from '../../../../apps/cli/src/types/index.types' + export enum EventSource { SECRET, VARIABLE, @@ -51,11 +53,11 @@ export enum EventType { export interface GetEventsRequest { workspaceId: string - source: EventSource + source: string } -export interface GetEventsResponse { - items: { +export interface GetEventsResponse + extends Page<{ id: string source: EventSource triggerer: EventTriggerer @@ -77,5 +79,4 @@ export interface GetEventsResponse { itemId: string userId: string workspaceId: string - }[] -} + }> {} diff --git a/packages/api-client/tests/event.spec.ts b/packages/api-client/tests/event.spec.ts index 29ced056..3d0b0120 100644 --- a/packages/api-client/tests/event.spec.ts +++ b/packages/api-client/tests/event.spec.ts @@ -1,8 +1,20 @@ -import client from '../src/core/client' +import { APIClient } from '../src/core/client' import EventController from '../src/controllers/event/event' -import { EventSource } from '../src/types/event.types' +export enum EventSource { + SECRET = 'SECRET', + VARIABLE = 'VARIABLE', + ENVIRONMENT = 'ENVIRONMENT', + PROJECT = 'PROJECT', + WORKSPACE = 'WORKSPACE', + WORKSPACE_ROLE = 'WORKSPACE_ROLE', + INTEGRATION = 'INTEGRATION' +} describe('Get Event Controller', () => { + const backendUrl = process.env.BACKEND_URL + + const client = new APIClient(backendUrl) + const eventController = new EventController(backendUrl) const email = 'johndoe@example.com' let projectId: string | null let workspaceId: string | null @@ -12,16 +24,18 @@ describe('Get Event Controller', () => { beforeAll(async () => { //Create the user's workspace - 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: 'Event Workspace' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + console.log(workspaceResponse) workspaceId = workspaceResponse.id }) @@ -33,101 +47,110 @@ describe('Get Event Controller', () => { }) it('should fetch a Project Event', async () => { - const project = (await client.post( - `/api/project/${workspaceId}`, - { - name: 'Project', - storePrivateKey: true - }, - { - 'x-e2e-user-email': email - } - )) as any - projectId = project.id + const projectResponse = (await ( + await client.post( + `/api/project/${workspaceId}`, + { + name: 'Project', + storePrivateKey: true + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any - const events = await EventController.getEvents( - { workspaceId, source: EventSource.PROJECT }, + projectId = projectResponse.id + const events = await eventController.getEvents( + { workspaceId, source: 'PROJECT' }, { 'x-e2e-user-email': email } ) - console.log(events) - expect(events[0].source).toBe('PROJECT') - expect(events[0].metadata.projectId).toBe(project.id) - expect(events[0].metadata.name).toBe('Project') + console.log(events.data.items) + expect(events.data.items[0].source).toBe(EventSource.PROJECT) + expect(events.data.items[0].metadata.projectId).toBe(projectId) + expect(events.data.items[0].metadata.name).toBe('Project') }) it('should fetch a Environment Event', async () => { - const environmentResponse = (await client.post( - `/api/environment/${projectId}`, - { - name: 'Dev' - }, - { - 'x-e2e-user-email': email - } - )) as any - const events = await EventController.getEvents( + const environmentResponse = (await ( + await client.post( + `/api/environment/${projectId}`, + { + name: 'Dev' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + const events = await eventController.getEvents( { workspaceId, source: EventSource.ENVIRONMENT }, { 'x-e2e-user-email': email } ) - console.log(events) - expect(events[0].source).toBe('ENVIRONMENT') - expect(events[0].metadata.environmentId).toBe(environmentResponse.id) - expect(events[0].metadata.name).toBe('Dev') + expect(events.data.items[0].source).toBe('ENVIRONMENT') + expect(events.data.items[0].metadata.environmentId).toBe( + environmentResponse.id + ) + expect(events.data.items[0].metadata.name).toBe('Dev') environment = environmentResponse }) it('should fetch a Secret Event', async () => { - secret = (await client.post( - `/api/secret/${projectId}`, - { - name: 'My secret', - entries: [ - { - value: 'My value', - environmentId: environment.id - } - ], - note: 'Some note', - rotateAfter: '720' - }, - { - 'x-e2e-user-email': email - } - )) as any - const events = await EventController.getEvents( + const secretRepsonse = (await ( + await client.post( + `/api/secret/${projectId}`, + { + name: 'My secret', + entries: [ + { + value: 'My value', + environmentId: environment.id + } + ], + note: 'Some note', + rotateAfter: '720' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + const events = await eventController.getEvents( { workspaceId, source: EventSource.SECRET }, { 'x-e2e-user-email': email } ) - console.log(events) - expect(events[0].source).toBe('SECRET') - expect(events[0].metadata.secretId).toBe(secret.id) - expect(events[0].metadata.name).toBe('My secret') + expect(events.data.items[0].source).toBe('SECRET') + expect(events.data.items[0].metadata.secretId).toBe(secretRepsonse.id) + expect(events.data.items[0].metadata.name).toBe('My secret') + secret = secretRepsonse }) it('should fetch a Variable Event', async () => { - variable = (await client.post( - `/api/variable/${projectId}`, - { - name: 'My variable', - entries: [ - { - value: 'My value', - environmentId: environment.id - } - ], - note: 'Some note' - }, - { - 'x-e2e-user-email': email - } - )) as any - const events = await EventController.getEvents( + const variableResponse = (await ( + await client.post( + `/api/variable/${projectId}`, + { + name: 'My variable', + entries: [ + { + value: 'My value', + environmentId: environment.id + } + ], + note: 'Some note' + }, + { + 'x-e2e-user-email': email + } + ) + ).json()) as any + const events = await eventController.getEvents( { workspaceId, source: EventSource.VARIABLE }, { 'x-e2e-user-email': email } ) - console.log(events) - expect(events[0].source).toBe('VARIABLE') - expect(events[0].metadata.variableId).toBe(variable.id) - expect(events[0].metadata.name).toBe('My variable') + expect(events.data.items[0].source).toBe('VARIABLE') + expect(events.data.items[0].metadata.variableId).toBe(variableResponse.id) + expect(events.data.items[0].metadata.name).toBe('My variable') + variable = variableResponse }) })