import z from 'zod'; import { authedServiceRequest, serviceRequest, serviceResponse, } from '../interfaces/common'; import { metadata } from '../interfaces/Metadata'; export const key = z.object({ _id: z.string(), organizationId: z.string(), name: z.string(), key: z.string(), archived: z.boolean(), lastUsedAt: z.string().nullable(), meta: metadata, }); export type Key = z.infer; export interface KeyService { create(request: CreateKeyRequest): Promise; archive(request: ArchiveKeyRequest): Promise; get(request: GetKeyRequest): Promise; list(request: ListKeysRequest): Promise; } /** ****************************************************************************** * Create Key ******************************************************************************* */ export const createKeyParams = z.object({ name: z.string().min(4).max(64), }); export const createKeyRequest = authedServiceRequest.merge( z.object({ params: createKeyParams, }), ); export const createKeyResponse = serviceResponse.merge( z.object({ key: key.optional(), }), ); export type CreateKeyParams = z.infer; export type CreateKeyRequest = z.infer; export type CreateKeyResponse = z.infer; /** ****************************************************************************** * Archive Key ******************************************************************************* */ export const archiveKeyParams = z.object({ _id: z.string(), }); export const archiveKeyRequest = authedServiceRequest.merge( z.object({ params: archiveKeyParams, }), ); export const archiveKeyResponse = serviceResponse.merge( z.object({ key: key.optional(), }), ); export type ArchiveKeyParams = z.infer; export type ArchiveKeyRequest = z.infer; export type ArchiveKeyResponse = z.infer; /** ****************************************************************************** * Get Key ******************************************************************************* */ export const getKeyParams = z.object({ keyId: z.string(), }); export const getKeyRequest = serviceRequest.merge( z.object({ params: getKeyParams, }), ); export const getKeyResponse = serviceResponse.merge( z.object({ key: key.nullable().optional(), }), ); export type GetKeyParams = z.infer; export type GetKeyRequest = z.infer; export type GetKeyResponse = z.infer; /** ****************************************************************************** * List Keys ******************************************************************************* */ export const listKeysParams = z.object({}); export const listKeysRequest = authedServiceRequest.merge( z.object({ params: listKeysParams, }), ); export const listKeysResponse = serviceResponse.merge( z.object({ keys: z.array(key).optional(), }), ); export type ListKeysParams = z.infer; export type ListKeysRequest = z.infer; export type ListKeysResponse = z.infer;