import {z} from 'zod' export type MarketCode = string; export type NationalIdentifier = {type: 'CIVIC' | 'COMPANY', market: MarketCode, number: string}; export type CountryCode = string; export type CountrySubdivision = string; export type Address = {country?: CountryCode | undefined, countrySubdivision?: CountrySubdivision | undefined, city?: string | undefined, postalCode?: string | undefined, streetName?: string | undefined, streetNumber?: string | undefined}; export type CreateEntityRequest = {legalName: string, alias?: string | undefined, nationalIdentifier?: NationalIdentifier | undefined, address?: Address | undefined, parentId?: string | undefined}; export type CreateApiFinancialDataV2EntitiesArgs = {body: CreateEntityRequest}; export type CreateApiFinancialDataV2EntitiesOptions = {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 marketCode = z.string().min(2).max(2); export const nationalIdentifier = z.object({type: z.enum(["CIVIC", "COMPANY"]), market: marketCode, number: z.string()}); export const countryCode = z.string().min(2).max(2); export const countrySubdivision = z.string().max(6); export const address = z.object({country: countryCode.optional(), countrySubdivision: countrySubdivision.optional(), city: z.string().max(64).optional(), postalCode: z.string().max(16).optional(), streetName: z.string().max(64).optional(), streetNumber: z.string().max(16).optional()}); export const createdTimestamp = z.string(); export const updatedTimestamp = z.string(); export const resourceVersion = z.number().int().gte(1); export const entity = z.object({id: resourceID, organizationId: organizationID, parentId: z.string().min(32).max(64).optional(), legalName: z.string(), alias: z.string().max(255).optional(), name: z.string().max(255), nationalIdentifier: nationalIdentifier.optional(), address: address.optional(), created: createdTimestamp, updated: updatedTimestamp, version: resourceVersion}); 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 entity * * POST /financial-data/v2/entities */ export const createApiFinancialDataV2Entities = async (args: CreateApiFinancialDataV2EntitiesArgs, options?: CreateApiFinancialDataV2EntitiesOptions) => { const {baseUrl = "https://api.atlar.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/financial-data/v2/entities${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(entity, await res.json()) };