import {z} from 'zod' export type GLClassificationTypeID = string; export type FindGLClassificationRequest = {type: GLClassificationTypeID, thirdPartyId?: string | undefined, vendorValueId: string}; export type CreateApiAccountingV2betaGlClassificationsFindArgs = {body: FindGLClassificationRequest}; export type CreateApiAccountingV2betaGlClassificationsFindOptions = {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 gLClassificationTypeID = z.string(); export const gLClassificationType = z.object({type: gLClassificationTypeID, name: z.string().min(1).max(64), fieldKey: z.string().min(1).max(64)}); 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 gLClassification = z.object({id: resourceID, organizationId: organizationID, thirdPartyId: z.string().optional(), vendorValueId: z.string().min(1).max(64), type: gLClassificationType, name: z.string().min(1).max(255), glEntityIds: z.array(z.string()).optional(), 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 }; /** * Find classification * * POST /accounting/v2beta/gl-classifications:find */ export const createApiAccountingV2betaGlClassificationsFind = async (args: CreateApiAccountingV2betaGlClassificationsFindArgs, options?: CreateApiAccountingV2betaGlClassificationsFindOptions) => { 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-classifications:find${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(gLClassification, await res.json()) };