import {z} from 'zod' export type GetApiV1AuthOnboardingOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const clientUserClientsEntity = z.object({mappingId: z.number(), role: z.object({}), clientUser: z.lazy(() => clientUserEntity), client: z.lazy(() => clientEntity)}); export const clientEntity: z.ZodType = z.object({id: z.number(), name: z.string(), createdAt: z.string(), updatedAt: z.string(), stripeId: z.string(), stripePaymentId: z.string(), defaultApp: z.number(), clientUsers: z.array(z.lazy(() => clientUserEntity)), clientUserClients: z.array(clientUserClientsEntity), licenseKey: z.string().optional(), licensePublicKey: z.string().optional(), licenseAuthToken: z.string().optional(), status: z.enum(["active", "send_suspended", "suspended"]), version: z.string().optional(), region: z.string().optional()}); export const clientUserEntity: z.ZodType = z.object({id: z.number(), firstName: z.string(), lastName: z.string(), address: z.string(), profilePicture: z.string(), email: z.string(), password: z.string(), address2: z.string(), city: z.string(), postcode: z.string(), country: z.string(), state: z.string(), cc: z.number(), phone: z.string(), activationHash: z.string(), activationEmailResentAt: z.string(), passwordLockedAt: z.string(), accountStatus: z.object({}), emailVerified: z.boolean(), timezone: z.string(), registeredIp: z.string(), createdAt: z.string(), updatedAt: z.string(), aid: z.string(), emailList: z.object({}), onboardingCompleted: z.number(), lastCheckedForInactivity: z.string(), receiveNewsletters: z.number(), lastLogin: z.string(), gitHubId: z.number(), googleId: z.number(), clients: z.array(clientEntity), clientUserClients: z.array(clientUserClientsEntity), source: z.string().optional(), isTwoFactorEnabled: z.number(), twoFactorSecret: z.string().optional(), passwordResetRequestedAt: 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 }; /** * GET /v1/auth/onboarding */ export const getApiV1AuthOnboarding = async (options: GetApiV1AuthOnboardingOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/auth/onboarding${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(clientUserEntity, await res.json()) };