Skip to content

Commit

Permalink
feat(schema): Add API KEY schemas and types
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 committed Nov 25, 2024
1 parent 08868c3 commit 46d47e5
Show file tree
Hide file tree
Showing 7 changed files with 410 additions and 24 deletions.
10 changes: 0 additions & 10 deletions packages/schema/src/api-key.ts

This file was deleted.

64 changes: 64 additions & 0 deletions packages/schema/src/api-key/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { z } from 'zod'
import { expiresAfterEnum, authorityEnum } from '@/enums'
import { PageRequestSchema } from '@/pagination'

export const ApiKeySchema = z.object({
id: z.string(),
name: z.string(),
slug: z.string(),
value: z.string(),
expiresAt: z
.date()
.transform((d) => d.toISOString())
.nullable(),
createdAt: z.date().transform((d) => d.toISOString()),
updatedAt: z.date().transform((d) => d.toISOString()),
authorities: z.array(authorityEnum),
userId: z.string()
})

export const CreateApiKeyRequestSchema = z.object({
name: ApiKeySchema.shape.name,
expiresAfter: expiresAfterEnum.optional(),
authorities: ApiKeySchema.shape.authorities.optional()
})

export const CreateApiKeyResponseSchema = ApiKeySchema

export const UpdateApiKeyRequestSchema =
CreateApiKeyRequestSchema.partial().extend({
apiKeySlug: ApiKeySchema.shape.slug
})

export const UpdateApiKeyResponseSchema = ApiKeySchema.omit({
value: true,
userId: true
})

export const DeleteApiKeyRequestSchema = z.object({
apiKeySlug: ApiKeySchema.shape.slug
})

export const DeleteApiKeyResponseSchema = z.void()

export const GetApiKeysOfUserRequestSchema = PageRequestSchema

export const GetApiKeysOfUserResponseSchema = ApiKeySchema.omit({
value: true,
userId: true
}).array()

export const GetApiKeyRequestSchema = z.object({
apiKeySlug: ApiKeySchema.shape.slug
})

export const GetApiKeyResponseSchema = ApiKeySchema.omit({
value: true,
userId: true
})

export const CanAccessLiveUpdatesApiKeyRequestSchema = z.void()

export const CanAccessLiveUpdatesApiKeyResponseSchema = z.object({
canAccessLiveUpdates: z.boolean()
})
50 changes: 50 additions & 0 deletions packages/schema/src/api-key/index.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { z } from 'zod'
import {
ApiKeySchema,
CreateApiKeyRequestSchema,
CreateApiKeyResponseSchema,
UpdateApiKeyRequestSchema,
UpdateApiKeyResponseSchema,
DeleteApiKeyRequestSchema,
DeleteApiKeyResponseSchema,
GetApiKeysOfUserRequestSchema,
GetApiKeysOfUserResponseSchema,
GetApiKeyRequestSchema,
GetApiKeyResponseSchema,
CanAccessLiveUpdatesApiKeyRequestSchema,
CanAccessLiveUpdatesApiKeyResponseSchema
} from '.'

export type ApiKey = z.infer<typeof ApiKeySchema>

export type CreateApiKeyRequest = z.infer<typeof CreateApiKeyRequestSchema>

export type CreateApiKeyResponse = z.infer<typeof CreateApiKeyResponseSchema>

export type UpdateApiKeyRequest = z.infer<typeof UpdateApiKeyRequestSchema>

export type UpdateApiKeyResponse = z.infer<typeof UpdateApiKeyResponseSchema>

export type DeleteApiKeyRequest = z.infer<typeof DeleteApiKeyRequestSchema>

export type DeleteApiKeyResponse = z.infer<typeof DeleteApiKeyResponseSchema>

export type GetApiKeysOfUserRequest = z.infer<
typeof GetApiKeysOfUserRequestSchema
>

export type GetApiKeysOfUserResponse = z.infer<
typeof GetApiKeysOfUserResponseSchema
>

export type GetApiKeyRequest = z.infer<typeof GetApiKeyRequestSchema>

export type GetApiKeyResponse = z.infer<typeof GetApiKeyResponseSchema>

export type CanAccessLiveUpdatesApiKeyRequest = z.infer<
typeof CanAccessLiveUpdatesApiKeyRequestSchema
>

export type CanAccessLiveUpdatesApiKeyResponse = z.infer<
typeof CanAccessLiveUpdatesApiKeyResponseSchema
>
3 changes: 2 additions & 1 deletion packages/schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//Export all Schemas and types
//Export all Schemas

export * from './pagination'
export * from './api-key'
Expand All @@ -13,3 +13,4 @@ export * from './variable'
export * from './workspace'
export * from './workspace-role'
export * from './event'
export * from './api-key'
5 changes: 1 addition & 4 deletions packages/schema/src/index.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { z } from 'zod'
import { CreateApiKeySchema, UpdateApiKeySchema } from './api-key'
import {
CreateWorkspaceRoleSchema,
UpdateWorkspaceRoleSchema
Expand All @@ -15,9 +14,7 @@ export * from './workspace/index.types'
export * from './variable/index.types'
export * from './event/index.types'
export * from './integration/index.types'

export type TCreateApiKey = z.infer<typeof CreateApiKeySchema>
export type TUpdateApiKey = z.infer<typeof UpdateApiKeySchema>
export * from './api-key/index.types'

export type TCreateWorkspaceRole = z.infer<typeof CreateWorkspaceRoleSchema>
export type TUpdateWorkspaceRole = z.infer<typeof UpdateWorkspaceRoleSchema>
Loading

0 comments on commit 46d47e5

Please sign in to comment.