Skip to content

Commit

Permalink
feat(package, api, cli): Add api-key schemas and types; Fix schema in…
Browse files Browse the repository at this point in the history
…consistencies; Minor fix for CLI build errors (#557)

Co-authored-by: rajdip-b <[email protected]>
  • Loading branch information
muntaxir4 and rajdip-b authored Nov 27, 2024
1 parent 28739d9 commit 126d024
Show file tree
Hide file tree
Showing 42 changed files with 965 additions and 263 deletions.
9 changes: 7 additions & 2 deletions api-collection/Integration Controller/Create integration.bru
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ docs {
### Request Body

- `name`: The name of the integration

- `type`: Integration type. Refer to prisma schema to get the valid types

- `metadata`: Additional key value pairs that the integration might need. This varies with the integration types.

- `notifyOn`: (Optional) Array of events that should trigger this integration. Refer to prisma schema to get the valid types.
- `environmentId`: (Optional) If the integration should be triggered when an event in a specific environment. Can only be specified when `projectId` is specified
- `projectId`: (Optional) If the integration should be triggered when an event occured in a specific project.

- `environmentSlug`: (Optional) If the integration should be triggered when an event in a specific environment. Can only be specified when `projectSlug` is specified

- `projectSlug`: (Optional) If the integration should be triggered when an event occured in a specific project.
}
4 changes: 2 additions & 2 deletions api-collection/Integration Controller/Update integration.bru
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ docs {

- `notifyOn`: (Optional) Array of events that should trigger this integration. Refer to prisma schema to get the valid types.

- `environmentId`: (Optional) If the integration should be triggered when an event in a specific environment. Can only be specified when `projectId` is specified
- `environmentSlug`: (Optional) If the integration should be triggered when an event in a specific environment. Can only be specified when `projectSlug` is specified

- `projectId`: (Optional) If the integration should be triggered when an event occured in a specific project.
- `projectSlug`: (Optional) If the integration should be triggered when an event occured in a specific project.
}
30 changes: 22 additions & 8 deletions apps/api/src/api-key/api-key.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,27 @@ describe('Api Key Role Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json()[0].id).toBe(apiKey.id)
expect(response.json()[0].name).toBe('Test Key')
expect(response.json()[0].slug).toBe(apiKey.slug)
expect(response.json()[0].authorities).toEqual([
expect(response.json().items[0].id).toBe(apiKey.id)
expect(response.json().items[0].name).toBe('Test Key')
expect(response.json().items[0].slug).toBe(apiKey.slug)
expect(response.json().items[0].authorities).toEqual([
'READ_API_KEY',
'CREATE_ENVIRONMENT'
])

const metadata = response.json().metadata
expect(metadata.totalCount).toEqual(1)
expect(metadata.links.self).toBe(
`/api-key?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.first).toBe(
`/api-key?page=0&limit=10&sort=name&order=asc&search=`
)
expect(metadata.links.previous).toEqual(null)
expect(metadata.links.next).toEqual(null)
expect(metadata.links.last).toBe(
`/api-key?page=0&limit=10&sort=name&order=asc&search=`
)
})

it('should be able to get all api keys using the API key', async () => {
Expand All @@ -304,10 +318,10 @@ describe('Api Key Role Controller Tests', () => {
})

expect(response.statusCode).toBe(200)
expect(response.json()[0].id).toBe(apiKey.id)
expect(response.json()[0].name).toBe('Test Key')
expect(response.json()[0].slug).toBe(apiKey.slug)
expect(response.json()[0].authorities).toEqual([
expect(response.json().items[0].id).toBe(apiKey.id)
expect(response.json().items[0].name).toBe('Test Key')
expect(response.json().items[0].slug).toBe(apiKey.slug)
expect(response.json().items[0].authorities).toEqual([
'READ_API_KEY',
'CREATE_ENVIRONMENT'
])
Expand Down
20 changes: 19 additions & 1 deletion apps/api/src/api-key/service/api-key.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ApiKey, User } from '@prisma/client'
import generateEntitySlug from '@/common/slug-generator'
import { generateApiKey, toSHA256 } from '@/common/cryptography'
import { addHoursToDate, limitMaxItemsPerPage } from '@/common/util'
import { paginate } from '@/common/paginate'

@Injectable()
export class ApiKeyService {
Expand Down Expand Up @@ -186,7 +187,7 @@ export class ApiKeyService {
order: string,
search: string
) {
return await this.prisma.apiKey.findMany({
const items = await this.prisma.apiKey.findMany({
where: {
userId: user.id,
name: {
Expand All @@ -200,6 +201,23 @@ export class ApiKeyService {
},
select: this.apiKeySelect
})

const totalCount = await this.prisma.apiKey.count({
where: {
userId: user.id,
name: {
contains: search
}
}
})
const metadata = paginate(totalCount, `/api-key`, {
page,
limit: limitMaxItemsPerPage(limit),
sort,
order,
search
})
return { items, metadata }
}

/**
Expand Down
8 changes: 4 additions & 4 deletions apps/cli/src/commands/environment/delete.environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ export class DeleteEnvironment extends BaseCommand {
}

async action({ args }: CommandActionData): Promise<void> {
const [environmentId] = args
const [environmentSlug] = args

if (!environmentId) {
Logger.error('Environment ID is required')
if (!environmentSlug) {
Logger.error('Environment Slug is required')
return
}

Logger.info('Deleting Environment...')

const { success, error } =
await ControllerInstance.getInstance().environmentController.deleteEnvironment(
{ id: environmentId },
{ slug: environmentSlug },
this.headers
)

Expand Down
2 changes: 1 addition & 1 deletion apps/cli/src/commands/project/create.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class CreateProject extends BaseCommand {
name: string
description?: string
storePrivateKey: boolean
accessLevel: string
accessLevel: 'PRIVATE' | 'GLOBAL' | 'INTERNAL'
}> {
let { name, description } = options
const { storePrivateKey, accessLevel } = options
Expand Down
8 changes: 4 additions & 4 deletions apps/cli/src/commands/run.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ export default class RunCommand extends BaseCommand {

const secretsResponse = await secretController.getAllSecretsOfEnvironment(
{
environmentId: this.environmentSlug,
projectId: this.projectSlug
environmentSlug: this.environmentSlug,
projectSlug: this.projectSlug
},
{
'x-keyshade-token': this.apiKey
Expand All @@ -205,8 +205,8 @@ export default class RunCommand extends BaseCommand {
const variablesResponse =
await variableController.getAllVariablesOfEnvironment(
{
environmentId: this.environmentSlug,
projectId: this.projectSlug
environmentSlug: this.environmentSlug,
projectSlug: this.projectSlug
},
{
'x-keyshade-token': this.apiKey
Expand Down
10 changes: 3 additions & 7 deletions apps/cli/src/commands/secret/rollback.secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,21 @@ export default class RollbackSecret extends BaseCommand {
)

if (success) {
Logger.info(`Secret ${data.name} (${data.slug}) updated successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
Logger.info(`Note: ${data.note}`)
Logger.info(`rotateAfter: ${data.rotateAfter}`)
Logger.info(`Secret rolled back by ${data.count} versions successfully.`)
} else {
Logger.error(`Failed to update secret: ${error.message}`)
}
}

private async parseInput(options: CommandActionData['options']): Promise<{
environmentSlug: string
version: string
version: number
}> {
const { environmentSlug, version } = options

return {
environmentSlug,
version
version: parseInt(version, 10)
}
}
}
8 changes: 2 additions & 6 deletions apps/cli/src/commands/secret/update.secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default class UpdateSecret extends BaseCommand {
async action({ args, options }: CommandActionData): Promise<void> {
const [secretSlug] = args

const { data, error, success } =
const { error, success } =
await ControllerInstance.getInstance().secretController.updateSecret(
{
secretSlug,
Expand All @@ -65,11 +65,7 @@ export default class UpdateSecret extends BaseCommand {
)

if (success) {
Logger.info(`Secret ${data.name} (${data.slug}) updated successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
Logger.info(`Note: ${data.note}`)
Logger.info(`rotateAfter: ${data.rotateAfter}`)
Logger.info('Secret updated successfully')
} else {
Logger.error(`Failed to update secret: ${error.message}`)
}
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/commands/variable/list.variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export default class ListVariable extends BaseCommand {

if (success) {
const variables = data
if (variables.length > 0) {
data.forEach((variable: any) => {
if (variables.items.length > 0) {
variables.items.forEach((variable: any) => {
Logger.info(`- ${variable.name} (${variable.value})`)
})
} else {
Expand Down
11 changes: 5 additions & 6 deletions apps/cli/src/commands/variable/rollback.variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,23 @@ export default class RollbackVariable extends BaseCommand {
)

if (success) {
Logger.info(`Variable ${data.name} (${data.slug}) updated successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
Logger.info(`Note: ${data.note}`)
Logger.info(
`Variable rolled back by ${data.count} versions successfully!`
)
} else {
Logger.error(`Failed to update variable: ${error.message}`)
}
}

private async parseInput(options: CommandActionData['options']): Promise<{
environmentSlug: string
version: string
version: number
}> {
const { environmentSlug, version } = options

return {
environmentSlug,
version
version: parseInt(version, 10)
}
}
}
7 changes: 2 additions & 5 deletions apps/cli/src/commands/variable/update.variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class UpdateVariable extends BaseCommand {
async action({ args, options }: CommandActionData): Promise<void> {
const [variableSlug] = args

const { data, error, success } =
const { error, success } =
await ControllerInstance.getInstance().variableController.updateVariable(
{
variableSlug,
Expand All @@ -58,10 +58,7 @@ export default class UpdateVariable extends BaseCommand {
)

if (success) {
Logger.info(`Variable ${data.name} (${data.slug}) updated successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
Logger.info(`Note: ${data.note}`)
Logger.info('Variable updated successfully!')
} else {
Logger.error(`Failed to update variable: ${error.message}`)
}
Expand Down
8 changes: 4 additions & 4 deletions apps/cli/src/commands/workspace/search.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,28 @@ export default class SearchWorkspace extends BaseCommand {

Logger.info(`Projects (${data.projects.length})`)
data.projects.forEach((project) => {
Logger.info(`- ${project.name} (${project.id})`)
Logger.info(`- ${project.name} (${project.slug})`)
})

Logger.info('')

Logger.info(`Environments (${data.environments.length})`)
data.environments.forEach((environment) => {
Logger.info(`- ${environment.name} (${environment.id})`)
Logger.info(`- ${environment.name} (${environment.slug})`)
})

Logger.info('')

Logger.info(`Variables (${data.variables.length})`)
data.variables.forEach((variable) => {
Logger.info(`- ${variable.name} (${variable.id})`)
Logger.info(`- ${variable.name} (${variable.slug})`)
})

Logger.info('')

Logger.info(`Secrets (${data.secrets.length})`)
data.secrets.forEach((secret) => {
Logger.info(`- ${secret.name} (${secret.id})`)
Logger.info(`- ${secret.name} (${secret.slug})`)
})

Logger.info('')
Expand Down
Loading

0 comments on commit 126d024

Please sign in to comment.