import {z} from 'zod' export type GetApiV4FormsMetadataArgs = {continuationToken?: string | undefined, size?: number | undefined, type?: ('additional' | 'initial' | 'reference' | 'review' | 'internal' | 'eligibility' | 'request') | undefined, projectId?: string | undefined}; export type GetApiV4FormsMetadataOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const formMetadataResponse = z.object({formId: z.string().nullable().optional(), name: z.string().nullable().optional(), formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable().optional(), createdBy: z.string().optional(), createdAt: z.string().nullable().optional(), updatedBy: z.string().optional(), updatedAt: z.string().nullable().optional()}); export const continuationTokenResponseFormMetadataResponse = z.object({continuationToken: z.string().nullable().optional(), items: z.array(formMetadataResponse).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 a page of form metadata * * GET /v4/forms/metadata */ export const getApiV4FormsMetadata = async (args: GetApiV4FormsMetadataArgs, options?: GetApiV4FormsMetadataOptions) => { const {baseUrl = "https://submittable-api.submittable.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.continuationToken != null) query.append("continuationToken", String(args.continuationToken)) if (args.size != null) query.append("size", String(args.size)) if (args.type != null) query.append("type", String(args.type)) if (args.projectId != null) query.append("projectId", String(args.projectId)) const url = `${baseUrl.replace(/\/+$/, '')}/v4/forms/metadata${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(continuationTokenResponseFormMetadataResponse, await res.json()) };