import {z} from 'zod' export type AgentKey = string; export type Email = string; export type Emails = Array; export type AgentMergeUpdate = {description?: string | undefined, technicalEmails?: Emails | undefined, billingEmails?: Emails | undefined, settlementEmails?: Emails | undefined, businessEmails?: Emails | undefined}; export type PatchApiAgentsAgentKeyArgs = {agentKey: AgentKey, 'If-Match'?: number | undefined, body: AgentMergeUpdate}; export type PatchApiAgentsAgentKeyOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const agentKey = z.string(); export const agentStatus = z.enum(["idle", "active", "archived"]); export const countryCodeAlpha3 = z.string().min(3).max(3); export const countryCodeAlpha2 = z.string().min(2).max(2); export const countryCodeNumeric = z.string().min(3).max(3); export const countryCode = z.object({alpha3: countryCodeAlpha3, alpha2: countryCodeAlpha2, numeric: countryCodeNumeric}); export const stateOrProvince = z.string().min(1).max(120); export const city = z.string().min(1).max(120); export const line1 = z.string().min(1).max(120); export const line2 = z.string().min(1).max(120); export const postalCode = z.string().min(1).max(10); export const binKey = z.string().min(5).max(120); export const acquirerSegmentId = z.number(); export const email = z.string(); export const emails = z.array(email); export const created = z.string(); export const lastModified = z.string(); export const version = z.number().int(); export const agent = z.object({key: agentKey, status: agentStatus, name: z.string().min(1).max(120), description: z.string().min(1).max(160).optional(), legalName: z.string().min(1).max(120), billingAddress: z.object({countryCode: countryCode, stateOrProvince: stateOrProvince.optional(), city: city, line1: line1, line2: line2.optional(), postalCode: postalCode}), registeredAddress: z.object({countryCode: countryCode, stateOrProvince: stateOrProvince.optional(), city: city, line1: line1, line2: line2.optional(), postalCode: postalCode}).optional(), vatId: z.string().min(1).max(50).optional(), binKeys: z.array(binKey), acquirerSegmentId: acquirerSegmentId.optional(), technicalEmails: emails, billingEmails: emails, settlementEmails: emails, businessEmails: emails.optional(), created: created, lastModified: lastModified.optional(), version: version}); 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 Agent * * PATCH /agents/{agentKey} * * Also accepts auth: BearerToken */ export const patchApiAgentsAgentKey = async (args: PatchApiAgentsAgentKeyArgs, options?: PatchApiAgentsAgentKeyOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/merge-patch+json") headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/agents/${encodeURIComponent(String(args.agentKey))}${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(agent, await res.json()) };