import {z} from 'zod' export type ResourceID = string; export type GetApiAccountingV2betaGlEntitiesArgs = {connectionId?: ResourceID | undefined, limit?: number | undefined, token?: string | undefined}; export type GetApiAccountingV2betaGlEntitiesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string().min(3).max(3); export const resourceID = z.string().min(32).max(64); export const externalID = z.string(); export const organizationID = z.string(); export const associatedConnection = z.object({connectionId: z.string()}); export const resourceVersion = z.number().int().gte(1); export const eTag = z.string(); export const createdTimestamp = z.string(); export const updatedTimestamp = z.string(); export const gLEntity = z.object({currency: currencyCode.optional(), id: resourceID, externalId: externalID, organizationId: organizationID, entityId: resourceID.optional(), associatedConnections: z.array(associatedConnection).optional(), name: z.string().min(1).max(64), version: resourceVersion, etag: eTag, created: createdTimestamp, updated: updatedTimestamp}); export const getApiAccountingV2betaGlEntitiesResponse = z.object({items: z.array(gLEntity), 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 entities * * GET /accounting/v2beta/gl-entities */ export const getApiAccountingV2betaGlEntities = async (args: GetApiAccountingV2betaGlEntitiesArgs, options?: GetApiAccountingV2betaGlEntitiesOptions) => { 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.connectionId != null) query.append("connectionId", String(args.connectionId)) 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(/\/+$/, '')}/accounting/v2beta/gl-entities${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(getApiAccountingV2betaGlEntitiesResponse, await res.json()) };