import {z} from 'zod' export type GetApiContactsOrganizationsArgs = {page?: number | undefined, names?: Array | undefined, phones?: Array | undefined, emails?: Array | undefined, managers?: Array | undefined, tags?: Array | undefined, created_at?: Array | undefined, modified_at?: Array | undefined, sort?: ('id' | '-id' | 'name' | '-name' | '-modified_at' | 'modified_at') | undefined}; export type GetApiContactsOrganizationsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiContactsOrganizationsResponse = z.object({}); 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 Organizations * * GET /contacts/organizations */ export const getApiContactsOrganizations = async (args: GetApiContactsOrganizationsArgs, options?: GetApiContactsOrganizationsOptions) => { const {baseUrl = "https://api.roapp.io/v2", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) for (const item of args.names ?? []) query.append("names", String(item)) for (const item of args.phones ?? []) query.append("phones", String(item)) for (const item of args.emails ?? []) query.append("emails", String(item)) for (const item of args.managers ?? []) query.append("managers", String(item)) for (const item of args.tags ?? []) query.append("tags", String(item)) for (const item of args.created_at ?? []) query.append("created_at", String(item)) for (const item of args.modified_at ?? []) query.append("modified_at", String(item)) if (args.sort != null) query.append("sort", String(args.sort)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/contacts/organizations${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(getApiContactsOrganizationsResponse, await res.json()) };