Skip to content

Commit

Permalink
feat(schema, api-client): Add workspace's schemas and types in schema…
Browse files Browse the repository at this point in the history
… package

- Export zod schemas and types for workspace and include its tests.
- Add extra schemas required in modules PageRequest.
- Add schema package as dependency for api-client and replace existing dependency on workspace's types.
  • Loading branch information
muntaxir4 authored and rajdip-b committed Nov 6, 2024
1 parent 5b27e32 commit 510ba40
Show file tree
Hide file tree
Showing 10 changed files with 934 additions and 272 deletions.
4 changes: 3 additions & 1 deletion packages/api-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
"lint": "eslint \"{src,tests}/**/*.ts\" --fix",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\""
},
"dependencies": {}
"dependencies": {
"@keyshade/schema": "workspace:*"
}
}
4 changes: 2 additions & 2 deletions packages/api-client/src/controllers/workspace.ts
Original file line number Diff line number Diff line change
@@ -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 {
CreateWorkspaceRequest,
CreateWorkspaceResponse,
Expand All @@ -17,7 +17,7 @@ import {
GlobalSearchResponse,
UpdateWorkspaceRequest,
UpdateWorkspaceResponse
} from '@api-client/types/workspace.types'
} from '@keyshade/schema'

export default class WorkspaceController {
private apiClient: APIClient
Expand Down
118 changes: 0 additions & 118 deletions packages/api-client/src/types/workspace.types.d.ts

This file was deleted.

7 changes: 6 additions & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"main": "dist/src/index.js",
"description": "This package holds the schemas that other applications can use to validate the input data.",
"private": true,
"exports": {
".": "./dist/src/index.js"
},
"scripts": {
"build": "tsc && tsc-alias",
"watch": "tsc -w",
Expand All @@ -13,5 +16,7 @@
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"test": "jest"
},
"dependencies": {}
"dependencies": {
"zod": "^3.23.6"
}
}
54 changes: 52 additions & 2 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
import { z } from 'zod'

export const PageRequestSchema = z.object({
page: z.number().optional(),
limit: z.number().optional(),
sort: z.string().optional(),
order: z.string().optional(),
search: z.string().optional()
})
export type PageRequest = z.infer<typeof PageRequestSchema>

export const PageResponseSchema = <T>(itemSchema: z.ZodType<T>) =>
z.object({
items: z.array(itemSchema),
metadata: z.object({
page: z.number(),
perPage: z.number(),
pageCount: z.number(),
totalCount: z.number(),
links: z.object({
self: z.string(),
first: z.string(),
previous: z.string().nullable(),
next: z.string().nullable(),
last: z.string()
})
})
})
export type PageResponse<T> = z.infer<ReturnType<typeof PageResponseSchema<T>>>

export const ResponseErrorSchema = z.object({
message: z.string(),
error: z.string(),
statusCode: z.number()
})
export type ResponseError = z.infer<typeof ResponseErrorSchema>

export const ClientResponseSchema = <T>(dataSchema: z.ZodType<T>) =>
z.object({
success: z.boolean(),
error: ResponseErrorSchema.nullable(),
data: dataSchema.nullable()
})
export type ClientResponse<T> = z.infer<
ReturnType<typeof ClientResponseSchema<T>>
>

//Export Other Schemas
export * from './api-key'
export * from './environment'
export * from './integration'
export * from './project'
export * from './secret'
export * from './variable'
export * from './workspace'
export * from './workspace-role'

export * from './workspace/workspace'
export * from './workspace/workspace.types'

export * from './workspace-role'
export * from './enums'
14 changes: 0 additions & 14 deletions packages/schema/src/workspace.ts

This file was deleted.

155 changes: 155 additions & 0 deletions packages/schema/src/workspace/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { z } from 'zod'
import { PageRequestSchema, PageResponseSchema } from '../index'

export const CreateWorkspaceSchema = z.object({
name: z.string(),
icon: z.string().optional(),
isDefault: z.boolean().optional()
})

export const UpdateWorkspaceSchema = CreateWorkspaceSchema.partial()

export const InviteMemberSchema = z.object({
email: z.string(),
roleIds: z.array(z.string()).optional()
})

//Request and Response types
export const WorkspaceSchema = z.object({
id: z.string(),
name: z.string(),
slug: z.string(),
icon: z.string(),
isFreeTier: z.boolean(),
createdAt: z.string(),
updatedAt: z.string(),
ownerId: z.string(),
isDefault: z.boolean(),
lastUpdatedBy: z.string()
})

export const CreateWorkspaceRequestSchema = z.object({
name: z.string(),
icon: z.string().optional()
})

export const CreateWorkspaceResponseSchema = WorkspaceSchema

export const UpdateWorkspaceRequestSchema =
CreateWorkspaceRequestSchema.partial().extend({
workspaceSlug: z.string()
})

export const UpdateWorkspaceResponseSchema = WorkspaceSchema

export const DeleteWorkspaceRequestSchema = z.object({
workspaceSlug: z.string()
})

export const DeleteWorkspaceResponseSchema = z.object({}).strict()

export const GetWorkspaceRequestSchema = z.object({
workspaceSlug: z.string()
})

export const GetWorkspaceResponseSchema = WorkspaceSchema

export const GetAllWorkspacesOfUserRequestSchema = PageRequestSchema

export const GetAllWorkspacesOfUserResponseSchema =
PageResponseSchema(WorkspaceSchema)

export const ExportDataRequestSchema = z.object({
workspaceSlug: z.string()
})

export const ExportDataResponseSchema = z.object({
name: z.string(),
icon: z.string(),
workspaceRoles: z.array(
z.object({
name: z.string(),
description: z.string(),
colorCode: z.string(),
hasAdminAuthority: z.boolean(),
authorities: z.array(z.string())
})
),
projects: z.array(
z.object({
name: z.string(),
description: z.string(),
publicKey: z.string(),
privateKey: z.string(),
storePrivateKey: z.boolean(),
accessLevel: z.enum(['GLOBAL', 'PRIVATE', 'INTERNAL']),
environments: z.array(
z.object({
name: z.string(),
description: z.string()
})
),
secrets: z.array(
z.object({
name: z.string(),
note: z.string(),
rotateAt: z.string(),
versions: z.array(
z.object({
value: z.string(),
version: z.number()
})
)
})
),
variables: z.array(
z.object({
name: z.string(),
note: z.string(),
versions: z.array(
z.object({
value: z.string(),
version: z.number()
})
)
})
)
})
)
})

export const GlobalSearchRequestSchema = z.object({
workspaceSlug: z.string(),
search: z.string()
})

export const GlobalSearchResponseSchema = z.object({
projects: z.array(
z.object({
slug: z.string(),
name: z.string(),
description: z.string()
})
),
environments: z.array(
z.object({
slug: z.string(),
name: z.string(),
description: z.string()
})
),
secrets: z.array(
z.object({
slug: z.string(),
name: z.string(),
note: z.string()
})
),
variables: z.array(
z.object({
slug: z.string(),
name: z.string(),
note: z.string()
})
)
})
Loading

0 comments on commit 510ba40

Please sign in to comment.