import {z} from 'zod' export type UserWatchlistType = 'USER_MANAGED_WATCHLIST' | 'CUSTOMER_MANAGED_WATCHLIST' | 'USER_FAVORITED_ENTITIES' | 'USER_HIDDEN_ENTITIES' | 'CUSTOMER_PORTFOLIO_LIST' | 'CUSTOMER_AFFINITY_SYNCED_LIST' | 'CUSTOMER_ATTIO_SYNCED_LIST' | 'GLOBAL_FIELD_LIST'; export type CompanyWatchlistInput = {name: string, shared_with_team: boolean, user_watchlist_type?: (UserWatchlistType | null) | undefined, companies?: Array | undefined}; export type CreateApiWatchlistsCompaniesArgs = {body: CompanyWatchlistInput}; export type CreateApiWatchlistsCompaniesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const dBOperationMetadata = z.object({count_total: z.number().int().nullable().optional(), count_changed: z.number().int().nullable().optional(), count_skipped: z.number().int().nullable().optional(), count_not_found: z.number().int().nullable().optional()}); export const resourceUrnResponseUnionCompanyWatchlistUrnSynapseListUrn = z.object({urn: z.union([z.string(), z.string()]), metadata: dBOperationMetadata.nullable().optional()}); 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 }; /** * Create Company Watchlist * * POST /watchlists/companies */ export const createApiWatchlistsCompanies = async (args: CreateApiWatchlistsCompaniesArgs, options: CreateApiWatchlistsCompaniesOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/watchlists/companies${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(resourceUrnResponseUnionCompanyWatchlistUrnSynapseListUrn, await res.json()) };