import {z} from 'zod' export type PhoneNumberDTO = {countryCode?: string | undefined, number?: string | undefined}; export type SectorUserRegistrationWithCredentialsDTO = {emailToken: string, firstName: string, lastName: string, jobTitle?: string | undefined, contactType?: ('SECTOR_ASSOCIATION' | 'OPERATOR' | 'CONSULTANT') | undefined, organisationName?: string | undefined, phoneNumber?: PhoneNumberDTO | undefined, mobileNumber?: PhoneNumberDTO | undefined, password: string}; export type UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndEnableInvitedSectorUserWithCredentialsArgs = {body: SectorUserRegistrationWithCredentialsDTO}; export type UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndEnableInvitedSectorUserWithCredentialsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneNumberDTO = z.object({countryCode: z.string().optional(), number: z.string().max(255).optional()}); export const sectorUserDTO = z.object({email: z.string().max(255), firstName: z.string().max(255), lastName: z.string().max(255), status: z.enum(["REGISTERED", "PENDING", "DELETED"]).optional(), jobTitle: z.string().max(255).optional(), phoneNumber: phoneNumberDTO.optional(), mobileNumber: phoneNumberDTO.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 }; /** * Accept authority and enable sector user from invitation token * * PUT /v1.0/sector-users/registration/accept-authority-and-enable-invited-sector-user-with-credentials */ export const updateApiV10SectorUsersRegistrationAcceptAuthorityAndEnableInvitedSectorUserWithCredentials = async (args: UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndEnableInvitedSectorUserWithCredentialsArgs, options?: UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndEnableInvitedSectorUserWithCredentialsOptions) => { const {baseUrl = "http://localhost:8082/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1.0/sector-users/registration/accept-authority-and-enable-invited-sector-user-with-credentials${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(sectorUserDTO, await res.json()) };