Skip to content

Commit

Permalink
chore(cli): Add Zod validation to parseInput function (keyshade-xyz#362)
Browse files Browse the repository at this point in the history
Co-authored-by: rajdip-b <agentR47@gmail.com>
  • Loading branch information
2 people authored and Kiranchaudhary537 committed Oct 13, 2024
1 parent f38b6eb commit 70b8fab
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 97 deletions.
81 changes: 49 additions & 32 deletions apps/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
"start": "node dist/main",
"dev": "cross-env NODE_ENV=dev nest start --watch",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"db:generate-types": "npx prisma generate --schema=src/prisma/schema.prisma",
"db:generate-migrations": "pnpx dotenv-cli -e ../../.env -- npx prisma migrate dev --create-only --skip-seed --schema=src/prisma/schema.prisma",
"db:deploy-migrations": "pnpx dotenv-cli -e ../../.env -- npx prisma migrate deploy --schema=src/prisma/schema.prisma",
"db:validate": "pnpx dotenv-cli -e ../../.env -- npx prisma validate --schema=src/prisma/schema.prisma",
"db:format": "npx prisma format --schema=src/prisma/schema.prisma",
"db:reset": "pnpx dotenv-cli -e ../../.env -- npx prisma migrate reset --force --schema=src/prisma/schema.prisma",
"db:generate-types": "pnpm dlx prisma generate --schema=src/prisma/schema.prisma",
"db:generate-migrations": "pnpx dotenv-cli -e ../../.env -- pnpm dlx prisma migrate dev --create-only --skip-seed --schema=src/prisma/schema.prisma",
"db:deploy-migrations": "pnpx dotenv-cli -e ../../.env -- pnpm dlx prisma migrate deploy --schema=src/prisma/schema.prisma",
"db:validate": "pnpx dotenv-cli -e ../../.env -- pnpm dlx prisma validate --schema=src/prisma/schema.prisma",
"db:format": "pnpm dlx prisma format --schema=src/prisma/schema.prisma",
"db:reset": "pnpx dotenv-cli -e ../../.env -- pnpm dlx prisma migrate reset --force --schema=src/prisma/schema.prisma",
"sourcemaps": "sentry-cli sourcemaps inject ./dist && sentry-cli sourcemaps upload ./dist || echo 'Failed to upload source maps to Sentry'",
"e2e:prepare": "cd ../../ && docker compose down && docker compose -f docker-compose-test.yml up -d && sleep 3 && cd apps/api && pnpm db:generate-types && cross-env NODE_ENV='e2e' DATABASE_URL='postgresql://prisma:prisma@localhost:5432/tests' pnpm run db:deploy-migrations",
"e2e": "pnpm run e2e:prepare && cross-env NODE_ENV='e2e' DATABASE_URL='postgresql://prisma:prisma@localhost:5432/tests' jest --runInBand --config=jest.e2e-config.ts --coverage --coverageDirectory=../../coverage-e2e/api --coverageReporters=json && pnpm run e2e:teardown",
Expand Down Expand Up @@ -54,7 +54,7 @@
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@prisma/client": "^5.18.0",
"@prisma/client": "^5.19.1",
"@types/cookie-parser": "^1.4.7",
"@types/eccrypto": "^1.1.6",
"@types/express": "^4.17.17",
Expand All @@ -66,7 +66,7 @@
"jest": "^29.5.0",
"jest-mock-extended": "^3.0.5",
"prettier": "^3.0.0",
"prisma": "5.18.0",
"prisma": "5.19.1",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/common/logger.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, LoggerService } from '@nestjs/common'
import * as chalk from 'chalk'
import * as moment from 'moment'
import chalk from 'chalk'
import moment from 'moment'

@Injectable()
export class CustomLoggerService implements LoggerService {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as Sentry from '@sentry/node'
import { ProfilingIntegration } from '@sentry/profiling-node'
import { RedisIoAdapter } from './socket/redis.adapter'
import { CustomLoggerService } from './common/logger.service'
import * as cookieParser from 'cookie-parser'
import cookieParser from 'cookie-parser'

export const sentryEnv = process.env.SENTRY_ENV || 'production'

Expand Down
2 changes: 1 addition & 1 deletion apps/api/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/out-tsc",
"module": "commonjs",
"module": "ES2022",
"types": ["node"],
"emitDecoratorMetadata": true,
"target": "es2021"
Expand Down
7 changes: 4 additions & 3 deletions apps/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "NodeNext",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"target": "ES2022",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
Expand All @@ -16,6 +16,7 @@
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,
"moduleResolution": "NodeNext"
}
}
2 changes: 1 addition & 1 deletion apps/api/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/out-tsc",
"module": "commonjs",
"module": "ES2022",
"types": ["jest", "node"]
},
"include": [
Expand Down
1 change: 0 additions & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "CLI for keyshade",
"main": "index.ts",
"private": false,
"type": "module",
"scripts": {
"build": "tsc && tsc-alias",
"start": "node dist/src/index.js",
Expand Down
34 changes: 27 additions & 7 deletions apps/cli/src/commands/profile/create.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { ProfileConfig } from '@/types/index.types'
import { fetchProfileConfig, writeProfileConfig } from '@/util/configuration'
import { API_BASE_URL } from '@/util/constants'
import { intro, outro, confirm, spinner, text } from '@clack/prompts'
import { z } from 'zod'

export default class CreateProfile extends BaseCommand {
private profiles: ProfileConfig
Expand Down Expand Up @@ -65,13 +66,12 @@ export default class CreateProfile extends BaseCommand {
}

private async parseInput(options: CommandActionData['options']): Promise<{
name: string
apiKey: string
baseUrl: string
setDefault: boolean
name?: string
apiKey?: string
baseUrl?: string
setDefault?: boolean
}> {
let { name, apiKey } = options
const { baseUrl, setDefault } = options
let { name, apiKey, baseUrl, setDefault } = options

if (!name) {
name = await text({
Expand All @@ -87,7 +87,27 @@ export default class CreateProfile extends BaseCommand {
})
}

return { name, apiKey, baseUrl, setDefault }
const inputSchema = z.object({
name: z
.string()
.regex(
/^[a-zA-Z0-9]+$/,
'Name must contain only letters and numbers without spaces.'
),
apiKey: z
.string()
.regex(
/^ks_[a-zA-Z0-9]+$/,
'API key must start with "ks_" and contain only letters and numbers.'
),
baseUrl: z.string().url().or(z.string().length(0)).optional(),
setDefault: z.boolean().optional()
})

// Validate the collected data
const parsedData = inputSchema.parse({ name, apiKey, baseUrl, setDefault })

return parsedData
}

private async checkOverwriteExistingProfile(name: string): Promise<void> {
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "2.3.0",
"license": "MPL-2.0",
"private": true,
"type": "module",
"engineStrict": false,
"packageManager": "pnpm@9.2.0",
"release": {
Expand Down Expand Up @@ -110,8 +109,8 @@
"build:api": "pnpm db:generate-types && turbo run build --filter=api",
"build:web": "turbo run build --filter=web",
"build:platform": "turbo run build --filter=platform",
"build:cli": "turbo run build --filter=cli",
"build:api-client": "pnpm run --filter=api-client build",
"build:cli": "pnpm build:api-client && turbo run build --filter=cli",
"build:api-client": "turbo run --filter=api-client build",
"start": "turbo run start",
"start:api": "turbo run start --filter=api",
"start:web": "turbo run start --filter=web",
Expand Down
1 change: 0 additions & 1 deletion packages/api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"main": "dist/src/index.js",
"description": "This package holds all the API request logic",
"private": true,
"type": "module",
"scripts": {
"build": "tsc && tsc-alias",
"watch": "tsc -w",
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/tests/config/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ function startAPI(): Promise<void> {
setTimeout(() => {
console.log('API launched')
resolve()
}, 10000)
}, 15000)
})
}
2 changes: 1 addition & 1 deletion packages/api-client/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist/out-tsc",
"module": "commonjs",
"module": "ES2022",
"types": ["jest", "node"]
},
"include": [
Expand Down
Loading

0 comments on commit 70b8fab

Please sign in to comment.