import {z} from 'zod' export type Patch = Array<{op: 'add' | 'remove' | 'replace', path: string, value?: Record | undefined}>; export type PatchApiVaultsVaultUuidItemsItemUuidArgs = {vaultUuid: string, itemUuid: string, body: Patch}; export type PatchApiVaultsVaultUuidItemsItemUuidOptions = {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 }; /** * Update a subset of Item attributes * * PATCH /vaults/{vaultUuid}/items/{itemUuid} */ export const patchApiVaultsVaultUuidItemsItemUuid = async (args: PatchApiVaultsVaultUuidItemsItemUuidArgs, options?: PatchApiVaultsVaultUuidItemsItemUuidOptions) => { 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/${encodeURIComponent(String(args.itemUuid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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()) };