import {z} from 'zod' export type GLAccountType = 'UNSPECIFIED' | 'BANK' | 'ACCOUNTS_PAYABLE' | 'ACCOUNTS_RECEIVABLE'; export type ResourceID = string; export type GetApiAccountingV2betaGlAccountsArgs = {type?: GLAccountType | undefined, excludeIgnored?: boolean | undefined, connectionId?: ResourceID | undefined, accountId?: ResourceID | undefined, glEntityId?: ResourceID | undefined, inactive?: boolean | undefined, limit?: number | undefined, token?: string | undefined}; export type GetApiAccountingV2betaGlAccountsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; 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 currencyCode = z.string().min(3).max(3); export const gLAccountType = z.enum(["UNSPECIFIED", "BANK", "ACCOUNTS_PAYABLE", "ACCOUNTS_RECEIVABLE"]); 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 gLAccount = z.object({id: resourceID, externalId: externalID, organizationId: organizationID, thirdPartyId: z.string().optional(), associatedConnections: z.array(associatedConnection).optional(), accountId: resourceID.optional(), glEntityIds: z.array(z.string()), name: z.string().min(1).max(64), number: z.string().min(1).max(255).optional(), currency: currencyCode.optional(), type: gLAccountType.optional(), displayType: z.string().min(1).max(64), ignored: z.boolean(), inactive: z.boolean(), version: resourceVersion, etag: eTag, created: createdTimestamp, updated: updatedTimestamp}); export const getApiAccountingV2betaGlAccountsResponse = z.object({items: z.array(gLAccount), 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 GL accounts * * GET /accounting/v2beta/gl-accounts */ export const getApiAccountingV2betaGlAccounts = async (args: GetApiAccountingV2betaGlAccountsArgs, options?: GetApiAccountingV2betaGlAccountsOptions) => { 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.type != null) query.append("type", String(args.type)) if (args.excludeIgnored != null) query.append("excludeIgnored", String(args.excludeIgnored)) if (args.connectionId != null) query.append("connectionId", String(args.connectionId)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.glEntityId != null) query.append("glEntityId", String(args.glEntityId)) if (args.inactive != null) query.append("inactive", String(args.inactive)) 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-accounts${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(getApiAccountingV2betaGlAccountsResponse, await res.json()) };