From 8d6a126b481dbf6cc5f33d8668ebb6be2d5a087e Mon Sep 17 00:00:00 2001 From: muntaxir4 Date: Wed, 20 Nov 2024 01:08:19 +0530 Subject: [PATCH 1/2] refactor(schema): Add variable schemas and types along with tests. Add invite schemas in workspace - Also add npmignore to exclude source ts files --- packages/schema/.npmignore | 8 + packages/schema/src/index.types.ts | 5 +- packages/schema/src/variable.ts | 14 - packages/schema/src/variable/index.ts | 148 +++++++ packages/schema/src/variable/index.types.ts | 70 +++ packages/schema/src/workspace/index.ts | 6 +- packages/schema/src/workspace/index.types.ts | 6 + packages/schema/tests/variable.spec.ts | 436 ++++++++++++++++++- packages/schema/tests/workspace.spec.ts | 44 +- 9 files changed, 680 insertions(+), 57 deletions(-) create mode 100644 packages/schema/.npmignore delete mode 100644 packages/schema/src/variable.ts create mode 100644 packages/schema/src/variable/index.ts create mode 100644 packages/schema/src/variable/index.types.ts diff --git a/packages/schema/.npmignore b/packages/schema/.npmignore new file mode 100644 index 00000000..42ebed03 --- /dev/null +++ b/packages/schema/.npmignore @@ -0,0 +1,8 @@ +.turbo/ +src/ +tests/ +eslintrc.js +jest.config.ts +tsconfig.json +tsconfig.spec.json +node_modules/ diff --git a/packages/schema/src/index.types.ts b/packages/schema/src/index.types.ts index ccf33cce..01c96b50 100644 --- a/packages/schema/src/index.types.ts +++ b/packages/schema/src/index.types.ts @@ -1,7 +1,6 @@ import { z } from 'zod' import { CreateApiKeySchema, UpdateApiKeySchema } from './api-key' import { CreateIntegrationSchema, UpdateIntegrationSchema } from './integration' -import { CreateVariableSchema, UpdateVariableSchema } from './variable' import { CreateWorkspaceRoleSchema, UpdateWorkspaceRoleSchema @@ -14,6 +13,7 @@ export * from './project/index.types' export * from './secret/index.types' export * from './user/index.types' export * from './workspace/index.types' +export * from './variable/index.types' export type TCreateApiKey = z.infer export type TUpdateApiKey = z.infer @@ -21,8 +21,5 @@ export type TUpdateApiKey = z.infer export type TCreateIntegration = z.infer export type TUpdateIntegration = z.infer -export type TCreateVariable = z.infer -export type TUpdateVariable = z.infer - export type TCreateWorkspaceRole = z.infer export type TUpdateWorkspaceRole = z.infer diff --git a/packages/schema/src/variable.ts b/packages/schema/src/variable.ts deleted file mode 100644 index 4d7c7830..00000000 --- a/packages/schema/src/variable.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { z } from 'zod' - -export const CreateVariableSchema = z.object({ - name: z.string(), - note: z.string().optional(), - entries: z.array( - z.object({ - environmentId: z.string(), - value: z.string() - }) - ) -}) - -export const UpdateVariableSchema = CreateVariableSchema.partial() diff --git a/packages/schema/src/variable/index.ts b/packages/schema/src/variable/index.ts new file mode 100644 index 00000000..46f71f28 --- /dev/null +++ b/packages/schema/src/variable/index.ts @@ -0,0 +1,148 @@ +import { z } from 'zod' +import { PageRequestSchema, PageResponseSchema } from '@/pagination' + +export const VariableSchema = z.object({ + id: z.string(), + name: z.string(), + slug: z.string(), + createdAt: z.string(), + updatedAt: z.string(), + note: z.string().nullable(), + lastUpdatedById: z.string(), + projectId: z.string(), + project: z.object({ + workspaceId: z.string() + }), + versions: z.array( + z.object({ + value: z.string(), + environmentId: z.string(), + environment: z.object({ + id: z.string(), + slug: z.string() + }) + }) + ) +}) + +export const CreateVariableRequestSchema = z.object({ + projectSlug: z.string(), + name: z.string(), + note: z.string().optional(), + entries: z + .array( + z.object({ + value: z.string(), + environmentSlug: z.string() + }) + ) + .optional() +}) + +export const CreateVariableResponseSchema = VariableSchema + +export const UpdateVariableRequestSchema = z.object({ + variableSlug: z.string(), + name: z.string().optional(), + note: z.string().optional(), + entries: z + .array( + z.object({ + value: z.string(), + environmentSlug: z.string() + }) + ) + .optional() +}) + +export const UpdateVariableResponseSchema = z.object({ + variable: VariableSchema.pick({ + id: true, + name: true, + slug: true, + note: true + }), + updatedVersions: z.array( + z.object({ + value: z.string(), + environmentId: z.string(), + environment: z.object({ + id: z.string(), + slug: z.string() + }) + }) + ) +}) + +export const RollBackVariableRequestSchema = z.object({ + variableSlug: z.string(), + version: z.number(), + environmentSlug: z.string() +}) + +export const RollBackVariableResponseSchema = z.object({ + count: z.string() +}) + +export const DeleteVariableRequestSchema = z.object({ + variableSlug: z.string() +}) + +export const DeleteVariableResponseSchema = z.void() + +export const GetAllVariablesOfProjectRequestSchema = PageRequestSchema.extend({ + projectSlug: z.string() +}) + +export const GetAllVariablesOfProjectResponseSchema = PageResponseSchema( + VariableSchema.omit({ project: true, versions: true }).extend({ + variable: z.object({ + lastUpdatedBy: z.object({ + id: z.string(), + name: z.string() + }) + }), + values: z.array( + z.object({ + environment: z.object({ + id: z.string(), + name: z.string(), + slug: z.string() + }), + value: z.string(), + version: z.number() + }) + ) + }) +) + +export const GetAllVariablesOfEnvironmentRequestSchema = z.object({ + projectSlug: z.string(), + environmentSlug: z.string() +}) + +export const GetAllVariablesOfEnvironmentResponseSchema = z.array( + z.object({ + name: z.string(), + value: z.string(), + isPlaintext: z.boolean() + }) +) + +export const GetRevisionsOfVariableRequestSchema = + PageRequestSchema.partial().extend({ + variableSlug: z.string(), + environmentSlug: z.string() + }) + +export const GetRevisionsOfVariableResponseSchema = PageResponseSchema( + z.object({ + id: z.string(), + value: z.string(), + version: z.number(), + variableId: z.string(), + createdOn: z.string(), + createdById: z.string(), + environmentId: z.string() + }) +) diff --git a/packages/schema/src/variable/index.types.ts b/packages/schema/src/variable/index.types.ts new file mode 100644 index 00000000..a2b71f32 --- /dev/null +++ b/packages/schema/src/variable/index.types.ts @@ -0,0 +1,70 @@ +import { z } from 'zod' +import { + VariableSchema, + CreateVariableRequestSchema, + CreateVariableResponseSchema, + UpdateVariableRequestSchema, + UpdateVariableResponseSchema, + RollBackVariableRequestSchema, + RollBackVariableResponseSchema, + DeleteVariableRequestSchema, + DeleteVariableResponseSchema, + GetAllVariablesOfProjectRequestSchema, + GetAllVariablesOfProjectResponseSchema, + GetAllVariablesOfEnvironmentRequestSchema, + GetAllVariablesOfEnvironmentResponseSchema, + GetRevisionsOfVariableRequestSchema, + GetRevisionsOfVariableResponseSchema +} from '.' + +export type Variable = z.infer + +export type CreateVariableRequest = z.infer + +export type CreateVariableResponse = z.infer< + typeof CreateVariableResponseSchema +> + +export type UpdateVariableRequest = z.infer + +export type UpdateVariableResponse = z.infer< + typeof UpdateVariableResponseSchema +> + +export type RollBackVariableRequest = z.infer< + typeof RollBackVariableRequestSchema +> + +export type RollBackVariableResponse = z.infer< + typeof RollBackVariableResponseSchema +> + +export type DeleteVariableRequest = z.infer + +export type DeleteVariableResponse = z.infer< + typeof DeleteVariableResponseSchema +> + +export type GetAllVariablesOfProjectRequest = z.infer< + typeof GetAllVariablesOfProjectRequestSchema +> + +export type GetAllVariablesOfProjectResponse = z.infer< + typeof GetAllVariablesOfProjectResponseSchema +> + +export type GetAllVariablesOfEnvironmentRequest = z.infer< + typeof GetAllVariablesOfEnvironmentRequestSchema +> + +export type GetAllVariablesOfEnvironmentResponse = z.infer< + typeof GetAllVariablesOfEnvironmentResponseSchema +> + +export type GetRevisionsOfVariableRequest = z.infer< + typeof GetRevisionsOfVariableRequestSchema +> + +export type GetRevisionsOfVariableResponse = z.infer< + typeof GetRevisionsOfVariableResponseSchema +> diff --git a/packages/schema/src/workspace/index.ts b/packages/schema/src/workspace/index.ts index 37a07c56..2bd68cf2 100644 --- a/packages/schema/src/workspace/index.ts +++ b/packages/schema/src/workspace/index.ts @@ -40,12 +40,14 @@ export const GetWorkspaceRequestSchema = z.object({ workspaceSlug: z.string() }) -export const InviteMemberSchema = z.object({ +export const GetWorkspaceResponseSchema = WorkspaceSchema + +export const InviteMemberRequestSchema = z.object({ email: z.string().email(), roleSlugs: z.array(z.string()).optional() }) -export const GetWorkspaceResponseSchema = WorkspaceSchema +export const InviteMemberResponseSchema = z.void() export const GetAllWorkspacesOfUserRequestSchema = PageRequestSchema diff --git a/packages/schema/src/workspace/index.types.ts b/packages/schema/src/workspace/index.types.ts index 3b45f584..97b025d1 100644 --- a/packages/schema/src/workspace/index.types.ts +++ b/packages/schema/src/workspace/index.types.ts @@ -9,6 +9,8 @@ import { DeleteWorkspaceResponseSchema, GetWorkspaceRequestSchema, GetWorkspaceResponseSchema, + InviteMemberRequestSchema, + InviteMemberResponseSchema, GetAllWorkspacesOfUserResponseSchema, ExportDataRequestSchema, ExportDataResponseSchema, @@ -47,6 +49,10 @@ export type GetWorkspaceRequest = z.infer export type GetWorkspaceResponse = z.infer +export type InviteMemberRequest = z.infer + +export type InviteMemberResponse = z.infer + export type GetAllWorkspacesOfUserRequest = z.infer export type GetAllWorkspacesOfUserResponse = z.infer< diff --git a/packages/schema/tests/variable.spec.ts b/packages/schema/tests/variable.spec.ts index feeb23bc..329ab129 100644 --- a/packages/schema/tests/variable.spec.ts +++ b/packages/schema/tests/variable.spec.ts @@ -1,59 +1,449 @@ -import { CreateVariableSchema } from '@/variable' +import { + VariableSchema, + CreateVariableRequestSchema, + CreateVariableResponseSchema, + UpdateVariableRequestSchema, + UpdateVariableResponseSchema, + RollBackVariableRequestSchema, + RollBackVariableResponseSchema, + DeleteVariableRequestSchema, + DeleteVariableResponseSchema, + GetAllVariablesOfProjectRequestSchema, + GetAllVariablesOfProjectResponseSchema +} from '@/variable' describe('Variable Schema Tests', () => { - it('should validate if proper input is specified for CreateVariableSchema', () => { - const result = CreateVariableSchema.safeParse({ - name: 'Variable Test', - entries: [{ environmentId: 'env123', value: 'variable-value' }] + // Tests for VariableSchema + it('should validate a valid VariableSchema', () => { + const result = VariableSchema.safeParse({ + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + createdAt: '2024-10-01T00:00:00Z', + updatedAt: '2024-10-01T00:00:00Z', + note: 'This is a note', + lastUpdatedById: 'user123', + projectId: 'project123', + project: { + workspaceId: 'workspace123' + }, + versions: [ + { + value: 'variable-value', + environmentId: 'env123', + environment: { + id: 'env123', + slug: 'development' + } + } + ] }) + expect(result.success).toBe(true) + }) + it('should validate a valid VariableSchema with no note', () => { + const result = VariableSchema.safeParse({ + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + createdAt: '2024-10-01T00:00:00Z', + updatedAt: '2024-10-01T00:00:00Z', + note: null, + lastUpdatedById: 'user123', + projectId: 'project123', + project: { + workspaceId: 'workspace123' + }, + versions: [ + { + value: 'variable-value', + environmentId: 'env123', + environment: { + id: 'env123', + slug: 'development' + } + } + ] + }) expect(result.success).toBe(true) }) - it('should validate if only required fields are specified for CreateVariableSchema', () => { - const result = CreateVariableSchema.safeParse({ + it('should not validate an invalid VariableSchema', () => { + const result = VariableSchema.safeParse({ + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + createdAt: '2024-10-01T00:00:00Z', + updatedAt: '2024-10-01T00:00:00Z', + note: 'This is a note', + lastUpdatedById: 'user123', + projectId: 'project123', + project: { + workspaceId: 'workspace123' + }, + versions: [ + { + value: 'variable-value', + environmentId: 'env123', + environment: { + id: 'env123' + // Missing slug + } + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for CreateVariableRequestSchema + it('should validate if proper input is specified for CreateVariableRequestSchema', () => { + const result = CreateVariableRequestSchema.safeParse({ + projectSlug: 'projectTest', name: 'Variable Test', - entries: [{ environmentId: 'env123', value: 'variable-value' }] + entries: [{ environmentSlug: 'env123', value: 'variable-value' }] }) expect(result.success).toBe(true) }) - it('should not validate if required fields are missing for CreateVariableSchema', () => { - const result = CreateVariableSchema.safeParse({ - entries: [{ environmentId: 'env123', value: 'variable-value' }] + it('should validate if only required fields are specified for CreateVariableRequestSchema', () => { + const result = CreateVariableRequestSchema.safeParse({ + projectSlug: 'projectTest', + name: 'Variable Test' }) - expect(result.success).toBe(false) - expect(result.error?.issues).toHaveLength(1) + expect(result.success).toBe(true) }) - it('should not validate if invalid types are specified for CreateVariableSchema', () => { - const result = CreateVariableSchema.safeParse({ - name: 123, - entries: [{ environmentId: 'env123', value: 456 }] + it('should not validate if required fields are missing for CreateVariableRequestSchema', () => { + const result = CreateVariableRequestSchema.safeParse({ + entries: [{ environmentSlug: 'env123', value: 'variable-value' }] }) expect(result.success).toBe(false) expect(result.error?.issues).toHaveLength(2) }) - it('should validate if optional fields are omitted for CreateVariableSchema', () => { - const result = CreateVariableSchema.safeParse({ + it('should not validate if invalid types are specified for CreateVariableRequestSchema', () => { + const result = CreateVariableRequestSchema.safeParse({ + projectSlug: 123, + name: 456, + entries: [{ environmentSlug: 'env123', value: 456 }] + }) + + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(3) + }) + + it('should validate if optional fields are omitted for CreateVariableRequestSchema', () => { + const result = CreateVariableRequestSchema.safeParse({ + projectSlug: 'projectTest', name: 'Variable Test', - entries: [{ environmentId: 'env123', value: 'variable-value' }] + entries: [{ environmentSlug: 'env123', value: 'variable-value' }] }) expect(result.success).toBe(true) }) - it('should validate if note field is provided for CreateVariableSchema', () => { - const result = CreateVariableSchema.safeParse({ + it('should validate if note field is provided for CreateVariableRequestSchema', () => { + const result = CreateVariableRequestSchema.safeParse({ + projectSlug: 'projectTest', name: 'Variable Test', note: 'This is a note', - entries: [{ environmentId: 'env123', value: 'variable-value' }] + entries: [{ environmentSlug: 'env123', value: 'variable-value' }] }) expect(result.success).toBe(true) }) + + // Tests for CreateVariableResponseSchema + it('should validate a valid CreateVariableResponseSchema', () => { + const result = CreateVariableResponseSchema.safeParse({ + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + createdAt: '2024-10-01T00:00:00Z', + updatedAt: '2024-10-01T00:00:00Z', + note: 'This is a note', + lastUpdatedById: 'user123', + projectId: 'project123', + project: { + workspaceId: 'workspace123' + }, + versions: [ + { + value: 'variable-value', + environmentId: 'env123', + environment: { + id: 'env123', + slug: 'development' + } + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid CreateVariableResponseSchema', () => { + const result = CreateVariableResponseSchema.safeParse({ + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + createdAt: '2024-10-01T00:00:00Z', + updatedAt: '2024-10-01T00:00:00Z', + note: 'This is a note', + lastUpdatedById: 'user123', + projectId: 'project123', + project: { + workspaceId: 'workspace123' + }, + versions: [ + { + value: 'variable-value', + environmentId: 'env123', + environment: { + id: 'env123' + // Missing slug + } + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for UpdateVariableRequestSchema + it('should validate a valid UpdateVariableRequestSchema', () => { + const result = UpdateVariableRequestSchema.safeParse({ + variableSlug: 'variable-slug', + name: 'Updated Variable Name', + note: 'Updated note', + entries: [ + { + value: 'variable-value', + environmentSlug: 'development' + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid UpdateVariableRequestSchema', () => { + const result = UpdateVariableRequestSchema.safeParse({ + variableSlug: 123, // Should be a string + name: 'Updated Variable Name', + note: 'Updated note', + entries: [ + { + value: 'variable-value', + environmentSlug: 'development' + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for UpdateVariableResponseSchema + it('should validate a valid UpdateVariableResponseSchema', () => { + const result = UpdateVariableResponseSchema.safeParse({ + variable: { + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + note: 'This is a note' + }, + updatedVersions: [ + { + value: 'variable-value', + environmentId: 'env123', + environment: { + id: 'env123', + slug: 'development' + } + } + ] + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid UpdateVariableResponseSchema', () => { + const result = UpdateVariableResponseSchema.safeParse({ + variable: { + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + note: 'This is a note' + }, + updatedVersions: [ + { + value: 'variable-value', + environmentId: 'env123', + environment: { + id: 'env123' + // Missing slug + } + } + ] + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for RollBackVariableRequestSchema + it('should validate a valid RollBackVariableRequestSchema', () => { + const result = RollBackVariableRequestSchema.safeParse({ + variableSlug: 'variable-slug', + version: 1, + environmentSlug: 'development' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid RollBackVariableRequestSchema', () => { + const result = RollBackVariableRequestSchema.safeParse({ + variableSlug: 'variable-slug', + version: 'one', // Should be a number + environmentSlug: 'development' + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for RollBackVariableResponseSchema + it('should validate a valid RollBackVariableResponseSchema', () => { + const result = RollBackVariableResponseSchema.safeParse({ + count: '1' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid RollBackVariableResponseSchema', () => { + const result = RollBackVariableResponseSchema.safeParse({ + count: 1 // Should be a string + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for DeleteVariableRequestSchema + it('should validate a valid DeleteVariableRequestSchema', () => { + const result = DeleteVariableRequestSchema.safeParse({ + variableSlug: 'variable-slug' + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid DeleteVariableRequestSchema', () => { + const result = DeleteVariableRequestSchema.safeParse({ + variableSlug: 123 // Should be a string + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for DeleteVariableResponseSchema + it('should validate a valid DeleteVariableResponseSchema', () => { + const result = DeleteVariableResponseSchema.safeParse(undefined) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid DeleteVariableResponseSchema', () => { + const result = DeleteVariableResponseSchema.safeParse({ + unexpectedField: 'value' + }) + expect(result.success).toBe(false) + }) + + // Tests for GetAllVariablesOfProjectRequestSchema + it('should validate a valid GetAllVariablesOfProjectRequestSchema', () => { + const result = GetAllVariablesOfProjectRequestSchema.safeParse({ + projectSlug: 'project-slug', + page: 1, + limit: 10 + }) + expect(result.success).toBe(true) + }) + + it('should not validate an invalid GetAllVariablesOfProjectRequestSchema', () => { + const result = GetAllVariablesOfProjectRequestSchema.safeParse({ + projectSlug: 123, // Should be a string + page: 1, + limit: 10 + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + + // Tests for GetAllVariablesOfProjectResponseSchema + it('should validate a valid GetAllVariablesOfProjectResponseSchema', () => { + const result = GetAllVariablesOfProjectResponseSchema.safeParse({ + items: [ + { + id: 'variable123', + name: 'Variable Name', + slug: 'variable-slug', + createdAt: '2024-10-01T00:00:00Z', + updatedAt: '2024-10-01T00:00:00Z', + note: 'This is a note', + lastUpdatedById: 'user123', + projectId: 'project123', + variable: { + lastUpdatedBy: { + id: 'user123', + name: 'John Doe' + } + }, + values: [ + { + environment: { + id: 'env123', + name: 'Development', + slug: 'development' + }, + value: 'variable-value', + version: 1 + } + ] + } + ], + metadata: { + page: 1, + perPage: 10, + pageCount: 1, + totalCount: 1, + links: { + self: 'http://example.com/page/1', + first: 'http://example.com/page/1', + previous: null, + next: null, + last: 'http://example.com/page/1' + } + } + }) + + expect(result.success).toBe(true) + }) + + it('should not validate an invalid GetAllVariablesOfProjectResponseSchema', () => { + const result = GetAllVariablesOfProjectResponseSchema.safeParse({ + items: 'not-an-array', // Should be an array + metadata: { + page: 1, + perPage: 10, + pageCount: 1, + totalCount: 1, + links: { + self: 'http://example.com/page/1', + first: 'http://example.com/page/1', + previous: null, + next: null, + last: 'http://example.com/page/1' + } + } + }) + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) }) diff --git a/packages/schema/tests/workspace.spec.ts b/packages/schema/tests/workspace.spec.ts index 6c2216f8..f7cdb317 100644 --- a/packages/schema/tests/workspace.spec.ts +++ b/packages/schema/tests/workspace.spec.ts @@ -1,15 +1,16 @@ import { - InviteMemberSchema, CreateWorkspaceRequestSchema, UpdateWorkspaceRequestSchema, DeleteWorkspaceRequestSchema, - GetWorkspaceRequestSchema, ExportDataRequestSchema, GlobalSearchRequestSchema, CreateWorkspaceResponseSchema, UpdateWorkspaceResponseSchema, DeleteWorkspaceResponseSchema, + GetWorkspaceRequestSchema, GetWorkspaceResponseSchema, + InviteMemberRequestSchema, + InviteMemberResponseSchema, ExportDataResponseSchema, GetAllWorkspacesOfUserResponseSchema, GlobalSearchResponseSchema, @@ -17,8 +18,8 @@ import { } from '@/workspace' describe('Workspace Schema Tests', () => { - it('should validate if proper input is specified for InviteMemberSchema', () => { - const result = InviteMemberSchema.safeParse({ + it('should validate if proper input is specified for InviteMemberRequestSchema', () => { + const result = InviteMemberRequestSchema.safeParse({ email: 'test@example.com', roleSlugs: ['role1', 'role2'] }) @@ -26,16 +27,16 @@ describe('Workspace Schema Tests', () => { expect(result.success).toBe(true) }) - it('should validate if only required fields are specified for InviteMemberSchema', () => { - const result = InviteMemberSchema.safeParse({ + it('should validate if only required fields are specified for InviteMemberRequestSchema', () => { + const result = InviteMemberRequestSchema.safeParse({ email: 'test@example.com' }) expect(result.success).toBe(true) }) - it('should not validate if required fields are missing for InviteMemberSchema', () => { - const result = InviteMemberSchema.safeParse({ + it('should not validate if required fields are missing for InviteMemberRequestSchema', () => { + const result = InviteMemberRequestSchema.safeParse({ roleSlugs: ['role1'] }) @@ -43,8 +44,8 @@ describe('Workspace Schema Tests', () => { expect(result.error?.issues).toHaveLength(1) }) - it('should not validate if invalid email string is specified for InviteMemberSchema', () => { - const result = InviteMemberSchema.safeParse({ + it('should not validate if invalid email string is specified for InviteMemberRequestSchema', () => { + const result = InviteMemberRequestSchema.safeParse({ email: 'invalid-email' }) @@ -52,8 +53,8 @@ describe('Workspace Schema Tests', () => { expect(result.error?.issues).toHaveLength(1) }) - it('should not validate if invalid types are specified for InviteMemberSchema', () => { - const result = InviteMemberSchema.safeParse({ + it('should not validate if invalid types are specified for InviteMemberRequestSchema', () => { + const result = InviteMemberRequestSchema.safeParse({ email: 123, roleSlugs: 'invalid_role' }) @@ -62,8 +63,8 @@ describe('Workspace Schema Tests', () => { expect(result.error?.issues).toHaveLength(2) }) - it('should not validate if roleIds are specified instead of roleSlugs for InviteMemberSchema', () => { - const result = InviteMemberSchema.safeParse({ + it('should not validate if roleIds are specified instead of roleSlugs for InviteMemberRequestSchema', () => { + const result = InviteMemberRequestSchema.safeParse({ roleIds: ['role1', 'role2'] //should be roleSlugs }) @@ -71,6 +72,21 @@ describe('Workspace Schema Tests', () => { expect(result.error?.issues).toHaveLength(1) }) + it('should validate an empty response for InviteMemberResponseSchema', () => { + const result = InviteMemberResponseSchema.safeParse(undefined) + + expect(result.success).toBe(true) + }) + + it('should not validate if unexpected fields are provided for InviteMemberResponseSchema', () => { + const result = InviteMemberResponseSchema.safeParse({ + unexpectedField: 'value' + }) + + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + it('should validate if proper input is specified for CreateWorkspaceRequestSchema', () => { const result = CreateWorkspaceRequestSchema.safeParse({ name: 'Workspace Test', From 260622e612462f7133ba1acc0be6422ae1e5efdc Mon Sep 17 00:00:00 2001 From: muntaxir4 Date: Wed, 20 Nov 2024 01:10:40 +0530 Subject: [PATCH 2/2] refactor(api-client): Migrate variable types to @keyshade/schema --- .../api-client/src/controllers/variable.ts | 2 +- .../api-client/src/types/variable.types.d.ts | 131 ------------------ 2 files changed, 1 insertion(+), 132 deletions(-) delete mode 100644 packages/api-client/src/types/variable.types.d.ts diff --git a/packages/api-client/src/controllers/variable.ts b/packages/api-client/src/controllers/variable.ts index 8981d581..42f1968a 100644 --- a/packages/api-client/src/controllers/variable.ts +++ b/packages/api-client/src/controllers/variable.ts @@ -17,7 +17,7 @@ import { RollBackVariableResponse, UpdateVariableRequest, UpdateVariableResponse -} from '@api-client/types/variable.types' +} from '@keyshade/schema' export default class VariableController { private apiClient: APIClient diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts deleted file mode 100644 index a9d1c2cd..00000000 --- a/packages/api-client/src/types/variable.types.d.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { PageRequest, PageResponse } from '@keyshade/schema' - -export interface Variable { - id: string - name: string - slug: string - createdAt: string - updatedAt: string - note: string | null - lastUpdatedById: string - projectId: string - project: { - workspaceId: string - } - versions: [ - { - value: string - environmentId: string - environment: { - id: string - slug: string - } - } - ] -} - -export interface CreateVariableRequest { - projectSlug: string - name: string - note?: string - entries?: [ - { - value: string - environmentSlug: string - } - ] -} - -export interface CreateVariableResponse extends Variable {} - -export interface UpdateVariableRequest { - variableSlug: string - name?: string - entries?: [ - { - value: string - environmentSlug: string - } - ] -} -export interface UpdateVariableResponse { - variable: Pick - updatedVersions: [ - { - value: string - environmentId: string - environment: { - id: string - slug: string - } - } - ] -} - -export interface RollBackVariableRequest { - variableSlug: string - version: number - environmentSlug: string -} - -export interface RollBackVariableResponse { - count: string -} - -export interface DeleteVariableRequest { - variableSlug: string -} - -export interface DeleteVariableResponse {} - -export interface GetAllVariablesOfProjectRequest extends PageRequest { - projectSlug: string -} - -export interface GetAllVariablesOfProjectResponse - extends PageResponse< - Omit & { - variable: { - lastUpdatedBy: { - id: string - name: string - } - } - values: { - environment: { - id: string - name: string - slug: string - } - value: string - version: number - }[] - } - > {} - -export interface GetAllVariablesOfEnvironmentRequest { - projectSlug: string - environmentSlug: string -} - -export type GetAllVariablesOfEnvironmentResponse = { - name: string - value: string - isPlaintext: boolean -}[] - -export interface GetRevisionsOfVariableRequest extends Partial { - variableSlug: string - environmentSlug: string -} - -export interface GetRevisionsOfVariableResponse - extends PageResponse<{ - id: string - value: string - version: number - variableId: string - createdOn: string - createdById: string - environmentId: string - }> {}