import {z} from 'zod' export type ExternalID = string; export type AssociatedConnection = {connectionId: string}; export type CurrencyCode = string; export type GLAccountType = 'UNSPECIFIED' | 'BANK' | 'ACCOUNTS_PAYABLE' | 'ACCOUNTS_RECEIVABLE'; export type CreateGLAccountRequest = {externalId: ExternalID, thirdPartyId?: string | undefined, associatedConnections?: Array | undefined, name: string, number?: string | undefined, glEntityIds: Array, currency?: CurrencyCode | undefined, type?: GLAccountType | undefined, displayType: string, inactive?: boolean | undefined}; export type CreateApiAccountingV2betaGlAccountsArgs = {body: CreateGLAccountRequest}; export type CreateApiAccountingV2betaGlAccountsOptions = {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 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 account * * POST /accounting/v2beta/gl-accounts */ export const createApiAccountingV2betaGlAccounts = async (args: CreateApiAccountingV2betaGlAccountsArgs, options?: CreateApiAccountingV2betaGlAccountsOptions) => { 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(/\/+$/, '')}/accounting/v2beta/gl-accounts${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(gLAccount, await res.json()) };