import {z} from 'zod' export type GetApiApprovalArgs = {page?: number | undefined, pageSize?: number | undefined, sort?: string | undefined}; export type GetApiApprovalOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const approverStatus = z.object({id: z.string().optional(), version: z.string().optional(), createdDate: z.number().int().optional(), groupId: z.string().optional(), lastModifiedDate: z.number().int().optional(), personId: z.string().optional(), positionNumber: z.number().int().optional(), status: z.enum(["APPROVED", "OPEN", "REJECTED"]).optional(), userId: z.string().optional()}); export const approval = z.object({id: z.string().optional(), version: z.string().optional(), active: z.boolean().optional(), approverStatuses: z.array(approverStatus).optional(), createdDate: z.number().int().optional(), currentApproverStatusId: z.string().optional(), currentStatusType: z.enum(["APPROVED", "OPEN", "REJECTED"]).optional(), entityId: z.string().optional(), entityName: z.string().optional(), lastModifiedDate: z.number().int().optional(), note: z.string().optional(), requesterId: z.string().optional(), ruleId: z.string().optional()}); export const getApiApprovalResponse = z.object({result: z.array(approval).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 }; /** * query approval * * GET /approval */ export const getApiApproval = async (args: GetApiApprovalArgs, options: GetApiApprovalOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.sort != null) query.append("sort", String(args.sort)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/approval${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(getApiApprovalResponse, await res.json()) };