import {z} from 'zod' export type RetrieveDataRequest = {entity_id?: string | undefined, vault_id?: string | undefined}; export type JWEReq = {header?: string | undefined, iv?: string | undefined, encrypted_payload?: string | undefined, tag?: string | undefined, encrypted_key?: string | undefined}; export type CreateApiApiV2VaultRetrieveArgs = {'x-tenant-id'?: string | undefined, body: RetrieveDataRequest | JWEReq}; export type CreateApiApiV2VaultRetrieveOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const secret = z.object({}); export const retrieveDataResponse = z.object({data: secret.optional()}); export const jWERes = z.object({header: z.string().optional(), iv: z.string().optional(), encrypted_payload: z.string().optional(), tag: z.string().optional(), encrypted_key: z.string().optional()}); export const createApiApiV2VaultRetrieveResponse = z.union([retrieveDataResponse, jWERes]); 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 }; /** * Retrieve Data from Locker * * POST /api/v2/vault/retrieve */ export const createApiApiV2VaultRetrieve = async (args: CreateApiApiV2VaultRetrieveArgs, options: CreateApiApiV2VaultRetrieveOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-tenant-id'] != null) headers.set("x-tenant-id", String(args['x-tenant-id'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/vault/retrieve${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(createApiApiV2VaultRetrieveResponse, await res.json()) };