import {z} from 'zod' export type GetApiFinancialDataV2betaPortfoliosArgs = {entityId?: string | undefined, limit?: number | undefined, token?: string | undefined}; export type GetApiFinancialDataV2betaPortfoliosOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const resourceID = z.string().min(32).max(64); export const organizationID = z.string(); export const createdTimestamp = z.string(); export const updatedTimestamp = z.string(); export const resourceVersion = z.number().int().gte(1); export const metadata = z.record(z.string(), z.string()).nullable(); export const eTag = z.string(); export const portfolio = z.object({id: resourceID, organizationId: organizationID, entityId: resourceID, affiliationId: resourceID, thirdPartyId: resourceID, name: z.string().max(255), alias: z.string().optional(), identifiers: z.array(z.unknown()), created: createdTimestamp, updated: updatedTimestamp, version: resourceVersion, metadata: metadata.optional(), etag: eTag}); export const getApiFinancialDataV2betaPortfoliosResponse = z.object({items: z.array(portfolio), nextToken: z.string(), token: z.string(), limit: z.number().int()}); 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 }; /** * List portfolios * * GET /financial-data/v2beta/portfolios */ export const getApiFinancialDataV2betaPortfolios = async (args: GetApiFinancialDataV2betaPortfoliosArgs, options?: GetApiFinancialDataV2betaPortfoliosOptions) => { const {baseUrl = "https://api.atlar.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.entityId != null) query.append("entityId", String(args.entityId)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.token != null) query.append("token", String(args.token)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/financial-data/v2beta/portfolios${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(getApiFinancialDataV2betaPortfoliosResponse, await res.json()) };