import {z} from 'zod' export type MePostInitialWorkspace = {initial_pricing_plan?: number | undefined, name?: string | undefined}; export type MePostUser = {country_id?: number | undefined, created_with?: string | undefined, email: string, full_name?: string | undefined, invitation_code?: string | undefined, password: string, timezone?: string | undefined, tos_accepted: boolean, workspace?: MePostInitialWorkspace | undefined}; export type CreateApiSignupArgs = {body: MePostUser}; export type CreateApiSignupOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const modelsOptions = z.record(z.string(), z.string()); export const modelsTogglUser = z.object({api_token: z.string().optional(), at: z.string().optional(), beginning_of_week: z.number().int().optional(), country_id: z.number().int().optional(), created_at: z.string().optional(), default_workspace_id: z.number().int().optional(), email: z.string().optional(), fullname: z.string().optional(), has_password: z.boolean().optional(), id: z.number().int().optional(), image_url: z.string().optional(), openid_email: z.string().optional(), openid_enabled: z.boolean().optional(), options: modelsOptions.optional(), timezone: z.string().optional(), updated_at: z.string().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 }; /** * Signup * * POST /signup */ export const createApiSignup = async (args: CreateApiSignupArgs, options: CreateApiSignupOptions) => { const {baseUrl, 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(/\/+$/, '')}/signup${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(modelsTogglUser, await res.json()) };