diff --git a/packages/api-client/src/controllers/auth.ts b/packages/api-client/src/controllers/auth.ts index fb49c175..6ceb041d 100644 --- a/packages/api-client/src/controllers/auth.ts +++ b/packages/api-client/src/controllers/auth.ts @@ -1,10 +1,7 @@ -import { - ResendOTPResponse, - ResendOTPRequest -} from '@api-client/types/auth.types' +import { ResendOTPResponse, ResendOTPRequest } from '@keyshade/schema' import { APIClient } from '@api-client/core/client' import { parseResponse } from '@api-client/core/response-parser' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' export default class AuthController { private apiClient: APIClient diff --git a/packages/api-client/src/controllers/environment.ts b/packages/api-client/src/controllers/environment.ts index 15fb11e9..67f21b0e 100644 --- a/packages/api-client/src/controllers/environment.ts +++ b/packages/api-client/src/controllers/environment.ts @@ -13,7 +13,7 @@ import { UpdateEnvironmentRequest, UpdateEnvironmentResponse } from '@api-client/types/environment.types' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' export default class EnvironmentController { private apiClient: APIClient diff --git a/packages/api-client/src/controllers/event.ts b/packages/api-client/src/controllers/event.ts index f4b1c092..77450752 100644 --- a/packages/api-client/src/controllers/event.ts +++ b/packages/api-client/src/controllers/event.ts @@ -3,7 +3,7 @@ import { GetEventsResponse } from '@api-client/types/event.types' import { APIClient } from '../core/client' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' import { parseResponse } from '@api-client/core/response-parser' export default class EventController { diff --git a/packages/api-client/src/controllers/integration.ts b/packages/api-client/src/controllers/integration.ts index 1f903914..aad60816 100644 --- a/packages/api-client/src/controllers/integration.ts +++ b/packages/api-client/src/controllers/integration.ts @@ -11,7 +11,7 @@ import { UpdateIntegrationResponse } from '@api-client/types/integration.types' import { APIClient } from '@api-client/core/client' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' import { parseResponse } from '@api-client/core/response-parser' import { parsePaginationUrl } from '@api-client/core/pagination-parser' diff --git a/packages/api-client/src/controllers/project.ts b/packages/api-client/src/controllers/project.ts index b65fa69d..7c2bfae9 100644 --- a/packages/api-client/src/controllers/project.ts +++ b/packages/api-client/src/controllers/project.ts @@ -1,4 +1,4 @@ -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' import { APIClient } from '@api-client/core/client' import { CreateProjectRequest, diff --git a/packages/api-client/src/controllers/secret.ts b/packages/api-client/src/controllers/secret.ts index 1560bd06..25b6c20f 100644 --- a/packages/api-client/src/controllers/secret.ts +++ b/packages/api-client/src/controllers/secret.ts @@ -1,5 +1,5 @@ import { APIClient } from '@api-client/core/client' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' import { parseResponse } from '@api-client/core/response-parser' import { CreateSecretRequest, diff --git a/packages/api-client/src/controllers/user.ts b/packages/api-client/src/controllers/user.ts index 472da504..85dab4df 100644 --- a/packages/api-client/src/controllers/user.ts +++ b/packages/api-client/src/controllers/user.ts @@ -1,6 +1,6 @@ import { APIClient } from '@api-client/core/client' import { parseResponse } from '@api-client/core/response-parser' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' import { GetSelfResponse, UpdateSelfRequest, diff --git a/packages/api-client/src/controllers/variable.ts b/packages/api-client/src/controllers/variable.ts index 25256b10..8981d581 100644 --- a/packages/api-client/src/controllers/variable.ts +++ b/packages/api-client/src/controllers/variable.ts @@ -1,7 +1,7 @@ import { APIClient } from '@api-client/core/client' import { parsePaginationUrl } from '@api-client/core/pagination-parser' import { parseResponse } from '@api-client/core/response-parser' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' import { CreateVariableRequest, CreateVariableResponse, diff --git a/packages/api-client/src/controllers/workspace-membership.ts b/packages/api-client/src/controllers/workspace-membership.ts index c6324fb4..d49f8475 100644 --- a/packages/api-client/src/controllers/workspace-membership.ts +++ b/packages/api-client/src/controllers/workspace-membership.ts @@ -23,7 +23,7 @@ import { GetMembersRequest, GetMembersResponse } from '@api-client/types/workspace-membership.types' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' export default class WorkspaceMembershipController { private apiClient: APIClient diff --git a/packages/api-client/src/controllers/workspace-role.ts b/packages/api-client/src/controllers/workspace-role.ts index 4d7a30cd..b777162f 100644 --- a/packages/api-client/src/controllers/workspace-role.ts +++ b/packages/api-client/src/controllers/workspace-role.ts @@ -1,7 +1,7 @@ import { APIClient } from '@api-client/core/client' import { parsePaginationUrl } from '@api-client/core/pagination-parser' import { parseResponse } from '@api-client/core/response-parser' -import { ClientResponse } from '@api-client/types/index.types' +import { ClientResponse } from '@keyshade/schema' import { CreateWorkspaceRoleRequest, CreateWorkspaceRoleResponse, diff --git a/packages/api-client/src/core/pagination-parser.ts b/packages/api-client/src/core/pagination-parser.ts index b25a1dd8..fd0f609f 100644 --- a/packages/api-client/src/core/pagination-parser.ts +++ b/packages/api-client/src/core/pagination-parser.ts @@ -1,4 +1,4 @@ -import { PageRequest } from '@api-client/types/index.types' +import { PageRequest } from '@keyshade/schema' /** * Constructs a URL by appending the given page request diff --git a/packages/api-client/src/core/response-parser.ts b/packages/api-client/src/core/response-parser.ts index dcd65e63..0d616d7d 100644 --- a/packages/api-client/src/core/response-parser.ts +++ b/packages/api-client/src/core/response-parser.ts @@ -1,4 +1,4 @@ -import { ClientResponse, ResponseError } from '@api-client/types/index.types' +import { ClientResponse, ResponseError } from '@keyshade/schema' /** * Takes a Response object and parses its contents into a ClientResponse object. diff --git a/packages/api-client/src/types/auth.types.ts b/packages/api-client/src/types/auth.types.ts deleted file mode 100644 index c33c37fc..00000000 --- a/packages/api-client/src/types/auth.types.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface ResendOTPRequest { - userEmail: string -} -export interface ResendOTPResponse {} diff --git a/packages/api-client/src/types/environment.types.d.ts b/packages/api-client/src/types/environment.types.d.ts index c8d3f1f5..fab55d8f 100644 --- a/packages/api-client/src/types/environment.types.d.ts +++ b/packages/api-client/src/types/environment.types.d.ts @@ -1,4 +1,4 @@ -import { PageRequest, PageResponse } from './index.types' +import { PageRequest, PageResponse } from '@keyshade/schema' export interface Environment { id: string diff --git a/packages/api-client/src/types/event.types.d.ts b/packages/api-client/src/types/event.types.d.ts index af8f61c6..b267592e 100644 --- a/packages/api-client/src/types/event.types.d.ts +++ b/packages/api-client/src/types/event.types.d.ts @@ -1,4 +1,4 @@ -import { PageResponse } from './index.types' +import { PageResponse } from '@keyshade/schema' export enum EventSource { SECRET, diff --git a/packages/api-client/src/types/index.types.ts b/packages/api-client/src/types/index.types.ts deleted file mode 100644 index 854252ab..00000000 --- a/packages/api-client/src/types/index.types.ts +++ /dev/null @@ -1,36 +0,0 @@ -export interface PageResponse { - items: T[] - metadata: { - page: number - perPage: number - pageCount: number - totalCount: number - links: { - self: string - first: string - previous: string | null - next: string | null - last: string - } - } -} - -export interface PageRequest { - page?: number - limit?: number - sort?: string - order?: string - search?: string -} - -export interface ResponseError { - message: string - error: string - statusCode: number -} - -export interface ClientResponse { - success: boolean - error: ResponseError | null - data: T | null -} diff --git a/packages/api-client/src/types/integration.types.d.ts b/packages/api-client/src/types/integration.types.d.ts index fed5ac03..3628c47a 100644 --- a/packages/api-client/src/types/integration.types.d.ts +++ b/packages/api-client/src/types/integration.types.d.ts @@ -1,4 +1,4 @@ -import { PageRequest, PageResponse } from './index.types' +import { PageRequest, PageResponse } from '@keyshade/schema' export enum IntegrationType { DISCORD, diff --git a/packages/api-client/src/types/project.types.d.ts b/packages/api-client/src/types/project.types.d.ts index 5d06edf0..0cb2db81 100644 --- a/packages/api-client/src/types/project.types.d.ts +++ b/packages/api-client/src/types/project.types.d.ts @@ -1,4 +1,4 @@ -import { PageRequest, PageResponse } from './index.types' +import { PageRequest, PageResponse } from '@keyshade/schema' export interface Project { id: string diff --git a/packages/api-client/src/types/secret.types.d.ts b/packages/api-client/src/types/secret.types.d.ts index 397d8b8f..9383c940 100644 --- a/packages/api-client/src/types/secret.types.d.ts +++ b/packages/api-client/src/types/secret.types.d.ts @@ -1,4 +1,4 @@ -import { PageRequest, PageResponse } from './index.types' +import { PageRequest, PageResponse } from '@keyshade/schema' export interface Secret { id: string diff --git a/packages/api-client/src/types/user.types.d.ts b/packages/api-client/src/types/user.types.d.ts index 2252665f..dde1907c 100644 --- a/packages/api-client/src/types/user.types.d.ts +++ b/packages/api-client/src/types/user.types.d.ts @@ -1,4 +1,4 @@ -import { Workspace } from '@api-client/types/workspace.types' +import { Workspace } from '@keyshade/schema' export interface GetSelfResponse { id: string diff --git a/packages/api-client/src/types/variable.types.d.ts b/packages/api-client/src/types/variable.types.d.ts index 1a34d3c9..a9d1c2cd 100644 --- a/packages/api-client/src/types/variable.types.d.ts +++ b/packages/api-client/src/types/variable.types.d.ts @@ -1,4 +1,4 @@ -import { PageRequest, PageResponse } from './index.types' +import { PageRequest, PageResponse } from '@keyshade/schema' export interface Variable { id: string diff --git a/packages/api-client/src/types/workspace-role.types.d.ts b/packages/api-client/src/types/workspace-role.types.d.ts index c3ddf7e1..4ffe65bc 100644 --- a/packages/api-client/src/types/workspace-role.types.d.ts +++ b/packages/api-client/src/types/workspace-role.types.d.ts @@ -1,4 +1,4 @@ -import { PageRequest, PageResponse } from './index.types' +import { PageRequest, PageResponse } from '@keyshade/schema' export interface WorkspaceRole { id: string diff --git a/packages/api-client/tsconfig.json b/packages/api-client/tsconfig.json index 0ea2662a..c0a2690c 100644 --- a/packages/api-client/tsconfig.json +++ b/packages/api-client/tsconfig.json @@ -7,17 +7,13 @@ "@api-client/*": ["./src/*"] }, "outDir": "dist", - "plugins": [{ "transform": "typescript-transform-paths" }] + "plugins": [{ "transform": "typescript-transform-paths" }], + "moduleResolution": "Bundler" }, "tsc-alias": { "resolveFullPaths": true, "verbose": false }, - "include": [ - "src/**/*.ts", - "tests/**/*.ts", - "jest.config.ts", - ".eslintrc.cjs" - ], + "include": ["src/**/*.ts", "tests/**/*.ts", "jest.config.ts"], "exclude": ["node_modules", "dist"] } diff --git a/packages/schema/package.json b/packages/schema/package.json index 6118fc31..45b1f14a 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -6,6 +6,10 @@ "private": true, "exports": { ".": { + "types": "./dist/src/index.types.d.ts", + "default": "./dist/src/index.types.js" + }, + "./schemas": { "types": "./dist/src/index.d.ts", "default": "./dist/src/index.js" } diff --git a/packages/schema/src/auth/auth.ts b/packages/schema/src/auth/auth.ts new file mode 100644 index 00000000..f9bee454 --- /dev/null +++ b/packages/schema/src/auth/auth.ts @@ -0,0 +1,7 @@ +import { z } from 'zod' + +export const ResendOTPRequestSchema = z.object({ + userEmail: z.string().email() +}) + +export const ResendOTPResponseSchema = z.void() diff --git a/packages/schema/src/auth/auth.types.ts b/packages/schema/src/auth/auth.types.ts new file mode 100644 index 00000000..99135bcb --- /dev/null +++ b/packages/schema/src/auth/auth.types.ts @@ -0,0 +1,6 @@ +import { z } from 'zod' +import { ResendOTPRequestSchema, ResendOTPResponseSchema } from './auth' + +export type ResendOTPRequest = z.infer + +export type ResendOTPResponse = z.infer diff --git a/packages/schema/src/index.ts b/packages/schema/src/index.ts index 55dd6912..b7a78ad8 100644 --- a/packages/schema/src/index.ts +++ b/packages/schema/src/index.ts @@ -1,9 +1,11 @@ //Export all Schemas and types export * from './pagination/pagination' -export * from './pagination/pagination.types' export * from './api-key' + +export * from './auth/auth' + export * from './environment' export * from './integration' export * from './project' @@ -11,7 +13,6 @@ export * from './secret' export * from './variable' export * from './workspace/workspace' -export * from './workspace/workspace.types' export * from './workspace-role' diff --git a/packages/schema/src/index.types.d.ts b/packages/schema/src/index.types.ts similarity index 84% rename from packages/schema/src/index.types.d.ts rename to packages/schema/src/index.types.ts index 248d52f6..f9d3511c 100644 --- a/packages/schema/src/index.types.d.ts +++ b/packages/schema/src/index.types.ts @@ -9,19 +9,20 @@ import { } from './project' import { CreateSecretSchema, UpdateSecretSchema } from './secret' import { CreateVariableSchema, UpdateVariableSchema } from './variable' -import { - CreateWorkspaceSchema, - InviteMemberSchema, - UpdateWorkspaceSchema -} from './workspace' import { CreateWorkspaceRoleSchema, UpdateWorkspaceRoleSchema } from './workspace-role' +//Export types from pagination.types.ts +export * from './pagination/pagination.types' + export type TCreateApiKey = z.infer export type TUpdateApiKey = z.infer +// Export types from auth.types.ts +export * from './auth/auth.types' + export type TCreateEnvironment = z.infer export type TUpdateEnvironment = z.infer @@ -38,9 +39,8 @@ export type TUpdateSecret = z.infer export type TCreateVariable = z.infer export type TUpdateVariable = z.infer -export type TCreateWorkspace = z.infer -export type TUpdateWorkspace = z.infer -export type TInviteMember = z.infer +// Export types from workspace.types.ts +export * from './workspace/workspace.types' export type TCreateWorkspaceRole = z.infer export type TUpdateWorkspaceRole = z.infer diff --git a/packages/schema/src/workspace/workspace.ts b/packages/schema/src/workspace/workspace.ts index d5df2cf6..ff4f97e0 100644 --- a/packages/schema/src/workspace/workspace.ts +++ b/packages/schema/src/workspace/workspace.ts @@ -3,7 +3,7 @@ import { PageRequestSchema, PageResponseSchema } from '@/pagination/pagination' import { projectAccessLevelEnum, rotateAfterEnum } from '@/enums' export const InviteMemberSchema = z.object({ - email: z.string(), + email: z.string().email(), roleSlugs: z.array(z.string()).optional() }) @@ -40,7 +40,7 @@ export const DeleteWorkspaceRequestSchema = z.object({ workspaceSlug: z.string() }) -export const DeleteWorkspaceResponseSchema = z.never() +export const DeleteWorkspaceResponseSchema = z.void() export const GetWorkspaceRequestSchema = z.object({ workspaceSlug: z.string() @@ -120,6 +120,7 @@ export const GlobalSearchRequestSchema = z.object({ export const GlobalSearchResponseSchema = z.object({ projects: z.array( z.object({ + id: z.string(), slug: z.string(), name: z.string(), description: z.string() @@ -127,6 +128,7 @@ export const GlobalSearchResponseSchema = z.object({ ), environments: z.array( z.object({ + id: z.string(), slug: z.string(), name: z.string(), description: z.string() @@ -134,6 +136,7 @@ export const GlobalSearchResponseSchema = z.object({ ), secrets: z.array( z.object({ + id: z.string(), slug: z.string(), name: z.string(), note: z.string() @@ -141,6 +144,7 @@ export const GlobalSearchResponseSchema = z.object({ ), variables: z.array( z.object({ + id: z.string(), slug: z.string(), name: z.string(), note: z.string() diff --git a/packages/schema/tests/auth.spec.ts b/packages/schema/tests/auth.spec.ts new file mode 100644 index 00000000..8fa020ab --- /dev/null +++ b/packages/schema/tests/auth.spec.ts @@ -0,0 +1,55 @@ +import { + ResendOTPRequestSchema, + ResendOTPResponseSchema +} from '../src/auth/auth' + +describe('Auth Schema Tests', () => { + // Tests for ResendOTPRequestSchema + it('should validate a valid ResendOTPRequestSchema', () => { + const result = ResendOTPRequestSchema.safeParse({ + userEmail: 'test@example.com' + }) + + expect(result.success).toBe(true) + }) + + it('should not validate an invalid ResendOTPRequestSchema with a non email string', () => { + const result = ResendOTPRequestSchema.safeParse({ + userEmail: 'invalid-email' + }) + + expect(result.success).toBe(false) + expect(result.error?.issues[0]?.path).toEqual(['userEmail']) + }) + + it('should not validate an invalid ResendOTPRequestSchema with userEmail of different type', () => { + const result = ResendOTPRequestSchema.safeParse({ + userEmail: 456 + }) + + expect(result.success).toBe(false) + expect(result.error?.issues[0]?.path).toEqual(['userEmail']) + }) + + it('should not validate an invalid ResendOTPRequestSchema with missing userEmail', () => { + const result = ResendOTPRequestSchema.safeParse({}) + + expect(result.success).toBe(false) + expect(result.error?.issues[0]?.path).toEqual(['userEmail']) + }) + + // Tests for ResendOTPResponseSchema + it('should validate an empty response for ResendOTPResponseSchema', () => { + const result = ResendOTPResponseSchema.safeParse(undefined) + + expect(result.success).toBe(true) + }) + + it('should not validate when unexpected fields are provided for ResendOTPResponseSchema', () => { + const result = ResendOTPResponseSchema.safeParse({ + unexpectedField: 'value' + }) + + expect(result.success).toBe(false) + }) +}) diff --git a/packages/schema/tests/workspace.spec.ts b/packages/schema/tests/workspace.spec.ts index 556552e5..d0b83533 100644 --- a/packages/schema/tests/workspace.spec.ts +++ b/packages/schema/tests/workspace.spec.ts @@ -43,6 +43,15 @@ 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({ + email: 'invalid-email' + }) + + expect(result.success).toBe(false) + expect(result.error?.issues).toHaveLength(1) + }) + it('should not validate if invalid types are specified for InviteMemberSchema', () => { const result = InviteMemberSchema.safeParse({ email: 123, @@ -215,6 +224,12 @@ describe('Workspace Schema Tests', () => { expect(result.success).toBe(true) }) + it('should validate an empty response for DeleteWorkspaceResponseSchema', () => { + const result = DeleteWorkspaceResponseSchema.safeParse(undefined) + + expect(result.success).toBe(true) + }) + it('should not validate if unexpected fields are provided for DeleteWorkspaceResponseSchema', () => { const result = DeleteWorkspaceResponseSchema.safeParse({ unexpectedField: 'value' @@ -396,6 +411,7 @@ describe('Workspace Schema Tests', () => { const result = GlobalSearchResponseSchema.safeParse({ projects: [ { + id: 'project-id', slug: 'project-slug', name: 'Project Name', description: 'Project Description' @@ -403,6 +419,7 @@ describe('Workspace Schema Tests', () => { ], environments: [ { + id: 'environment-id', slug: 'environment-slug', name: 'Environment Name', description: 'Environment Description' @@ -410,6 +427,7 @@ describe('Workspace Schema Tests', () => { ], secrets: [ { + id: 'secret-id', slug: 'secret-slug', name: 'Secret Name', note: 'Secret Note' @@ -417,6 +435,7 @@ describe('Workspace Schema Tests', () => { ], variables: [ { + id: 'variable-id', slug: 'variable-slug', name: 'Variable Name', note: 'Variable Note' @@ -442,10 +461,6 @@ describe('Workspace Schema Tests', () => { }) expect(result.success).toBe(false) - expect(result.error?.issues[0]?.path).toEqual([ - 'projects', - 0, - 'description' - ]) + expect(result.error?.issues[0]?.path).toEqual(['projects', 0, 'id']) }) })