import {z} from 'zod' export type UserInCreate = {email: string, password: string}; export type BodyAuthRegisterApiV5UsersPost = {user: UserInCreate}; export type CreateApiApiV5UsersArgs = {body: BodyAuthRegisterApiV5UsersPost}; export type CreateApiApiV5UsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const userSettingsTypes = z.enum(["integer", "float", "string", "boolean"]); export const userSetting = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), deletedAt: z.string().optional(), id: z.number().int().optional(), settingKey: z.string(), settingValue: z.string(), settingType: userSettingsTypes, settingMeta: z.object({}).optional()}); export const userWithToken = z.object({username: z.string().optional(), email: z.string(), phone: z.string().optional(), countryCode: z.string().optional(), phoneConfirmed: z.boolean().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(), resetPasswordToken: z.string().optional(), resetPasswordSentAt: z.string().optional(), profilePictureId: z.number().int().optional(), profilePictureUrl: z.string().optional(), emailConfirmed: z.boolean().optional(), anonymous: z.boolean().optional(), role: z.string().optional(), timezone: z.string().optional(), settings: z.record(z.string(), userSetting).optional(), id: z.number().int(), token: z.string()}); export const userInResponse = z.object({user: userWithToken}); 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 }; /** * Register * * POST /api/v5/users/ */ export const createApiApiV5Users = async (args: CreateApiApiV5UsersArgs, options?: CreateApiApiV5UsersOptions) => { const {baseUrl = "https://api.pavlok.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v5/users/${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(userInResponse, await res.json()) };