import {z} from 'zod' export type HostedSignupRequest = {email: string, company_name: string, requested_slug?: (string | null) | undefined, hosting_mode?: ('definite_cloud' | 'self_hosted') | undefined, terms_accepted: boolean}; export type CreateApiOnpremV1HostedSignupsArgs = {'X-Definite-Hosted-Signup-Token'?: (string | null) | undefined, body: HostedSignupRequest}; export type CreateApiOnpremV1HostedSignupsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const hostedSignupResponse = z.object({signup_id: z.union([z.string(), z.string()]), status_token: z.string(), status: z.enum(["email_pending", "approval_pending", "accepted", "rejected", "converted"]), hosting_mode: z.enum(["definite_cloud", "self_hosted"]), email_domain_kind: z.enum(["unknown", "business", "free"]), approval_status: z.enum(["not_required", "pending", "approved", "rejected"])}); export const responseMeta = z.object({requestId: z.string().optional(), timestamp: z.string().optional(), durationMs: z.number().int().nullable().optional()}); export const successResponseHostedSignupResponse = z.object({success: z.literal(true), data: hostedSignupResponse, meta: responseMeta.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 }; /** * Create a Definite signup and send an email verification code * * POST /onprem/v1/hosted-signups */ export const createApiOnpremV1HostedSignups = async (args: CreateApiOnpremV1HostedSignupsArgs, options: CreateApiOnpremV1HostedSignupsOptions) => { 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") if (args['X-Definite-Hosted-Signup-Token'] != null) headers.set("X-Definite-Hosted-Signup-Token", String(args['X-Definite-Hosted-Signup-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/onprem/v1/hosted-signups${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(successResponseHostedSignupResponse, await res.json()) };