import {z} from 'zod' export type CounterpartyType = 'standard' | 'clearloop-exchange' | 'agency-desk'; export type GetApiOrganizationsOrganizationIdCounterpartiesArgs = {search?: string | undefined, offset?: string | undefined, limit?: string | undefined, isConnected?: boolean | undefined, counterpartyIds?: Array | undefined, counterpartyTypes?: Array | undefined, includeRelatedCounterpartyIds?: boolean | undefined, includeConnections?: boolean | undefined, includeCounterpartyDetails?: boolean | undefined, organizationId: string}; export type GetApiOrganizationsOrganizationIdCounterpartiesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const counterpartyType = z.enum(["standard", "clearloop-exchange", "agency-desk"]); export const counterpartyExtra = z.object({exchangeId: z.string().optional()}); export const counterpartyTag = z.literal("featured"); export const counterparty = z.object({backgroundImageId: z.string().optional(), counterpartyDescription: z.string().optional(), counterpartyId: z.string(), counterpartyName: z.string(), counterpartyType: counterpartyType, extra: counterpartyExtra.optional(), imageId: z.string().optional(), networkTag: z.string(), priority: z.string().optional(), tags: z.array(counterpartyTag).optional()}); export const counterparties = z.object({counterparties: z.array(counterparty)}); 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 }; /** * Get Counterparties * * GET /organizations/{organizationId}/counterparties */ export const getApiOrganizationsOrganizationIdCounterparties = async (args: GetApiOrganizationsOrganizationIdCounterpartiesArgs, options?: GetApiOrganizationsOrganizationIdCounterpartiesOptions) => { const {baseUrl = "https://api.copper.co/platform", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.isConnected != null) query.append("isConnected", String(args.isConnected)) for (const item of args.counterpartyIds ?? []) query.append("counterpartyIds", String(item)) for (const item of args.counterpartyTypes ?? []) query.append("counterpartyTypes", String(item)) if (args.includeRelatedCounterpartyIds != null) query.append("includeRelatedCounterpartyIds", String(args.includeRelatedCounterpartyIds)) if (args.includeConnections != null) query.append("includeConnections", String(args.includeConnections)) if (args.includeCounterpartyDetails != null) query.append("includeCounterpartyDetails", String(args.includeCounterpartyDetails)) const url = `${baseUrl.replace(/\/+$/, '')}/organizations/${encodeURIComponent(String(args.organizationId))}/counterparties${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(counterparties, await res.json()) };