import {z} from 'zod' export type UpdateUserDto = {enabled?: boolean | undefined, password?: string | undefined}; export type PatchApiApiV1UsersIdArgs = {body: UpdateUserDto}; export type PatchApiApiV1UsersIdOptions = {baseUrl: string, LocalUserJWT?: string | undefined, fetch?: typeof fetch | undefined}; export const roleAssignment = z.object({id: z.number(), user: z.lazy(() => user), role: z.string(), createdAt: z.string(), updatedAt: z.string(), deletedAt: z.string()}); export const user: z.ZodType = z.object({userId: z.string(), cardinalId: z.string().optional(), cachedCloudUser: z.object({subscription: z.enum(["free", "early_adopter", "starter", "pro", "ultimate"]).optional()}), cachedCloudUserAt: z.string().optional(), enabled: z.boolean().optional(), role: z.string(), designation: z.string().optional(), activityStatus: z.string().optional(), activityStatusUpdatedAt: z.string().optional(), roles: z.array(roleAssignment), username: z.string().optional(), password: z.string().optional(), id: z.number(), createdAt: z.string(), updatedAt: z.string(), deletedAt: z.string()}); 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 a user. * * PATCH /api/v1/users/{id} */ export const patchApiApiV1UsersId = async (args: PatchApiApiV1UsersIdArgs, options: PatchApiApiV1UsersIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, LocalUserJWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (LocalUserJWT !== undefined) headers.set('authorization', `Bearer ${LocalUserJWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/users/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(user, await res.json()) };