import {z} from 'zod' export type TitleFieldResponseRequest = {fieldType: string, formFieldId: string}; export type NameFieldResponseRequest = {fieldType: string, formFieldId: string}; export type AddressFieldResponseRequest = {fieldType: string, formFieldId: string}; export type ShortAnswerFieldResponseRequest = {fieldType: string, formFieldId: string}; export type LongAnswerFieldResponseRequest = {fieldType: string, formFieldId: string}; export type SingleCheckboxFieldResponseRequest = {fieldType: string, formFieldId: string}; export type MultipleResponseFieldResponseRequest = {fieldType: string, formFieldId: string}; export type SingleResponseFieldResponseRequest = {fieldType: string, formFieldId: string}; export type FileUploadFieldResponseRequest = {fieldType: string, formFieldId: string}; export type TableFieldResponseRequest = {fieldType: string, formFieldId: string}; export type NumberFieldResponseRequest = {fieldType: string, formFieldId: string}; export type DateFieldResponseRequest = {fieldType: string, formFieldId: string}; export type EmailFieldResponseRequest = {fieldType: string, formFieldId: string}; export type WebsiteFieldResponseRequest = {fieldType: string, formFieldId: string}; export type PhoneFieldResponseRequest = {fieldType: string, formFieldId: string}; export type CoverLetterFieldResponseRequest = {fieldType: string, formFieldId: string}; export type TextOnlyFieldResponseRequest = {fieldType: string, formFieldId: string}; export type ReferenceFormFieldResponseRequest = {fieldType: string, formFieldId: string}; export type DividerFieldResponseRequest = {fieldType: string, formFieldId: string}; export type SingleRatingFieldResponseRequest = {fieldType: string, formFieldId: string}; export type MultipleRatingFieldResponseRequest = {fieldType: string, formFieldId: string}; export type DropdownRatingFieldResponseRequest = {fieldType: string, formFieldId: string}; export type DropdownListFieldResponseRequest = {fieldType: string, formFieldId: string}; export type CharityCheckFieldResponseRequest = {fieldType: string, formFieldId: string}; export type EligibilityCharityCheckFieldResponseRequest = {fieldType: string, formFieldId: string}; export type EligibilitySingleCheckboxFieldResponseRequest = {fieldType: string, formFieldId: string}; export type EligibilitySingleResponseFieldResponseRequest = {fieldType: string, formFieldId: string}; export type EligibilityDropdownListFieldResponseRequest = {fieldType: string, formFieldId: string}; export type BankDetailsFieldResponseRequest = {fieldType: string, formFieldId: string}; export type FraudPreventionResponseRequest = {fieldType: string, formFieldId: string}; export type SsnItinResponseRequest = {fieldType: string, formFieldId: string}; export type EinFieldResponseRequest = {fieldType: string, formFieldId: string}; export type UeiFieldResponseRequest = {fieldType: string, formFieldId: string}; export type DisclosureFieldResponseRequest = {fieldType: string, formFieldId: string}; export type ESignResponseRequest = {fieldType: string, formFieldId: string}; export type UpdateInternalFormEntryRequest = {formType: 'additional' | 'initial' | 'reference' | 'review' | 'internal' | 'eligibility' | 'request', fieldData: Array}; export type UpdateApiV4EntriesEntryIdArgs = {entryId: string, body: UpdateInternalFormEntryRequest}; export type UpdateApiV4EntriesEntryIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const anyFormTypeInitialFormEntryResponse = z.object({formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable()}); export const anyFormTypeAdditionalFormEntryResponse = z.object({formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable()}); export const anyFormTypeInternalFormEntryResponse = z.object({formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable()}); export const anyFormTypeReferenceFormEntryResponse = z.object({formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable()}); export const anyFormTypeReviewFormEntryResponse = z.object({formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable()}); export const anyFormTypeEligibilityFormEntryResponse = z.object({formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable()}); export const anyFormTypeRequestFormEntryResponse = z.object({formType: z.enum(["additional", "initial", "reference", "review", "internal", "eligibility", "request"]).nullable()}); export const updateApiV4EntriesEntryIdResponse = z.discriminatedUnion("formType", [anyFormTypeInitialFormEntryResponse, anyFormTypeAdditionalFormEntryResponse, anyFormTypeInternalFormEntryResponse, anyFormTypeReferenceFormEntryResponse, anyFormTypeReviewFormEntryResponse, anyFormTypeEligibilityFormEntryResponse, anyFormTypeRequestFormEntryResponse]); 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 }; /** * Update a form entry * * PUT /v4/entries/{entryId} */ export const updateApiV4EntriesEntryId = async (args: UpdateApiV4EntriesEntryIdArgs, options?: UpdateApiV4EntriesEntryIdOptions) => { const {baseUrl = "https://submittable-api.submittable.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v4/entries/${encodeURIComponent(String(args.entryId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(updateApiV4EntriesEntryIdResponse, await res.json()) };