import {z} from 'zod' export type SecretReference = {name: string, version?: ('latest' | number) | undefined, unwrapped?: string | undefined}; export type CredentialFillOnePassword = {token: string | SecretReference, vault: string, allowedOrigins: Array}; export type OnePasswordField = {item: string, section?: string | undefined, field: string}; export type CredentialFillField = {field?: string | undefined, selector: string, ref?: OnePasswordField | undefined}; export type CredentialFillParams = {credential?: string | undefined, vault?: string | undefined, onePassword?: CredentialFillOnePassword | undefined, fields: Array, pageId?: string | undefined, pageUrl?: string | undefined, frame?: {url: string} | undefined}; export type CreateApiApiSessionIdCredentialsFillArgs = {id: string, body: CredentialFillParams}; export type CreateApiApiSessionIdCredentialsFillOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const credentialFillOutcome = z.enum(["filled", "not_found", "ambiguous", "not_editable", "not_accepted"]); export const credentialFillFieldResult = z.object({selector: z.string(), field: z.string().optional(), outcome: credentialFillOutcome, secretId: z.string(), version: z.number().int(), expiresAt: z.string().optional(), reference: z.string().optional()}); export const credentialFillResponse = z.object({version: z.number().int().optional(), pageId: z.string(), origin: z.string(), fields: z.array(credentialFillFieldResult)}); 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 }; /** * Fill a credential * * POST /api/session/{id}/credentials/fill */ export const createApiApiSessionIdCredentialsFill = async (args: CreateApiApiSessionIdCredentialsFillArgs, options?: CreateApiApiSessionIdCredentialsFillOptions) => { const {baseUrl = "https://api.hyperbrowser.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/session/${encodeURIComponent(String(args.id))}/credentials/fill${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(credentialFillResponse, await res.json()) };