import {z} from 'zod' export type ArchivedStatus = 'archived' | 'notArchived' | 'either'; export type AdditionalFormsStatus = 'anyCompleted' | 'allCompleted' | 'noneCompleted' | 'anyEditable' | 'any' | 'none'; export type RequestFormsStatus = 'anyReceived' | 'anyInProgress' | 'anyAccepted' | 'anyDeclined' | 'anyCompleted' | 'any' | 'none'; export type ReviewStatus = 'pending' | 'completed' | 'pendingOrCompleted'; export type SubmissionSortField = 'submissionId' | 'submissionTitle' | 'date' | 'reviewStageScore' | 'submitterName' | 'publicOrgName' | 'organizationName' | 'projectTitle'; export type SubmissionSortOrder = 'ascending' | 'descending'; export type GetApiV4SubmissionsArgs = {continuationToken?: string | undefined, size?: number | undefined, Search?: string | undefined, 'PublicOrganizationIds.Include'?: Array | undefined, 'PublicOrganizationIds.Exclude'?: Array | undefined, 'Projects.Include'?: Array | undefined, 'Projects.Exclude'?: Array | undefined, 'Statuses.Include'?: (Array<'new' | 'in_progress' | 'accepted' | 'declined' | 'withdrawn' | 'completed' | 'editable' | 'viewed' | 'received' | 'published'>) | undefined, 'Statuses.Exclude'?: (Array<'new' | 'in_progress' | 'accepted' | 'declined' | 'withdrawn' | 'completed' | 'editable' | 'viewed' | 'received' | 'published'>) | undefined, Reviewers?: Array | undefined, ArchivedStatus?: ArchivedStatus | undefined, 'Labels.IncludeAny'?: Array | undefined, 'Labels.IncludeAll'?: Array | undefined, 'Labels.IncludeUnlabeled'?: boolean | undefined, 'Labels.ExcludeAny'?: Array | undefined, 'Labels.ExcludeAll'?: Array | undefined, 'Labels.ExcludeUnlabeled'?: boolean | undefined, DateFrom?: string | undefined, DateTo?: string | undefined, AdditionalFormStatuses?: Array | undefined, RequestFormStatuses?: Array | undefined, ReviewStatus?: ReviewStatus | undefined, ReviewerProgress?: ReviewStatus | undefined, 'ReviewStages.Include'?: Array | undefined, 'ReviewStages.Exclude'?: Array | undefined, 'SortBy.Field'?: SubmissionSortField | undefined, 'SortBy.Order'?: SubmissionSortOrder | undefined, 'LabelColors.IncludeAny'?: Array | undefined, 'LabelColors.ExcludeAny'?: Array | undefined}; export type GetApiV4SubmissionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const submissionListResponse = z.object({submissionId: z.string().nullable().optional(), projectId: z.string().nullable().optional(), submitterId: z.string().nullable().optional(), reviewStageId: z.string().nullable().optional(), submissionStatus: z.enum(["new", "in_progress", "accepted", "declined", "withdrawn", "completed", "editable", "viewed", "received", "published"]).nullable().optional(), isArchived: z.boolean().optional(), isPaid: z.boolean().optional(), labels: z.array(z.string()).nullable().optional(), reviewers: z.array(z.string()).nullable().optional(), submitterFirstName: z.string().nullable().optional(), submitterLastName: z.string().nullable().optional(), submitterEmail: z.string().nullable().optional(), submitterLastLoginDate: z.string().nullable().optional(), submissionTitle: z.string().nullable().optional(), projectTitle: z.string().nullable().optional(), submissionDate: z.string().optional(), isConcealed: z.boolean().optional(), isInitialFormEntryEditable: z.boolean().optional(), publicOrgId: z.string().nullable().optional(), publicOrgVersion: z.number().int().nullable().optional(), publicOrgName: z.string().nullable().optional(), concealPublicOrgName: z.boolean().optional()}); export const continuationTokenResponseSubmissionListResponse = z.object({continuationToken: z.string().nullable().optional(), items: z.array(submissionListResponse).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 submissions * * GET /v4/submissions */ export const getApiV4Submissions = async (args: GetApiV4SubmissionsArgs, options?: GetApiV4SubmissionsOptions) => { 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.Search != null) query.append("Search", String(args.Search)) for (const item of args['PublicOrganizationIds.Include'] ?? []) query.append("PublicOrganizationIds.Include", String(item)) for (const item of args['PublicOrganizationIds.Exclude'] ?? []) query.append("PublicOrganizationIds.Exclude", String(item)) for (const item of args['Projects.Include'] ?? []) query.append("Projects.Include", String(item)) for (const item of args['Projects.Exclude'] ?? []) query.append("Projects.Exclude", String(item)) for (const item of args['Statuses.Include'] ?? []) query.append("Statuses.Include", String(item)) for (const item of args['Statuses.Exclude'] ?? []) query.append("Statuses.Exclude", String(item)) for (const item of args.Reviewers ?? []) query.append("Reviewers", String(item)) if (args.ArchivedStatus != null) query.append("ArchivedStatus", String(args.ArchivedStatus)) for (const item of args['Labels.IncludeAny'] ?? []) query.append("Labels.IncludeAny", String(item)) for (const item of args['Labels.IncludeAll'] ?? []) query.append("Labels.IncludeAll", String(item)) if (args['Labels.IncludeUnlabeled'] != null) query.append("Labels.IncludeUnlabeled", String(args['Labels.IncludeUnlabeled'])) for (const item of args['Labels.ExcludeAny'] ?? []) query.append("Labels.ExcludeAny", String(item)) for (const item of args['Labels.ExcludeAll'] ?? []) query.append("Labels.ExcludeAll", String(item)) if (args['Labels.ExcludeUnlabeled'] != null) query.append("Labels.ExcludeUnlabeled", String(args['Labels.ExcludeUnlabeled'])) if (args.DateFrom != null) query.append("DateFrom", String(args.DateFrom)) if (args.DateTo != null) query.append("DateTo", String(args.DateTo)) for (const item of args.AdditionalFormStatuses ?? []) query.append("AdditionalFormStatuses", String(item)) for (const item of args.RequestFormStatuses ?? []) query.append("RequestFormStatuses", String(item)) if (args.ReviewStatus != null) query.append("ReviewStatus", String(args.ReviewStatus)) if (args.ReviewerProgress != null) query.append("ReviewerProgress", String(args.ReviewerProgress)) for (const item of args['ReviewStages.Include'] ?? []) query.append("ReviewStages.Include", String(item)) for (const item of args['ReviewStages.Exclude'] ?? []) query.append("ReviewStages.Exclude", String(item)) if (args['SortBy.Field'] != null) query.append("SortBy.Field", String(args['SortBy.Field'])) if (args['SortBy.Order'] != null) query.append("SortBy.Order", String(args['SortBy.Order'])) for (const item of args['LabelColors.IncludeAny'] ?? []) query.append("LabelColors.IncludeAny", String(item)) for (const item of args['LabelColors.ExcludeAny'] ?? []) query.append("LabelColors.ExcludeAny", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/v4/submissions${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(continuationTokenResponseSubmissionListResponse, await res.json()) };