import {z} from 'zod' export type TitleFieldResponseResponse = {fieldType: string, formFieldId: string}; export type NameFieldResponseResponse = {fieldType: string, formFieldId: string}; export type AddressFieldResponseResponse = {fieldType: string, formFieldId: string}; export type ShortAnswerFieldResponseResponse = {fieldType: string, formFieldId: string}; export type LongAnswerFieldResponseResponse = {fieldType: string, formFieldId: string}; export type SingleCheckboxFieldResponseResponse = {fieldType: string, formFieldId: string}; export type MultipleResponseFieldResponseResponse = {fieldType: string, formFieldId: string}; export type SingleResponseFieldResponseResponse = {fieldType: string, formFieldId: string}; export type FileUploadFieldResponseResponse = {fieldType: string, formFieldId: string}; export type TableFieldResponseResponse = {fieldType: string, formFieldId: string}; export type NumberFieldResponseResponse = {fieldType: string, formFieldId: string}; export type DateFieldResponseResponse = {fieldType: string, formFieldId: string}; export type EmailFieldResponseResponse = {fieldType: string, formFieldId: string}; export type WebsiteFieldResponseResponse = {fieldType: string, formFieldId: string}; export type PhoneFieldResponseResponse = {fieldType: string, formFieldId: string}; export type CoverLetterFieldResponseResponse = {fieldType: string, formFieldId: string}; export type TextOnlyFieldResponseResponse = {fieldType: string, formFieldId: string}; export type ReferenceFormFieldResponseResponse = {fieldType: string, formFieldId: string}; export type DividerFieldResponseResponse = {fieldType: string, formFieldId: string}; export type SingleRatingFieldResponseResponse = {fieldType: string, formFieldId: string}; export type MultipleRatingFieldResponseResponse = {fieldType: string, formFieldId: string}; export type DropdownRatingFieldResponseResponse = {fieldType: string, formFieldId: string}; export type DropdownListFieldResponseResponse = {fieldType: string, formFieldId: string}; export type CharityCheckFieldResponseResponse = {fieldType: string, formFieldId: string}; export type EligibilityCharityCheckFieldResponseResponse = {fieldType: string, formFieldId: string}; export type EligibilitySingleCheckboxFieldResponseResponse = {fieldType: string, formFieldId: string}; export type EligibilitySingleResponseFieldResponseResponse = {fieldType: string, formFieldId: string}; export type EligibilityDropdownListFieldResponseResponse = {fieldType: string, formFieldId: string}; export type BankDetailsFieldResponseResponse = {fieldType: string, formFieldId: string}; export type FraudPreventionFieldResponseResponse = {fieldType: string, formFieldId: string}; export type SsnItinFieldResponseResponse = {fieldType: string, formFieldId: string}; export type EinFieldResponseResponse = {fieldType: string, formFieldId: string}; export type UeiFieldResponseResponse = {fieldType: string, formFieldId: string}; export type DisclosureFieldResponseResponse = {fieldType: string, formFieldId: string}; export type ESignResponseResponse = {fieldType: string, formFieldId: string}; export type SubmissionByEmailRequest = {fieldData: Array, governmentId?: (string | null) | undefined}; export type SubmissionByNameRequest = {fieldData: Array, governmentId?: (string | null) | undefined}; export type SubmissionBySubmitterIdRequest = {fieldData: Array, governmentId?: (string | null) | undefined}; export type BulkImportSubmissionsRequest = {projectId?: (string | null) | undefined, submissions?: (Array | null) | undefined}; export type CreateApiV4SubmissionsBulkArgs = {body: BulkImportSubmissionsRequest}; export type CreateApiV4SubmissionsBulkOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkSubmissionResponse = z.object({submissionId: z.string().nullable().optional(), submitterFirstName: z.string().nullable().optional(), submitterLastName: z.string().nullable().optional(), submitterEmail: z.string().nullable().optional(), submissionTitle: z.string().nullable().optional(), publicOrganizationId: z.string().nullable().optional()}); export const bulkImportSubmissionsResponse = z.object({submissions: z.array(bulkSubmissionResponse).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 }; /** * Bulk import submissions * * POST /v4/submissions/bulk */ export const createApiV4SubmissionsBulk = async (args: CreateApiV4SubmissionsBulkArgs, options?: CreateApiV4SubmissionsBulkOptions) => { 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/submissions/bulk${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(bulkImportSubmissionsResponse, await res.json()) };