import {z} from 'zod' export type MailingListAddMember = {degreeStatusId?: (number | null) | undefined, candidateId?: (string | null) | undefined, qualificationId?: (string | null) | undefined, preferredTeachingSubjectId: string, acceptedPolicyId: string, considerationJourneyStageId: number, channelId?: (number | null) | undefined, creationChannelSourceId?: (number | null) | undefined, creationChannelServiceId?: (number | null) | undefined, creationChannelActivityId?: (number | null) | undefined, email: string, firstName: string, lastName: string, addressPostcode?: (string | null) | undefined, welcomeGuideVariant?: (string | null) | undefined, alreadySubscribedToEvents?: boolean | undefined, alreadySubscribedToMailingList?: boolean | undefined, alreadySubscribedToTeacherTrainingAdviser?: boolean | undefined, defaultContactCreationChannel?: (number | null) | undefined, defaultCreationChannelSourceId?: (number | null) | undefined, defaultCreationChannelServiceId?: (number | null) | undefined, defaultCreationChannelActivityId?: (number | null) | undefined, situation?: (number | null) | undefined, citizenship?: (number | null) | undefined, visaStatus?: (number | null) | undefined, location?: (number | null) | undefined, graduationYear?: (number | null) | undefined, inferredGraduationDate?: (string | null) | undefined}; export type CreateApiApiMailingListMembersArgs = {body: MailingListAddMember}; export type CreateApiApiMailingListMembersOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const degreeStatusResponse = z.object({degreeStatusId: z.number().int().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 }; /** * Adds a new member to the mailing list. * * POST /api/mailing_list/members */ export const createApiApiMailingListMembers = async (args: CreateApiApiMailingListMembersArgs, options: CreateApiApiMailingListMembersOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/mailing_list/members${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(degreeStatusResponse, await res.json()) };