import {z} from 'zod' export type UserInUpdatePasswordAdmin = {password: string, re_password: string}; export type BodyUsersAdminUpdateUserPasswordApiV5AdminUsersUserIdChangePasswordPut = {user: UserInUpdatePasswordAdmin}; export type UpdateApiApiV5AdminUsersUserIdChangePasswordArgs = {user_id: number, body: BodyUsersAdminUpdateUserPasswordApiV5AdminUsersUserIdChangePasswordPut}; export type UpdateApiApiV5AdminUsersUserIdChangePasswordOptions = {baseUrl?: string | undefined, RWAPIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const fullUser = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), email: z.string(), resetPasswordSentAt: z.string().optional(), signInCount: z.number().int().optional(), currentSignInIp: z.string().optional(), lastSignInIp: z.string().optional(), name: z.string().optional(), age: z.number().int().optional(), phone: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), birthDate: z.string().optional(), weight: z.number().optional(), weightMeasurementUnit: z.string().optional(), height: z.number().optional(), heightMeasurementUnit: z.string().optional(), emailConfirmed: z.boolean().optional(), role: z.string().optional(), countryCode: z.string().optional(), profilePictureId: z.number().int().optional(), profilePictureUrl: z.string().optional()}); export const userInResponseForAdmin = z.object({user: fullUser}); 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 }; /** * Update user password * * PUT /api/v5/admin/users/{user_id}/change-password */ export const updateApiApiV5AdminUsersUserIdChangePassword = async (args: UpdateApiApiV5AdminUsersUserIdChangePasswordArgs, options?: UpdateApiApiV5AdminUsersUserIdChangePasswordOptions) => { const {baseUrl = "https://api.pavlok.com", fetch: fetchFn = fetch, RWAPIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (RWAPIKeyHeader !== undefined) headers.set("Authorization", RWAPIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v5/admin/users/${encodeURIComponent(String(args.user_id))}/change-password${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(userInResponseForAdmin, await res.json()) };