Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(api-client, schema): Add workspace's schemas and types in @keyshade/schema #520

Merged
merged 4 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

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

export const CreateApiKeySchema = z.object({
name: z.string(),
Expand Down
10 changes: 9 additions & 1 deletion packages/schema/src/index.ts
muntaxir4 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
//Export all Schemas and types

export * from './pagination/pagination'
export * from './pagination/pagination.types'

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/workspace'
export * from './workspace/workspace.types'

export * from './workspace-role'

export * from './enums'
2 changes: 1 addition & 1 deletion packages/schema/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod'
import { eventTypeEnum, integrationTypeEnum } from './enums'
import { eventTypeEnum, integrationTypeEnum } from '@/enums'

export const CreateIntegrationSchema = z.object({
name: z.string(),
Expand Down
46 changes: 46 additions & 0 deletions packages/schema/src/pagination/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 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 const ResponseErrorSchema = z.object({
message: z.string(),
error: z.string(),
statusCode: z.number()
})

export const ClientResponseSchema = <T>(dataSchema: z.ZodType<T>) =>
z
.object({
success: z.boolean(),
error: ResponseErrorSchema.nullable(),
data: dataSchema.nullable()
})
.refine((obj) =>
obj.success
? obj.data !== null && obj.error == null
: obj.data == null && obj.error !== null
)
17 changes: 17 additions & 0 deletions packages/schema/src/pagination/pagination.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { z } from 'zod'
import {
ClientResponseSchema,
PageRequestSchema,
PageResponseSchema,
ResponseErrorSchema
} from './pagination'

export type PageRequest = z.infer<typeof PageRequestSchema>

export type PageResponse<T> = z.infer<ReturnType<typeof PageResponseSchema<T>>>

export type ResponseError = z.infer<typeof ResponseErrorSchema>

export type ClientResponse<T> = z.infer<
ReturnType<typeof ClientResponseSchema<T>>
>
2 changes: 1 addition & 1 deletion packages/schema/src/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod'
import { projectAccessLevelEnum } from './enums'
import { projectAccessLevelEnum } from '@/enums'
import { CreateEnvironmentSchema } from './environment'

export const CreateProjectSchema = z.object({
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/secret.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod'
import { rotateAfterEnum } from './enums'
import { rotateAfterEnum } from '@/enums'

export const CreateSecretSchema = z.object({
name: z.string(),
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/workspace-role.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod'
import { authorityEnum } from './enums'
import { authorityEnum } from '@/enums'

export const CreateWorkspaceRoleSchema = z.object({
name: z.string(),
Expand Down
14 changes: 0 additions & 14 deletions packages/schema/src/workspace.ts

This file was deleted.

Loading
Loading