import {z} from 'zod' export type JournalType = 0 | 1; export type GetApiV1JournalsArgs = {type: JournalType, Filter?: string | undefined, Page?: number | undefined, PageSize?: number | undefined, SortColumn?: string | undefined, SortOrder?: string | undefined, locationId?: number | undefined, operationId?: number | undefined, showPosted?: boolean | undefined, Account: unknown}; export type GetApiV1JournalsOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const journalType = z.union([z.literal(0), z.literal(1)]); export const journalProcessType = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const journalResponse = z.object({id: z.number().int().optional(), operationId: z.number().int().nullable().optional(), operationName: z.string().nullable().optional(), locationId: z.number().int().nullable().optional(), locationName: z.string().nullable().optional(), journalType: journalType.optional(), processType: journalProcessType.optional(), description: z.string().nullable().optional(), date: z.string().optional(), posted: z.boolean().optional(), postedDate: z.string().nullable().optional(), linesCount: z.number().int().optional()}); export const journalResponsePagedResult = z.object({pageSize: z.number().int().optional(), page: z.number().int().optional(), totalCount: z.number().int().optional(), filteredCount: z.number().int().optional(), data: z.array(journalResponse).nullable().optional()}); 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 }; /** * GET /v1/Journals */ export const getApiV1Journals = async (args: GetApiV1JournalsArgs, options: GetApiV1JournalsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = 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.Filter != null) query.append("Filter", String(args.Filter)) if (args.Page != null) query.append("Page", String(args.Page)) if (args.PageSize != null) query.append("PageSize", String(args.PageSize)) if (args.SortColumn != null) query.append("SortColumn", String(args.SortColumn)) if (args.SortOrder != null) query.append("SortOrder", String(args.SortOrder)) if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.operationId != null) query.append("operationId", String(args.operationId)) if (args.showPosted != null) query.append("showPosted", String(args.showPosted)) if (args.Account != null) query.append("Account", String(args.Account)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/Journals${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(journalResponsePagedResult, await res.json()) };