import {z} from 'zod' export type DocumentOrderingFieldsEnum = 'name' | 'date_created' | 'date_status_changed' | 'date_of_last_action' | 'date_modified' | 'date_sent' | 'date_completed' | 'date_expiration' | 'date_declined' | 'status' | '-name' | '-date_created' | '-date_status_changed' | '-date_of_last_action' | '-date_modified' | '-date_sent' | '-date_completed' | '-date_expiration' | '-date_declined' | '-status'; export type DocumentStatusRequestEnum = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14; export type GetApiPublicV1DocumentsArgs = {template_id?: string | undefined, form_id?: string | undefined, folder_uuid?: string | undefined, contact_id?: string | undefined, count?: number | undefined, page?: number | undefined, order_by?: DocumentOrderingFieldsEnum | undefined, created_from?: string | undefined, created_to?: string | undefined, deleted?: boolean | undefined, id?: string | undefined, completed_from?: string | undefined, completed_to?: string | undefined, membership_id?: string | undefined, metadata?: Array | undefined, modified_from?: string | undefined, modified_to?: string | undefined, q?: string | undefined, status?: DocumentStatusRequestEnum | undefined, status__ne?: DocumentStatusRequestEnum | undefined, tag?: string | undefined}; export type GetApiPublicV1DocumentsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const documentListResponse = z.object({results: z.array(z.object({id: z.string().optional(), name: z.string().optional(), status: z.string().optional(), date_created: z.string().optional(), date_modified: z.string().optional(), date_sent: z.string().nullable().optional(), date_completed: z.string().nullable().optional(), expiration_date: z.string().nullable().optional(), version: z.string().optional()})).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 }; /** * List Documents * * GET /public/v1/documents * * Also accepts auth: oauth2 */ export const getApiPublicV1Documents = async (args: GetApiPublicV1DocumentsArgs, options?: GetApiPublicV1DocumentsOptions) => { const {baseUrl = "https://api.pandadoc.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.template_id != null) query.append("template_id", String(args.template_id)) if (args.form_id != null) query.append("form_id", String(args.form_id)) if (args.folder_uuid != null) query.append("folder_uuid", String(args.folder_uuid)) if (args.contact_id != null) query.append("contact_id", String(args.contact_id)) if (args.count != null) query.append("count", String(args.count)) if (args.page != null) query.append("page", String(args.page)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.created_from != null) query.append("created_from", String(args.created_from)) if (args.created_to != null) query.append("created_to", String(args.created_to)) if (args.deleted != null) query.append("deleted", String(args.deleted)) if (args.id != null) query.append("id", String(args.id)) if (args.completed_from != null) query.append("completed_from", String(args.completed_from)) if (args.completed_to != null) query.append("completed_to", String(args.completed_to)) if (args.membership_id != null) query.append("membership_id", String(args.membership_id)) for (const item of args.metadata ?? []) query.append("metadata", String(item)) if (args.modified_from != null) query.append("modified_from", String(args.modified_from)) if (args.modified_to != null) query.append("modified_to", String(args.modified_to)) if (args.q != null) query.append("q", String(args.q)) if (args.status != null) query.append("status", String(args.status)) if (args.status__ne != null) query.append("status__ne", String(args.status__ne)) if (args.tag != null) query.append("tag", String(args.tag)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/public/v1/documents${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(documentListResponse, await res.json()) };