import {z} from 'zod' export type ResourceID = string; export type GLEntityJSONPatchReplaceEntityId = {op: 'replace', path: '/entityId', value: ResourceID}; export type GLEntityJSONPatchRemoveEntityId = {op: 'remove', path: '/entityId'}; export type CurrencyCode = string; export type GLEntityJSONPatchReplaceCurrency = {op: 'replace', path: '/currency', value: CurrencyCode}; export type GLEntityJSONPatchRequest = Array; export type PatchApiAccountingV2betaGlEntitiesIdArgs = {id: string, 'If-Match'?: string | undefined, body: GLEntityJSONPatchRequest}; export type PatchApiAccountingV2betaGlEntitiesIdOptions = {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 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 }; /** * Update entity * * PATCH /accounting/v2beta/gl-entities/{id} */ export const patchApiAccountingV2betaGlEntitiesId = async (args: PatchApiAccountingV2betaGlEntitiesIdArgs, options?: PatchApiAccountingV2betaGlEntitiesIdOptions) => { 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-patch+json") headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounting/v2beta/gl-entities/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(gLEntity, await res.json()) };