import {z} from 'zod' export type GLClassificationTypeID = string; export type ResourceID = string; export type GetApiAccountingV2betaGlClassificationsArgs = {type?: GLClassificationTypeID | undefined, glEntityId?: ResourceID | undefined, name?: string | undefined, inactive?: boolean | undefined, limit?: number | undefined, token?: string | undefined}; export type GetApiAccountingV2betaGlClassificationsOptions = {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 getApiAccountingV2betaGlClassificationsResponse = z.object({items: z.array(gLClassification), 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 classifications * * GET /accounting/v2beta/gl-classifications */ export const getApiAccountingV2betaGlClassifications = async (args: GetApiAccountingV2betaGlClassificationsArgs, options?: GetApiAccountingV2betaGlClassificationsOptions) => { 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.glEntityId != null) query.append("glEntityId", String(args.glEntityId)) if (args.name != null) query.append("name", String(args.name)) 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-classifications${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(getApiAccountingV2betaGlClassificationsResponse, await res.json()) };