import {z} from 'zod' export type GeneratorRecipe = {length?: number | undefined, characterSets?: (Array<'LETTERS' | 'DIGITS' | 'SYMBOLS'>) | undefined, excludeCharacters?: string | undefined}; export type Field = {id: string, section?: ({id?: string | undefined}) | undefined, type: 'STRING' | 'EMAIL' | 'CONCEALED' | 'URL' | 'TOTP' | 'DATE' | 'MONTH_YEAR' | 'MENU', purpose?: ('' | 'USERNAME' | 'PASSWORD' | 'NOTES') | undefined, label?: string | undefined, value?: string | undefined, generate?: boolean | undefined, recipe?: GeneratorRecipe | undefined, entropy?: number | undefined}; export type File = {id?: string | undefined, name?: string | undefined, size?: number | undefined, content_path?: string | undefined, section?: ({id?: string | undefined}) | undefined, content?: string | undefined}; export type FullItem = {id?: string | undefined, title?: string | undefined, vault: {id: string}, category: 'LOGIN' | 'PASSWORD' | 'API_CREDENTIAL' | 'SERVER' | 'DATABASE' | 'CREDIT_CARD' | 'MEMBERSHIP' | 'PASSPORT' | 'SOFTWARE_LICENSE' | 'OUTDOOR_LICENSE' | 'SECURE_NOTE' | 'WIRELESS_ROUTER' | 'BANK_ACCOUNT' | 'DRIVER_LICENSE' | 'IDENTITY' | 'REWARD_PROGRAM' | 'DOCUMENT' | 'EMAIL_ACCOUNT' | 'SOCIAL_SECURITY_NUMBER' | 'MEDICAL_RECORD' | 'SSH_KEY' | 'CUSTOM', urls?: (Array<{label?: string | undefined, primary?: boolean | undefined, href: string}>) | undefined, favorite?: boolean | undefined, tags?: Array | undefined, version?: number | undefined, state?: ('ARCHIVED' | 'DELETED') | undefined, createdAt?: string | undefined, updatedAt?: string | undefined, lastEditedBy?: string | undefined, sections?: (Array<{id?: string | undefined, label?: string | undefined}>) | undefined, fields?: Array | undefined, files?: Array | undefined}; export type CreateApiVaultsVaultUuidItemsArgs = {vaultUuid: string, body: FullItem}; export type CreateApiVaultsVaultUuidItemsOptions = {baseUrl?: string | undefined, ConnectToken?: string | undefined, fetch?: typeof fetch | undefined}; export const generatorRecipe = z.object({length: z.number().int().gte(1).lte(64).optional(), characterSets: z.array(z.enum(["LETTERS", "DIGITS", "SYMBOLS"])).optional(), excludeCharacters: z.string().optional()}); export const field = z.object({id: z.string(), section: z.object({id: z.string().optional()}).optional(), type: z.enum(["STRING", "EMAIL", "CONCEALED", "URL", "TOTP", "DATE", "MONTH_YEAR", "MENU"]), purpose: z.enum(["", "USERNAME", "PASSWORD", "NOTES"]).optional(), label: z.string().optional(), value: z.string().optional(), generate: z.boolean().optional(), recipe: generatorRecipe.optional(), entropy: z.number().optional()}); export const file = z.object({id: z.string().optional(), name: z.string().optional(), size: z.number().int().optional(), content_path: z.string().optional(), section: z.object({id: z.string().optional()}).optional(), content: z.string().optional()}); export const fullItem = z.object({id: z.string().optional(), title: z.string().optional(), vault: z.object({id: z.string()}), category: z.enum(["LOGIN", "PASSWORD", "API_CREDENTIAL", "SERVER", "DATABASE", "CREDIT_CARD", "MEMBERSHIP", "PASSPORT", "SOFTWARE_LICENSE", "OUTDOOR_LICENSE", "SECURE_NOTE", "WIRELESS_ROUTER", "BANK_ACCOUNT", "DRIVER_LICENSE", "IDENTITY", "REWARD_PROGRAM", "DOCUMENT", "EMAIL_ACCOUNT", "SOCIAL_SECURITY_NUMBER", "MEDICAL_RECORD", "SSH_KEY", "CUSTOM"]), urls: z.array(z.object({label: z.string().optional(), primary: z.boolean().optional(), href: z.string()})).optional(), favorite: z.boolean().optional(), tags: z.array(z.string()).optional(), version: z.number().int().optional(), state: z.enum(["ARCHIVED", "DELETED"]).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), lastEditedBy: z.string().optional(), sections: z.array(z.object({id: z.string().optional(), label: z.string().optional()})).optional(), fields: z.array(field).optional(), files: z.array(file).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create a new Item * * POST /vaults/{vaultUuid}/items */ export const createApiVaultsVaultUuidItems = async (args: CreateApiVaultsVaultUuidItemsArgs, options?: CreateApiVaultsVaultUuidItemsOptions) => { const {baseUrl = "http://localhost:8080/v1", fetch: fetchFn = fetch, ConnectToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ConnectToken !== undefined) headers.set('authorization', `Bearer ${ConnectToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/vaults/${encodeURIComponent(String(args.vaultUuid))}/items${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(fullItem, await res.json()) };