import {z} from 'zod' export type InvitedUserCredentialsDTO = {invitationToken: string, password: string}; export type UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndSetCredentialsToSectorUserArgs = {body: InvitedUserCredentialsDTO}; export type UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndSetCredentialsToSectorUserOptions = {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 set credentials to sector user * * PUT /v1.0/sector-users/registration/accept-authority-and-set-credentials-to-sector-user */ export const updateApiV10SectorUsersRegistrationAcceptAuthorityAndSetCredentialsToSectorUser = async (args: UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndSetCredentialsToSectorUserArgs, options?: UpdateApiV10SectorUsersRegistrationAcceptAuthorityAndSetCredentialsToSectorUserOptions) => { 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-set-credentials-to-sector-user${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()) };