import {z} from 'zod' export type DateISO = string; export type GetApiAttachmentsSigningRequestsArgs = {limit?: number | undefined, cursor?: string | undefined, modifiedSince?: DateISO | undefined, attachmentIds?: Array | undefined}; export type GetApiAttachmentsSigningRequestsOptions = {baseUrl: string, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dateISO = z.string(); export const userBadgeDto = z.object({id: z.string(), name: z.string(), avatarUrl: z.string().nullable(), deactivatedAt: dateISO.nullable()}); export const idDto = z.object({id: z.string().min(1)}); export const attachmentSigningRecipientDto = z.object({id: z.string(), status: z.enum(["CREATED", "SENT", "DELIVERED", "SIGNED", "COMPLETED", "DECLINED", "AUTO_RESPONDED"]), name: z.string(), emailAddress: z.string(), signedAt: dateISO.nullable(), declinedAt: dateISO.nullable(), candidate: idDto.nullable(), clientContact: idDto.nullable()}); export const attachmentCustomTypeDto = z.object({id: z.string(), name: z.string()}); export const attachmentDto = z.object({attachmentId: z.string(), attachmentName: z.string().nullable(), type: z.enum(["UNASSIGNED", "CV"]).nullable(), customType: attachmentCustomTypeDto.nullable(), createdAt: dateISO, extension: z.string().nullable(), size: z.number().nullable()}); export const attachmentSigningRequestDto = z.object({id: z.string(), envelopeId: z.string().nullable(), status: z.enum(["CREATED", "SENT", "DELIVERED", "COMPLETED", "DECLINED", "VOIDED"]), sender: userBadgeDto, sentAt: dateISO.nullable(), completedAt: dateISO.nullable(), createdAt: dateISO, recipients: z.array(attachmentSigningRecipientDto), documents: z.array(attachmentDto)}); export const signingRequestCursorPaginationDto = z.object({cursor: z.string().nullable()}); export const getSigningRequestsResponseDto = z.object({items: z.array(attachmentSigningRequestDto), pagination: signingRequestCursorPaginationDto}); 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 signing requests * * GET /attachments/signing-requests */ export const getApiAttachmentsSigningRequests = async (args: GetApiAttachmentsSigningRequestsArgs, options: GetApiAttachmentsSigningRequestsOptions) => { const {baseUrl, fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.modifiedSince != null) query.append("modifiedSince", String(args.modifiedSince)) for (const item of args.attachmentIds ?? []) query.append("attachmentIds", String(item)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/attachments/signing-requests${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(getSigningRequestsResponseDto, await res.json()) };