Skip to content

Commit

Permalink
According to new convention
Browse files Browse the repository at this point in the history
  • Loading branch information
vr-varad authored and rajdip-b committed Aug 3, 2024
1 parent f6c0e39 commit 9e1bafb
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 96 deletions.
16 changes: 11 additions & 5 deletions packages/api-client/src/controllers/event/event.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>
): Promise<GetEventsResponse> {
return this.apiClient.get(
): Promise<ClientResponse<GetEventsResponse>> {
const response = await this.apiClient.get(
`/api/event/${request.workspaceId}?source=${request.source}`,
headers
)
return await parseResponse<GetEventsResponse>(response)
}
}
11 changes: 6 additions & 5 deletions packages/api-client/src/types/event.types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Page } from '../../../../apps/cli/src/types/index.types'

export enum EventSource {
SECRET,
VARIABLE,
Expand Down Expand Up @@ -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
Expand All @@ -77,5 +79,4 @@ export interface GetEventsResponse {
itemId: string
userId: string
workspaceId: string
}[]
}
}> {}
195 changes: 109 additions & 86 deletions packages/api-client/tests/event.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
})

Expand All @@ -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
})
})

0 comments on commit 9e1bafb

Please sign in to comment.